blob: 8be8f6b9145c88ebc7ef9ec3e833514def60a110 [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/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
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 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100143 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
144 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000145 return FAIL;
146 if (keeping_dict != NULL)
147 *keeping_dict = TRUE;
148 }
149 else if (vartype == VAR_STRING)
150 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000151 typep->type_curr = &t_string;
152 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000153 if ((is_slice
154 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
155 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
156 return FAIL;
157 }
158 else if (vartype == VAR_BLOB)
159 {
160 if (is_slice)
161 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000162 typep->type_curr = &t_blob;
163 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000164 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
165 return FAIL;
166 }
167 else
168 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000169 typep->type_curr = &t_number;
170 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
172 return FAIL;
173 }
174 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000175 else if (vartype == VAR_LIST || typep->type_curr == &t_any
176 || typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000177 {
178 if (is_slice)
179 {
180 if (generate_instr_drop(cctx,
181 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
182 2) == FAIL)
183 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000184 // a copy is made so the member type is no longer declared
185 if (typep->type_decl->tt_type == VAR_LIST)
186 typep->type_decl = &t_list_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000187 }
188 else
189 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000190 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000192 typep->type_curr = typep->type_curr->tt_member;
193 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000194 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000195 typep->type_curr = &t_any;
196 if (typep->type_decl->tt_type == VAR_LIST)
197 {
198 typep->type_decl = typep->type_decl->tt_member;
199 if (typep->type_decl == &t_unknown)
200 // empty list was used
201 typep->type_decl = &t_any;
202 }
203 else
204 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 }
206 if (generate_instr_drop(cctx,
207 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
208 == FAIL)
209 return FAIL;
210 }
211 }
212 else
213 {
214 switch (vartype)
215 {
216 case VAR_FUNC:
217 case VAR_PARTIAL:
218 emsg(_(e_cannot_index_a_funcref));
219 break;
220 case VAR_BOOL:
221 case VAR_SPECIAL:
222 case VAR_JOB:
223 case VAR_CHANNEL:
224 case VAR_INSTR:
225 case VAR_UNKNOWN:
226 case VAR_ANY:
227 case VAR_VOID:
228 emsg(_(e_cannot_index_special_variable));
229 break;
230 default:
231 emsg(_(e_string_list_dict_or_blob_required));
232 }
233 return FAIL;
234 }
235 return OK;
236}
237
238/*
239 * Generate an instruction to load script-local variable "name", without the
240 * leading "s:".
241 * Also finds imported variables.
242 */
243 int
244compile_load_scriptvar(
245 cctx_T *cctx,
246 char_u *name, // variable NUL terminated
247 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100248 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249{
250 scriptitem_T *si;
251 int idx;
252 imported_T *import;
253
254 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
255 return FAIL;
256 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000257 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000258 if (idx >= 0)
259 {
260 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
261
262 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
263 current_sctx.sc_sid, idx, sv->sv_type);
264 return OK;
265 }
266
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000267 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000268 if (import != NULL)
269 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000270 char_u *p = skipwhite(*end);
271 char_u *exp_name;
272 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100273 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000274 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000275 int done = FALSE;
276 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000277
278 // Need to lookup the member.
279 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000280 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000281 semsg(_(e_expected_dot_after_name_str), start);
282 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000284 ++p;
285 if (VIM_ISWHITE(*p))
286 {
287 emsg(_(e_no_white_space_allowed_after_dot));
288 return FAIL;
289 }
290
291 // isolate one name
292 exp_name = p;
293 while (eval_isnamec(*p))
294 ++p;
295 cc = *p;
296 *p = NUL;
297
Bram Moolenaard041f422022-01-12 19:54:00 +0000298 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100299 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
300 // "import autoload './dir/script.vim'" or
301 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100302 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100303
304 if (res == OK)
305 {
306 if (si->sn_autoload_prefix != NULL
307 && si->sn_state == SN_STATE_NOT_LOADED)
308 {
309 char_u *auto_name =
310 concat_str(si->sn_autoload_prefix, exp_name);
311
312 // autoload script must be loaded later, access by the autoload
313 // name. If a '(' follows it must be a function. Otherwise we
314 // don't know, it can be "script.Func".
315 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100316 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100317 else
318 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
319 vim_free(auto_name);
320 done = TRUE;
321 }
322 else if (si->sn_import_autoload
323 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100324 {
325 // If a '(' follows it must be a function. Otherwise we don't
326 // know, it can be "script.Func".
327 if (cc == '(' || paren_follows_after_expr)
328 {
329 char_u sid_name[MAX_FUNC_NAME_LEN];
330
331 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100332 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100333 }
334 else
335 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
336 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100337 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100338 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100339 else
340 {
341 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
342 cctx, NULL, TRUE);
343 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100344 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100345
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000346 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000347 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000348 if (done)
349 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000350
351 if (idx < 0)
352 {
353 if (ufunc != NULL)
354 {
355 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100356 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000357 return OK;
358 }
359 return FAIL;
360 }
361
362 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
363 import->imp_sid,
364 idx,
365 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366 return OK;
367 }
368
Bram Moolenaard0132f42022-05-12 11:05:40 +0100369 // Can only get here if we know "name" is a script variable and not in a
370 // Vim9 script (variable is not in sn_var_vals): old style script.
371 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000372 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000373}
374
375 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000376generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000377{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000378 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000379 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000381 // Reject a global non-autoload function found without the "g:" prefix.
382 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383 return FAIL;
384
385 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000386 compile_type = get_compile_type(ufunc);
387 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000388 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000389 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100390 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000391}
392
393/*
394 * Compile a variable name into a load instruction.
395 * "end" points to just after the name.
396 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
397 * When "error" is FALSE do not give an error when not found.
398 */
399 int
400compile_load(
401 char_u **arg,
402 char_u *end_arg,
403 cctx_T *cctx,
404 int is_expr,
405 int error)
406{
407 type_T *type;
408 char_u *name = NULL;
409 char_u *end = end_arg;
410 int res = FAIL;
411 int prev_called_emsg = called_emsg;
412
413 if (*(*arg + 1) == ':')
414 {
415 if (end <= *arg + 2)
416 {
417 isntype_T isn_type;
418
419 // load dictionary of namespace
420 switch (**arg)
421 {
422 case 'g': isn_type = ISN_LOADGDICT; break;
423 case 'w': isn_type = ISN_LOADWDICT; break;
424 case 't': isn_type = ISN_LOADTDICT; break;
425 case 'b': isn_type = ISN_LOADBDICT; break;
426 default:
427 semsg(_(e_namespace_not_supported_str), *arg);
428 goto theend;
429 }
430 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
431 goto theend;
432 res = OK;
433 }
434 else
435 {
436 isntype_T isn_type = ISN_DROP;
437
438 // load namespaced variable
439 name = vim_strnsave(*arg + 2, end - (*arg + 2));
440 if (name == NULL)
441 return FAIL;
442
443 switch (**arg)
444 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100445 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000446 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000447 case 's': if (current_script_is_vim9())
448 {
449 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
450 *arg);
451 vim_free(name);
452 return FAIL;
453 }
Kota Kato948a3892022-08-16 16:09:59 +0100454 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000455 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 else
457 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100458 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 break;
460 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
461 {
462 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000463 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000464 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000465 else
466 isn_type = ISN_LOADG;
467 }
468 else
469 {
470 isn_type = ISN_LOADAUTO;
471 vim_free(name);
472 name = vim_strnsave(*arg, end - *arg);
473 if (name == NULL)
474 return FAIL;
475 }
476 break;
477 case 'w': isn_type = ISN_LOADW; break;
478 case 't': isn_type = ISN_LOADT; break;
479 case 'b': isn_type = ISN_LOADB; break;
480 default: // cannot happen, just in case
481 semsg(_(e_namespace_not_supported_str), *arg);
482 goto theend;
483 }
484 if (isn_type != ISN_DROP)
485 {
486 // Global, Buffer-local, Window-local and Tabpage-local
487 // variables can be defined later, thus we don't check if it
488 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000489 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000490 }
491 }
492 }
493 else
494 {
495 size_t len = end - *arg;
496 int idx;
497 int gen_load = FALSE;
498 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100499 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100500 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000501
502 name = vim_strnsave(*arg, end - *arg);
503 if (name == NULL)
504 return FAIL;
505
506 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
507 {
508 script_autoload(name, FALSE);
509 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
510 }
511 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
512 == OK)
513 {
514 if (gen_load_outer == 0)
515 gen_load = TRUE;
516 }
517 else
518 {
519 lvar_T lvar;
520
521 if (lookup_local(*arg, len, &lvar, cctx) == OK)
522 {
523 type = lvar.lv_type;
524 idx = lvar.lv_idx;
525 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100526 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000527 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100528 outer_loop_depth = lvar.lv_loop_depth;
529 outer_loop_idx = lvar.lv_loop_idx;
530 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531 else
532 gen_load = TRUE;
533 }
534 else
535 {
536 // "var" can be script-local even without using "s:" if it
537 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000538 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000539 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100540 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000541
542 // When evaluating an expression and the name starts with an
543 // uppercase letter it can be a user defined function.
544 // generate_funcref() will fail if the function can't be found.
545 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000546 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547 }
548 }
549 if (gen_load)
550 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
551 if (gen_load_outer > 0)
552 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100553 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
554 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000555 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/*
620 * Compile the argument expressions.
621 * "arg" points to just after the "(" and is advanced to after the ")"
622 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100623 int
LemonBoyf3b48952022-05-05 13:53:03 +0100624compile_arguments(
625 char_u **arg,
626 cctx_T *cctx,
627 int *argcount,
628 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000629{
630 char_u *p = *arg;
631 char_u *whitep = *arg;
632 int must_end = FALSE;
633 int instr_count;
634
635 for (;;)
636 {
637 if (may_get_next_line(whitep, &p, cctx) == FAIL)
638 goto failret;
639 if (*p == ')')
640 {
641 *arg = p + 1;
642 return OK;
643 }
644 if (must_end)
645 {
646 semsg(_(e_missing_comma_before_argument_str), p);
647 return FAIL;
648 }
649
650 instr_count = cctx->ctx_instr.ga_len;
651 if (compile_expr0(&p, cctx) == FAIL)
652 return FAIL;
653 ++*argcount;
654
LemonBoyf3b48952022-05-05 13:53:03 +0100655 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000656 && cctx->ctx_instr.ga_len == instr_count + 1)
657 {
658 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
659
660 // {skip} argument of searchpair() can be compiled if not empty
661 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100662 compile_string(isn, cctx, 0);
663 }
664 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
665 && cctx->ctx_instr.ga_len == instr_count + 1)
666 {
667 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
668
669 // {sub} argument of substitute() can be compiled if it starts
670 // with \=
671 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
672 && isn->isn_arg.string[1] == '=')
673 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000674 }
675
676 if (*p != ',' && *skipwhite(p) == ',')
677 {
678 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
679 p = skipwhite(p);
680 }
681 if (*p == ',')
682 {
683 ++p;
684 if (*p != NUL && !VIM_ISWHITE(*p))
685 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
686 }
687 else
688 must_end = TRUE;
689 whitep = p;
690 p = skipwhite(p);
691 }
692failret:
693 emsg(_(e_missing_closing_paren));
694 return FAIL;
695}
696
697/*
698 * Compile a function call: name(arg1, arg2)
699 * "arg" points to "name", "arg + varlen" to the "(".
700 * "argcount_init" is 1 for "value->method()"
701 * Instructions:
702 * EVAL arg1
703 * EVAL arg2
704 * BCALL / DCALL / UCALL
705 */
706 static int
707compile_call(
708 char_u **arg,
709 size_t varlen,
710 cctx_T *cctx,
711 ppconst_T *ppconst,
712 int argcount_init)
713{
714 char_u *name = *arg;
715 char_u *p;
716 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100717 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718 char_u fname_buf[FLEN_FIXED + 1];
719 char_u *tofree = NULL;
720 int error = FCERR_NONE;
721 ufunc_T *ufunc = NULL;
722 int res = FAIL;
723 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000724 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100725 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000726 imported_T *import;
727
728 if (varlen >= sizeof(namebuf))
729 {
730 semsg(_(e_name_too_long_str), name);
731 return FAIL;
732 }
733 vim_strncpy(namebuf, *arg, varlen);
734
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000735 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000736 if (import != NULL)
737 {
738 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
739 return FAIL;
740 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741
742 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100743 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100745 if ((varlen == 3
746 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
748 {
749 char_u *s = skipwhite(*arg + varlen + 1);
750 typval_T argvars[2];
751 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100752 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753
754 argvars[0].v_type = VAR_UNKNOWN;
755 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100756 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000757 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100758 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 s = skipwhite(s);
760 if (*s == ')' && argvars[0].v_type == VAR_STRING
761 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
762 || !is_has))
763 {
764 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
765
766 *arg = s + 1;
767 argvars[1].v_type = VAR_UNKNOWN;
768 tv->v_type = VAR_NUMBER;
769 tv->vval.v_number = 0;
770 if (is_has)
771 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100772 else if (is_len)
773 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774 else
775 f_exists(argvars, tv);
776 clear_tv(&argvars[0]);
777 ++ppconst->pp_used;
778 return OK;
779 }
780 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100781 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 {
783 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
784 return FAIL;
785 }
786 }
787
788 if (generate_ppconst(cctx, ppconst) == FAIL)
789 return FAIL;
790
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
792
793 // We handle the "skip" argument of searchpair() and searchpairpos()
794 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100795 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
796 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
797 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
798 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
799 special_fn = CA_SEARCHPAIR;
800 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
801 special_fn = CA_SUBSTITUTE;
802 else
803 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000804
805 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100806 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807 goto theend;
808
809 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
810 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
811 {
812 int idx;
813
814 // builtin function
815 idx = find_internal_func(name);
816 if (idx >= 0)
817 {
818 if (STRCMP(name, "flatten") == 0)
819 {
820 emsg(_(e_cannot_use_flatten_in_vim9_script));
821 goto theend;
822 }
823
824 if (STRCMP(name, "add") == 0 && argcount == 2)
825 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000826 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827
828 // add() can be compiled to instructions if we know the type
829 if (type->tt_type == VAR_LIST)
830 {
831 // inline "add(list, item)" so that the type can be checked
832 res = generate_LISTAPPEND(cctx);
833 idx = -1;
834 }
835 else if (type->tt_type == VAR_BLOB)
836 {
837 // inline "add(blob, nr)" so that the type can be checked
838 res = generate_BLOBAPPEND(cctx);
839 idx = -1;
840 }
841 }
842
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100843 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
844 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100845 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100846 // May have the "D" or "R" flag, reserve a variable for a
847 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100848 if (get_defer_var_idx(cctx) == 0)
849 idx = -1;
850 }
851
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852 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
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100985 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986
987 for (;;)
988 {
989 if (may_get_next_line(whitep, &p, cctx) == FAIL)
990 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000991 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000992 return FAIL;
993 }
994 if (*p == ',')
995 {
996 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
997 return FAIL;
998 }
999 if (*p == ']')
1000 {
1001 ++p;
1002 break;
1003 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001004 if (must_end)
1005 {
1006 semsg(_(e_missing_comma_in_list_str), p);
1007 return FAIL;
1008 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001009 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1010 return FAIL;
1011 if (!is_const)
1012 is_all_const = FALSE;
1013 ++count;
1014 if (*p == ',')
1015 {
1016 ++p;
1017 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1018 {
1019 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1020 return FAIL;
1021 }
1022 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001023 else
1024 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001025 whitep = p;
1026 p = skipwhite(p);
1027 }
1028 *arg = p;
1029
1030 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001031 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001032}
1033
1034/*
1035 * Parse a lambda: "(arg, arg) => expr"
1036 * "*arg" points to the '('.
1037 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1038 */
1039 static int
1040compile_lambda(char_u **arg, cctx_T *cctx)
1041{
1042 int r;
1043 typval_T rettv;
1044 ufunc_T *ufunc;
1045 evalarg_T evalarg;
1046
1047 init_evalarg(&evalarg);
1048 evalarg.eval_flags = EVAL_EVALUATE;
1049 evalarg.eval_cctx = cctx;
1050
1051 // Get the funcref in "rettv".
1052 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1053 if (r != OK)
1054 {
1055 clear_evalarg(&evalarg, NULL);
1056 return r;
1057 }
1058
1059 // "rettv" will now be a partial referencing the function.
1060 ufunc = rettv.vval.v_partial->pt_func;
1061 ++ufunc->uf_refcount;
1062 clear_tv(&rettv);
1063
1064 // Compile it here to get the return type. The return type is optional,
1065 // when it's missing use t_unknown. This is recognized in
1066 // compile_return().
1067 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1068 ufunc->uf_ret_type = &t_unknown;
1069 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1070
1071 // When the outer function is compiled for profiling or debugging, the
1072 // lambda may be called without profiling or debugging. Compile it here in
1073 // the right context.
1074 if (cctx->ctx_compile_type == CT_DEBUG
1075#ifdef FEAT_PROFILE
1076 || cctx->ctx_compile_type == CT_PROFILE
1077#endif
1078 )
1079 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1080
Bram Moolenaar139575d2022-03-15 19:29:30 +00001081 // if the outer function is not compiled for debugging or profiling, this
1082 // one might be
1083 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001084 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001085 compiletype_T compile_type = get_compile_type(ufunc);
1086
1087 if (compile_type != CT_NONE)
1088 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001089 }
1090
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001091 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1092 // "*arg" may point into it. Point into the original line to avoid a
1093 // dangling pointer.
1094 if (evalarg.eval_using_cmdline)
1095 {
1096 garray_T *gap = &evalarg.eval_tofree_ga;
1097 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1098
1099 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1100 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001101 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102 }
1103
1104 clear_evalarg(&evalarg, NULL);
1105
1106 if (ufunc->uf_def_status == UF_COMPILED)
1107 {
1108 // The return type will now be known.
1109 set_function_type(ufunc);
1110
1111 // The function reference count will be 1. When the ISN_FUNCREF
1112 // instruction is deleted the reference count is decremented and the
1113 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001114 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 }
1116
1117 func_ptr_unref(ufunc);
1118 return FAIL;
1119}
1120
1121/*
1122 * Get a lambda and compile it. Uses Vim9 syntax.
1123 */
1124 int
1125get_lambda_tv_and_compile(
1126 char_u **arg,
1127 typval_T *rettv,
1128 int types_optional,
1129 evalarg_T *evalarg)
1130{
1131 int r;
1132 ufunc_T *ufunc;
1133 int save_sc_version = current_sctx.sc_version;
1134
1135 // Get the funcref in "rettv".
1136 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1137 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1138 current_sctx.sc_version = save_sc_version;
1139 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001140 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141
1142 // "rettv" will now be a partial referencing the function.
1143 ufunc = rettv->vval.v_partial->pt_func;
1144
1145 // Compile it here to get the return type. The return type is optional,
1146 // when it's missing use t_unknown. This is recognized in
1147 // compile_return().
1148 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1149 ufunc->uf_ret_type = &t_unknown;
1150 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1151
1152 if (ufunc->uf_def_status == UF_COMPILED)
1153 {
1154 // The return type will now be known.
1155 set_function_type(ufunc);
1156 return OK;
1157 }
1158 clear_tv(rettv);
1159 return FAIL;
1160}
1161
1162/*
1163 * parse a dict: {key: val, [key]: val}
1164 * "*arg" points to the '{'.
1165 * ppconst->pp_is_const is set if all item values are a constant.
1166 */
1167 static int
1168compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1169{
1170 garray_T *instr = &cctx->ctx_instr;
1171 int count = 0;
1172 dict_T *d = dict_alloc();
1173 dictitem_T *item;
1174 char_u *whitep = *arg + 1;
1175 char_u *p;
1176 int is_const;
1177 int is_all_const = TRUE; // reset when non-const encountered
1178
1179 if (d == NULL)
1180 return FAIL;
1181 if (generate_ppconst(cctx, ppconst) == FAIL)
1182 return FAIL;
1183 for (;;)
1184 {
1185 char_u *key = NULL;
1186
1187 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1188 {
1189 *arg = NULL;
1190 goto failret;
1191 }
1192
1193 if (**arg == '}')
1194 break;
1195
1196 if (**arg == '[')
1197 {
1198 isn_T *isn;
1199
1200 // {[expr]: value} uses an evaluated key.
1201 *arg = skipwhite(*arg + 1);
1202 if (compile_expr0(arg, cctx) == FAIL)
1203 return FAIL;
1204 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1205 if (isn->isn_type == ISN_PUSHNR)
1206 {
1207 char buf[NUMBUFLEN];
1208
1209 // Convert to string at compile time.
1210 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1211 isn->isn_type = ISN_PUSHS;
1212 isn->isn_arg.string = vim_strsave((char_u *)buf);
1213 }
1214 if (isn->isn_type == ISN_PUSHS)
1215 key = isn->isn_arg.string;
1216 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1217 return FAIL;
1218 *arg = skipwhite(*arg);
1219 if (**arg != ']')
1220 {
1221 emsg(_(e_missing_matching_bracket_after_dict_key));
1222 return FAIL;
1223 }
1224 ++*arg;
1225 }
1226 else
1227 {
1228 // {"name": value},
1229 // {'name': value},
1230 // {name: value} use "name" as a literal key
1231 key = get_literal_key(arg);
1232 if (key == NULL)
1233 return FAIL;
1234 if (generate_PUSHS(cctx, &key) == FAIL)
1235 return FAIL;
1236 }
1237
1238 // Check for duplicate keys, if using string keys.
1239 if (key != NULL)
1240 {
1241 item = dict_find(d, key, -1);
1242 if (item != NULL)
1243 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001244 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001245 goto failret;
1246 }
1247 item = dictitem_alloc(key);
1248 if (item != NULL)
1249 {
1250 item->di_tv.v_type = VAR_UNKNOWN;
1251 item->di_tv.v_lock = 0;
1252 if (dict_add(d, item) == FAIL)
1253 dictitem_free(item);
1254 }
1255 }
1256
1257 if (**arg != ':')
1258 {
1259 if (*skipwhite(*arg) == ':')
1260 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1261 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001262 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263 return FAIL;
1264 }
1265 whitep = *arg + 1;
1266 if (!IS_WHITE_OR_NUL(*whitep))
1267 {
1268 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1269 return FAIL;
1270 }
1271
1272 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1273 {
1274 *arg = NULL;
1275 goto failret;
1276 }
1277
1278 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1279 return FAIL;
1280 if (!is_const)
1281 is_all_const = FALSE;
1282 ++count;
1283
1284 whitep = *arg;
1285 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1286 {
1287 *arg = NULL;
1288 goto failret;
1289 }
1290 if (**arg == '}')
1291 break;
1292 if (**arg != ',')
1293 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001294 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001295 goto failret;
1296 }
1297 if (IS_WHITE_OR_NUL(*whitep))
1298 {
1299 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1300 return FAIL;
1301 }
1302 whitep = *arg + 1;
1303 if (!IS_WHITE_OR_NUL(*whitep))
1304 {
1305 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1306 return FAIL;
1307 }
1308 *arg = skipwhite(whitep);
1309 }
1310
1311 *arg = *arg + 1;
1312
1313 // Allow for following comment, after at least one space.
1314 p = skipwhite(*arg);
1315 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1316 *arg += STRLEN(*arg);
1317
1318 dict_unref(d);
1319 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001320 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321
1322failret:
1323 if (*arg == NULL)
1324 {
1325 semsg(_(e_missing_dict_end), _("[end of lines]"));
1326 *arg = (char_u *)"";
1327 }
1328 dict_unref(d);
1329 return FAIL;
1330}
1331
1332/*
1333 * Compile "&option".
1334 */
1335 static int
1336compile_get_option(char_u **arg, cctx_T *cctx)
1337{
1338 typval_T rettv;
1339 char_u *start = *arg;
1340 int ret;
1341
1342 // parse the option and get the current value to get the type.
1343 rettv.v_type = VAR_UNKNOWN;
1344 ret = eval_option(arg, &rettv, TRUE);
1345 if (ret == OK)
1346 {
1347 // include the '&' in the name, eval_option() expects it.
1348 char_u *name = vim_strnsave(start, *arg - start);
1349 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1350 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1351
1352 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1353 vim_free(name);
1354 }
1355 clear_tv(&rettv);
1356
1357 return ret;
1358}
1359
1360/*
1361 * Compile "$VAR".
1362 */
1363 static int
1364compile_get_env(char_u **arg, cctx_T *cctx)
1365{
1366 char_u *start = *arg;
1367 int len;
1368 int ret;
1369 char_u *name;
1370
1371 ++*arg;
1372 len = get_env_len(arg);
1373 if (len == 0)
1374 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001375 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001376 return FAIL;
1377 }
1378
1379 // include the '$' in the name, eval_env_var() expects it.
1380 name = vim_strnsave(start, len + 1);
1381 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1382 vim_free(name);
1383 return ret;
1384}
1385
1386/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001387 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001388 */
1389 static int
1390compile_interp_string(char_u **arg, cctx_T *cctx)
1391{
1392 typval_T tv;
1393 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001394 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001395 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001396 int count = 0;
1397 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001398
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001399 // *arg is on the '$' character, move it to the first string character.
1400 ++*arg;
1401 quote = **arg;
1402 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001403
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001404 for (;;)
1405 {
1406 // Get the string up to the matching quote or to a single '{'.
1407 // "arg" is advanced to either the quote or the '{'.
1408 if (quote == '"')
1409 ret = eval_string(arg, &tv, evaluate, TRUE);
1410 else
1411 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1412 if (ret == FAIL)
1413 break;
1414 if (evaluate)
1415 {
1416 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1417 || (**arg != '{' && count == 0))
1418 {
1419 // generate non-empty string or empty string if it's the only
1420 // one
1421 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1422 return FAIL;
1423 tv.vval.v_string = NULL; // don't free it now
1424 ++count;
1425 }
1426 clear_tv(&tv);
1427 }
1428
1429 if (**arg != '{')
1430 {
1431 // found terminating quote
1432 ++*arg;
1433 break;
1434 }
1435
1436 p = compile_one_expr_in_str(*arg, cctx);
1437 if (p == NULL)
1438 {
1439 ret = FAIL;
1440 break;
1441 }
1442 ++count;
1443 *arg = p;
1444 }
LemonBoy2eaef102022-05-06 13:14:50 +01001445
1446 if (ret == FAIL || !evaluate)
1447 return ret;
1448
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001449 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1450 if (count > 1)
1451 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001452
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001453 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001454}
1455
1456/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001457 * Compile "@r".
1458 */
1459 static int
1460compile_get_register(char_u **arg, cctx_T *cctx)
1461{
1462 int ret;
1463
1464 ++*arg;
1465 if (**arg == NUL)
1466 {
1467 semsg(_(e_syntax_error_at_str), *arg - 1);
1468 return FAIL;
1469 }
1470 if (!valid_yank_reg(**arg, FALSE))
1471 {
1472 emsg_invreg(**arg);
1473 return FAIL;
1474 }
1475 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1476 ++*arg;
1477 return ret;
1478}
1479
1480/*
1481 * Apply leading '!', '-' and '+' to constant "rettv".
1482 * When "numeric_only" is TRUE do not apply '!'.
1483 */
1484 static int
1485apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1486{
1487 char_u *p = *end;
1488
1489 // this works from end to start
1490 while (p > start)
1491 {
1492 --p;
1493 if (*p == '-' || *p == '+')
1494 {
1495 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001496 if (rettv->v_type == VAR_FLOAT)
1497 {
1498 if (*p == '-')
1499 rettv->vval.v_float = -rettv->vval.v_float;
1500 }
1501 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502 {
1503 varnumber_T val;
1504 int error = FALSE;
1505
1506 // tv_get_number_chk() accepts a string, but we don't want that
1507 // here
1508 if (check_not_string(rettv) == FAIL)
1509 return FAIL;
1510 val = tv_get_number_chk(rettv, &error);
1511 clear_tv(rettv);
1512 if (error)
1513 return FAIL;
1514 if (*p == '-')
1515 val = -val;
1516 rettv->v_type = VAR_NUMBER;
1517 rettv->vval.v_number = val;
1518 }
1519 }
1520 else if (numeric_only)
1521 {
1522 ++p;
1523 break;
1524 }
1525 else if (*p == '!')
1526 {
1527 int v = tv2bool(rettv);
1528
1529 // '!' is permissive in the type.
1530 clear_tv(rettv);
1531 rettv->v_type = VAR_BOOL;
1532 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1533 }
1534 }
1535 *end = p;
1536 return OK;
1537}
1538
1539/*
1540 * Recognize v: variables that are constants and set "rettv".
1541 */
1542 static void
1543get_vim_constant(char_u **arg, typval_T *rettv)
1544{
1545 if (STRNCMP(*arg, "v:true", 6) == 0)
1546 {
1547 rettv->v_type = VAR_BOOL;
1548 rettv->vval.v_number = VVAL_TRUE;
1549 *arg += 6;
1550 }
1551 else if (STRNCMP(*arg, "v:false", 7) == 0)
1552 {
1553 rettv->v_type = VAR_BOOL;
1554 rettv->vval.v_number = VVAL_FALSE;
1555 *arg += 7;
1556 }
1557 else if (STRNCMP(*arg, "v:null", 6) == 0)
1558 {
1559 rettv->v_type = VAR_SPECIAL;
1560 rettv->vval.v_number = VVAL_NULL;
1561 *arg += 6;
1562 }
1563 else if (STRNCMP(*arg, "v:none", 6) == 0)
1564 {
1565 rettv->v_type = VAR_SPECIAL;
1566 rettv->vval.v_number = VVAL_NONE;
1567 *arg += 6;
1568 }
1569}
1570
1571 exprtype_T
1572get_compare_type(char_u *p, int *len, int *type_is)
1573{
1574 exprtype_T type = EXPR_UNKNOWN;
1575 int i;
1576
1577 switch (p[0])
1578 {
1579 case '=': if (p[1] == '=')
1580 type = EXPR_EQUAL;
1581 else if (p[1] == '~')
1582 type = EXPR_MATCH;
1583 break;
1584 case '!': if (p[1] == '=')
1585 type = EXPR_NEQUAL;
1586 else if (p[1] == '~')
1587 type = EXPR_NOMATCH;
1588 break;
1589 case '>': if (p[1] != '=')
1590 {
1591 type = EXPR_GREATER;
1592 *len = 1;
1593 }
1594 else
1595 type = EXPR_GEQUAL;
1596 break;
1597 case '<': if (p[1] != '=')
1598 {
1599 type = EXPR_SMALLER;
1600 *len = 1;
1601 }
1602 else
1603 type = EXPR_SEQUAL;
1604 break;
1605 case 'i': if (p[1] == 's')
1606 {
1607 // "is" and "isnot"; but not a prefix of a name
1608 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1609 *len = 5;
1610 i = p[*len];
1611 if (!isalnum(i) && i != '_')
1612 {
1613 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1614 *type_is = TRUE;
1615 }
1616 }
1617 break;
1618 }
1619 return type;
1620}
1621
1622/*
1623 * Skip over an expression, ignoring most errors.
1624 */
1625 void
1626skip_expr_cctx(char_u **arg, cctx_T *cctx)
1627{
1628 evalarg_T evalarg;
1629
1630 init_evalarg(&evalarg);
1631 evalarg.eval_cctx = cctx;
1632 skip_expr(arg, &evalarg);
1633 clear_evalarg(&evalarg, NULL);
1634}
1635
1636/*
1637 * Check that the top of the type stack has a type that can be used as a
1638 * condition. Give an error and return FAIL if not.
1639 */
1640 int
1641bool_on_stack(cctx_T *cctx)
1642{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 type_T *type;
1644
Bram Moolenaar078a4612022-01-04 15:17:03 +00001645 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001646 if (type == &t_bool)
1647 return OK;
1648
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001649 if (type == &t_any
1650 || type == &t_unknown
1651 || type == &t_number
1652 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001653 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1654 // This requires a runtime type check.
1655 return generate_COND2BOOL(cctx);
1656
1657 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1658}
1659
1660/*
1661 * Give the "white on both sides" error, taking the operator from "p[len]".
1662 */
1663 void
1664error_white_both(char_u *op, int len)
1665{
1666 char_u buf[10];
1667
1668 vim_strncpy(buf, op, len);
1669 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1670}
1671
1672/*
1673 * Compile code to apply '-', '+' and '!'.
1674 * When "numeric_only" is TRUE do not apply '!'.
1675 */
1676 static int
1677compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1678{
1679 char_u *p = *end;
1680
1681 // this works from end to start
1682 while (p > start)
1683 {
1684 --p;
1685 while (VIM_ISWHITE(*p))
1686 --p;
1687 if (*p == '-' || *p == '+')
1688 {
1689 int negate = *p == '-';
1690 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691 type_T *type;
1692
Bram Moolenaar078a4612022-01-04 15:17:03 +00001693 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694 if (type != &t_float && need_type(type, &t_number,
1695 -1, 0, cctx, FALSE, FALSE) == FAIL)
1696 return FAIL;
1697
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698 // only '-' has an effect, for '+' we only check the type
1699 if (negate)
1700 {
1701 isn = generate_instr(cctx, ISN_NEGATENR);
1702 if (isn == NULL)
1703 return FAIL;
1704 }
1705 }
1706 else if (numeric_only)
1707 {
1708 ++p;
1709 break;
1710 }
1711 else
1712 {
1713 int invert = *p == '!';
1714
1715 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1716 {
1717 if (p[-1] == '!')
1718 invert = !invert;
1719 --p;
1720 }
1721 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1722 return FAIL;
1723 }
1724 }
1725 *end = p;
1726 return OK;
1727}
1728
1729/*
1730 * Compile "(expression)": recursive!
1731 * Return FAIL/OK.
1732 */
1733 static int
1734compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1735{
1736 int ret;
1737 char_u *p = *arg + 1;
1738
1739 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1740 return FAIL;
1741 if (ppconst->pp_used <= PPSIZE - 10)
1742 {
1743 ret = compile_expr1(arg, cctx, ppconst);
1744 }
1745 else
1746 {
1747 // Not enough space in ppconst, flush constants.
1748 if (generate_ppconst(cctx, ppconst) == FAIL)
1749 return FAIL;
1750 ret = compile_expr0(arg, cctx);
1751 }
1752 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1753 return FAIL;
1754 if (**arg == ')')
1755 ++*arg;
1756 else if (ret == OK)
1757 {
1758 emsg(_(e_missing_closing_paren));
1759 ret = FAIL;
1760 }
1761 return ret;
1762}
1763
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001764static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001765
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766/*
1767 * Compile whatever comes after "name" or "name()".
1768 * Advances "*arg" only when something was recognized.
1769 */
1770 static int
1771compile_subscript(
1772 char_u **arg,
1773 cctx_T *cctx,
1774 char_u *start_leader,
1775 char_u **end_leader,
1776 ppconst_T *ppconst)
1777{
1778 char_u *name_start = *end_leader;
1779 int keeping_dict = FALSE;
1780
1781 for (;;)
1782 {
1783 char_u *p = skipwhite(*arg);
1784
1785 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1786 {
1787 char_u *next = peek_next_line_from_context(cctx);
1788
1789 // If a following line starts with "->{" or "->X" advance to that
1790 // line, so that a line break before "->" is allowed.
1791 // Also if a following line starts with ".x".
1792 if (next != NULL &&
1793 ((next[0] == '-' && next[1] == '>'
1794 && (next[2] == '{'
1795 || ASCII_ISALPHA(*skipwhite(next + 2))))
1796 || (next[0] == '.' && eval_isdictc(next[1]))))
1797 {
1798 next = next_line_from_context(cctx, TRUE);
1799 if (next == NULL)
1800 return FAIL;
1801 *arg = next;
1802 p = skipwhite(*arg);
1803 }
1804 }
1805
1806 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1807 // is not a function call.
1808 if (**arg == '(')
1809 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001810 type_T *type;
1811 int argcount = 0;
1812
1813 if (generate_ppconst(cctx, ppconst) == FAIL)
1814 return FAIL;
1815 ppconst->pp_is_const = FALSE;
1816
1817 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001818 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819
1820 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001821 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 return FAIL;
1823 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1824 return FAIL;
1825 if (keeping_dict)
1826 {
1827 keeping_dict = FALSE;
1828 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1829 return FAIL;
1830 }
1831 }
1832 else if (*p == '-' && p[1] == '>')
1833 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001834 char_u *pstart = p;
1835 int alt;
1836 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837
Bram Moolenaarc7349932022-01-16 20:59:39 +00001838 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839 if (generate_ppconst(cctx, ppconst) == FAIL)
1840 return FAIL;
1841 ppconst->pp_is_const = FALSE;
1842
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 // Apply the '!', '-' and '+' first:
1844 // -1.0->func() works like (-1.0)->func()
1845 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1846 return FAIL;
1847
1848 p += 2;
1849 *arg = skipwhite(p);
1850 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001851
1852 // Three alternatives handled here:
1853 // 1. "base->name(" only a name, use compile_call()
1854 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1855 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001857 alt = 2;
1858 else
1859 {
1860 // alternative 1 or 3
1861 p = *arg;
1862 if (!eval_isnamec1(*p))
1863 {
1864 semsg(_(e_trailing_characters_str), pstart);
1865 return FAIL;
1866 }
1867 if (ASCII_ISALPHA(*p) && p[1] == ':')
1868 p += 2;
1869 for ( ; eval_isnamec(*p); ++p)
1870 ;
1871 if (*p == '(')
1872 {
1873 // alternative 1
1874 alt = 1;
1875 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1876 return FAIL;
1877 }
1878 else
1879 {
1880 // Must be alternative 3, find the "(". Only works within
1881 // one line.
1882 alt = 3;
1883 paren = vim_strchr(p, '(');
1884 if (paren == NULL)
1885 {
1886 semsg(_(e_missing_parenthesis_str), *arg);
1887 return FAIL;
1888 }
1889 }
1890 }
1891
1892 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001893 {
1894 int argcount = 1;
1895 garray_T *stack = &cctx->ctx_type_stack;
1896 int type_idx_start = stack->ga_len;
1897 type_T *type;
1898 int expr_isn_start = cctx->ctx_instr.ga_len;
1899 int expr_isn_end;
1900 int arg_isn_count;
1901
Bram Moolenaarc7349932022-01-16 20:59:39 +00001902 if (alt == 2)
1903 {
1904 // Funcref call: list->(Refs[2])(arg)
1905 // or lambda: list->((arg) => expr)(arg)
1906 //
1907 // Fist compile the function expression.
1908 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1909 return FAIL;
1910 }
1911 else
1912 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001913 int fail;
1914 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001915 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001916
Bram Moolenaarc7349932022-01-16 20:59:39 +00001917 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001918
1919 // instead of using LOADG for "import.Func" use PUSHFUNC
1920 ++paren_follows_after_expr;
1921
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001922 // do not look in the next line
1923 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001924
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001925 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001926 || *skipwhite(*arg) != NUL;
1927 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001928 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001929 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001930
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001931 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001932 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001933 if (did_emsg == prev_did_emsg)
1934 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001935 return FAIL;
1936 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001937 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001939 // Compile the arguments.
1940 if (**arg != '(')
1941 {
1942 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001943 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001944 else
1945 semsg(_(e_missing_parenthesis_str), *arg);
1946 return FAIL;
1947 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001948
1949 // Remember the next instruction index, where the instructions
1950 // for arguments are being written.
1951 expr_isn_end = cctx->ctx_instr.ga_len;
1952
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001953 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001954 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1955 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956 return FAIL;
1957
1958 // Move the instructions for the arguments to before the
1959 // instructions of the expression and move the type of the
1960 // expression after the argument types. This is what ISN_PCALL
1961 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001962 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1963 if (arg_isn_count > 0)
1964 {
1965 int expr_isn_count = expr_isn_end - expr_isn_start;
1966 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001967 type_T *decl_type;
1968 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969
1970 if (isn == NULL)
1971 return FAIL;
1972 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1973 + expr_isn_start,
1974 sizeof(isn_T) * expr_isn_count);
1975 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1976 + expr_isn_start,
1977 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1978 sizeof(isn_T) * arg_isn_count);
1979 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1980 + expr_isn_start + arg_isn_count,
1981 isn, sizeof(isn_T) * expr_isn_count);
1982 vim_free(isn);
1983
Bram Moolenaar078a4612022-01-04 15:17:03 +00001984 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1985 type = typep->type_curr;
1986 decl_type = typep->type_decl;
1987 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1988 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1989 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001991 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1992 typep->type_curr = type;
1993 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001994 }
1995
Bram Moolenaar078a4612022-01-04 15:17:03 +00001996 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1998 return FAIL;
1999 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002000
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002001 if (keeping_dict)
2002 {
2003 keeping_dict = FALSE;
2004 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2005 return FAIL;
2006 }
2007 }
2008 else if (**arg == '[')
2009 {
2010 int is_slice = FALSE;
2011
2012 // list index: list[123]
2013 // dict member: dict[key]
2014 // string index: text[123]
2015 // blob index: blob[123]
2016 if (generate_ppconst(cctx, ppconst) == FAIL)
2017 return FAIL;
2018 ppconst->pp_is_const = FALSE;
2019
2020 ++p;
2021 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2022 return FAIL;
2023 if (**arg == ':')
2024 {
2025 // missing first index is equal to zero
2026 generate_PUSHNR(cctx, 0);
2027 }
2028 else
2029 {
2030 if (compile_expr0(arg, cctx) == FAIL)
2031 return FAIL;
2032 if (**arg == ':')
2033 {
2034 semsg(_(e_white_space_required_before_and_after_str_at_str),
2035 ":", *arg);
2036 return FAIL;
2037 }
2038 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2039 return FAIL;
2040 *arg = skipwhite(*arg);
2041 }
2042 if (**arg == ':')
2043 {
2044 is_slice = TRUE;
2045 ++*arg;
2046 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2047 {
2048 semsg(_(e_white_space_required_before_and_after_str_at_str),
2049 ":", *arg);
2050 return FAIL;
2051 }
2052 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2053 return FAIL;
2054 if (**arg == ']')
2055 // missing second index is equal to end of string
2056 generate_PUSHNR(cctx, -1);
2057 else
2058 {
2059 if (compile_expr0(arg, cctx) == FAIL)
2060 return FAIL;
2061 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2062 return FAIL;
2063 *arg = skipwhite(*arg);
2064 }
2065 }
2066
2067 if (**arg != ']')
2068 {
2069 emsg(_(e_missing_closing_square_brace));
2070 return FAIL;
2071 }
2072 *arg = *arg + 1;
2073
2074 if (keeping_dict)
2075 {
2076 keeping_dict = FALSE;
2077 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2078 return FAIL;
2079 }
2080 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2081 return FAIL;
2082 }
2083 else if (*p == '.' && p[1] != '.')
2084 {
2085 // dictionary member: dict.name
2086 if (generate_ppconst(cctx, ppconst) == FAIL)
2087 return FAIL;
2088 ppconst->pp_is_const = FALSE;
2089
2090 *arg = p + 1;
2091 if (IS_WHITE_OR_NUL(**arg))
2092 {
2093 emsg(_(e_missing_name_after_dot));
2094 return FAIL;
2095 }
2096 p = *arg;
2097 if (eval_isdictc(*p))
2098 while (eval_isnamec(*p))
2099 MB_PTR_ADV(p);
2100 if (p == *arg)
2101 {
2102 semsg(_(e_syntax_error_at_str), *arg);
2103 return FAIL;
2104 }
2105 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2106 return FAIL;
2107 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2108 return FAIL;
2109 keeping_dict = TRUE;
2110 *arg = p;
2111 }
2112 else
2113 break;
2114 }
2115
2116 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2117 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002118 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2119 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002120 return FAIL;
2121
2122 return OK;
2123}
2124
2125/*
2126 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2127 * "arg" is advanced until after the expression, skipping white space.
2128 *
2129 * If the value is a constant "ppconst->pp_used" will be non-zero.
2130 * Before instructions are generated, any values in "ppconst" will generated.
2131 *
2132 * This is the compiling equivalent of eval1(), eval2(), etc.
2133 */
2134
2135/*
2136 * number number constant
2137 * 0zFFFFFFFF Blob constant
2138 * "string" string constant
2139 * 'string' literal string constant
2140 * &option-name option value
2141 * @r register contents
2142 * identifier variable value
2143 * function() function call
2144 * $VAR environment variable
2145 * (expression) nested expression
2146 * [expr, expr] List
2147 * {key: val, [key]: val} Dictionary
2148 *
2149 * Also handle:
2150 * ! in front logical NOT
2151 * - in front unary minus
2152 * + in front unary plus (ignored)
2153 * trailing (arg) funcref/partial call
2154 * trailing [] subscript in String or List
2155 * trailing .name entry in Dictionary
2156 * trailing ->name() method call
2157 */
2158 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002159compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002160 char_u **arg,
2161 cctx_T *cctx,
2162 ppconst_T *ppconst)
2163{
2164 char_u *start_leader, *end_leader;
2165 int ret = OK;
2166 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2167 int used_before = ppconst->pp_used;
2168
2169 ppconst->pp_is_const = FALSE;
2170
2171 /*
2172 * Skip '!', '-' and '+' characters. They are handled later.
2173 */
2174 start_leader = *arg;
2175 if (eval_leader(arg, TRUE) == FAIL)
2176 return FAIL;
2177 end_leader = *arg;
2178
2179 rettv->v_type = VAR_UNKNOWN;
2180 switch (**arg)
2181 {
2182 /*
2183 * Number constant.
2184 */
2185 case '0': // also for blob starting with 0z
2186 case '1':
2187 case '2':
2188 case '3':
2189 case '4':
2190 case '5':
2191 case '6':
2192 case '7':
2193 case '8':
2194 case '9':
2195 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2196 return FAIL;
2197 // Apply "-" and "+" just before the number now, right to
2198 // left. Matters especially when "->" follows. Stops at
2199 // '!'.
2200 if (apply_leader(rettv, TRUE,
2201 start_leader, &end_leader) == FAIL)
2202 {
2203 clear_tv(rettv);
2204 return FAIL;
2205 }
2206 break;
2207
2208 /*
2209 * String constant: "string".
2210 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002211 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002212 return FAIL;
2213 break;
2214
2215 /*
2216 * Literal string constant: 'str''ing'.
2217 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002218 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002219 return FAIL;
2220 break;
2221
2222 /*
2223 * Constant Vim variable.
2224 */
2225 case 'v': get_vim_constant(arg, rettv);
2226 ret = NOTDONE;
2227 break;
2228
2229 /*
2230 * "true" constant
2231 */
2232 case 't': if (STRNCMP(*arg, "true", 4) == 0
2233 && !eval_isnamec((*arg)[4]))
2234 {
2235 *arg += 4;
2236 rettv->v_type = VAR_BOOL;
2237 rettv->vval.v_number = VVAL_TRUE;
2238 }
2239 else
2240 ret = NOTDONE;
2241 break;
2242
2243 /*
2244 * "false" constant
2245 */
2246 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2247 && !eval_isnamec((*arg)[5]))
2248 {
2249 *arg += 5;
2250 rettv->v_type = VAR_BOOL;
2251 rettv->vval.v_number = VVAL_FALSE;
2252 }
2253 else
2254 ret = NOTDONE;
2255 break;
2256
2257 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002258 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002259 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002260 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002262 char_u *p = *arg + 4;
2263 int len;
2264
2265 for (len = 0; eval_isnamec(p[len]); ++len)
2266 ;
2267 ret = handle_predefined(*arg, len + 4, rettv);
2268 if (ret == FAIL)
2269 ret = NOTDONE;
2270 else
2271 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002272 }
2273 else
2274 ret = NOTDONE;
2275 break;
2276
2277 /*
2278 * List: [expr, expr]
2279 */
2280 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2281 return FAIL;
2282 ret = compile_list(arg, cctx, ppconst);
2283 break;
2284
2285 /*
2286 * Dictionary: {'key': val, 'key': val}
2287 */
2288 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2289 return FAIL;
2290 ret = compile_dict(arg, cctx, ppconst);
2291 break;
2292
2293 /*
2294 * Option value: &name
2295 */
2296 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2297 return FAIL;
2298 ret = compile_get_option(arg, cctx);
2299 break;
2300
2301 /*
2302 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002303 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304 */
2305 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2306 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002307 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2308 ret = compile_interp_string(arg, cctx);
2309 else
2310 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002311 break;
2312
2313 /*
2314 * Register contents: @r.
2315 */
2316 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2317 return FAIL;
2318 ret = compile_get_register(arg, cctx);
2319 break;
2320 /*
2321 * nested expression: (expression).
2322 * lambda: (arg, arg) => expr
2323 * funcref: (arg, arg) => { statement }
2324 */
2325 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2326 ret = compile_lambda(arg, cctx);
2327 if (ret == NOTDONE)
2328 ret = compile_parenthesis(arg, cctx, ppconst);
2329 break;
2330
2331 default: ret = NOTDONE;
2332 break;
2333 }
2334 if (ret == FAIL)
2335 return FAIL;
2336
2337 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2338 {
2339 if (cctx->ctx_skip == SKIP_YES)
2340 clear_tv(rettv);
2341 else
2342 // A constant expression can possibly be handled compile time,
2343 // return the value instead of generating code.
2344 ++ppconst->pp_used;
2345 }
2346 else if (ret == NOTDONE)
2347 {
2348 char_u *p;
2349 int r;
2350
2351 if (!eval_isnamec1(**arg))
2352 {
2353 if (!vim9_bad_comment(*arg))
2354 {
2355 if (ends_excmd(*skipwhite(*arg)))
2356 semsg(_(e_empty_expression_str), *arg);
2357 else
2358 semsg(_(e_name_expected_str), *arg);
2359 }
2360 return FAIL;
2361 }
2362
2363 // "name" or "name()"
2364 p = to_name_end(*arg, TRUE);
2365 if (p - *arg == (size_t)1 && **arg == '_')
2366 {
2367 emsg(_(e_cannot_use_underscore_here));
2368 return FAIL;
2369 }
2370
2371 if (*p == '(')
2372 {
2373 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2374 }
2375 else
2376 {
2377 if (cctx->ctx_skip != SKIP_YES
2378 && generate_ppconst(cctx, ppconst) == FAIL)
2379 return FAIL;
2380 r = compile_load(arg, p, cctx, TRUE, TRUE);
2381 }
2382 if (r == FAIL)
2383 return FAIL;
2384 }
2385
2386 // Handle following "[]", ".member", etc.
2387 // Then deal with prefixed '-', '+' and '!', if not done already.
2388 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2389 ppconst) == FAIL)
2390 return FAIL;
2391 if (ppconst->pp_used > 0)
2392 {
2393 // apply the '!', '-' and '+' before the constant
2394 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2395 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2396 return FAIL;
2397 return OK;
2398 }
2399 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2400 return FAIL;
2401 return OK;
2402}
2403
2404/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002405 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002406 */
2407 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002408compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409{
2410 type_T *want_type = NULL;
2411
2412 // Recognize <type>
2413 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2414 {
2415 ++*arg;
2416 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2417 if (want_type == NULL)
2418 return FAIL;
2419
2420 if (**arg != '>')
2421 {
2422 if (*skipwhite(*arg) == '>')
2423 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2424 else
2425 emsg(_(e_missing_gt));
2426 return FAIL;
2427 }
2428 ++*arg;
2429 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2430 return FAIL;
2431 }
2432
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002433 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002434 return FAIL;
2435
2436 if (want_type != NULL)
2437 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002438 type_T *actual;
2439 where_T where = WHERE_INIT;
2440
2441 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002442 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002443 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002445 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2446 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447 return FAIL;
2448 }
2449 }
2450
2451 return OK;
2452}
2453
2454/*
2455 * * number multiplication
2456 * / number division
2457 * % number modulo
2458 */
2459 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002460compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461{
2462 char_u *op;
2463 char_u *next;
2464 int ppconst_used = ppconst->pp_used;
2465
2466 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002467 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002468 return FAIL;
2469
2470 /*
2471 * Repeat computing, until no "*", "/" or "%" is following.
2472 */
2473 for (;;)
2474 {
2475 op = may_peek_next_line(cctx, *arg, &next);
2476 if (*op != '*' && *op != '/' && *op != '%')
2477 break;
2478 if (next != NULL)
2479 {
2480 *arg = next_line_from_context(cctx, TRUE);
2481 op = skipwhite(*arg);
2482 }
2483
2484 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2485 {
2486 error_white_both(op, 1);
2487 return FAIL;
2488 }
2489 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2490 return FAIL;
2491
2492 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002493 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002494 return FAIL;
2495
2496 if (ppconst->pp_used == ppconst_used + 2
2497 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2498 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2499 {
2500 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2501 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2502 varnumber_T res = 0;
2503 int failed = FALSE;
2504
2505 // both are numbers: compute the result
2506 switch (*op)
2507 {
2508 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2509 break;
2510 case '/': res = num_divide(tv1->vval.v_number,
2511 tv2->vval.v_number, &failed);
2512 break;
2513 case '%': res = num_modulus(tv1->vval.v_number,
2514 tv2->vval.v_number, &failed);
2515 break;
2516 }
2517 if (failed)
2518 return FAIL;
2519 tv1->vval.v_number = res;
2520 --ppconst->pp_used;
2521 }
2522 else
2523 {
2524 generate_ppconst(cctx, ppconst);
2525 generate_two_op(cctx, op);
2526 }
2527 }
2528
2529 return OK;
2530}
2531
2532/*
2533 * + number addition or list/blobl concatenation
2534 * - number subtraction
2535 * .. string concatenation
2536 */
2537 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002538compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539{
2540 char_u *op;
2541 char_u *next;
2542 int oplen;
2543 int ppconst_used = ppconst->pp_used;
2544
2545 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002546 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002547 return FAIL;
2548
2549 /*
2550 * Repeat computing, until no "+", "-" or ".." is following.
2551 */
2552 for (;;)
2553 {
2554 op = may_peek_next_line(cctx, *arg, &next);
2555 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2556 break;
2557 if (op[0] == op[1] && *op != '.' && next)
2558 // Finding "++" or "--" on the next line is a separate command.
2559 // But ".." is concatenation.
2560 break;
2561 oplen = (*op == '.' ? 2 : 1);
2562 if (next != NULL)
2563 {
2564 *arg = next_line_from_context(cctx, TRUE);
2565 op = skipwhite(*arg);
2566 }
2567
2568 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2569 {
2570 error_white_both(op, oplen);
2571 return FAIL;
2572 }
2573
2574 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2575 return FAIL;
2576
2577 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002578 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002579 return FAIL;
2580
2581 if (ppconst->pp_used == ppconst_used + 2
2582 && (*op == '.'
2583 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2584 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2585 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2586 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2587 {
2588 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2589 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2590
2591 // concat/subtract/add constant numbers
2592 if (*op == '+')
2593 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2594 else if (*op == '-')
2595 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2596 else
2597 {
2598 // concatenate constant strings
2599 char_u *s1 = tv1->vval.v_string;
2600 char_u *s2 = tv2->vval.v_string;
2601 size_t len1 = STRLEN(s1);
2602
2603 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2604 if (tv1->vval.v_string == NULL)
2605 {
2606 clear_ppconst(ppconst);
2607 return FAIL;
2608 }
2609 mch_memmove(tv1->vval.v_string, s1, len1);
2610 STRCPY(tv1->vval.v_string + len1, s2);
2611 vim_free(s1);
2612 vim_free(s2);
2613 }
2614 --ppconst->pp_used;
2615 }
2616 else
2617 {
2618 generate_ppconst(cctx, ppconst);
2619 ppconst->pp_is_const = FALSE;
2620 if (*op == '.')
2621 {
2622 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2623 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2624 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002625 if (generate_CONCAT(cctx, 2) == FAIL)
2626 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002627 }
2628 else
2629 generate_two_op(cctx, op);
2630 }
2631 }
2632
2633 return OK;
2634}
2635
2636/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002637 * expr6a >> expr6b
2638 * expr6a << expr6b
2639 *
2640 * Produces instructions:
2641 * OPNR bitwise left or right shift
2642 */
2643 static int
2644compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2645{
2646 exprtype_T type = EXPR_UNKNOWN;
2647 char_u *p;
2648 char_u *next;
2649 int len = 2;
2650 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002651 isn_T *isn;
2652
2653 // get the first variable
2654 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2655 return FAIL;
2656
2657 /*
2658 * Repeat computing, until no "+", "-" or ".." is following.
2659 */
2660 for (;;)
2661 {
2662 type = EXPR_UNKNOWN;
2663
2664 p = may_peek_next_line(cctx, *arg, &next);
2665 if (p[0] == '<' && p[1] == '<')
2666 type = EXPR_LSHIFT;
2667 else if (p[0] == '>' && p[1] == '>')
2668 type = EXPR_RSHIFT;
2669
2670 if (type == EXPR_UNKNOWN)
2671 return OK;
2672
2673 // Handle a bitwise left or right shift operator
2674 if (ppconst->pp_used == ppconst_used + 1)
2675 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002676 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002677 {
2678 // left operand should be a number
2679 emsg(_(e_bitshift_ops_must_be_number));
2680 return FAIL;
2681 }
2682 }
2683 else
2684 {
2685 type_T *t = get_type_on_stack(cctx, 0);
2686
2687 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2688 {
2689 emsg(_(e_bitshift_ops_must_be_number));
2690 return FAIL;
2691 }
2692 }
2693
2694 if (next != NULL)
2695 {
2696 *arg = next_line_from_context(cctx, TRUE);
2697 p = skipwhite(*arg);
2698 }
2699
2700 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2701 {
2702 error_white_both(p, len);
2703 return FAIL;
2704 }
2705
2706 // get the second variable
2707 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2708 return FAIL;
2709
2710 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2711 return FAIL;
2712
2713 if (ppconst->pp_used == ppconst_used + 2)
2714 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002715 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2716 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2717
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002718 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002719 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2720 {
2721 // right operand should be a positive number
2722 if (tv2->v_type != VAR_NUMBER)
2723 emsg(_(e_bitshift_ops_must_be_number));
2724 else
2725 emsg(_(e_bitshift_ops_must_be_postive));
2726 return FAIL;
2727 }
2728
2729 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2730 tv1->vval.v_number = 0;
2731 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002732 tv1->vval.v_number =
2733 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002734 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002735 tv1->vval.v_number =
2736 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002737 clear_tv(tv2);
2738 --ppconst->pp_used;
2739 }
2740 else
2741 {
2742 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2743 FALSE, FALSE) == FAIL)
2744 {
2745 emsg(_(e_bitshift_ops_must_be_number));
2746 return FAIL;
2747 }
2748
2749 generate_ppconst(cctx, ppconst);
2750
2751 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2752 if (isn == NULL)
2753 return FAIL;
2754
2755 if (isn != NULL)
2756 isn->isn_arg.op.op_type = type;
2757 }
2758 }
2759
2760 return OK;
2761}
2762
2763/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002764 * expr5a == expr5b
2765 * expr5a =~ expr5b
2766 * expr5a != expr5b
2767 * expr5a !~ expr5b
2768 * expr5a > expr5b
2769 * expr5a >= expr5b
2770 * expr5a < expr5b
2771 * expr5a <= expr5b
2772 * expr5a is expr5b
2773 * expr5a isnot expr5b
2774 *
2775 * Produces instructions:
2776 * EVAL expr5a Push result of "expr5a"
2777 * EVAL expr5b Push result of "expr5b"
2778 * COMPARE one of the compare instructions
2779 */
2780 static int
2781compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2782{
2783 exprtype_T type = EXPR_UNKNOWN;
2784 char_u *p;
2785 char_u *next;
2786 int len = 2;
2787 int type_is = FALSE;
2788 int ppconst_used = ppconst->pp_used;
2789
2790 // get the first variable
2791 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2792 return FAIL;
2793
2794 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002795
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002796 type = get_compare_type(p, &len, &type_is);
2797
2798 /*
2799 * If there is a comparative operator, use it.
2800 */
2801 if (type != EXPR_UNKNOWN)
2802 {
2803 int ic = FALSE; // Default: do not ignore case
2804
2805 if (next != NULL)
2806 {
2807 *arg = next_line_from_context(cctx, TRUE);
2808 p = skipwhite(*arg);
2809 }
2810 if (type_is && (p[len] == '?' || p[len] == '#'))
2811 {
2812 semsg(_(e_invalid_expression_str), *arg);
2813 return FAIL;
2814 }
2815 // extra question mark appended: ignore case
2816 if (p[len] == '?')
2817 {
2818 ic = TRUE;
2819 ++len;
2820 }
2821 // extra '#' appended: match case (ignored)
2822 else if (p[len] == '#')
2823 ++len;
2824 // nothing appended: match case
2825
2826 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2827 {
2828 error_white_both(p, len);
2829 return FAIL;
2830 }
2831
2832 // get the second variable
2833 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2834 return FAIL;
2835
2836 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2837 return FAIL;
2838
2839 if (ppconst->pp_used == ppconst_used + 2)
2840 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002841 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002842 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2843 int ret;
2844
2845 // Both sides are a constant, compute the result now.
2846 // First check for a valid combination of types, this is more
2847 // strict than typval_compare().
2848 if (check_compare_types(type, tv1, tv2) == FAIL)
2849 ret = FAIL;
2850 else
2851 {
2852 ret = typval_compare(tv1, tv2, type, ic);
2853 tv1->v_type = VAR_BOOL;
2854 tv1->vval.v_number = tv1->vval.v_number
2855 ? VVAL_TRUE : VVAL_FALSE;
2856 clear_tv(tv2);
2857 --ppconst->pp_used;
2858 }
2859 return ret;
2860 }
2861
2862 generate_ppconst(cctx, ppconst);
2863 return generate_COMPARE(cctx, type, ic);
2864 }
2865
2866 return OK;
2867}
2868
2869static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2870
2871/*
2872 * Compile || or &&.
2873 */
2874 static int
2875compile_and_or(
2876 char_u **arg,
2877 cctx_T *cctx,
2878 char *op,
2879 ppconst_T *ppconst,
2880 int ppconst_used UNUSED)
2881{
2882 char_u *next;
2883 char_u *p = may_peek_next_line(cctx, *arg, &next);
2884 int opchar = *op;
2885
2886 if (p[0] == opchar && p[1] == opchar)
2887 {
2888 garray_T *instr = &cctx->ctx_instr;
2889 garray_T end_ga;
2890 int save_skip = cctx->ctx_skip;
2891
2892 /*
2893 * Repeat until there is no following "||" or "&&"
2894 */
2895 ga_init2(&end_ga, sizeof(int), 10);
2896 while (p[0] == opchar && p[1] == opchar)
2897 {
2898 long start_lnum = SOURCING_LNUM;
2899 long save_sourcing_lnum;
2900 int start_ctx_lnum = cctx->ctx_lnum;
2901 int save_lnum;
2902 int const_used;
2903 int status;
2904 jumpwhen_T jump_when = opchar == '|'
2905 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2906
2907 if (next != NULL)
2908 {
2909 *arg = next_line_from_context(cctx, TRUE);
2910 p = skipwhite(*arg);
2911 }
2912
2913 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2914 {
2915 semsg(_(e_white_space_required_before_and_after_str_at_str),
2916 op, p);
2917 ga_clear(&end_ga);
2918 return FAIL;
2919 }
2920
2921 save_sourcing_lnum = SOURCING_LNUM;
2922 SOURCING_LNUM = start_lnum;
2923 save_lnum = cctx->ctx_lnum;
2924 cctx->ctx_lnum = start_ctx_lnum;
2925
2926 status = check_ppconst_bool(ppconst);
2927 if (status != FAIL)
2928 {
2929 // Use the last ppconst if possible.
2930 if (ppconst->pp_used > 0)
2931 {
2932 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2933 int is_true = tv2bool(tv);
2934
2935 if ((is_true && opchar == '|')
2936 || (!is_true && opchar == '&'))
2937 {
2938 // For "false && expr" and "true || expr" the "expr"
2939 // does not need to be evaluated.
2940 cctx->ctx_skip = SKIP_YES;
2941 clear_tv(tv);
2942 tv->v_type = VAR_BOOL;
2943 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2944 }
2945 else
2946 {
2947 // For "true && expr" and "false || expr" only "expr"
2948 // needs to be evaluated.
2949 --ppconst->pp_used;
2950 jump_when = JUMP_NEVER;
2951 }
2952 }
2953 else
2954 {
2955 // Every part must evaluate to a bool.
2956 status = bool_on_stack(cctx);
2957 }
2958 }
2959 if (status != FAIL)
2960 status = ga_grow(&end_ga, 1);
2961 cctx->ctx_lnum = save_lnum;
2962 if (status == FAIL)
2963 {
2964 ga_clear(&end_ga);
2965 return FAIL;
2966 }
2967
2968 if (jump_when != JUMP_NEVER)
2969 {
2970 if (cctx->ctx_skip != SKIP_YES)
2971 {
2972 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2973 ++end_ga.ga_len;
2974 }
2975 generate_JUMP(cctx, jump_when, 0);
2976 }
2977
2978 // eval the next expression
2979 SOURCING_LNUM = save_sourcing_lnum;
2980 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2981 {
2982 ga_clear(&end_ga);
2983 return FAIL;
2984 }
2985
2986 const_used = ppconst->pp_used;
2987 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2988 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2989 {
2990 ga_clear(&end_ga);
2991 return FAIL;
2992 }
2993
2994 // "0 || 1" results in true, "1 && 0" results in false.
2995 if (ppconst->pp_used == const_used + 1)
2996 {
2997 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2998
2999 if (tv->v_type == VAR_NUMBER
3000 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3001 {
3002 tv->vval.v_number = tv->vval.v_number == 1
3003 ? VVAL_TRUE : VVAL_FALSE;
3004 tv->v_type = VAR_BOOL;
3005 }
3006 }
3007
3008 p = may_peek_next_line(cctx, *arg, &next);
3009 }
3010
3011 if (check_ppconst_bool(ppconst) == FAIL)
3012 {
3013 ga_clear(&end_ga);
3014 return FAIL;
3015 }
3016
3017 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3018 // Every part must evaluate to a bool.
3019 if (bool_on_stack(cctx) == FAIL)
3020 {
3021 ga_clear(&end_ga);
3022 return FAIL;
3023 }
3024
3025 if (end_ga.ga_len > 0)
3026 {
3027 // Fill in the end label in all jumps.
3028 generate_ppconst(cctx, ppconst);
3029 while (end_ga.ga_len > 0)
3030 {
3031 isn_T *isn;
3032
3033 --end_ga.ga_len;
3034 isn = ((isn_T *)instr->ga_data)
3035 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3036 isn->isn_arg.jump.jump_where = instr->ga_len;
3037 }
3038 }
3039 ga_clear(&end_ga);
3040
3041 cctx->ctx_skip = save_skip;
3042 }
3043
3044 return OK;
3045}
3046
3047/*
3048 * expr4a && expr4a && expr4a logical AND
3049 *
3050 * Produces instructions:
3051 * EVAL expr4a Push result of "expr4a"
3052 * COND2BOOL convert to bool if needed
3053 * JUMP_IF_COND_FALSE end
3054 * EVAL expr4b Push result of "expr4b"
3055 * JUMP_IF_COND_FALSE end
3056 * EVAL expr4c Push result of "expr4c"
3057 * end:
3058 */
3059 static int
3060compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3061{
3062 int ppconst_used = ppconst->pp_used;
3063
3064 // get the first variable
3065 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3066 return FAIL;
3067
3068 // || and && work almost the same
3069 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3070}
3071
3072/*
3073 * expr3a || expr3b || expr3c logical OR
3074 *
3075 * Produces instructions:
3076 * EVAL expr3a Push result of "expr3a"
3077 * COND2BOOL convert to bool if needed
3078 * JUMP_IF_COND_TRUE end
3079 * EVAL expr3b Push result of "expr3b"
3080 * JUMP_IF_COND_TRUE end
3081 * EVAL expr3c Push result of "expr3c"
3082 * end:
3083 */
3084 static int
3085compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3086{
3087 int ppconst_used = ppconst->pp_used;
3088
3089 // eval the first expression
3090 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3091 return FAIL;
3092
3093 // || and && work almost the same
3094 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3095}
3096
3097/*
3098 * Toplevel expression: expr2 ? expr1a : expr1b
3099 * Produces instructions:
3100 * EVAL expr2 Push result of "expr2"
3101 * JUMP_IF_FALSE alt jump if false
3102 * EVAL expr1a
3103 * JUMP_ALWAYS end
3104 * alt: EVAL expr1b
3105 * end:
3106 *
3107 * Toplevel expression: expr2 ?? expr1
3108 * Produces instructions:
3109 * EVAL expr2 Push result of "expr2"
3110 * JUMP_AND_KEEP_IF_TRUE end jump if true
3111 * EVAL expr1
3112 * end:
3113 */
3114 int
3115compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3116{
3117 char_u *p;
3118 int ppconst_used = ppconst->pp_used;
3119 char_u *next;
3120
3121 // Ignore all kinds of errors when not producing code.
3122 if (cctx->ctx_skip == SKIP_YES)
3123 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003124 int prev_did_emsg = did_emsg;
3125
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003126 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003127 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003128 }
3129
3130 // Evaluate the first expression.
3131 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3132 return FAIL;
3133
3134 p = may_peek_next_line(cctx, *arg, &next);
3135 if (*p == '?')
3136 {
3137 int op_falsy = p[1] == '?';
3138 garray_T *instr = &cctx->ctx_instr;
3139 garray_T *stack = &cctx->ctx_type_stack;
3140 int alt_idx = instr->ga_len;
3141 int end_idx = 0;
3142 isn_T *isn;
3143 type_T *type1 = NULL;
3144 int has_const_expr = FALSE;
3145 int const_value = FALSE;
3146 int save_skip = cctx->ctx_skip;
3147
3148 if (next != NULL)
3149 {
3150 *arg = next_line_from_context(cctx, TRUE);
3151 p = skipwhite(*arg);
3152 }
3153
3154 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3155 {
3156 semsg(_(e_white_space_required_before_and_after_str_at_str),
3157 op_falsy ? "??" : "?", p);
3158 return FAIL;
3159 }
3160
3161 if (ppconst->pp_used == ppconst_used + 1)
3162 {
3163 // the condition is a constant, we know whether the ? or the :
3164 // expression is to be evaluated.
3165 has_const_expr = TRUE;
3166 if (op_falsy)
3167 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3168 else
3169 {
3170 int error = FALSE;
3171
3172 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3173 &error);
3174 if (error)
3175 return FAIL;
3176 }
3177 cctx->ctx_skip = save_skip == SKIP_YES ||
3178 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3179
3180 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3181 // "left ?? right" and "left" is truthy: produce "left"
3182 generate_ppconst(cctx, ppconst);
3183 else
3184 {
3185 clear_tv(&ppconst->pp_tv[ppconst_used]);
3186 --ppconst->pp_used;
3187 }
3188 }
3189 else
3190 {
3191 generate_ppconst(cctx, ppconst);
3192 if (op_falsy)
3193 end_idx = instr->ga_len;
3194 generate_JUMP(cctx, op_falsy
3195 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3196 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003197 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003198 }
3199
3200 // evaluate the second expression; any type is accepted
3201 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3202 return FAIL;
3203 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3204 return FAIL;
3205
3206 if (!has_const_expr)
3207 {
3208 generate_ppconst(cctx, ppconst);
3209
3210 if (!op_falsy)
3211 {
3212 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003213 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003214 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003215
3216 end_idx = instr->ga_len;
3217 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3218
3219 // jump here from JUMP_IF_FALSE
3220 isn = ((isn_T *)instr->ga_data) + alt_idx;
3221 isn->isn_arg.jump.jump_where = instr->ga_len;
3222 }
3223 }
3224
3225 if (!op_falsy)
3226 {
3227 // Check for the ":".
3228 p = may_peek_next_line(cctx, *arg, &next);
3229 if (*p != ':')
3230 {
3231 emsg(_(e_missing_colon_after_questionmark));
3232 return FAIL;
3233 }
3234 if (next != NULL)
3235 {
3236 *arg = next_line_from_context(cctx, TRUE);
3237 p = skipwhite(*arg);
3238 }
3239
3240 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3241 {
3242 semsg(_(e_white_space_required_before_and_after_str_at_str),
3243 ":", p);
3244 return FAIL;
3245 }
3246
3247 // evaluate the third expression
3248 if (has_const_expr)
3249 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3250 ? SKIP_YES : SKIP_NOT;
3251 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3252 return FAIL;
3253 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3254 return FAIL;
3255 }
3256
3257 if (!has_const_expr)
3258 {
3259 type_T **typep;
3260
3261 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003262 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003263
3264 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003265 typep = &((((type2_T *)stack->ga_data)
3266 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003267 common_type(type1, *typep, typep, cctx->ctx_type_list);
3268
3269 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3270 isn = ((isn_T *)instr->ga_data) + end_idx;
3271 isn->isn_arg.jump.jump_where = instr->ga_len;
3272 }
3273
3274 cctx->ctx_skip = save_skip;
3275 }
3276 return OK;
3277}
3278
3279/*
3280 * Toplevel expression.
3281 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3282 * Returns OK or FAIL.
3283 */
3284 int
3285compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3286{
3287 ppconst_T ppconst;
3288
3289 CLEAR_FIELD(ppconst);
3290 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3291 {
3292 clear_ppconst(&ppconst);
3293 return FAIL;
3294 }
3295 if (is_const != NULL)
3296 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3297 if (generate_ppconst(cctx, &ppconst) == FAIL)
3298 return FAIL;
3299 return OK;
3300}
3301
3302/*
3303 * Toplevel expression.
3304 */
3305 int
3306compile_expr0(char_u **arg, cctx_T *cctx)
3307{
3308 return compile_expr0_ext(arg, cctx, NULL);
3309}
3310
3311
3312#endif // defined(FEAT_EVAL)