blob: 8bda461de27a9c4ad8a07b3457d3b398b7390bd5 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* 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 * vim9cmds.c: Dealing with compiled function expressions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar078a4612022-01-04 15:17:03 +000096 if ((typep->type_curr == &t_any || typep->type_curr == &t_unknown)
97 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000098 vartype = VAR_DICT;
99 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
100 {
101 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
102 return FAIL;
103 if (is_slice)
104 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000105 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000106 if (need_type(idxtype, &t_number, -2, 0, cctx,
107 FALSE, FALSE) == FAIL)
108 return FAIL;
109 }
110 }
111
112 if (vartype == VAR_DICT)
113 {
114 if (is_slice)
115 {
116 emsg(_(e_cannot_slice_dictionary));
117 return FAIL;
118 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000119 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000120 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 typep->type_curr = typep->type_curr->tt_member;
122 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000123 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000124 typep->type_curr = &t_any;
125 if (typep->type_decl->tt_type == VAR_DICT)
126 {
127 typep->type_decl = typep->type_decl->tt_member;
128 if (typep->type_decl == &t_unknown)
129 // empty dict was used
130 typep->type_decl = &t_any;
131 }
132 else
133 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000134 }
135 else
136 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000137 if (need_type(typep->type_curr, &t_dict_any, -2, 0, cctx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000138 FALSE, FALSE) == FAIL)
139 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000140 typep->type_curr = &t_any;
141 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000142 }
143 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
144 return FAIL;
145 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
146 return FAIL;
147 if (keeping_dict != NULL)
148 *keeping_dict = TRUE;
149 }
150 else if (vartype == VAR_STRING)
151 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000152 typep->type_curr = &t_string;
153 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000154 if ((is_slice
155 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
156 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
157 return FAIL;
158 }
159 else if (vartype == VAR_BLOB)
160 {
161 if (is_slice)
162 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000163 typep->type_curr = &t_blob;
164 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000165 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
166 return FAIL;
167 }
168 else
169 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 typep->type_curr = &t_number;
171 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000172 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
173 return FAIL;
174 }
175 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000176 else if (vartype == VAR_LIST || typep->type_curr == &t_any
177 || typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000178 {
179 if (is_slice)
180 {
181 if (generate_instr_drop(cctx,
182 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
183 2) == FAIL)
184 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000185 // a copy is made so the member type is no longer declared
186 if (typep->type_decl->tt_type == VAR_LIST)
187 typep->type_decl = &t_list_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000188 }
189 else
190 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000191 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000193 typep->type_curr = typep->type_curr->tt_member;
194 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000195 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000196 typep->type_curr = &t_any;
197 if (typep->type_decl->tt_type == VAR_LIST)
198 {
199 typep->type_decl = typep->type_decl->tt_member;
200 if (typep->type_decl == &t_unknown)
201 // empty list was used
202 typep->type_decl = &t_any;
203 }
204 else
205 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 }
207 if (generate_instr_drop(cctx,
208 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
209 == FAIL)
210 return FAIL;
211 }
212 }
213 else
214 {
215 switch (vartype)
216 {
217 case VAR_FUNC:
218 case VAR_PARTIAL:
219 emsg(_(e_cannot_index_a_funcref));
220 break;
221 case VAR_BOOL:
222 case VAR_SPECIAL:
223 case VAR_JOB:
224 case VAR_CHANNEL:
225 case VAR_INSTR:
226 case VAR_UNKNOWN:
227 case VAR_ANY:
228 case VAR_VOID:
229 emsg(_(e_cannot_index_special_variable));
230 break;
231 default:
232 emsg(_(e_string_list_dict_or_blob_required));
233 }
234 return FAIL;
235 }
236 return OK;
237}
238
239/*
240 * Generate an instruction to load script-local variable "name", without the
241 * leading "s:".
242 * Also finds imported variables.
243 */
244 int
245compile_load_scriptvar(
246 cctx_T *cctx,
247 char_u *name, // variable NUL terminated
248 char_u *start, // start of variable
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000249 char_u **end, // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000250 int error) // when TRUE may give error
251{
252 scriptitem_T *si;
253 int idx;
254 imported_T *import;
255
256 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
257 return FAIL;
258 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000259 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000260 if (idx >= 0)
261 {
262 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
263
264 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
265 current_sctx.sc_sid, idx, sv->sv_type);
266 return OK;
267 }
268
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000269 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000270 if (import != NULL)
271 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000272 char_u *p = skipwhite(*end);
273 char_u *exp_name;
274 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100275 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000276 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000277 int done = FALSE;
278 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000279
280 // Need to lookup the member.
281 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000282 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000283 semsg(_(e_expected_dot_after_name_str), start);
284 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000285 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000286 ++p;
287 if (VIM_ISWHITE(*p))
288 {
289 emsg(_(e_no_white_space_allowed_after_dot));
290 return FAIL;
291 }
292
293 // isolate one name
294 exp_name = p;
295 while (eval_isnamec(*p))
296 ++p;
297 cc = *p;
298 *p = NUL;
299
Bram Moolenaard041f422022-01-12 19:54:00 +0000300 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100301 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
302 // "import autoload './dir/script.vim'" or
303 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100304 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100305
306 if (res == OK)
307 {
308 if (si->sn_autoload_prefix != NULL
309 && si->sn_state == SN_STATE_NOT_LOADED)
310 {
311 char_u *auto_name =
312 concat_str(si->sn_autoload_prefix, exp_name);
313
314 // autoload script must be loaded later, access by the autoload
315 // name. If a '(' follows it must be a function. Otherwise we
316 // don't know, it can be "script.Func".
317 if (cc == '(' || paren_follows_after_expr)
318 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any);
319 else
320 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
321 vim_free(auto_name);
322 done = TRUE;
323 }
324 else if (si->sn_import_autoload
325 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100326 {
327 // If a '(' follows it must be a function. Otherwise we don't
328 // know, it can be "script.Func".
329 if (cc == '(' || paren_follows_after_expr)
330 {
331 char_u sid_name[MAX_FUNC_NAME_LEN];
332
333 func_name_with_sid(exp_name, import->imp_sid, sid_name);
334 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any);
335 }
336 else
337 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
338 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100339 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100340 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100341 else
342 {
343 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
344 cctx, NULL, TRUE);
345 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100346 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100347
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000348 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000349 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000350 if (done)
351 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000352
353 if (idx < 0)
354 {
355 if (ufunc != NULL)
356 {
357 // function call or function reference
358 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL);
359 return OK;
360 }
361 return FAIL;
362 }
363
364 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
365 import->imp_sid,
366 idx,
367 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000368 return OK;
369 }
370
Bram Moolenaar62aec932022-01-29 21:45:34 +0000371 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
372 // variable is not in sn_var_vals: old style script.
373 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
374 &t_any);
375
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000376 if (error)
377 semsg(_(e_item_not_found_str), name);
378 return FAIL;
379}
380
381 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000382generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000384 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000385 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000386
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000387 // Reject a global non-autoload function found without the "g:" prefix.
388 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000389 return FAIL;
390
391 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000392 compile_type = get_compile_type(ufunc);
393 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000394 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000395 return FAIL;
396 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
397}
398
399/*
400 * Compile a variable name into a load instruction.
401 * "end" points to just after the name.
402 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
403 * When "error" is FALSE do not give an error when not found.
404 */
405 int
406compile_load(
407 char_u **arg,
408 char_u *end_arg,
409 cctx_T *cctx,
410 int is_expr,
411 int error)
412{
413 type_T *type;
414 char_u *name = NULL;
415 char_u *end = end_arg;
416 int res = FAIL;
417 int prev_called_emsg = called_emsg;
418
419 if (*(*arg + 1) == ':')
420 {
421 if (end <= *arg + 2)
422 {
423 isntype_T isn_type;
424
425 // load dictionary of namespace
426 switch (**arg)
427 {
428 case 'g': isn_type = ISN_LOADGDICT; break;
429 case 'w': isn_type = ISN_LOADWDICT; break;
430 case 't': isn_type = ISN_LOADTDICT; break;
431 case 'b': isn_type = ISN_LOADBDICT; break;
432 default:
433 semsg(_(e_namespace_not_supported_str), *arg);
434 goto theend;
435 }
436 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
437 goto theend;
438 res = OK;
439 }
440 else
441 {
442 isntype_T isn_type = ISN_DROP;
443
444 // load namespaced variable
445 name = vim_strnsave(*arg + 2, end - (*arg + 2));
446 if (name == NULL)
447 return FAIL;
448
449 switch (**arg)
450 {
451 case 'v': res = generate_LOADV(cctx, name, error);
452 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000453 case 's': if (current_script_is_vim9())
454 {
455 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
456 *arg);
457 vim_free(name);
458 return FAIL;
459 }
460 if (is_expr && ASCII_ISUPPER(*name)
461 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000462 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000463 else
464 res = compile_load_scriptvar(cctx, name,
465 NULL, &end, error);
466 break;
467 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
468 {
469 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000470 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000471 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000472 else
473 isn_type = ISN_LOADG;
474 }
475 else
476 {
477 isn_type = ISN_LOADAUTO;
478 vim_free(name);
479 name = vim_strnsave(*arg, end - *arg);
480 if (name == NULL)
481 return FAIL;
482 }
483 break;
484 case 'w': isn_type = ISN_LOADW; break;
485 case 't': isn_type = ISN_LOADT; break;
486 case 'b': isn_type = ISN_LOADB; break;
487 default: // cannot happen, just in case
488 semsg(_(e_namespace_not_supported_str), *arg);
489 goto theend;
490 }
491 if (isn_type != ISN_DROP)
492 {
493 // Global, Buffer-local, Window-local and Tabpage-local
494 // variables can be defined later, thus we don't check if it
495 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000496 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000497 }
498 }
499 }
500 else
501 {
502 size_t len = end - *arg;
503 int idx;
504 int gen_load = FALSE;
505 int gen_load_outer = 0;
506
507 name = vim_strnsave(*arg, end - *arg);
508 if (name == NULL)
509 return FAIL;
510
511 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
512 {
513 script_autoload(name, FALSE);
514 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
515 }
516 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
517 == OK)
518 {
519 if (gen_load_outer == 0)
520 gen_load = TRUE;
521 }
522 else
523 {
524 lvar_T lvar;
525
526 if (lookup_local(*arg, len, &lvar, cctx) == OK)
527 {
528 type = lvar.lv_type;
529 idx = lvar.lv_idx;
530 if (lvar.lv_from_outer != 0)
531 gen_load_outer = lvar.lv_from_outer;
532 else
533 gen_load = TRUE;
534 }
535 else
536 {
537 // "var" can be script-local even without using "s:" if it
538 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000539 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000540 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000541 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
542
543 // When evaluating an expression and the name starts with an
544 // uppercase letter it can be a user defined function.
545 // generate_funcref() will fail if the function can't be found.
546 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000547 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000548 }
549 }
550 if (gen_load)
551 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
552 if (gen_load_outer > 0)
553 {
554 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
555 cctx->ctx_outer_used = TRUE;
556 }
557 }
558
559 *arg = end;
560
561theend:
562 if (res == FAIL && error && called_emsg == prev_called_emsg)
563 semsg(_(e_variable_not_found_str), name);
564 vim_free(name);
565 return res;
566}
567
568/*
569 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100570 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000571 * Returns FAIL if compilation fails.
572 */
573 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100574compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000575{
LemonBoyf3b48952022-05-05 13:53:03 +0100576 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000577 garray_T save_ga = cctx->ctx_instr;
578 int expr_res;
579 int trailing_error;
580 int instr_count;
581 isn_T *instr = NULL;
582
583 // Remove the string type from the stack.
584 --cctx->ctx_type_stack.ga_len;
585
586 // Temporarily reset the list of instructions so that the jump labels are
587 // correct.
588 cctx->ctx_instr.ga_len = 0;
589 cctx->ctx_instr.ga_maxlen = 0;
590 cctx->ctx_instr.ga_data = NULL;
591 expr_res = compile_expr0(&s, cctx);
592 s = skipwhite(s);
593 trailing_error = *s != NUL;
594
595 if (expr_res == FAIL || trailing_error
596 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
597 {
598 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000599 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600 clear_instr_ga(&cctx->ctx_instr);
601 cctx->ctx_instr = save_ga;
602 ++cctx->ctx_type_stack.ga_len;
603 return FAIL;
604 }
605
606 // Move the generated instructions into the ISN_INSTR instruction, then
607 // restore the list of instructions.
608 instr_count = cctx->ctx_instr.ga_len;
609 instr = cctx->ctx_instr.ga_data;
610 instr[instr_count].isn_type = ISN_FINISH;
611
612 cctx->ctx_instr = save_ga;
613 vim_free(isn->isn_arg.string);
614 isn->isn_type = ISN_INSTR;
615 isn->isn_arg.instr = instr;
616 return OK;
617}
618
619/*
LemonBoyf3b48952022-05-05 13:53:03 +0100620 * List of special functions for "compile_arguments".
621 */
622typedef enum {
623 CA_NOT_SPECIAL,
624 CA_SEARCHPAIR, // {skip} in searchpair() and searchpairpos()
625 CA_SUBSTITUTE, // {sub} in substitute(), when prefixed with \=
626} ca_special_T;
627
628/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000629 * Compile the argument expressions.
630 * "arg" points to just after the "(" and is advanced to after the ")"
631 */
632 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100633compile_arguments(
634 char_u **arg,
635 cctx_T *cctx,
636 int *argcount,
637 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000638{
639 char_u *p = *arg;
640 char_u *whitep = *arg;
641 int must_end = FALSE;
642 int instr_count;
643
644 for (;;)
645 {
646 if (may_get_next_line(whitep, &p, cctx) == FAIL)
647 goto failret;
648 if (*p == ')')
649 {
650 *arg = p + 1;
651 return OK;
652 }
653 if (must_end)
654 {
655 semsg(_(e_missing_comma_before_argument_str), p);
656 return FAIL;
657 }
658
659 instr_count = cctx->ctx_instr.ga_len;
660 if (compile_expr0(&p, cctx) == FAIL)
661 return FAIL;
662 ++*argcount;
663
LemonBoyf3b48952022-05-05 13:53:03 +0100664 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000665 && cctx->ctx_instr.ga_len == instr_count + 1)
666 {
667 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
668
669 // {skip} argument of searchpair() can be compiled if not empty
670 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100671 compile_string(isn, cctx, 0);
672 }
673 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
674 && cctx->ctx_instr.ga_len == instr_count + 1)
675 {
676 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
677
678 // {sub} argument of substitute() can be compiled if it starts
679 // with \=
680 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
681 && isn->isn_arg.string[1] == '=')
682 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000683 }
684
685 if (*p != ',' && *skipwhite(p) == ',')
686 {
687 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
688 p = skipwhite(p);
689 }
690 if (*p == ',')
691 {
692 ++p;
693 if (*p != NUL && !VIM_ISWHITE(*p))
694 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
695 }
696 else
697 must_end = TRUE;
698 whitep = p;
699 p = skipwhite(p);
700 }
701failret:
702 emsg(_(e_missing_closing_paren));
703 return FAIL;
704}
705
706/*
707 * Compile a function call: name(arg1, arg2)
708 * "arg" points to "name", "arg + varlen" to the "(".
709 * "argcount_init" is 1 for "value->method()"
710 * Instructions:
711 * EVAL arg1
712 * EVAL arg2
713 * BCALL / DCALL / UCALL
714 */
715 static int
716compile_call(
717 char_u **arg,
718 size_t varlen,
719 cctx_T *cctx,
720 ppconst_T *ppconst,
721 int argcount_init)
722{
723 char_u *name = *arg;
724 char_u *p;
725 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100726 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 char_u fname_buf[FLEN_FIXED + 1];
728 char_u *tofree = NULL;
729 int error = FCERR_NONE;
730 ufunc_T *ufunc = NULL;
731 int res = FAIL;
732 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000733 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100734 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000735 imported_T *import;
736
737 if (varlen >= sizeof(namebuf))
738 {
739 semsg(_(e_name_too_long_str), name);
740 return FAIL;
741 }
742 vim_strncpy(namebuf, *arg, varlen);
743
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000744 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000745 if (import != NULL)
746 {
747 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
748 return FAIL;
749 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750
751 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100752 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100754 if ((varlen == 3
755 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
757 {
758 char_u *s = skipwhite(*arg + varlen + 1);
759 typval_T argvars[2];
760 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100761 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762
763 argvars[0].v_type = VAR_UNKNOWN;
764 if (*s == '"')
765 (void)eval_string(&s, &argvars[0], TRUE);
766 else if (*s == '\'')
767 (void)eval_lit_string(&s, &argvars[0], TRUE);
768 s = skipwhite(s);
769 if (*s == ')' && argvars[0].v_type == VAR_STRING
770 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
771 || !is_has))
772 {
773 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
774
775 *arg = s + 1;
776 argvars[1].v_type = VAR_UNKNOWN;
777 tv->v_type = VAR_NUMBER;
778 tv->vval.v_number = 0;
779 if (is_has)
780 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100781 else if (is_len)
782 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783 else
784 f_exists(argvars, tv);
785 clear_tv(&argvars[0]);
786 ++ppconst->pp_used;
787 return OK;
788 }
789 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100790 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 {
792 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
793 return FAIL;
794 }
795 }
796
797 if (generate_ppconst(cctx, ppconst) == FAIL)
798 return FAIL;
799
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000800 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
801
802 // We handle the "skip" argument of searchpair() and searchpairpos()
803 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100804 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
805 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
806 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
807 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
808 special_fn = CA_SEARCHPAIR;
809 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
810 special_fn = CA_SUBSTITUTE;
811 else
812 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000813
814 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100815 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 goto theend;
817
818 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
819 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
820 {
821 int idx;
822
823 // builtin function
824 idx = find_internal_func(name);
825 if (idx >= 0)
826 {
827 if (STRCMP(name, "flatten") == 0)
828 {
829 emsg(_(e_cannot_use_flatten_in_vim9_script));
830 goto theend;
831 }
832
833 if (STRCMP(name, "add") == 0 && argcount == 2)
834 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000835 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000836
837 // add() can be compiled to instructions if we know the type
838 if (type->tt_type == VAR_LIST)
839 {
840 // inline "add(list, item)" so that the type can be checked
841 res = generate_LISTAPPEND(cctx);
842 idx = -1;
843 }
844 else if (type->tt_type == VAR_BLOB)
845 {
846 // inline "add(blob, nr)" so that the type can be checked
847 res = generate_BLOBAPPEND(cctx);
848 idx = -1;
849 }
850 }
851
852 if (idx >= 0)
853 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
854 }
855 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100856 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857 goto theend;
858 }
859
Bram Moolenaar62aec932022-01-29 21:45:34 +0000860 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
861
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000862 // An argument or local variable can be a function reference, this
863 // overrules a function name.
864 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
865 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
866 {
867 // If we can find the function by name generate the right call.
868 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000869 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000870 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000872 if (!func_is_global(ufunc))
873 {
874 res = generate_CALL(cctx, ufunc, argcount);
875 goto theend;
876 }
877 if (!has_g_namespace
878 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
879 {
880 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100881 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000882 goto theend;
883 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884 }
885 }
886
887 // If the name is a variable, load it and use PCALL.
888 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000889 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000890 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000891 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
893 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000894 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895
896 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
897 goto theend;
898 }
899
900 // If we can find a global function by name generate the right call.
901 if (ufunc != NULL)
902 {
903 res = generate_CALL(cctx, ufunc, argcount);
904 goto theend;
905 }
906
907 // A global function may be defined only later. Need to figure out at
908 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000909 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 res = generate_UCALL(cctx, name, argcount);
911 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100912 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000913
914theend:
915 vim_free(tofree);
916 return res;
917}
918
919// like NAMESPACE_CHAR but with 'a' and 'l'.
920#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
921
922/*
923 * Find the end of a variable or function name. Unlike find_name_end() this
924 * does not recognize magic braces.
925 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
926 * Return a pointer to just after the name. Equal to "arg" if there is no
927 * valid name.
928 */
929 char_u *
930to_name_end(char_u *arg, int use_namespace)
931{
932 char_u *p;
933
934 // Quick check for valid starting character.
935 if (!eval_isnamec1(*arg))
936 return arg;
937
938 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
939 // Include a namespace such as "s:var" and "v:var". But "n:" is not
940 // and can be used in slice "[n:]".
941 if (*p == ':' && (p != arg + 1
942 || !use_namespace
943 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
944 break;
945 return p;
946}
947
948/*
949 * Like to_name_end() but also skip over a list or dict constant.
950 * Also accept "<SNR>123_Func".
951 * This intentionally does not handle line continuation.
952 */
953 char_u *
954to_name_const_end(char_u *arg)
955{
956 char_u *p = arg;
957 typval_T rettv;
958
959 if (STRNCMP(p, "<SNR>", 5) == 0)
960 p = skipdigits(p + 5);
961 p = to_name_end(p, TRUE);
962 if (p == arg && *arg == '[')
963 {
964
965 // Can be "[1, 2, 3]->Func()".
966 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
967 p = arg;
968 }
969 return p;
970}
971
972/*
973 * parse a list: [expr, expr]
974 * "*arg" points to the '['.
975 * ppconst->pp_is_const is set if all items are a constant.
976 */
977 static int
978compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
979{
980 char_u *p = skipwhite(*arg + 1);
981 char_u *whitep = *arg + 1;
982 int count = 0;
983 int is_const;
984 int is_all_const = TRUE; // reset when non-const encountered
985
986 for (;;)
987 {
988 if (may_get_next_line(whitep, &p, cctx) == FAIL)
989 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000990 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000991 return FAIL;
992 }
993 if (*p == ',')
994 {
995 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
996 return FAIL;
997 }
998 if (*p == ']')
999 {
1000 ++p;
1001 break;
1002 }
1003 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1004 return FAIL;
1005 if (!is_const)
1006 is_all_const = FALSE;
1007 ++count;
1008 if (*p == ',')
1009 {
1010 ++p;
1011 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1012 {
1013 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1014 return FAIL;
1015 }
1016 }
1017 whitep = p;
1018 p = skipwhite(p);
1019 }
1020 *arg = p;
1021
1022 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001023 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001024}
1025
1026/*
1027 * Parse a lambda: "(arg, arg) => expr"
1028 * "*arg" points to the '('.
1029 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1030 */
1031 static int
1032compile_lambda(char_u **arg, cctx_T *cctx)
1033{
1034 int r;
1035 typval_T rettv;
1036 ufunc_T *ufunc;
1037 evalarg_T evalarg;
1038
1039 init_evalarg(&evalarg);
1040 evalarg.eval_flags = EVAL_EVALUATE;
1041 evalarg.eval_cctx = cctx;
1042
1043 // Get the funcref in "rettv".
1044 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1045 if (r != OK)
1046 {
1047 clear_evalarg(&evalarg, NULL);
1048 return r;
1049 }
1050
1051 // "rettv" will now be a partial referencing the function.
1052 ufunc = rettv.vval.v_partial->pt_func;
1053 ++ufunc->uf_refcount;
1054 clear_tv(&rettv);
1055
1056 // Compile it here to get the return type. The return type is optional,
1057 // when it's missing use t_unknown. This is recognized in
1058 // compile_return().
1059 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1060 ufunc->uf_ret_type = &t_unknown;
1061 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1062
1063 // When the outer function is compiled for profiling or debugging, the
1064 // lambda may be called without profiling or debugging. Compile it here in
1065 // the right context.
1066 if (cctx->ctx_compile_type == CT_DEBUG
1067#ifdef FEAT_PROFILE
1068 || cctx->ctx_compile_type == CT_PROFILE
1069#endif
1070 )
1071 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1072
Bram Moolenaar139575d2022-03-15 19:29:30 +00001073 // if the outer function is not compiled for debugging or profiling, this
1074 // one might be
1075 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001076 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001077 compiletype_T compile_type = get_compile_type(ufunc);
1078
1079 if (compile_type != CT_NONE)
1080 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001081 }
1082
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001083 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1084 // "*arg" may point into it. Point into the original line to avoid a
1085 // dangling pointer.
1086 if (evalarg.eval_using_cmdline)
1087 {
1088 garray_T *gap = &evalarg.eval_tofree_ga;
1089 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1090
1091 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1092 + off;
1093 }
1094
1095 clear_evalarg(&evalarg, NULL);
1096
1097 if (ufunc->uf_def_status == UF_COMPILED)
1098 {
1099 // The return type will now be known.
1100 set_function_type(ufunc);
1101
1102 // The function reference count will be 1. When the ISN_FUNCREF
1103 // instruction is deleted the reference count is decremented and the
1104 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001105 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 }
1107
1108 func_ptr_unref(ufunc);
1109 return FAIL;
1110}
1111
1112/*
1113 * Get a lambda and compile it. Uses Vim9 syntax.
1114 */
1115 int
1116get_lambda_tv_and_compile(
1117 char_u **arg,
1118 typval_T *rettv,
1119 int types_optional,
1120 evalarg_T *evalarg)
1121{
1122 int r;
1123 ufunc_T *ufunc;
1124 int save_sc_version = current_sctx.sc_version;
1125
1126 // Get the funcref in "rettv".
1127 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1128 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1129 current_sctx.sc_version = save_sc_version;
1130 if (r != OK)
1131 return r;
1132
1133 // "rettv" will now be a partial referencing the function.
1134 ufunc = rettv->vval.v_partial->pt_func;
1135
1136 // Compile it here to get the return type. The return type is optional,
1137 // when it's missing use t_unknown. This is recognized in
1138 // compile_return().
1139 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1140 ufunc->uf_ret_type = &t_unknown;
1141 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1142
1143 if (ufunc->uf_def_status == UF_COMPILED)
1144 {
1145 // The return type will now be known.
1146 set_function_type(ufunc);
1147 return OK;
1148 }
1149 clear_tv(rettv);
1150 return FAIL;
1151}
1152
1153/*
1154 * parse a dict: {key: val, [key]: val}
1155 * "*arg" points to the '{'.
1156 * ppconst->pp_is_const is set if all item values are a constant.
1157 */
1158 static int
1159compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1160{
1161 garray_T *instr = &cctx->ctx_instr;
1162 int count = 0;
1163 dict_T *d = dict_alloc();
1164 dictitem_T *item;
1165 char_u *whitep = *arg + 1;
1166 char_u *p;
1167 int is_const;
1168 int is_all_const = TRUE; // reset when non-const encountered
1169
1170 if (d == NULL)
1171 return FAIL;
1172 if (generate_ppconst(cctx, ppconst) == FAIL)
1173 return FAIL;
1174 for (;;)
1175 {
1176 char_u *key = NULL;
1177
1178 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1179 {
1180 *arg = NULL;
1181 goto failret;
1182 }
1183
1184 if (**arg == '}')
1185 break;
1186
1187 if (**arg == '[')
1188 {
1189 isn_T *isn;
1190
1191 // {[expr]: value} uses an evaluated key.
1192 *arg = skipwhite(*arg + 1);
1193 if (compile_expr0(arg, cctx) == FAIL)
1194 return FAIL;
1195 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1196 if (isn->isn_type == ISN_PUSHNR)
1197 {
1198 char buf[NUMBUFLEN];
1199
1200 // Convert to string at compile time.
1201 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1202 isn->isn_type = ISN_PUSHS;
1203 isn->isn_arg.string = vim_strsave((char_u *)buf);
1204 }
1205 if (isn->isn_type == ISN_PUSHS)
1206 key = isn->isn_arg.string;
1207 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1208 return FAIL;
1209 *arg = skipwhite(*arg);
1210 if (**arg != ']')
1211 {
1212 emsg(_(e_missing_matching_bracket_after_dict_key));
1213 return FAIL;
1214 }
1215 ++*arg;
1216 }
1217 else
1218 {
1219 // {"name": value},
1220 // {'name': value},
1221 // {name: value} use "name" as a literal key
1222 key = get_literal_key(arg);
1223 if (key == NULL)
1224 return FAIL;
1225 if (generate_PUSHS(cctx, &key) == FAIL)
1226 return FAIL;
1227 }
1228
1229 // Check for duplicate keys, if using string keys.
1230 if (key != NULL)
1231 {
1232 item = dict_find(d, key, -1);
1233 if (item != NULL)
1234 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001235 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001236 goto failret;
1237 }
1238 item = dictitem_alloc(key);
1239 if (item != NULL)
1240 {
1241 item->di_tv.v_type = VAR_UNKNOWN;
1242 item->di_tv.v_lock = 0;
1243 if (dict_add(d, item) == FAIL)
1244 dictitem_free(item);
1245 }
1246 }
1247
1248 if (**arg != ':')
1249 {
1250 if (*skipwhite(*arg) == ':')
1251 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1252 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001253 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001254 return FAIL;
1255 }
1256 whitep = *arg + 1;
1257 if (!IS_WHITE_OR_NUL(*whitep))
1258 {
1259 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1260 return FAIL;
1261 }
1262
1263 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1264 {
1265 *arg = NULL;
1266 goto failret;
1267 }
1268
1269 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1270 return FAIL;
1271 if (!is_const)
1272 is_all_const = FALSE;
1273 ++count;
1274
1275 whitep = *arg;
1276 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1277 {
1278 *arg = NULL;
1279 goto failret;
1280 }
1281 if (**arg == '}')
1282 break;
1283 if (**arg != ',')
1284 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001285 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001286 goto failret;
1287 }
1288 if (IS_WHITE_OR_NUL(*whitep))
1289 {
1290 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1291 return FAIL;
1292 }
1293 whitep = *arg + 1;
1294 if (!IS_WHITE_OR_NUL(*whitep))
1295 {
1296 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1297 return FAIL;
1298 }
1299 *arg = skipwhite(whitep);
1300 }
1301
1302 *arg = *arg + 1;
1303
1304 // Allow for following comment, after at least one space.
1305 p = skipwhite(*arg);
1306 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1307 *arg += STRLEN(*arg);
1308
1309 dict_unref(d);
1310 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001311 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312
1313failret:
1314 if (*arg == NULL)
1315 {
1316 semsg(_(e_missing_dict_end), _("[end of lines]"));
1317 *arg = (char_u *)"";
1318 }
1319 dict_unref(d);
1320 return FAIL;
1321}
1322
1323/*
1324 * Compile "&option".
1325 */
1326 static int
1327compile_get_option(char_u **arg, cctx_T *cctx)
1328{
1329 typval_T rettv;
1330 char_u *start = *arg;
1331 int ret;
1332
1333 // parse the option and get the current value to get the type.
1334 rettv.v_type = VAR_UNKNOWN;
1335 ret = eval_option(arg, &rettv, TRUE);
1336 if (ret == OK)
1337 {
1338 // include the '&' in the name, eval_option() expects it.
1339 char_u *name = vim_strnsave(start, *arg - start);
1340 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1341 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1342
1343 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1344 vim_free(name);
1345 }
1346 clear_tv(&rettv);
1347
1348 return ret;
1349}
1350
1351/*
1352 * Compile "$VAR".
1353 */
1354 static int
1355compile_get_env(char_u **arg, cctx_T *cctx)
1356{
1357 char_u *start = *arg;
1358 int len;
1359 int ret;
1360 char_u *name;
1361
1362 ++*arg;
1363 len = get_env_len(arg);
1364 if (len == 0)
1365 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001366 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367 return FAIL;
1368 }
1369
1370 // include the '$' in the name, eval_env_var() expects it.
1371 name = vim_strnsave(start, len + 1);
1372 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1373 vim_free(name);
1374 return ret;
1375}
1376
1377/*
LemonBoy2eaef102022-05-06 13:14:50 +01001378 * Compile "$"string"" or "$'string'".
1379 */
1380 static int
1381compile_interp_string(char_u **arg, cctx_T *cctx)
1382{
1383 typval_T tv;
1384 int ret;
1385 int evaluate = cctx->ctx_skip != SKIP_YES;
1386
1387 // *arg is on the '$' character.
1388 (*arg)++;
1389
1390 if (**arg == '"')
1391 ret = eval_string(arg, &tv, evaluate);
1392 else
1393 ret = eval_lit_string(arg, &tv, evaluate);
1394
1395 if (ret == FAIL || !evaluate)
1396 return ret;
1397
1398 ret = compile_all_expr_in_str(tv.vval.v_string, TRUE, cctx);
1399 clear_tv(&tv);
1400
1401 return ret;
1402}
1403
1404/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001405 * Compile "@r".
1406 */
1407 static int
1408compile_get_register(char_u **arg, cctx_T *cctx)
1409{
1410 int ret;
1411
1412 ++*arg;
1413 if (**arg == NUL)
1414 {
1415 semsg(_(e_syntax_error_at_str), *arg - 1);
1416 return FAIL;
1417 }
1418 if (!valid_yank_reg(**arg, FALSE))
1419 {
1420 emsg_invreg(**arg);
1421 return FAIL;
1422 }
1423 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1424 ++*arg;
1425 return ret;
1426}
1427
1428/*
1429 * Apply leading '!', '-' and '+' to constant "rettv".
1430 * When "numeric_only" is TRUE do not apply '!'.
1431 */
1432 static int
1433apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1434{
1435 char_u *p = *end;
1436
1437 // this works from end to start
1438 while (p > start)
1439 {
1440 --p;
1441 if (*p == '-' || *p == '+')
1442 {
1443 // only '-' has an effect, for '+' we only check the type
1444#ifdef FEAT_FLOAT
1445 if (rettv->v_type == VAR_FLOAT)
1446 {
1447 if (*p == '-')
1448 rettv->vval.v_float = -rettv->vval.v_float;
1449 }
1450 else
1451#endif
1452 {
1453 varnumber_T val;
1454 int error = FALSE;
1455
1456 // tv_get_number_chk() accepts a string, but we don't want that
1457 // here
1458 if (check_not_string(rettv) == FAIL)
1459 return FAIL;
1460 val = tv_get_number_chk(rettv, &error);
1461 clear_tv(rettv);
1462 if (error)
1463 return FAIL;
1464 if (*p == '-')
1465 val = -val;
1466 rettv->v_type = VAR_NUMBER;
1467 rettv->vval.v_number = val;
1468 }
1469 }
1470 else if (numeric_only)
1471 {
1472 ++p;
1473 break;
1474 }
1475 else if (*p == '!')
1476 {
1477 int v = tv2bool(rettv);
1478
1479 // '!' is permissive in the type.
1480 clear_tv(rettv);
1481 rettv->v_type = VAR_BOOL;
1482 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1483 }
1484 }
1485 *end = p;
1486 return OK;
1487}
1488
1489/*
1490 * Recognize v: variables that are constants and set "rettv".
1491 */
1492 static void
1493get_vim_constant(char_u **arg, typval_T *rettv)
1494{
1495 if (STRNCMP(*arg, "v:true", 6) == 0)
1496 {
1497 rettv->v_type = VAR_BOOL;
1498 rettv->vval.v_number = VVAL_TRUE;
1499 *arg += 6;
1500 }
1501 else if (STRNCMP(*arg, "v:false", 7) == 0)
1502 {
1503 rettv->v_type = VAR_BOOL;
1504 rettv->vval.v_number = VVAL_FALSE;
1505 *arg += 7;
1506 }
1507 else if (STRNCMP(*arg, "v:null", 6) == 0)
1508 {
1509 rettv->v_type = VAR_SPECIAL;
1510 rettv->vval.v_number = VVAL_NULL;
1511 *arg += 6;
1512 }
1513 else if (STRNCMP(*arg, "v:none", 6) == 0)
1514 {
1515 rettv->v_type = VAR_SPECIAL;
1516 rettv->vval.v_number = VVAL_NONE;
1517 *arg += 6;
1518 }
1519}
1520
1521 exprtype_T
1522get_compare_type(char_u *p, int *len, int *type_is)
1523{
1524 exprtype_T type = EXPR_UNKNOWN;
1525 int i;
1526
1527 switch (p[0])
1528 {
1529 case '=': if (p[1] == '=')
1530 type = EXPR_EQUAL;
1531 else if (p[1] == '~')
1532 type = EXPR_MATCH;
1533 break;
1534 case '!': if (p[1] == '=')
1535 type = EXPR_NEQUAL;
1536 else if (p[1] == '~')
1537 type = EXPR_NOMATCH;
1538 break;
1539 case '>': if (p[1] != '=')
1540 {
1541 type = EXPR_GREATER;
1542 *len = 1;
1543 }
1544 else
1545 type = EXPR_GEQUAL;
1546 break;
1547 case '<': if (p[1] != '=')
1548 {
1549 type = EXPR_SMALLER;
1550 *len = 1;
1551 }
1552 else
1553 type = EXPR_SEQUAL;
1554 break;
1555 case 'i': if (p[1] == 's')
1556 {
1557 // "is" and "isnot"; but not a prefix of a name
1558 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1559 *len = 5;
1560 i = p[*len];
1561 if (!isalnum(i) && i != '_')
1562 {
1563 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1564 *type_is = TRUE;
1565 }
1566 }
1567 break;
1568 }
1569 return type;
1570}
1571
1572/*
1573 * Skip over an expression, ignoring most errors.
1574 */
1575 void
1576skip_expr_cctx(char_u **arg, cctx_T *cctx)
1577{
1578 evalarg_T evalarg;
1579
1580 init_evalarg(&evalarg);
1581 evalarg.eval_cctx = cctx;
1582 skip_expr(arg, &evalarg);
1583 clear_evalarg(&evalarg, NULL);
1584}
1585
1586/*
1587 * Check that the top of the type stack has a type that can be used as a
1588 * condition. Give an error and return FAIL if not.
1589 */
1590 int
1591bool_on_stack(cctx_T *cctx)
1592{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001593 type_T *type;
1594
Bram Moolenaar078a4612022-01-04 15:17:03 +00001595 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001596 if (type == &t_bool)
1597 return OK;
1598
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001599 if (type == &t_any
1600 || type == &t_unknown
1601 || type == &t_number
1602 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001603 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1604 // This requires a runtime type check.
1605 return generate_COND2BOOL(cctx);
1606
1607 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1608}
1609
1610/*
1611 * Give the "white on both sides" error, taking the operator from "p[len]".
1612 */
1613 void
1614error_white_both(char_u *op, int len)
1615{
1616 char_u buf[10];
1617
1618 vim_strncpy(buf, op, len);
1619 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1620}
1621
1622/*
1623 * Compile code to apply '-', '+' and '!'.
1624 * When "numeric_only" is TRUE do not apply '!'.
1625 */
1626 static int
1627compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1628{
1629 char_u *p = *end;
1630
1631 // this works from end to start
1632 while (p > start)
1633 {
1634 --p;
1635 while (VIM_ISWHITE(*p))
1636 --p;
1637 if (*p == '-' || *p == '+')
1638 {
1639 int negate = *p == '-';
1640 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001641 type_T *type;
1642
Bram Moolenaar078a4612022-01-04 15:17:03 +00001643 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001644 if (type != &t_float && need_type(type, &t_number,
1645 -1, 0, cctx, FALSE, FALSE) == FAIL)
1646 return FAIL;
1647
1648 while (p > start && (p[-1] == '-' || p[-1] == '+'))
1649 {
1650 --p;
1651 if (*p == '-')
1652 negate = !negate;
1653 }
1654 // only '-' has an effect, for '+' we only check the type
1655 if (negate)
1656 {
1657 isn = generate_instr(cctx, ISN_NEGATENR);
1658 if (isn == NULL)
1659 return FAIL;
1660 }
1661 }
1662 else if (numeric_only)
1663 {
1664 ++p;
1665 break;
1666 }
1667 else
1668 {
1669 int invert = *p == '!';
1670
1671 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1672 {
1673 if (p[-1] == '!')
1674 invert = !invert;
1675 --p;
1676 }
1677 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1678 return FAIL;
1679 }
1680 }
1681 *end = p;
1682 return OK;
1683}
1684
1685/*
1686 * Compile "(expression)": recursive!
1687 * Return FAIL/OK.
1688 */
1689 static int
1690compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1691{
1692 int ret;
1693 char_u *p = *arg + 1;
1694
1695 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1696 return FAIL;
1697 if (ppconst->pp_used <= PPSIZE - 10)
1698 {
1699 ret = compile_expr1(arg, cctx, ppconst);
1700 }
1701 else
1702 {
1703 // Not enough space in ppconst, flush constants.
1704 if (generate_ppconst(cctx, ppconst) == FAIL)
1705 return FAIL;
1706 ret = compile_expr0(arg, cctx);
1707 }
1708 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1709 return FAIL;
1710 if (**arg == ')')
1711 ++*arg;
1712 else if (ret == OK)
1713 {
1714 emsg(_(e_missing_closing_paren));
1715 ret = FAIL;
1716 }
1717 return ret;
1718}
1719
Bram Moolenaarc7349932022-01-16 20:59:39 +00001720static int compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1721
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001722/*
1723 * Compile whatever comes after "name" or "name()".
1724 * Advances "*arg" only when something was recognized.
1725 */
1726 static int
1727compile_subscript(
1728 char_u **arg,
1729 cctx_T *cctx,
1730 char_u *start_leader,
1731 char_u **end_leader,
1732 ppconst_T *ppconst)
1733{
1734 char_u *name_start = *end_leader;
1735 int keeping_dict = FALSE;
1736
1737 for (;;)
1738 {
1739 char_u *p = skipwhite(*arg);
1740
1741 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1742 {
1743 char_u *next = peek_next_line_from_context(cctx);
1744
1745 // If a following line starts with "->{" or "->X" advance to that
1746 // line, so that a line break before "->" is allowed.
1747 // Also if a following line starts with ".x".
1748 if (next != NULL &&
1749 ((next[0] == '-' && next[1] == '>'
1750 && (next[2] == '{'
1751 || ASCII_ISALPHA(*skipwhite(next + 2))))
1752 || (next[0] == '.' && eval_isdictc(next[1]))))
1753 {
1754 next = next_line_from_context(cctx, TRUE);
1755 if (next == NULL)
1756 return FAIL;
1757 *arg = next;
1758 p = skipwhite(*arg);
1759 }
1760 }
1761
1762 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1763 // is not a function call.
1764 if (**arg == '(')
1765 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 type_T *type;
1767 int argcount = 0;
1768
1769 if (generate_ppconst(cctx, ppconst) == FAIL)
1770 return FAIL;
1771 ppconst->pp_is_const = FALSE;
1772
1773 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001774 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775
1776 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001777 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001778 return FAIL;
1779 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1780 return FAIL;
1781 if (keeping_dict)
1782 {
1783 keeping_dict = FALSE;
1784 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1785 return FAIL;
1786 }
1787 }
1788 else if (*p == '-' && p[1] == '>')
1789 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001790 char_u *pstart = p;
1791 int alt;
1792 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793
Bram Moolenaarc7349932022-01-16 20:59:39 +00001794 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001795 if (generate_ppconst(cctx, ppconst) == FAIL)
1796 return FAIL;
1797 ppconst->pp_is_const = FALSE;
1798
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799 // Apply the '!', '-' and '+' first:
1800 // -1.0->func() works like (-1.0)->func()
1801 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1802 return FAIL;
1803
1804 p += 2;
1805 *arg = skipwhite(p);
1806 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001807
1808 // Three alternatives handled here:
1809 // 1. "base->name(" only a name, use compile_call()
1810 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1811 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001813 alt = 2;
1814 else
1815 {
1816 // alternative 1 or 3
1817 p = *arg;
1818 if (!eval_isnamec1(*p))
1819 {
1820 semsg(_(e_trailing_characters_str), pstart);
1821 return FAIL;
1822 }
1823 if (ASCII_ISALPHA(*p) && p[1] == ':')
1824 p += 2;
1825 for ( ; eval_isnamec(*p); ++p)
1826 ;
1827 if (*p == '(')
1828 {
1829 // alternative 1
1830 alt = 1;
1831 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1832 return FAIL;
1833 }
1834 else
1835 {
1836 // Must be alternative 3, find the "(". Only works within
1837 // one line.
1838 alt = 3;
1839 paren = vim_strchr(p, '(');
1840 if (paren == NULL)
1841 {
1842 semsg(_(e_missing_parenthesis_str), *arg);
1843 return FAIL;
1844 }
1845 }
1846 }
1847
1848 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 {
1850 int argcount = 1;
1851 garray_T *stack = &cctx->ctx_type_stack;
1852 int type_idx_start = stack->ga_len;
1853 type_T *type;
1854 int expr_isn_start = cctx->ctx_instr.ga_len;
1855 int expr_isn_end;
1856 int arg_isn_count;
1857
Bram Moolenaarc7349932022-01-16 20:59:39 +00001858 if (alt == 2)
1859 {
1860 // Funcref call: list->(Refs[2])(arg)
1861 // or lambda: list->((arg) => expr)(arg)
1862 //
1863 // Fist compile the function expression.
1864 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1865 return FAIL;
1866 }
1867 else
1868 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001869 int fail;
1870 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
1871
Bram Moolenaarc7349932022-01-16 20:59:39 +00001872 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001873
1874 // instead of using LOADG for "import.Func" use PUSHFUNC
1875 ++paren_follows_after_expr;
1876
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001877 // do not look in the next line
1878 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001879
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001880 fail = compile_expr8(arg, cctx, ppconst) == FAIL
1881 || *skipwhite(*arg) != NUL;
1882 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001883 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001884 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001885
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001886 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001887 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001888 semsg(_(e_invalid_expression_str), pstart);
1889 return FAIL;
1890 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001891 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001892
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001893 // Compile the arguments.
1894 if (**arg != '(')
1895 {
1896 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001897 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001898 else
1899 semsg(_(e_missing_parenthesis_str), *arg);
1900 return FAIL;
1901 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001902
1903 // Remember the next instruction index, where the instructions
1904 // for arguments are being written.
1905 expr_isn_end = cctx->ctx_instr.ga_len;
1906
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001908 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1909 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 return FAIL;
1911
1912 // Move the instructions for the arguments to before the
1913 // instructions of the expression and move the type of the
1914 // expression after the argument types. This is what ISN_PCALL
1915 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001916 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1917 if (arg_isn_count > 0)
1918 {
1919 int expr_isn_count = expr_isn_end - expr_isn_start;
1920 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001921 type_T *decl_type;
1922 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923
1924 if (isn == NULL)
1925 return FAIL;
1926 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1927 + expr_isn_start,
1928 sizeof(isn_T) * expr_isn_count);
1929 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1930 + expr_isn_start,
1931 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1932 sizeof(isn_T) * arg_isn_count);
1933 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1934 + expr_isn_start + arg_isn_count,
1935 isn, sizeof(isn_T) * expr_isn_count);
1936 vim_free(isn);
1937
Bram Moolenaar078a4612022-01-04 15:17:03 +00001938 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1939 type = typep->type_curr;
1940 decl_type = typep->type_decl;
1941 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1942 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1943 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001944 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001945 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1946 typep->type_curr = type;
1947 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948 }
1949
Bram Moolenaar078a4612022-01-04 15:17:03 +00001950 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1952 return FAIL;
1953 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001954
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955 if (keeping_dict)
1956 {
1957 keeping_dict = FALSE;
1958 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1959 return FAIL;
1960 }
1961 }
1962 else if (**arg == '[')
1963 {
1964 int is_slice = FALSE;
1965
1966 // list index: list[123]
1967 // dict member: dict[key]
1968 // string index: text[123]
1969 // blob index: blob[123]
1970 if (generate_ppconst(cctx, ppconst) == FAIL)
1971 return FAIL;
1972 ppconst->pp_is_const = FALSE;
1973
1974 ++p;
1975 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1976 return FAIL;
1977 if (**arg == ':')
1978 {
1979 // missing first index is equal to zero
1980 generate_PUSHNR(cctx, 0);
1981 }
1982 else
1983 {
1984 if (compile_expr0(arg, cctx) == FAIL)
1985 return FAIL;
1986 if (**arg == ':')
1987 {
1988 semsg(_(e_white_space_required_before_and_after_str_at_str),
1989 ":", *arg);
1990 return FAIL;
1991 }
1992 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1993 return FAIL;
1994 *arg = skipwhite(*arg);
1995 }
1996 if (**arg == ':')
1997 {
1998 is_slice = TRUE;
1999 ++*arg;
2000 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2001 {
2002 semsg(_(e_white_space_required_before_and_after_str_at_str),
2003 ":", *arg);
2004 return FAIL;
2005 }
2006 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2007 return FAIL;
2008 if (**arg == ']')
2009 // missing second index is equal to end of string
2010 generate_PUSHNR(cctx, -1);
2011 else
2012 {
2013 if (compile_expr0(arg, cctx) == FAIL)
2014 return FAIL;
2015 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2016 return FAIL;
2017 *arg = skipwhite(*arg);
2018 }
2019 }
2020
2021 if (**arg != ']')
2022 {
2023 emsg(_(e_missing_closing_square_brace));
2024 return FAIL;
2025 }
2026 *arg = *arg + 1;
2027
2028 if (keeping_dict)
2029 {
2030 keeping_dict = FALSE;
2031 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2032 return FAIL;
2033 }
2034 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2035 return FAIL;
2036 }
2037 else if (*p == '.' && p[1] != '.')
2038 {
2039 // dictionary member: dict.name
2040 if (generate_ppconst(cctx, ppconst) == FAIL)
2041 return FAIL;
2042 ppconst->pp_is_const = FALSE;
2043
2044 *arg = p + 1;
2045 if (IS_WHITE_OR_NUL(**arg))
2046 {
2047 emsg(_(e_missing_name_after_dot));
2048 return FAIL;
2049 }
2050 p = *arg;
2051 if (eval_isdictc(*p))
2052 while (eval_isnamec(*p))
2053 MB_PTR_ADV(p);
2054 if (p == *arg)
2055 {
2056 semsg(_(e_syntax_error_at_str), *arg);
2057 return FAIL;
2058 }
2059 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2060 return FAIL;
2061 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2062 return FAIL;
2063 keeping_dict = TRUE;
2064 *arg = p;
2065 }
2066 else
2067 break;
2068 }
2069
2070 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2071 // This needs to be done at runtime to be able to check the type.
2072 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
2073 return FAIL;
2074
2075 return OK;
2076}
2077
2078/*
2079 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2080 * "arg" is advanced until after the expression, skipping white space.
2081 *
2082 * If the value is a constant "ppconst->pp_used" will be non-zero.
2083 * Before instructions are generated, any values in "ppconst" will generated.
2084 *
2085 * This is the compiling equivalent of eval1(), eval2(), etc.
2086 */
2087
2088/*
2089 * number number constant
2090 * 0zFFFFFFFF Blob constant
2091 * "string" string constant
2092 * 'string' literal string constant
2093 * &option-name option value
2094 * @r register contents
2095 * identifier variable value
2096 * function() function call
2097 * $VAR environment variable
2098 * (expression) nested expression
2099 * [expr, expr] List
2100 * {key: val, [key]: val} Dictionary
2101 *
2102 * Also handle:
2103 * ! in front logical NOT
2104 * - in front unary minus
2105 * + in front unary plus (ignored)
2106 * trailing (arg) funcref/partial call
2107 * trailing [] subscript in String or List
2108 * trailing .name entry in Dictionary
2109 * trailing ->name() method call
2110 */
2111 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00002112compile_expr8(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002113 char_u **arg,
2114 cctx_T *cctx,
2115 ppconst_T *ppconst)
2116{
2117 char_u *start_leader, *end_leader;
2118 int ret = OK;
2119 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2120 int used_before = ppconst->pp_used;
2121
2122 ppconst->pp_is_const = FALSE;
2123
2124 /*
2125 * Skip '!', '-' and '+' characters. They are handled later.
2126 */
2127 start_leader = *arg;
2128 if (eval_leader(arg, TRUE) == FAIL)
2129 return FAIL;
2130 end_leader = *arg;
2131
2132 rettv->v_type = VAR_UNKNOWN;
2133 switch (**arg)
2134 {
2135 /*
2136 * Number constant.
2137 */
2138 case '0': // also for blob starting with 0z
2139 case '1':
2140 case '2':
2141 case '3':
2142 case '4':
2143 case '5':
2144 case '6':
2145 case '7':
2146 case '8':
2147 case '9':
2148 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2149 return FAIL;
2150 // Apply "-" and "+" just before the number now, right to
2151 // left. Matters especially when "->" follows. Stops at
2152 // '!'.
2153 if (apply_leader(rettv, TRUE,
2154 start_leader, &end_leader) == FAIL)
2155 {
2156 clear_tv(rettv);
2157 return FAIL;
2158 }
2159 break;
2160
2161 /*
2162 * String constant: "string".
2163 */
2164 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
2165 return FAIL;
2166 break;
2167
2168 /*
2169 * Literal string constant: 'str''ing'.
2170 */
2171 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
2172 return FAIL;
2173 break;
2174
2175 /*
2176 * Constant Vim variable.
2177 */
2178 case 'v': get_vim_constant(arg, rettv);
2179 ret = NOTDONE;
2180 break;
2181
2182 /*
2183 * "true" constant
2184 */
2185 case 't': if (STRNCMP(*arg, "true", 4) == 0
2186 && !eval_isnamec((*arg)[4]))
2187 {
2188 *arg += 4;
2189 rettv->v_type = VAR_BOOL;
2190 rettv->vval.v_number = VVAL_TRUE;
2191 }
2192 else
2193 ret = NOTDONE;
2194 break;
2195
2196 /*
2197 * "false" constant
2198 */
2199 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2200 && !eval_isnamec((*arg)[5]))
2201 {
2202 *arg += 5;
2203 rettv->v_type = VAR_BOOL;
2204 rettv->vval.v_number = VVAL_FALSE;
2205 }
2206 else
2207 ret = NOTDONE;
2208 break;
2209
2210 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002211 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002212 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002213 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002214 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002215 char_u *p = *arg + 4;
2216 int len;
2217
2218 for (len = 0; eval_isnamec(p[len]); ++len)
2219 ;
2220 ret = handle_predefined(*arg, len + 4, rettv);
2221 if (ret == FAIL)
2222 ret = NOTDONE;
2223 else
2224 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002225 }
2226 else
2227 ret = NOTDONE;
2228 break;
2229
2230 /*
2231 * List: [expr, expr]
2232 */
2233 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2234 return FAIL;
2235 ret = compile_list(arg, cctx, ppconst);
2236 break;
2237
2238 /*
2239 * Dictionary: {'key': val, 'key': val}
2240 */
2241 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2242 return FAIL;
2243 ret = compile_dict(arg, cctx, ppconst);
2244 break;
2245
2246 /*
2247 * Option value: &name
2248 */
2249 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2250 return FAIL;
2251 ret = compile_get_option(arg, cctx);
2252 break;
2253
2254 /*
2255 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002256 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 */
2258 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2259 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002260 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2261 ret = compile_interp_string(arg, cctx);
2262 else
2263 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002264 break;
2265
2266 /*
2267 * Register contents: @r.
2268 */
2269 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2270 return FAIL;
2271 ret = compile_get_register(arg, cctx);
2272 break;
2273 /*
2274 * nested expression: (expression).
2275 * lambda: (arg, arg) => expr
2276 * funcref: (arg, arg) => { statement }
2277 */
2278 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2279 ret = compile_lambda(arg, cctx);
2280 if (ret == NOTDONE)
2281 ret = compile_parenthesis(arg, cctx, ppconst);
2282 break;
2283
2284 default: ret = NOTDONE;
2285 break;
2286 }
2287 if (ret == FAIL)
2288 return FAIL;
2289
2290 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2291 {
2292 if (cctx->ctx_skip == SKIP_YES)
2293 clear_tv(rettv);
2294 else
2295 // A constant expression can possibly be handled compile time,
2296 // return the value instead of generating code.
2297 ++ppconst->pp_used;
2298 }
2299 else if (ret == NOTDONE)
2300 {
2301 char_u *p;
2302 int r;
2303
2304 if (!eval_isnamec1(**arg))
2305 {
2306 if (!vim9_bad_comment(*arg))
2307 {
2308 if (ends_excmd(*skipwhite(*arg)))
2309 semsg(_(e_empty_expression_str), *arg);
2310 else
2311 semsg(_(e_name_expected_str), *arg);
2312 }
2313 return FAIL;
2314 }
2315
2316 // "name" or "name()"
2317 p = to_name_end(*arg, TRUE);
2318 if (p - *arg == (size_t)1 && **arg == '_')
2319 {
2320 emsg(_(e_cannot_use_underscore_here));
2321 return FAIL;
2322 }
2323
2324 if (*p == '(')
2325 {
2326 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2327 }
2328 else
2329 {
2330 if (cctx->ctx_skip != SKIP_YES
2331 && generate_ppconst(cctx, ppconst) == FAIL)
2332 return FAIL;
2333 r = compile_load(arg, p, cctx, TRUE, TRUE);
2334 }
2335 if (r == FAIL)
2336 return FAIL;
2337 }
2338
2339 // Handle following "[]", ".member", etc.
2340 // Then deal with prefixed '-', '+' and '!', if not done already.
2341 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2342 ppconst) == FAIL)
2343 return FAIL;
2344 if (ppconst->pp_used > 0)
2345 {
2346 // apply the '!', '-' and '+' before the constant
2347 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2348 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2349 return FAIL;
2350 return OK;
2351 }
2352 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2353 return FAIL;
2354 return OK;
2355}
2356
2357/*
Bram Moolenaar5da36052021-12-27 15:39:57 +00002358 * <type>expr8: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359 */
2360 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00002361compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362{
2363 type_T *want_type = NULL;
2364
2365 // Recognize <type>
2366 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2367 {
2368 ++*arg;
2369 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2370 if (want_type == NULL)
2371 return FAIL;
2372
2373 if (**arg != '>')
2374 {
2375 if (*skipwhite(*arg) == '>')
2376 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2377 else
2378 emsg(_(e_missing_gt));
2379 return FAIL;
2380 }
2381 ++*arg;
2382 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2383 return FAIL;
2384 }
2385
Bram Moolenaar5da36052021-12-27 15:39:57 +00002386 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 return FAIL;
2388
2389 if (want_type != NULL)
2390 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002391 type_T *actual;
2392 where_T where = WHERE_INIT;
2393
2394 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002395 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002396 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002397 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002398 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2399 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002400 return FAIL;
2401 }
2402 }
2403
2404 return OK;
2405}
2406
2407/*
2408 * * number multiplication
2409 * / number division
2410 * % number modulo
2411 */
2412 static int
2413compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2414{
2415 char_u *op;
2416 char_u *next;
2417 int ppconst_used = ppconst->pp_used;
2418
2419 // get the first expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002420 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421 return FAIL;
2422
2423 /*
2424 * Repeat computing, until no "*", "/" or "%" is following.
2425 */
2426 for (;;)
2427 {
2428 op = may_peek_next_line(cctx, *arg, &next);
2429 if (*op != '*' && *op != '/' && *op != '%')
2430 break;
2431 if (next != NULL)
2432 {
2433 *arg = next_line_from_context(cctx, TRUE);
2434 op = skipwhite(*arg);
2435 }
2436
2437 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2438 {
2439 error_white_both(op, 1);
2440 return FAIL;
2441 }
2442 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2443 return FAIL;
2444
2445 // get the second expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002446 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447 return FAIL;
2448
2449 if (ppconst->pp_used == ppconst_used + 2
2450 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2451 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2452 {
2453 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2454 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2455 varnumber_T res = 0;
2456 int failed = FALSE;
2457
2458 // both are numbers: compute the result
2459 switch (*op)
2460 {
2461 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2462 break;
2463 case '/': res = num_divide(tv1->vval.v_number,
2464 tv2->vval.v_number, &failed);
2465 break;
2466 case '%': res = num_modulus(tv1->vval.v_number,
2467 tv2->vval.v_number, &failed);
2468 break;
2469 }
2470 if (failed)
2471 return FAIL;
2472 tv1->vval.v_number = res;
2473 --ppconst->pp_used;
2474 }
2475 else
2476 {
2477 generate_ppconst(cctx, ppconst);
2478 generate_two_op(cctx, op);
2479 }
2480 }
2481
2482 return OK;
2483}
2484
2485/*
2486 * + number addition or list/blobl concatenation
2487 * - number subtraction
2488 * .. string concatenation
2489 */
2490 static int
2491compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2492{
2493 char_u *op;
2494 char_u *next;
2495 int oplen;
2496 int ppconst_used = ppconst->pp_used;
2497
2498 // get the first variable
2499 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2500 return FAIL;
2501
2502 /*
2503 * Repeat computing, until no "+", "-" or ".." is following.
2504 */
2505 for (;;)
2506 {
2507 op = may_peek_next_line(cctx, *arg, &next);
2508 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2509 break;
2510 if (op[0] == op[1] && *op != '.' && next)
2511 // Finding "++" or "--" on the next line is a separate command.
2512 // But ".." is concatenation.
2513 break;
2514 oplen = (*op == '.' ? 2 : 1);
2515 if (next != NULL)
2516 {
2517 *arg = next_line_from_context(cctx, TRUE);
2518 op = skipwhite(*arg);
2519 }
2520
2521 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2522 {
2523 error_white_both(op, oplen);
2524 return FAIL;
2525 }
2526
2527 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2528 return FAIL;
2529
2530 // get the second expression
2531 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2532 return FAIL;
2533
2534 if (ppconst->pp_used == ppconst_used + 2
2535 && (*op == '.'
2536 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2537 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2538 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2539 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2540 {
2541 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2542 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2543
2544 // concat/subtract/add constant numbers
2545 if (*op == '+')
2546 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2547 else if (*op == '-')
2548 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2549 else
2550 {
2551 // concatenate constant strings
2552 char_u *s1 = tv1->vval.v_string;
2553 char_u *s2 = tv2->vval.v_string;
2554 size_t len1 = STRLEN(s1);
2555
2556 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2557 if (tv1->vval.v_string == NULL)
2558 {
2559 clear_ppconst(ppconst);
2560 return FAIL;
2561 }
2562 mch_memmove(tv1->vval.v_string, s1, len1);
2563 STRCPY(tv1->vval.v_string + len1, s2);
2564 vim_free(s1);
2565 vim_free(s2);
2566 }
2567 --ppconst->pp_used;
2568 }
2569 else
2570 {
2571 generate_ppconst(cctx, ppconst);
2572 ppconst->pp_is_const = FALSE;
2573 if (*op == '.')
2574 {
2575 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2576 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2577 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002578 if (generate_CONCAT(cctx, 2) == FAIL)
2579 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 }
2581 else
2582 generate_two_op(cctx, op);
2583 }
2584 }
2585
2586 return OK;
2587}
2588
2589/*
2590 * expr5a == expr5b
2591 * expr5a =~ expr5b
2592 * expr5a != expr5b
2593 * expr5a !~ expr5b
2594 * expr5a > expr5b
2595 * expr5a >= expr5b
2596 * expr5a < expr5b
2597 * expr5a <= expr5b
2598 * expr5a is expr5b
2599 * expr5a isnot expr5b
2600 *
2601 * Produces instructions:
2602 * EVAL expr5a Push result of "expr5a"
2603 * EVAL expr5b Push result of "expr5b"
2604 * COMPARE one of the compare instructions
2605 */
2606 static int
2607compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2608{
2609 exprtype_T type = EXPR_UNKNOWN;
2610 char_u *p;
2611 char_u *next;
2612 int len = 2;
2613 int type_is = FALSE;
2614 int ppconst_used = ppconst->pp_used;
2615
2616 // get the first variable
2617 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2618 return FAIL;
2619
2620 p = may_peek_next_line(cctx, *arg, &next);
2621 type = get_compare_type(p, &len, &type_is);
2622
2623 /*
2624 * If there is a comparative operator, use it.
2625 */
2626 if (type != EXPR_UNKNOWN)
2627 {
2628 int ic = FALSE; // Default: do not ignore case
2629
2630 if (next != NULL)
2631 {
2632 *arg = next_line_from_context(cctx, TRUE);
2633 p = skipwhite(*arg);
2634 }
2635 if (type_is && (p[len] == '?' || p[len] == '#'))
2636 {
2637 semsg(_(e_invalid_expression_str), *arg);
2638 return FAIL;
2639 }
2640 // extra question mark appended: ignore case
2641 if (p[len] == '?')
2642 {
2643 ic = TRUE;
2644 ++len;
2645 }
2646 // extra '#' appended: match case (ignored)
2647 else if (p[len] == '#')
2648 ++len;
2649 // nothing appended: match case
2650
2651 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2652 {
2653 error_white_both(p, len);
2654 return FAIL;
2655 }
2656
2657 // get the second variable
2658 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2659 return FAIL;
2660
2661 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2662 return FAIL;
2663
2664 if (ppconst->pp_used == ppconst_used + 2)
2665 {
2666 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2667 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2668 int ret;
2669
2670 // Both sides are a constant, compute the result now.
2671 // First check for a valid combination of types, this is more
2672 // strict than typval_compare().
2673 if (check_compare_types(type, tv1, tv2) == FAIL)
2674 ret = FAIL;
2675 else
2676 {
2677 ret = typval_compare(tv1, tv2, type, ic);
2678 tv1->v_type = VAR_BOOL;
2679 tv1->vval.v_number = tv1->vval.v_number
2680 ? VVAL_TRUE : VVAL_FALSE;
2681 clear_tv(tv2);
2682 --ppconst->pp_used;
2683 }
2684 return ret;
2685 }
2686
2687 generate_ppconst(cctx, ppconst);
2688 return generate_COMPARE(cctx, type, ic);
2689 }
2690
2691 return OK;
2692}
2693
2694static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2695
2696/*
2697 * Compile || or &&.
2698 */
2699 static int
2700compile_and_or(
2701 char_u **arg,
2702 cctx_T *cctx,
2703 char *op,
2704 ppconst_T *ppconst,
2705 int ppconst_used UNUSED)
2706{
2707 char_u *next;
2708 char_u *p = may_peek_next_line(cctx, *arg, &next);
2709 int opchar = *op;
2710
2711 if (p[0] == opchar && p[1] == opchar)
2712 {
2713 garray_T *instr = &cctx->ctx_instr;
2714 garray_T end_ga;
2715 int save_skip = cctx->ctx_skip;
2716
2717 /*
2718 * Repeat until there is no following "||" or "&&"
2719 */
2720 ga_init2(&end_ga, sizeof(int), 10);
2721 while (p[0] == opchar && p[1] == opchar)
2722 {
2723 long start_lnum = SOURCING_LNUM;
2724 long save_sourcing_lnum;
2725 int start_ctx_lnum = cctx->ctx_lnum;
2726 int save_lnum;
2727 int const_used;
2728 int status;
2729 jumpwhen_T jump_when = opchar == '|'
2730 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2731
2732 if (next != NULL)
2733 {
2734 *arg = next_line_from_context(cctx, TRUE);
2735 p = skipwhite(*arg);
2736 }
2737
2738 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2739 {
2740 semsg(_(e_white_space_required_before_and_after_str_at_str),
2741 op, p);
2742 ga_clear(&end_ga);
2743 return FAIL;
2744 }
2745
2746 save_sourcing_lnum = SOURCING_LNUM;
2747 SOURCING_LNUM = start_lnum;
2748 save_lnum = cctx->ctx_lnum;
2749 cctx->ctx_lnum = start_ctx_lnum;
2750
2751 status = check_ppconst_bool(ppconst);
2752 if (status != FAIL)
2753 {
2754 // Use the last ppconst if possible.
2755 if (ppconst->pp_used > 0)
2756 {
2757 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2758 int is_true = tv2bool(tv);
2759
2760 if ((is_true && opchar == '|')
2761 || (!is_true && opchar == '&'))
2762 {
2763 // For "false && expr" and "true || expr" the "expr"
2764 // does not need to be evaluated.
2765 cctx->ctx_skip = SKIP_YES;
2766 clear_tv(tv);
2767 tv->v_type = VAR_BOOL;
2768 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2769 }
2770 else
2771 {
2772 // For "true && expr" and "false || expr" only "expr"
2773 // needs to be evaluated.
2774 --ppconst->pp_used;
2775 jump_when = JUMP_NEVER;
2776 }
2777 }
2778 else
2779 {
2780 // Every part must evaluate to a bool.
2781 status = bool_on_stack(cctx);
2782 }
2783 }
2784 if (status != FAIL)
2785 status = ga_grow(&end_ga, 1);
2786 cctx->ctx_lnum = save_lnum;
2787 if (status == FAIL)
2788 {
2789 ga_clear(&end_ga);
2790 return FAIL;
2791 }
2792
2793 if (jump_when != JUMP_NEVER)
2794 {
2795 if (cctx->ctx_skip != SKIP_YES)
2796 {
2797 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2798 ++end_ga.ga_len;
2799 }
2800 generate_JUMP(cctx, jump_when, 0);
2801 }
2802
2803 // eval the next expression
2804 SOURCING_LNUM = save_sourcing_lnum;
2805 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2806 {
2807 ga_clear(&end_ga);
2808 return FAIL;
2809 }
2810
2811 const_used = ppconst->pp_used;
2812 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2813 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2814 {
2815 ga_clear(&end_ga);
2816 return FAIL;
2817 }
2818
2819 // "0 || 1" results in true, "1 && 0" results in false.
2820 if (ppconst->pp_used == const_used + 1)
2821 {
2822 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2823
2824 if (tv->v_type == VAR_NUMBER
2825 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2826 {
2827 tv->vval.v_number = tv->vval.v_number == 1
2828 ? VVAL_TRUE : VVAL_FALSE;
2829 tv->v_type = VAR_BOOL;
2830 }
2831 }
2832
2833 p = may_peek_next_line(cctx, *arg, &next);
2834 }
2835
2836 if (check_ppconst_bool(ppconst) == FAIL)
2837 {
2838 ga_clear(&end_ga);
2839 return FAIL;
2840 }
2841
2842 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
2843 // Every part must evaluate to a bool.
2844 if (bool_on_stack(cctx) == FAIL)
2845 {
2846 ga_clear(&end_ga);
2847 return FAIL;
2848 }
2849
2850 if (end_ga.ga_len > 0)
2851 {
2852 // Fill in the end label in all jumps.
2853 generate_ppconst(cctx, ppconst);
2854 while (end_ga.ga_len > 0)
2855 {
2856 isn_T *isn;
2857
2858 --end_ga.ga_len;
2859 isn = ((isn_T *)instr->ga_data)
2860 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2861 isn->isn_arg.jump.jump_where = instr->ga_len;
2862 }
2863 }
2864 ga_clear(&end_ga);
2865
2866 cctx->ctx_skip = save_skip;
2867 }
2868
2869 return OK;
2870}
2871
2872/*
2873 * expr4a && expr4a && expr4a logical AND
2874 *
2875 * Produces instructions:
2876 * EVAL expr4a Push result of "expr4a"
2877 * COND2BOOL convert to bool if needed
2878 * JUMP_IF_COND_FALSE end
2879 * EVAL expr4b Push result of "expr4b"
2880 * JUMP_IF_COND_FALSE end
2881 * EVAL expr4c Push result of "expr4c"
2882 * end:
2883 */
2884 static int
2885compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2886{
2887 int ppconst_used = ppconst->pp_used;
2888
2889 // get the first variable
2890 if (compile_expr4(arg, cctx, ppconst) == FAIL)
2891 return FAIL;
2892
2893 // || and && work almost the same
2894 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
2895}
2896
2897/*
2898 * expr3a || expr3b || expr3c logical OR
2899 *
2900 * Produces instructions:
2901 * EVAL expr3a Push result of "expr3a"
2902 * COND2BOOL convert to bool if needed
2903 * JUMP_IF_COND_TRUE end
2904 * EVAL expr3b Push result of "expr3b"
2905 * JUMP_IF_COND_TRUE end
2906 * EVAL expr3c Push result of "expr3c"
2907 * end:
2908 */
2909 static int
2910compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2911{
2912 int ppconst_used = ppconst->pp_used;
2913
2914 // eval the first expression
2915 if (compile_expr3(arg, cctx, ppconst) == FAIL)
2916 return FAIL;
2917
2918 // || and && work almost the same
2919 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
2920}
2921
2922/*
2923 * Toplevel expression: expr2 ? expr1a : expr1b
2924 * Produces instructions:
2925 * EVAL expr2 Push result of "expr2"
2926 * JUMP_IF_FALSE alt jump if false
2927 * EVAL expr1a
2928 * JUMP_ALWAYS end
2929 * alt: EVAL expr1b
2930 * end:
2931 *
2932 * Toplevel expression: expr2 ?? expr1
2933 * Produces instructions:
2934 * EVAL expr2 Push result of "expr2"
2935 * JUMP_AND_KEEP_IF_TRUE end jump if true
2936 * EVAL expr1
2937 * end:
2938 */
2939 int
2940compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2941{
2942 char_u *p;
2943 int ppconst_used = ppconst->pp_used;
2944 char_u *next;
2945
2946 // Ignore all kinds of errors when not producing code.
2947 if (cctx->ctx_skip == SKIP_YES)
2948 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00002949 int prev_did_emsg = did_emsg;
2950
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002951 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00002952 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002953 }
2954
2955 // Evaluate the first expression.
2956 if (compile_expr2(arg, cctx, ppconst) == FAIL)
2957 return FAIL;
2958
2959 p = may_peek_next_line(cctx, *arg, &next);
2960 if (*p == '?')
2961 {
2962 int op_falsy = p[1] == '?';
2963 garray_T *instr = &cctx->ctx_instr;
2964 garray_T *stack = &cctx->ctx_type_stack;
2965 int alt_idx = instr->ga_len;
2966 int end_idx = 0;
2967 isn_T *isn;
2968 type_T *type1 = NULL;
2969 int has_const_expr = FALSE;
2970 int const_value = FALSE;
2971 int save_skip = cctx->ctx_skip;
2972
2973 if (next != NULL)
2974 {
2975 *arg = next_line_from_context(cctx, TRUE);
2976 p = skipwhite(*arg);
2977 }
2978
2979 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
2980 {
2981 semsg(_(e_white_space_required_before_and_after_str_at_str),
2982 op_falsy ? "??" : "?", p);
2983 return FAIL;
2984 }
2985
2986 if (ppconst->pp_used == ppconst_used + 1)
2987 {
2988 // the condition is a constant, we know whether the ? or the :
2989 // expression is to be evaluated.
2990 has_const_expr = TRUE;
2991 if (op_falsy)
2992 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
2993 else
2994 {
2995 int error = FALSE;
2996
2997 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
2998 &error);
2999 if (error)
3000 return FAIL;
3001 }
3002 cctx->ctx_skip = save_skip == SKIP_YES ||
3003 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3004
3005 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3006 // "left ?? right" and "left" is truthy: produce "left"
3007 generate_ppconst(cctx, ppconst);
3008 else
3009 {
3010 clear_tv(&ppconst->pp_tv[ppconst_used]);
3011 --ppconst->pp_used;
3012 }
3013 }
3014 else
3015 {
3016 generate_ppconst(cctx, ppconst);
3017 if (op_falsy)
3018 end_idx = instr->ga_len;
3019 generate_JUMP(cctx, op_falsy
3020 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3021 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003022 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003023 }
3024
3025 // evaluate the second expression; any type is accepted
3026 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3027 return FAIL;
3028 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3029 return FAIL;
3030
3031 if (!has_const_expr)
3032 {
3033 generate_ppconst(cctx, ppconst);
3034
3035 if (!op_falsy)
3036 {
3037 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003038 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003039 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003040
3041 end_idx = instr->ga_len;
3042 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3043
3044 // jump here from JUMP_IF_FALSE
3045 isn = ((isn_T *)instr->ga_data) + alt_idx;
3046 isn->isn_arg.jump.jump_where = instr->ga_len;
3047 }
3048 }
3049
3050 if (!op_falsy)
3051 {
3052 // Check for the ":".
3053 p = may_peek_next_line(cctx, *arg, &next);
3054 if (*p != ':')
3055 {
3056 emsg(_(e_missing_colon_after_questionmark));
3057 return FAIL;
3058 }
3059 if (next != NULL)
3060 {
3061 *arg = next_line_from_context(cctx, TRUE);
3062 p = skipwhite(*arg);
3063 }
3064
3065 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3066 {
3067 semsg(_(e_white_space_required_before_and_after_str_at_str),
3068 ":", p);
3069 return FAIL;
3070 }
3071
3072 // evaluate the third expression
3073 if (has_const_expr)
3074 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3075 ? SKIP_YES : SKIP_NOT;
3076 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3077 return FAIL;
3078 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3079 return FAIL;
3080 }
3081
3082 if (!has_const_expr)
3083 {
3084 type_T **typep;
3085
3086 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003087 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003088
3089 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003090 typep = &((((type2_T *)stack->ga_data)
3091 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003092 common_type(type1, *typep, typep, cctx->ctx_type_list);
3093
3094 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3095 isn = ((isn_T *)instr->ga_data) + end_idx;
3096 isn->isn_arg.jump.jump_where = instr->ga_len;
3097 }
3098
3099 cctx->ctx_skip = save_skip;
3100 }
3101 return OK;
3102}
3103
3104/*
3105 * Toplevel expression.
3106 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3107 * Returns OK or FAIL.
3108 */
3109 int
3110compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3111{
3112 ppconst_T ppconst;
3113
3114 CLEAR_FIELD(ppconst);
3115 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3116 {
3117 clear_ppconst(&ppconst);
3118 return FAIL;
3119 }
3120 if (is_const != NULL)
3121 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3122 if (generate_ppconst(cctx, &ppconst) == FAIL)
3123 return FAIL;
3124 return OK;
3125}
3126
3127/*
3128 * Toplevel expression.
3129 */
3130 int
3131compile_expr0(char_u **arg, cctx_T *cctx)
3132{
3133 return compile_expr0_ext(arg, cctx, NULL);
3134}
3135
3136
3137#endif // defined(FEAT_EVAL)