blob: d002dc9b2707e4e3e4a7c8b3dc1f8077502b8046 [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/*
1378 * Compile "@r".
1379 */
1380 static int
1381compile_get_register(char_u **arg, cctx_T *cctx)
1382{
1383 int ret;
1384
1385 ++*arg;
1386 if (**arg == NUL)
1387 {
1388 semsg(_(e_syntax_error_at_str), *arg - 1);
1389 return FAIL;
1390 }
1391 if (!valid_yank_reg(**arg, FALSE))
1392 {
1393 emsg_invreg(**arg);
1394 return FAIL;
1395 }
1396 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1397 ++*arg;
1398 return ret;
1399}
1400
1401/*
1402 * Apply leading '!', '-' and '+' to constant "rettv".
1403 * When "numeric_only" is TRUE do not apply '!'.
1404 */
1405 static int
1406apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1407{
1408 char_u *p = *end;
1409
1410 // this works from end to start
1411 while (p > start)
1412 {
1413 --p;
1414 if (*p == '-' || *p == '+')
1415 {
1416 // only '-' has an effect, for '+' we only check the type
1417#ifdef FEAT_FLOAT
1418 if (rettv->v_type == VAR_FLOAT)
1419 {
1420 if (*p == '-')
1421 rettv->vval.v_float = -rettv->vval.v_float;
1422 }
1423 else
1424#endif
1425 {
1426 varnumber_T val;
1427 int error = FALSE;
1428
1429 // tv_get_number_chk() accepts a string, but we don't want that
1430 // here
1431 if (check_not_string(rettv) == FAIL)
1432 return FAIL;
1433 val = tv_get_number_chk(rettv, &error);
1434 clear_tv(rettv);
1435 if (error)
1436 return FAIL;
1437 if (*p == '-')
1438 val = -val;
1439 rettv->v_type = VAR_NUMBER;
1440 rettv->vval.v_number = val;
1441 }
1442 }
1443 else if (numeric_only)
1444 {
1445 ++p;
1446 break;
1447 }
1448 else if (*p == '!')
1449 {
1450 int v = tv2bool(rettv);
1451
1452 // '!' is permissive in the type.
1453 clear_tv(rettv);
1454 rettv->v_type = VAR_BOOL;
1455 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1456 }
1457 }
1458 *end = p;
1459 return OK;
1460}
1461
1462/*
1463 * Recognize v: variables that are constants and set "rettv".
1464 */
1465 static void
1466get_vim_constant(char_u **arg, typval_T *rettv)
1467{
1468 if (STRNCMP(*arg, "v:true", 6) == 0)
1469 {
1470 rettv->v_type = VAR_BOOL;
1471 rettv->vval.v_number = VVAL_TRUE;
1472 *arg += 6;
1473 }
1474 else if (STRNCMP(*arg, "v:false", 7) == 0)
1475 {
1476 rettv->v_type = VAR_BOOL;
1477 rettv->vval.v_number = VVAL_FALSE;
1478 *arg += 7;
1479 }
1480 else if (STRNCMP(*arg, "v:null", 6) == 0)
1481 {
1482 rettv->v_type = VAR_SPECIAL;
1483 rettv->vval.v_number = VVAL_NULL;
1484 *arg += 6;
1485 }
1486 else if (STRNCMP(*arg, "v:none", 6) == 0)
1487 {
1488 rettv->v_type = VAR_SPECIAL;
1489 rettv->vval.v_number = VVAL_NONE;
1490 *arg += 6;
1491 }
1492}
1493
1494 exprtype_T
1495get_compare_type(char_u *p, int *len, int *type_is)
1496{
1497 exprtype_T type = EXPR_UNKNOWN;
1498 int i;
1499
1500 switch (p[0])
1501 {
1502 case '=': if (p[1] == '=')
1503 type = EXPR_EQUAL;
1504 else if (p[1] == '~')
1505 type = EXPR_MATCH;
1506 break;
1507 case '!': if (p[1] == '=')
1508 type = EXPR_NEQUAL;
1509 else if (p[1] == '~')
1510 type = EXPR_NOMATCH;
1511 break;
1512 case '>': if (p[1] != '=')
1513 {
1514 type = EXPR_GREATER;
1515 *len = 1;
1516 }
1517 else
1518 type = EXPR_GEQUAL;
1519 break;
1520 case '<': if (p[1] != '=')
1521 {
1522 type = EXPR_SMALLER;
1523 *len = 1;
1524 }
1525 else
1526 type = EXPR_SEQUAL;
1527 break;
1528 case 'i': if (p[1] == 's')
1529 {
1530 // "is" and "isnot"; but not a prefix of a name
1531 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1532 *len = 5;
1533 i = p[*len];
1534 if (!isalnum(i) && i != '_')
1535 {
1536 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1537 *type_is = TRUE;
1538 }
1539 }
1540 break;
1541 }
1542 return type;
1543}
1544
1545/*
1546 * Skip over an expression, ignoring most errors.
1547 */
1548 void
1549skip_expr_cctx(char_u **arg, cctx_T *cctx)
1550{
1551 evalarg_T evalarg;
1552
1553 init_evalarg(&evalarg);
1554 evalarg.eval_cctx = cctx;
1555 skip_expr(arg, &evalarg);
1556 clear_evalarg(&evalarg, NULL);
1557}
1558
1559/*
1560 * Check that the top of the type stack has a type that can be used as a
1561 * condition. Give an error and return FAIL if not.
1562 */
1563 int
1564bool_on_stack(cctx_T *cctx)
1565{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566 type_T *type;
1567
Bram Moolenaar078a4612022-01-04 15:17:03 +00001568 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569 if (type == &t_bool)
1570 return OK;
1571
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001572 if (type == &t_any
1573 || type == &t_unknown
1574 || type == &t_number
1575 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1577 // This requires a runtime type check.
1578 return generate_COND2BOOL(cctx);
1579
1580 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1581}
1582
1583/*
1584 * Give the "white on both sides" error, taking the operator from "p[len]".
1585 */
1586 void
1587error_white_both(char_u *op, int len)
1588{
1589 char_u buf[10];
1590
1591 vim_strncpy(buf, op, len);
1592 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1593}
1594
1595/*
1596 * Compile code to apply '-', '+' and '!'.
1597 * When "numeric_only" is TRUE do not apply '!'.
1598 */
1599 static int
1600compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1601{
1602 char_u *p = *end;
1603
1604 // this works from end to start
1605 while (p > start)
1606 {
1607 --p;
1608 while (VIM_ISWHITE(*p))
1609 --p;
1610 if (*p == '-' || *p == '+')
1611 {
1612 int negate = *p == '-';
1613 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001614 type_T *type;
1615
Bram Moolenaar078a4612022-01-04 15:17:03 +00001616 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001617 if (type != &t_float && need_type(type, &t_number,
1618 -1, 0, cctx, FALSE, FALSE) == FAIL)
1619 return FAIL;
1620
1621 while (p > start && (p[-1] == '-' || p[-1] == '+'))
1622 {
1623 --p;
1624 if (*p == '-')
1625 negate = !negate;
1626 }
1627 // only '-' has an effect, for '+' we only check the type
1628 if (negate)
1629 {
1630 isn = generate_instr(cctx, ISN_NEGATENR);
1631 if (isn == NULL)
1632 return FAIL;
1633 }
1634 }
1635 else if (numeric_only)
1636 {
1637 ++p;
1638 break;
1639 }
1640 else
1641 {
1642 int invert = *p == '!';
1643
1644 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1645 {
1646 if (p[-1] == '!')
1647 invert = !invert;
1648 --p;
1649 }
1650 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1651 return FAIL;
1652 }
1653 }
1654 *end = p;
1655 return OK;
1656}
1657
1658/*
1659 * Compile "(expression)": recursive!
1660 * Return FAIL/OK.
1661 */
1662 static int
1663compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1664{
1665 int ret;
1666 char_u *p = *arg + 1;
1667
1668 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1669 return FAIL;
1670 if (ppconst->pp_used <= PPSIZE - 10)
1671 {
1672 ret = compile_expr1(arg, cctx, ppconst);
1673 }
1674 else
1675 {
1676 // Not enough space in ppconst, flush constants.
1677 if (generate_ppconst(cctx, ppconst) == FAIL)
1678 return FAIL;
1679 ret = compile_expr0(arg, cctx);
1680 }
1681 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1682 return FAIL;
1683 if (**arg == ')')
1684 ++*arg;
1685 else if (ret == OK)
1686 {
1687 emsg(_(e_missing_closing_paren));
1688 ret = FAIL;
1689 }
1690 return ret;
1691}
1692
Bram Moolenaarc7349932022-01-16 20:59:39 +00001693static int compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1694
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695/*
1696 * Compile whatever comes after "name" or "name()".
1697 * Advances "*arg" only when something was recognized.
1698 */
1699 static int
1700compile_subscript(
1701 char_u **arg,
1702 cctx_T *cctx,
1703 char_u *start_leader,
1704 char_u **end_leader,
1705 ppconst_T *ppconst)
1706{
1707 char_u *name_start = *end_leader;
1708 int keeping_dict = FALSE;
1709
1710 for (;;)
1711 {
1712 char_u *p = skipwhite(*arg);
1713
1714 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1715 {
1716 char_u *next = peek_next_line_from_context(cctx);
1717
1718 // If a following line starts with "->{" or "->X" advance to that
1719 // line, so that a line break before "->" is allowed.
1720 // Also if a following line starts with ".x".
1721 if (next != NULL &&
1722 ((next[0] == '-' && next[1] == '>'
1723 && (next[2] == '{'
1724 || ASCII_ISALPHA(*skipwhite(next + 2))))
1725 || (next[0] == '.' && eval_isdictc(next[1]))))
1726 {
1727 next = next_line_from_context(cctx, TRUE);
1728 if (next == NULL)
1729 return FAIL;
1730 *arg = next;
1731 p = skipwhite(*arg);
1732 }
1733 }
1734
1735 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1736 // is not a function call.
1737 if (**arg == '(')
1738 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001739 type_T *type;
1740 int argcount = 0;
1741
1742 if (generate_ppconst(cctx, ppconst) == FAIL)
1743 return FAIL;
1744 ppconst->pp_is_const = FALSE;
1745
1746 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001747 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748
1749 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001750 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001751 return FAIL;
1752 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1753 return FAIL;
1754 if (keeping_dict)
1755 {
1756 keeping_dict = FALSE;
1757 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1758 return FAIL;
1759 }
1760 }
1761 else if (*p == '-' && p[1] == '>')
1762 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001763 char_u *pstart = p;
1764 int alt;
1765 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766
Bram Moolenaarc7349932022-01-16 20:59:39 +00001767 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768 if (generate_ppconst(cctx, ppconst) == FAIL)
1769 return FAIL;
1770 ppconst->pp_is_const = FALSE;
1771
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 // Apply the '!', '-' and '+' first:
1773 // -1.0->func() works like (-1.0)->func()
1774 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1775 return FAIL;
1776
1777 p += 2;
1778 *arg = skipwhite(p);
1779 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001780
1781 // Three alternatives handled here:
1782 // 1. "base->name(" only a name, use compile_call()
1783 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1784 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001785 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001786 alt = 2;
1787 else
1788 {
1789 // alternative 1 or 3
1790 p = *arg;
1791 if (!eval_isnamec1(*p))
1792 {
1793 semsg(_(e_trailing_characters_str), pstart);
1794 return FAIL;
1795 }
1796 if (ASCII_ISALPHA(*p) && p[1] == ':')
1797 p += 2;
1798 for ( ; eval_isnamec(*p); ++p)
1799 ;
1800 if (*p == '(')
1801 {
1802 // alternative 1
1803 alt = 1;
1804 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1805 return FAIL;
1806 }
1807 else
1808 {
1809 // Must be alternative 3, find the "(". Only works within
1810 // one line.
1811 alt = 3;
1812 paren = vim_strchr(p, '(');
1813 if (paren == NULL)
1814 {
1815 semsg(_(e_missing_parenthesis_str), *arg);
1816 return FAIL;
1817 }
1818 }
1819 }
1820
1821 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 {
1823 int argcount = 1;
1824 garray_T *stack = &cctx->ctx_type_stack;
1825 int type_idx_start = stack->ga_len;
1826 type_T *type;
1827 int expr_isn_start = cctx->ctx_instr.ga_len;
1828 int expr_isn_end;
1829 int arg_isn_count;
1830
Bram Moolenaarc7349932022-01-16 20:59:39 +00001831 if (alt == 2)
1832 {
1833 // Funcref call: list->(Refs[2])(arg)
1834 // or lambda: list->((arg) => expr)(arg)
1835 //
1836 // Fist compile the function expression.
1837 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1838 return FAIL;
1839 }
1840 else
1841 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001842 int fail;
1843 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
1844
Bram Moolenaarc7349932022-01-16 20:59:39 +00001845 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001846
1847 // instead of using LOADG for "import.Func" use PUSHFUNC
1848 ++paren_follows_after_expr;
1849
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001850 // do not look in the next line
1851 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001852
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001853 fail = compile_expr8(arg, cctx, ppconst) == FAIL
1854 || *skipwhite(*arg) != NUL;
1855 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001856 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001857 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001858
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001859 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001860 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001861 semsg(_(e_invalid_expression_str), pstart);
1862 return FAIL;
1863 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001864 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001865
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001866 // Compile the arguments.
1867 if (**arg != '(')
1868 {
1869 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001870 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001871 else
1872 semsg(_(e_missing_parenthesis_str), *arg);
1873 return FAIL;
1874 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001875
1876 // Remember the next instruction index, where the instructions
1877 // for arguments are being written.
1878 expr_isn_end = cctx->ctx_instr.ga_len;
1879
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001880 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001881 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1882 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001883 return FAIL;
1884
1885 // Move the instructions for the arguments to before the
1886 // instructions of the expression and move the type of the
1887 // expression after the argument types. This is what ISN_PCALL
1888 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1890 if (arg_isn_count > 0)
1891 {
1892 int expr_isn_count = expr_isn_end - expr_isn_start;
1893 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001894 type_T *decl_type;
1895 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001896
1897 if (isn == NULL)
1898 return FAIL;
1899 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1900 + expr_isn_start,
1901 sizeof(isn_T) * expr_isn_count);
1902 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1903 + expr_isn_start,
1904 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1905 sizeof(isn_T) * arg_isn_count);
1906 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1907 + expr_isn_start + arg_isn_count,
1908 isn, sizeof(isn_T) * expr_isn_count);
1909 vim_free(isn);
1910
Bram Moolenaar078a4612022-01-04 15:17:03 +00001911 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1912 type = typep->type_curr;
1913 decl_type = typep->type_decl;
1914 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1915 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1916 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001917 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001918 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1919 typep->type_curr = type;
1920 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001921 }
1922
Bram Moolenaar078a4612022-01-04 15:17:03 +00001923 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001924 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1925 return FAIL;
1926 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001927
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928 if (keeping_dict)
1929 {
1930 keeping_dict = FALSE;
1931 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1932 return FAIL;
1933 }
1934 }
1935 else if (**arg == '[')
1936 {
1937 int is_slice = FALSE;
1938
1939 // list index: list[123]
1940 // dict member: dict[key]
1941 // string index: text[123]
1942 // blob index: blob[123]
1943 if (generate_ppconst(cctx, ppconst) == FAIL)
1944 return FAIL;
1945 ppconst->pp_is_const = FALSE;
1946
1947 ++p;
1948 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1949 return FAIL;
1950 if (**arg == ':')
1951 {
1952 // missing first index is equal to zero
1953 generate_PUSHNR(cctx, 0);
1954 }
1955 else
1956 {
1957 if (compile_expr0(arg, cctx) == FAIL)
1958 return FAIL;
1959 if (**arg == ':')
1960 {
1961 semsg(_(e_white_space_required_before_and_after_str_at_str),
1962 ":", *arg);
1963 return FAIL;
1964 }
1965 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1966 return FAIL;
1967 *arg = skipwhite(*arg);
1968 }
1969 if (**arg == ':')
1970 {
1971 is_slice = TRUE;
1972 ++*arg;
1973 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
1974 {
1975 semsg(_(e_white_space_required_before_and_after_str_at_str),
1976 ":", *arg);
1977 return FAIL;
1978 }
1979 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1980 return FAIL;
1981 if (**arg == ']')
1982 // missing second index is equal to end of string
1983 generate_PUSHNR(cctx, -1);
1984 else
1985 {
1986 if (compile_expr0(arg, cctx) == FAIL)
1987 return FAIL;
1988 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1989 return FAIL;
1990 *arg = skipwhite(*arg);
1991 }
1992 }
1993
1994 if (**arg != ']')
1995 {
1996 emsg(_(e_missing_closing_square_brace));
1997 return FAIL;
1998 }
1999 *arg = *arg + 1;
2000
2001 if (keeping_dict)
2002 {
2003 keeping_dict = FALSE;
2004 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2005 return FAIL;
2006 }
2007 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2008 return FAIL;
2009 }
2010 else if (*p == '.' && p[1] != '.')
2011 {
2012 // dictionary member: dict.name
2013 if (generate_ppconst(cctx, ppconst) == FAIL)
2014 return FAIL;
2015 ppconst->pp_is_const = FALSE;
2016
2017 *arg = p + 1;
2018 if (IS_WHITE_OR_NUL(**arg))
2019 {
2020 emsg(_(e_missing_name_after_dot));
2021 return FAIL;
2022 }
2023 p = *arg;
2024 if (eval_isdictc(*p))
2025 while (eval_isnamec(*p))
2026 MB_PTR_ADV(p);
2027 if (p == *arg)
2028 {
2029 semsg(_(e_syntax_error_at_str), *arg);
2030 return FAIL;
2031 }
2032 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2033 return FAIL;
2034 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2035 return FAIL;
2036 keeping_dict = TRUE;
2037 *arg = p;
2038 }
2039 else
2040 break;
2041 }
2042
2043 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2044 // This needs to be done at runtime to be able to check the type.
2045 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
2046 return FAIL;
2047
2048 return OK;
2049}
2050
2051/*
2052 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2053 * "arg" is advanced until after the expression, skipping white space.
2054 *
2055 * If the value is a constant "ppconst->pp_used" will be non-zero.
2056 * Before instructions are generated, any values in "ppconst" will generated.
2057 *
2058 * This is the compiling equivalent of eval1(), eval2(), etc.
2059 */
2060
2061/*
2062 * number number constant
2063 * 0zFFFFFFFF Blob constant
2064 * "string" string constant
2065 * 'string' literal string constant
2066 * &option-name option value
2067 * @r register contents
2068 * identifier variable value
2069 * function() function call
2070 * $VAR environment variable
2071 * (expression) nested expression
2072 * [expr, expr] List
2073 * {key: val, [key]: val} Dictionary
2074 *
2075 * Also handle:
2076 * ! in front logical NOT
2077 * - in front unary minus
2078 * + in front unary plus (ignored)
2079 * trailing (arg) funcref/partial call
2080 * trailing [] subscript in String or List
2081 * trailing .name entry in Dictionary
2082 * trailing ->name() method call
2083 */
2084 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00002085compile_expr8(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002086 char_u **arg,
2087 cctx_T *cctx,
2088 ppconst_T *ppconst)
2089{
2090 char_u *start_leader, *end_leader;
2091 int ret = OK;
2092 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2093 int used_before = ppconst->pp_used;
2094
2095 ppconst->pp_is_const = FALSE;
2096
2097 /*
2098 * Skip '!', '-' and '+' characters. They are handled later.
2099 */
2100 start_leader = *arg;
2101 if (eval_leader(arg, TRUE) == FAIL)
2102 return FAIL;
2103 end_leader = *arg;
2104
2105 rettv->v_type = VAR_UNKNOWN;
2106 switch (**arg)
2107 {
2108 /*
2109 * Number constant.
2110 */
2111 case '0': // also for blob starting with 0z
2112 case '1':
2113 case '2':
2114 case '3':
2115 case '4':
2116 case '5':
2117 case '6':
2118 case '7':
2119 case '8':
2120 case '9':
2121 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2122 return FAIL;
2123 // Apply "-" and "+" just before the number now, right to
2124 // left. Matters especially when "->" follows. Stops at
2125 // '!'.
2126 if (apply_leader(rettv, TRUE,
2127 start_leader, &end_leader) == FAIL)
2128 {
2129 clear_tv(rettv);
2130 return FAIL;
2131 }
2132 break;
2133
2134 /*
2135 * String constant: "string".
2136 */
2137 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
2138 return FAIL;
2139 break;
2140
2141 /*
2142 * Literal string constant: 'str''ing'.
2143 */
2144 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
2145 return FAIL;
2146 break;
2147
2148 /*
2149 * Constant Vim variable.
2150 */
2151 case 'v': get_vim_constant(arg, rettv);
2152 ret = NOTDONE;
2153 break;
2154
2155 /*
2156 * "true" constant
2157 */
2158 case 't': if (STRNCMP(*arg, "true", 4) == 0
2159 && !eval_isnamec((*arg)[4]))
2160 {
2161 *arg += 4;
2162 rettv->v_type = VAR_BOOL;
2163 rettv->vval.v_number = VVAL_TRUE;
2164 }
2165 else
2166 ret = NOTDONE;
2167 break;
2168
2169 /*
2170 * "false" constant
2171 */
2172 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2173 && !eval_isnamec((*arg)[5]))
2174 {
2175 *arg += 5;
2176 rettv->v_type = VAR_BOOL;
2177 rettv->vval.v_number = VVAL_FALSE;
2178 }
2179 else
2180 ret = NOTDONE;
2181 break;
2182
2183 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002184 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002185 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002186 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002188 char_u *p = *arg + 4;
2189 int len;
2190
2191 for (len = 0; eval_isnamec(p[len]); ++len)
2192 ;
2193 ret = handle_predefined(*arg, len + 4, rettv);
2194 if (ret == FAIL)
2195 ret = NOTDONE;
2196 else
2197 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002198 }
2199 else
2200 ret = NOTDONE;
2201 break;
2202
2203 /*
2204 * List: [expr, expr]
2205 */
2206 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2207 return FAIL;
2208 ret = compile_list(arg, cctx, ppconst);
2209 break;
2210
2211 /*
2212 * Dictionary: {'key': val, 'key': val}
2213 */
2214 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2215 return FAIL;
2216 ret = compile_dict(arg, cctx, ppconst);
2217 break;
2218
2219 /*
2220 * Option value: &name
2221 */
2222 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2223 return FAIL;
2224 ret = compile_get_option(arg, cctx);
2225 break;
2226
2227 /*
2228 * Environment variable: $VAR.
2229 */
2230 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2231 return FAIL;
2232 ret = compile_get_env(arg, cctx);
2233 break;
2234
2235 /*
2236 * Register contents: @r.
2237 */
2238 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2239 return FAIL;
2240 ret = compile_get_register(arg, cctx);
2241 break;
2242 /*
2243 * nested expression: (expression).
2244 * lambda: (arg, arg) => expr
2245 * funcref: (arg, arg) => { statement }
2246 */
2247 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2248 ret = compile_lambda(arg, cctx);
2249 if (ret == NOTDONE)
2250 ret = compile_parenthesis(arg, cctx, ppconst);
2251 break;
2252
2253 default: ret = NOTDONE;
2254 break;
2255 }
2256 if (ret == FAIL)
2257 return FAIL;
2258
2259 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2260 {
2261 if (cctx->ctx_skip == SKIP_YES)
2262 clear_tv(rettv);
2263 else
2264 // A constant expression can possibly be handled compile time,
2265 // return the value instead of generating code.
2266 ++ppconst->pp_used;
2267 }
2268 else if (ret == NOTDONE)
2269 {
2270 char_u *p;
2271 int r;
2272
2273 if (!eval_isnamec1(**arg))
2274 {
2275 if (!vim9_bad_comment(*arg))
2276 {
2277 if (ends_excmd(*skipwhite(*arg)))
2278 semsg(_(e_empty_expression_str), *arg);
2279 else
2280 semsg(_(e_name_expected_str), *arg);
2281 }
2282 return FAIL;
2283 }
2284
2285 // "name" or "name()"
2286 p = to_name_end(*arg, TRUE);
2287 if (p - *arg == (size_t)1 && **arg == '_')
2288 {
2289 emsg(_(e_cannot_use_underscore_here));
2290 return FAIL;
2291 }
2292
2293 if (*p == '(')
2294 {
2295 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2296 }
2297 else
2298 {
2299 if (cctx->ctx_skip != SKIP_YES
2300 && generate_ppconst(cctx, ppconst) == FAIL)
2301 return FAIL;
2302 r = compile_load(arg, p, cctx, TRUE, TRUE);
2303 }
2304 if (r == FAIL)
2305 return FAIL;
2306 }
2307
2308 // Handle following "[]", ".member", etc.
2309 // Then deal with prefixed '-', '+' and '!', if not done already.
2310 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2311 ppconst) == FAIL)
2312 return FAIL;
2313 if (ppconst->pp_used > 0)
2314 {
2315 // apply the '!', '-' and '+' before the constant
2316 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2317 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2318 return FAIL;
2319 return OK;
2320 }
2321 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2322 return FAIL;
2323 return OK;
2324}
2325
2326/*
Bram Moolenaar5da36052021-12-27 15:39:57 +00002327 * <type>expr8: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 */
2329 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00002330compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002331{
2332 type_T *want_type = NULL;
2333
2334 // Recognize <type>
2335 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2336 {
2337 ++*arg;
2338 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2339 if (want_type == NULL)
2340 return FAIL;
2341
2342 if (**arg != '>')
2343 {
2344 if (*skipwhite(*arg) == '>')
2345 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2346 else
2347 emsg(_(e_missing_gt));
2348 return FAIL;
2349 }
2350 ++*arg;
2351 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2352 return FAIL;
2353 }
2354
Bram Moolenaar5da36052021-12-27 15:39:57 +00002355 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002356 return FAIL;
2357
2358 if (want_type != NULL)
2359 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002360 type_T *actual;
2361 where_T where = WHERE_INIT;
2362
2363 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002364 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002365 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002366 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002367 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2368 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 return FAIL;
2370 }
2371 }
2372
2373 return OK;
2374}
2375
2376/*
2377 * * number multiplication
2378 * / number division
2379 * % number modulo
2380 */
2381 static int
2382compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2383{
2384 char_u *op;
2385 char_u *next;
2386 int ppconst_used = ppconst->pp_used;
2387
2388 // get the first expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002389 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002390 return FAIL;
2391
2392 /*
2393 * Repeat computing, until no "*", "/" or "%" is following.
2394 */
2395 for (;;)
2396 {
2397 op = may_peek_next_line(cctx, *arg, &next);
2398 if (*op != '*' && *op != '/' && *op != '%')
2399 break;
2400 if (next != NULL)
2401 {
2402 *arg = next_line_from_context(cctx, TRUE);
2403 op = skipwhite(*arg);
2404 }
2405
2406 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2407 {
2408 error_white_both(op, 1);
2409 return FAIL;
2410 }
2411 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2412 return FAIL;
2413
2414 // get the second expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002415 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002416 return FAIL;
2417
2418 if (ppconst->pp_used == ppconst_used + 2
2419 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2420 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2421 {
2422 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2423 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2424 varnumber_T res = 0;
2425 int failed = FALSE;
2426
2427 // both are numbers: compute the result
2428 switch (*op)
2429 {
2430 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2431 break;
2432 case '/': res = num_divide(tv1->vval.v_number,
2433 tv2->vval.v_number, &failed);
2434 break;
2435 case '%': res = num_modulus(tv1->vval.v_number,
2436 tv2->vval.v_number, &failed);
2437 break;
2438 }
2439 if (failed)
2440 return FAIL;
2441 tv1->vval.v_number = res;
2442 --ppconst->pp_used;
2443 }
2444 else
2445 {
2446 generate_ppconst(cctx, ppconst);
2447 generate_two_op(cctx, op);
2448 }
2449 }
2450
2451 return OK;
2452}
2453
2454/*
2455 * + number addition or list/blobl concatenation
2456 * - number subtraction
2457 * .. string concatenation
2458 */
2459 static int
2460compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2461{
2462 char_u *op;
2463 char_u *next;
2464 int oplen;
2465 int ppconst_used = ppconst->pp_used;
2466
2467 // get the first variable
2468 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2469 return FAIL;
2470
2471 /*
2472 * Repeat computing, until no "+", "-" or ".." is following.
2473 */
2474 for (;;)
2475 {
2476 op = may_peek_next_line(cctx, *arg, &next);
2477 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2478 break;
2479 if (op[0] == op[1] && *op != '.' && next)
2480 // Finding "++" or "--" on the next line is a separate command.
2481 // But ".." is concatenation.
2482 break;
2483 oplen = (*op == '.' ? 2 : 1);
2484 if (next != NULL)
2485 {
2486 *arg = next_line_from_context(cctx, TRUE);
2487 op = skipwhite(*arg);
2488 }
2489
2490 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2491 {
2492 error_white_both(op, oplen);
2493 return FAIL;
2494 }
2495
2496 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2497 return FAIL;
2498
2499 // get the second expression
2500 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2501 return FAIL;
2502
2503 if (ppconst->pp_used == ppconst_used + 2
2504 && (*op == '.'
2505 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2506 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2507 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2508 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2509 {
2510 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2511 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2512
2513 // concat/subtract/add constant numbers
2514 if (*op == '+')
2515 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2516 else if (*op == '-')
2517 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2518 else
2519 {
2520 // concatenate constant strings
2521 char_u *s1 = tv1->vval.v_string;
2522 char_u *s2 = tv2->vval.v_string;
2523 size_t len1 = STRLEN(s1);
2524
2525 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2526 if (tv1->vval.v_string == NULL)
2527 {
2528 clear_ppconst(ppconst);
2529 return FAIL;
2530 }
2531 mch_memmove(tv1->vval.v_string, s1, len1);
2532 STRCPY(tv1->vval.v_string + len1, s2);
2533 vim_free(s1);
2534 vim_free(s2);
2535 }
2536 --ppconst->pp_used;
2537 }
2538 else
2539 {
2540 generate_ppconst(cctx, ppconst);
2541 ppconst->pp_is_const = FALSE;
2542 if (*op == '.')
2543 {
2544 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2545 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2546 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002547 if (generate_CONCAT(cctx, 2) == FAIL)
2548 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 }
2550 else
2551 generate_two_op(cctx, op);
2552 }
2553 }
2554
2555 return OK;
2556}
2557
2558/*
2559 * expr5a == expr5b
2560 * expr5a =~ expr5b
2561 * expr5a != expr5b
2562 * expr5a !~ expr5b
2563 * expr5a > expr5b
2564 * expr5a >= expr5b
2565 * expr5a < expr5b
2566 * expr5a <= expr5b
2567 * expr5a is expr5b
2568 * expr5a isnot expr5b
2569 *
2570 * Produces instructions:
2571 * EVAL expr5a Push result of "expr5a"
2572 * EVAL expr5b Push result of "expr5b"
2573 * COMPARE one of the compare instructions
2574 */
2575 static int
2576compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2577{
2578 exprtype_T type = EXPR_UNKNOWN;
2579 char_u *p;
2580 char_u *next;
2581 int len = 2;
2582 int type_is = FALSE;
2583 int ppconst_used = ppconst->pp_used;
2584
2585 // get the first variable
2586 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2587 return FAIL;
2588
2589 p = may_peek_next_line(cctx, *arg, &next);
2590 type = get_compare_type(p, &len, &type_is);
2591
2592 /*
2593 * If there is a comparative operator, use it.
2594 */
2595 if (type != EXPR_UNKNOWN)
2596 {
2597 int ic = FALSE; // Default: do not ignore case
2598
2599 if (next != NULL)
2600 {
2601 *arg = next_line_from_context(cctx, TRUE);
2602 p = skipwhite(*arg);
2603 }
2604 if (type_is && (p[len] == '?' || p[len] == '#'))
2605 {
2606 semsg(_(e_invalid_expression_str), *arg);
2607 return FAIL;
2608 }
2609 // extra question mark appended: ignore case
2610 if (p[len] == '?')
2611 {
2612 ic = TRUE;
2613 ++len;
2614 }
2615 // extra '#' appended: match case (ignored)
2616 else if (p[len] == '#')
2617 ++len;
2618 // nothing appended: match case
2619
2620 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2621 {
2622 error_white_both(p, len);
2623 return FAIL;
2624 }
2625
2626 // get the second variable
2627 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2628 return FAIL;
2629
2630 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2631 return FAIL;
2632
2633 if (ppconst->pp_used == ppconst_used + 2)
2634 {
2635 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2636 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2637 int ret;
2638
2639 // Both sides are a constant, compute the result now.
2640 // First check for a valid combination of types, this is more
2641 // strict than typval_compare().
2642 if (check_compare_types(type, tv1, tv2) == FAIL)
2643 ret = FAIL;
2644 else
2645 {
2646 ret = typval_compare(tv1, tv2, type, ic);
2647 tv1->v_type = VAR_BOOL;
2648 tv1->vval.v_number = tv1->vval.v_number
2649 ? VVAL_TRUE : VVAL_FALSE;
2650 clear_tv(tv2);
2651 --ppconst->pp_used;
2652 }
2653 return ret;
2654 }
2655
2656 generate_ppconst(cctx, ppconst);
2657 return generate_COMPARE(cctx, type, ic);
2658 }
2659
2660 return OK;
2661}
2662
2663static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2664
2665/*
2666 * Compile || or &&.
2667 */
2668 static int
2669compile_and_or(
2670 char_u **arg,
2671 cctx_T *cctx,
2672 char *op,
2673 ppconst_T *ppconst,
2674 int ppconst_used UNUSED)
2675{
2676 char_u *next;
2677 char_u *p = may_peek_next_line(cctx, *arg, &next);
2678 int opchar = *op;
2679
2680 if (p[0] == opchar && p[1] == opchar)
2681 {
2682 garray_T *instr = &cctx->ctx_instr;
2683 garray_T end_ga;
2684 int save_skip = cctx->ctx_skip;
2685
2686 /*
2687 * Repeat until there is no following "||" or "&&"
2688 */
2689 ga_init2(&end_ga, sizeof(int), 10);
2690 while (p[0] == opchar && p[1] == opchar)
2691 {
2692 long start_lnum = SOURCING_LNUM;
2693 long save_sourcing_lnum;
2694 int start_ctx_lnum = cctx->ctx_lnum;
2695 int save_lnum;
2696 int const_used;
2697 int status;
2698 jumpwhen_T jump_when = opchar == '|'
2699 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2700
2701 if (next != NULL)
2702 {
2703 *arg = next_line_from_context(cctx, TRUE);
2704 p = skipwhite(*arg);
2705 }
2706
2707 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2708 {
2709 semsg(_(e_white_space_required_before_and_after_str_at_str),
2710 op, p);
2711 ga_clear(&end_ga);
2712 return FAIL;
2713 }
2714
2715 save_sourcing_lnum = SOURCING_LNUM;
2716 SOURCING_LNUM = start_lnum;
2717 save_lnum = cctx->ctx_lnum;
2718 cctx->ctx_lnum = start_ctx_lnum;
2719
2720 status = check_ppconst_bool(ppconst);
2721 if (status != FAIL)
2722 {
2723 // Use the last ppconst if possible.
2724 if (ppconst->pp_used > 0)
2725 {
2726 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2727 int is_true = tv2bool(tv);
2728
2729 if ((is_true && opchar == '|')
2730 || (!is_true && opchar == '&'))
2731 {
2732 // For "false && expr" and "true || expr" the "expr"
2733 // does not need to be evaluated.
2734 cctx->ctx_skip = SKIP_YES;
2735 clear_tv(tv);
2736 tv->v_type = VAR_BOOL;
2737 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2738 }
2739 else
2740 {
2741 // For "true && expr" and "false || expr" only "expr"
2742 // needs to be evaluated.
2743 --ppconst->pp_used;
2744 jump_when = JUMP_NEVER;
2745 }
2746 }
2747 else
2748 {
2749 // Every part must evaluate to a bool.
2750 status = bool_on_stack(cctx);
2751 }
2752 }
2753 if (status != FAIL)
2754 status = ga_grow(&end_ga, 1);
2755 cctx->ctx_lnum = save_lnum;
2756 if (status == FAIL)
2757 {
2758 ga_clear(&end_ga);
2759 return FAIL;
2760 }
2761
2762 if (jump_when != JUMP_NEVER)
2763 {
2764 if (cctx->ctx_skip != SKIP_YES)
2765 {
2766 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2767 ++end_ga.ga_len;
2768 }
2769 generate_JUMP(cctx, jump_when, 0);
2770 }
2771
2772 // eval the next expression
2773 SOURCING_LNUM = save_sourcing_lnum;
2774 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2775 {
2776 ga_clear(&end_ga);
2777 return FAIL;
2778 }
2779
2780 const_used = ppconst->pp_used;
2781 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2782 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2783 {
2784 ga_clear(&end_ga);
2785 return FAIL;
2786 }
2787
2788 // "0 || 1" results in true, "1 && 0" results in false.
2789 if (ppconst->pp_used == const_used + 1)
2790 {
2791 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2792
2793 if (tv->v_type == VAR_NUMBER
2794 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2795 {
2796 tv->vval.v_number = tv->vval.v_number == 1
2797 ? VVAL_TRUE : VVAL_FALSE;
2798 tv->v_type = VAR_BOOL;
2799 }
2800 }
2801
2802 p = may_peek_next_line(cctx, *arg, &next);
2803 }
2804
2805 if (check_ppconst_bool(ppconst) == FAIL)
2806 {
2807 ga_clear(&end_ga);
2808 return FAIL;
2809 }
2810
2811 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
2812 // Every part must evaluate to a bool.
2813 if (bool_on_stack(cctx) == FAIL)
2814 {
2815 ga_clear(&end_ga);
2816 return FAIL;
2817 }
2818
2819 if (end_ga.ga_len > 0)
2820 {
2821 // Fill in the end label in all jumps.
2822 generate_ppconst(cctx, ppconst);
2823 while (end_ga.ga_len > 0)
2824 {
2825 isn_T *isn;
2826
2827 --end_ga.ga_len;
2828 isn = ((isn_T *)instr->ga_data)
2829 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2830 isn->isn_arg.jump.jump_where = instr->ga_len;
2831 }
2832 }
2833 ga_clear(&end_ga);
2834
2835 cctx->ctx_skip = save_skip;
2836 }
2837
2838 return OK;
2839}
2840
2841/*
2842 * expr4a && expr4a && expr4a logical AND
2843 *
2844 * Produces instructions:
2845 * EVAL expr4a Push result of "expr4a"
2846 * COND2BOOL convert to bool if needed
2847 * JUMP_IF_COND_FALSE end
2848 * EVAL expr4b Push result of "expr4b"
2849 * JUMP_IF_COND_FALSE end
2850 * EVAL expr4c Push result of "expr4c"
2851 * end:
2852 */
2853 static int
2854compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2855{
2856 int ppconst_used = ppconst->pp_used;
2857
2858 // get the first variable
2859 if (compile_expr4(arg, cctx, ppconst) == FAIL)
2860 return FAIL;
2861
2862 // || and && work almost the same
2863 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
2864}
2865
2866/*
2867 * expr3a || expr3b || expr3c logical OR
2868 *
2869 * Produces instructions:
2870 * EVAL expr3a Push result of "expr3a"
2871 * COND2BOOL convert to bool if needed
2872 * JUMP_IF_COND_TRUE end
2873 * EVAL expr3b Push result of "expr3b"
2874 * JUMP_IF_COND_TRUE end
2875 * EVAL expr3c Push result of "expr3c"
2876 * end:
2877 */
2878 static int
2879compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2880{
2881 int ppconst_used = ppconst->pp_used;
2882
2883 // eval the first expression
2884 if (compile_expr3(arg, cctx, ppconst) == FAIL)
2885 return FAIL;
2886
2887 // || and && work almost the same
2888 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
2889}
2890
2891/*
2892 * Toplevel expression: expr2 ? expr1a : expr1b
2893 * Produces instructions:
2894 * EVAL expr2 Push result of "expr2"
2895 * JUMP_IF_FALSE alt jump if false
2896 * EVAL expr1a
2897 * JUMP_ALWAYS end
2898 * alt: EVAL expr1b
2899 * end:
2900 *
2901 * Toplevel expression: expr2 ?? expr1
2902 * Produces instructions:
2903 * EVAL expr2 Push result of "expr2"
2904 * JUMP_AND_KEEP_IF_TRUE end jump if true
2905 * EVAL expr1
2906 * end:
2907 */
2908 int
2909compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2910{
2911 char_u *p;
2912 int ppconst_used = ppconst->pp_used;
2913 char_u *next;
2914
2915 // Ignore all kinds of errors when not producing code.
2916 if (cctx->ctx_skip == SKIP_YES)
2917 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00002918 int prev_did_emsg = did_emsg;
2919
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002920 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00002921 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002922 }
2923
2924 // Evaluate the first expression.
2925 if (compile_expr2(arg, cctx, ppconst) == FAIL)
2926 return FAIL;
2927
2928 p = may_peek_next_line(cctx, *arg, &next);
2929 if (*p == '?')
2930 {
2931 int op_falsy = p[1] == '?';
2932 garray_T *instr = &cctx->ctx_instr;
2933 garray_T *stack = &cctx->ctx_type_stack;
2934 int alt_idx = instr->ga_len;
2935 int end_idx = 0;
2936 isn_T *isn;
2937 type_T *type1 = NULL;
2938 int has_const_expr = FALSE;
2939 int const_value = FALSE;
2940 int save_skip = cctx->ctx_skip;
2941
2942 if (next != NULL)
2943 {
2944 *arg = next_line_from_context(cctx, TRUE);
2945 p = skipwhite(*arg);
2946 }
2947
2948 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
2949 {
2950 semsg(_(e_white_space_required_before_and_after_str_at_str),
2951 op_falsy ? "??" : "?", p);
2952 return FAIL;
2953 }
2954
2955 if (ppconst->pp_used == ppconst_used + 1)
2956 {
2957 // the condition is a constant, we know whether the ? or the :
2958 // expression is to be evaluated.
2959 has_const_expr = TRUE;
2960 if (op_falsy)
2961 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
2962 else
2963 {
2964 int error = FALSE;
2965
2966 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
2967 &error);
2968 if (error)
2969 return FAIL;
2970 }
2971 cctx->ctx_skip = save_skip == SKIP_YES ||
2972 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
2973
2974 if (op_falsy && cctx->ctx_skip == SKIP_YES)
2975 // "left ?? right" and "left" is truthy: produce "left"
2976 generate_ppconst(cctx, ppconst);
2977 else
2978 {
2979 clear_tv(&ppconst->pp_tv[ppconst_used]);
2980 --ppconst->pp_used;
2981 }
2982 }
2983 else
2984 {
2985 generate_ppconst(cctx, ppconst);
2986 if (op_falsy)
2987 end_idx = instr->ga_len;
2988 generate_JUMP(cctx, op_falsy
2989 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
2990 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002991 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002992 }
2993
2994 // evaluate the second expression; any type is accepted
2995 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
2996 return FAIL;
2997 if (compile_expr1(arg, cctx, ppconst) == FAIL)
2998 return FAIL;
2999
3000 if (!has_const_expr)
3001 {
3002 generate_ppconst(cctx, ppconst);
3003
3004 if (!op_falsy)
3005 {
3006 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003007 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003008 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003009
3010 end_idx = instr->ga_len;
3011 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3012
3013 // jump here from JUMP_IF_FALSE
3014 isn = ((isn_T *)instr->ga_data) + alt_idx;
3015 isn->isn_arg.jump.jump_where = instr->ga_len;
3016 }
3017 }
3018
3019 if (!op_falsy)
3020 {
3021 // Check for the ":".
3022 p = may_peek_next_line(cctx, *arg, &next);
3023 if (*p != ':')
3024 {
3025 emsg(_(e_missing_colon_after_questionmark));
3026 return FAIL;
3027 }
3028 if (next != NULL)
3029 {
3030 *arg = next_line_from_context(cctx, TRUE);
3031 p = skipwhite(*arg);
3032 }
3033
3034 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3035 {
3036 semsg(_(e_white_space_required_before_and_after_str_at_str),
3037 ":", p);
3038 return FAIL;
3039 }
3040
3041 // evaluate the third expression
3042 if (has_const_expr)
3043 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3044 ? SKIP_YES : SKIP_NOT;
3045 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3046 return FAIL;
3047 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3048 return FAIL;
3049 }
3050
3051 if (!has_const_expr)
3052 {
3053 type_T **typep;
3054
3055 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003056 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003057
3058 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003059 typep = &((((type2_T *)stack->ga_data)
3060 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003061 common_type(type1, *typep, typep, cctx->ctx_type_list);
3062
3063 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3064 isn = ((isn_T *)instr->ga_data) + end_idx;
3065 isn->isn_arg.jump.jump_where = instr->ga_len;
3066 }
3067
3068 cctx->ctx_skip = save_skip;
3069 }
3070 return OK;
3071}
3072
3073/*
3074 * Toplevel expression.
3075 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3076 * Returns OK or FAIL.
3077 */
3078 int
3079compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3080{
3081 ppconst_T ppconst;
3082
3083 CLEAR_FIELD(ppconst);
3084 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3085 {
3086 clear_ppconst(&ppconst);
3087 return FAIL;
3088 }
3089 if (is_const != NULL)
3090 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3091 if (generate_ppconst(cctx, &ppconst) == FAIL)
3092 return FAIL;
3093 return OK;
3094}
3095
3096/*
3097 * Toplevel expression.
3098 */
3099 int
3100compile_expr0(char_u **arg, cctx_T *cctx)
3101{
3102 return compile_expr0_ext(arg, cctx, NULL);
3103}
3104
3105
3106#endif // defined(FEAT_EVAL)