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