blob: 3da556cb10293c7cb835928612da347c2a8a5ce7 [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;
499
500 name = vim_strnsave(*arg, end - *arg);
501 if (name == NULL)
502 return FAIL;
503
504 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
505 {
506 script_autoload(name, FALSE);
507 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
508 }
509 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
510 == OK)
511 {
512 if (gen_load_outer == 0)
513 gen_load = TRUE;
514 }
515 else
516 {
517 lvar_T lvar;
518
519 if (lookup_local(*arg, len, &lvar, cctx) == OK)
520 {
521 type = lvar.lv_type;
522 idx = lvar.lv_idx;
523 if (lvar.lv_from_outer != 0)
524 gen_load_outer = lvar.lv_from_outer;
525 else
526 gen_load = TRUE;
527 }
528 else
529 {
530 // "var" can be script-local even without using "s:" if it
531 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000532 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000533 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100534 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535
536 // When evaluating an expression and the name starts with an
537 // uppercase letter it can be a user defined function.
538 // generate_funcref() will fail if the function can't be found.
539 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000540 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000541 }
542 }
543 if (gen_load)
544 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
545 if (gen_load_outer > 0)
546 {
547 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
548 cctx->ctx_outer_used = TRUE;
549 }
550 }
551
552 *arg = end;
553
554theend:
555 if (res == FAIL && error && called_emsg == prev_called_emsg)
556 semsg(_(e_variable_not_found_str), name);
557 vim_free(name);
558 return res;
559}
560
561/*
562 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100563 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000564 * Returns FAIL if compilation fails.
565 */
566 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100567compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568{
LemonBoyf3b48952022-05-05 13:53:03 +0100569 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570 garray_T save_ga = cctx->ctx_instr;
571 int expr_res;
572 int trailing_error;
573 int instr_count;
574 isn_T *instr = NULL;
575
576 // Remove the string type from the stack.
577 --cctx->ctx_type_stack.ga_len;
578
579 // Temporarily reset the list of instructions so that the jump labels are
580 // correct.
581 cctx->ctx_instr.ga_len = 0;
582 cctx->ctx_instr.ga_maxlen = 0;
583 cctx->ctx_instr.ga_data = NULL;
584 expr_res = compile_expr0(&s, cctx);
585 s = skipwhite(s);
586 trailing_error = *s != NUL;
587
588 if (expr_res == FAIL || trailing_error
589 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
590 {
591 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000592 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000593 clear_instr_ga(&cctx->ctx_instr);
594 cctx->ctx_instr = save_ga;
595 ++cctx->ctx_type_stack.ga_len;
596 return FAIL;
597 }
598
599 // Move the generated instructions into the ISN_INSTR instruction, then
600 // restore the list of instructions.
601 instr_count = cctx->ctx_instr.ga_len;
602 instr = cctx->ctx_instr.ga_data;
603 instr[instr_count].isn_type = ISN_FINISH;
604
605 cctx->ctx_instr = save_ga;
606 vim_free(isn->isn_arg.string);
607 isn->isn_type = ISN_INSTR;
608 isn->isn_arg.instr = instr;
609 return OK;
610}
611
612/*
613 * Compile the argument expressions.
614 * "arg" points to just after the "(" and is advanced to after the ")"
615 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100616 int
LemonBoyf3b48952022-05-05 13:53:03 +0100617compile_arguments(
618 char_u **arg,
619 cctx_T *cctx,
620 int *argcount,
621 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622{
623 char_u *p = *arg;
624 char_u *whitep = *arg;
625 int must_end = FALSE;
626 int instr_count;
627
628 for (;;)
629 {
630 if (may_get_next_line(whitep, &p, cctx) == FAIL)
631 goto failret;
632 if (*p == ')')
633 {
634 *arg = p + 1;
635 return OK;
636 }
637 if (must_end)
638 {
639 semsg(_(e_missing_comma_before_argument_str), p);
640 return FAIL;
641 }
642
643 instr_count = cctx->ctx_instr.ga_len;
644 if (compile_expr0(&p, cctx) == FAIL)
645 return FAIL;
646 ++*argcount;
647
LemonBoyf3b48952022-05-05 13:53:03 +0100648 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000649 && cctx->ctx_instr.ga_len == instr_count + 1)
650 {
651 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
652
653 // {skip} argument of searchpair() can be compiled if not empty
654 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100655 compile_string(isn, cctx, 0);
656 }
657 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
658 && cctx->ctx_instr.ga_len == instr_count + 1)
659 {
660 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
661
662 // {sub} argument of substitute() can be compiled if it starts
663 // with \=
664 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
665 && isn->isn_arg.string[1] == '=')
666 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000667 }
668
669 if (*p != ',' && *skipwhite(p) == ',')
670 {
671 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
672 p = skipwhite(p);
673 }
674 if (*p == ',')
675 {
676 ++p;
677 if (*p != NUL && !VIM_ISWHITE(*p))
678 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
679 }
680 else
681 must_end = TRUE;
682 whitep = p;
683 p = skipwhite(p);
684 }
685failret:
686 emsg(_(e_missing_closing_paren));
687 return FAIL;
688}
689
690/*
691 * Compile a function call: name(arg1, arg2)
692 * "arg" points to "name", "arg + varlen" to the "(".
693 * "argcount_init" is 1 for "value->method()"
694 * Instructions:
695 * EVAL arg1
696 * EVAL arg2
697 * BCALL / DCALL / UCALL
698 */
699 static int
700compile_call(
701 char_u **arg,
702 size_t varlen,
703 cctx_T *cctx,
704 ppconst_T *ppconst,
705 int argcount_init)
706{
707 char_u *name = *arg;
708 char_u *p;
709 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100710 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000711 char_u fname_buf[FLEN_FIXED + 1];
712 char_u *tofree = NULL;
713 int error = FCERR_NONE;
714 ufunc_T *ufunc = NULL;
715 int res = FAIL;
716 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000717 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100718 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000719 imported_T *import;
720
721 if (varlen >= sizeof(namebuf))
722 {
723 semsg(_(e_name_too_long_str), name);
724 return FAIL;
725 }
726 vim_strncpy(namebuf, *arg, varlen);
727
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000728 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000729 if (import != NULL)
730 {
731 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
732 return FAIL;
733 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734
735 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100736 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100738 if ((varlen == 3
739 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000740 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
741 {
742 char_u *s = skipwhite(*arg + varlen + 1);
743 typval_T argvars[2];
744 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100745 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746
747 argvars[0].v_type = VAR_UNKNOWN;
748 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100749 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100751 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752 s = skipwhite(s);
753 if (*s == ')' && argvars[0].v_type == VAR_STRING
754 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
755 || !is_has))
756 {
757 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
758
759 *arg = s + 1;
760 argvars[1].v_type = VAR_UNKNOWN;
761 tv->v_type = VAR_NUMBER;
762 tv->vval.v_number = 0;
763 if (is_has)
764 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100765 else if (is_len)
766 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767 else
768 f_exists(argvars, tv);
769 clear_tv(&argvars[0]);
770 ++ppconst->pp_used;
771 return OK;
772 }
773 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100774 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000775 {
776 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
777 return FAIL;
778 }
779 }
780
781 if (generate_ppconst(cctx, ppconst) == FAIL)
782 return FAIL;
783
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
785
786 // We handle the "skip" argument of searchpair() and searchpairpos()
787 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100788 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
789 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
790 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
791 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
792 special_fn = CA_SEARCHPAIR;
793 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
794 special_fn = CA_SUBSTITUTE;
795 else
796 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797
798 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100799 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000800 goto theend;
801
802 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
803 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
804 {
805 int idx;
806
807 // builtin function
808 idx = find_internal_func(name);
809 if (idx >= 0)
810 {
811 if (STRCMP(name, "flatten") == 0)
812 {
813 emsg(_(e_cannot_use_flatten_in_vim9_script));
814 goto theend;
815 }
816
817 if (STRCMP(name, "add") == 0 && argcount == 2)
818 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000819 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820
821 // add() can be compiled to instructions if we know the type
822 if (type->tt_type == VAR_LIST)
823 {
824 // inline "add(list, item)" so that the type can be checked
825 res = generate_LISTAPPEND(cctx);
826 idx = -1;
827 }
828 else if (type->tt_type == VAR_BLOB)
829 {
830 // inline "add(blob, nr)" so that the type can be checked
831 res = generate_BLOBAPPEND(cctx);
832 idx = -1;
833 }
834 }
835
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100836 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
837 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100838 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100839 // May have the "D" or "R" flag, reserve a variable for a
840 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100841 if (get_defer_var_idx(cctx) == 0)
842 idx = -1;
843 }
844
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845 if (idx >= 0)
846 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
847 }
848 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100849 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 goto theend;
851 }
852
Bram Moolenaar62aec932022-01-29 21:45:34 +0000853 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
854
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 // An argument or local variable can be a function reference, this
856 // overrules a function name.
857 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
858 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
859 {
860 // If we can find the function by name generate the right call.
861 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000862 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000863 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000865 if (!func_is_global(ufunc))
866 {
867 res = generate_CALL(cctx, ufunc, argcount);
868 goto theend;
869 }
870 if (!has_g_namespace
871 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
872 {
873 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100874 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000875 goto theend;
876 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 }
878 }
879
880 // If the name is a variable, load it and use PCALL.
881 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000882 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000884 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
886 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000887 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888
889 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
890 goto theend;
891 }
892
893 // If we can find a global function by name generate the right call.
894 if (ufunc != NULL)
895 {
896 res = generate_CALL(cctx, ufunc, argcount);
897 goto theend;
898 }
899
900 // A global function may be defined only later. Need to figure out at
901 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000902 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 res = generate_UCALL(cctx, name, argcount);
904 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100905 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906
907theend:
908 vim_free(tofree);
909 return res;
910}
911
912// like NAMESPACE_CHAR but with 'a' and 'l'.
913#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
914
915/*
916 * Find the end of a variable or function name. Unlike find_name_end() this
917 * does not recognize magic braces.
918 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
919 * Return a pointer to just after the name. Equal to "arg" if there is no
920 * valid name.
921 */
922 char_u *
923to_name_end(char_u *arg, int use_namespace)
924{
925 char_u *p;
926
927 // Quick check for valid starting character.
928 if (!eval_isnamec1(*arg))
929 return arg;
930
931 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
932 // Include a namespace such as "s:var" and "v:var". But "n:" is not
933 // and can be used in slice "[n:]".
934 if (*p == ':' && (p != arg + 1
935 || !use_namespace
936 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
937 break;
938 return p;
939}
940
941/*
942 * Like to_name_end() but also skip over a list or dict constant.
943 * Also accept "<SNR>123_Func".
944 * This intentionally does not handle line continuation.
945 */
946 char_u *
947to_name_const_end(char_u *arg)
948{
949 char_u *p = arg;
950 typval_T rettv;
951
952 if (STRNCMP(p, "<SNR>", 5) == 0)
953 p = skipdigits(p + 5);
954 p = to_name_end(p, TRUE);
955 if (p == arg && *arg == '[')
956 {
957
958 // Can be "[1, 2, 3]->Func()".
959 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
960 p = arg;
961 }
962 return p;
963}
964
965/*
966 * parse a list: [expr, expr]
967 * "*arg" points to the '['.
968 * ppconst->pp_is_const is set if all items are a constant.
969 */
970 static int
971compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
972{
973 char_u *p = skipwhite(*arg + 1);
974 char_u *whitep = *arg + 1;
975 int count = 0;
976 int is_const;
977 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100978 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000979
980 for (;;)
981 {
982 if (may_get_next_line(whitep, &p, cctx) == FAIL)
983 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000984 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000985 return FAIL;
986 }
987 if (*p == ',')
988 {
989 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
990 return FAIL;
991 }
992 if (*p == ']')
993 {
994 ++p;
995 break;
996 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100997 if (must_end)
998 {
999 semsg(_(e_missing_comma_in_list_str), p);
1000 return FAIL;
1001 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001002 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1003 return FAIL;
1004 if (!is_const)
1005 is_all_const = FALSE;
1006 ++count;
1007 if (*p == ',')
1008 {
1009 ++p;
1010 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1011 {
1012 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1013 return FAIL;
1014 }
1015 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001016 else
1017 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001018 whitep = p;
1019 p = skipwhite(p);
1020 }
1021 *arg = p;
1022
1023 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001024 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001025}
1026
1027/*
1028 * Parse a lambda: "(arg, arg) => expr"
1029 * "*arg" points to the '('.
1030 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1031 */
1032 static int
1033compile_lambda(char_u **arg, cctx_T *cctx)
1034{
1035 int r;
1036 typval_T rettv;
1037 ufunc_T *ufunc;
1038 evalarg_T evalarg;
1039
1040 init_evalarg(&evalarg);
1041 evalarg.eval_flags = EVAL_EVALUATE;
1042 evalarg.eval_cctx = cctx;
1043
1044 // Get the funcref in "rettv".
1045 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1046 if (r != OK)
1047 {
1048 clear_evalarg(&evalarg, NULL);
1049 return r;
1050 }
1051
1052 // "rettv" will now be a partial referencing the function.
1053 ufunc = rettv.vval.v_partial->pt_func;
1054 ++ufunc->uf_refcount;
1055 clear_tv(&rettv);
1056
1057 // Compile it here to get the return type. The return type is optional,
1058 // when it's missing use t_unknown. This is recognized in
1059 // compile_return().
1060 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1061 ufunc->uf_ret_type = &t_unknown;
1062 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1063
1064 // When the outer function is compiled for profiling or debugging, the
1065 // lambda may be called without profiling or debugging. Compile it here in
1066 // the right context.
1067 if (cctx->ctx_compile_type == CT_DEBUG
1068#ifdef FEAT_PROFILE
1069 || cctx->ctx_compile_type == CT_PROFILE
1070#endif
1071 )
1072 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1073
Bram Moolenaar139575d2022-03-15 19:29:30 +00001074 // if the outer function is not compiled for debugging or profiling, this
1075 // one might be
1076 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001077 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001078 compiletype_T compile_type = get_compile_type(ufunc);
1079
1080 if (compile_type != CT_NONE)
1081 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001082 }
1083
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1085 // "*arg" may point into it. Point into the original line to avoid a
1086 // dangling pointer.
1087 if (evalarg.eval_using_cmdline)
1088 {
1089 garray_T *gap = &evalarg.eval_tofree_ga;
1090 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1091
1092 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1093 + off;
1094 }
1095
1096 clear_evalarg(&evalarg, NULL);
1097
1098 if (ufunc->uf_def_status == UF_COMPILED)
1099 {
1100 // The return type will now be known.
1101 set_function_type(ufunc);
1102
1103 // The function reference count will be 1. When the ISN_FUNCREF
1104 // instruction is deleted the reference count is decremented and the
1105 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001106 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107 }
1108
1109 func_ptr_unref(ufunc);
1110 return FAIL;
1111}
1112
1113/*
1114 * Get a lambda and compile it. Uses Vim9 syntax.
1115 */
1116 int
1117get_lambda_tv_and_compile(
1118 char_u **arg,
1119 typval_T *rettv,
1120 int types_optional,
1121 evalarg_T *evalarg)
1122{
1123 int r;
1124 ufunc_T *ufunc;
1125 int save_sc_version = current_sctx.sc_version;
1126
1127 // Get the funcref in "rettv".
1128 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1129 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1130 current_sctx.sc_version = save_sc_version;
1131 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001132 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001133
1134 // "rettv" will now be a partial referencing the function.
1135 ufunc = rettv->vval.v_partial->pt_func;
1136
1137 // Compile it here to get the return type. The return type is optional,
1138 // when it's missing use t_unknown. This is recognized in
1139 // compile_return().
1140 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1141 ufunc->uf_ret_type = &t_unknown;
1142 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1143
1144 if (ufunc->uf_def_status == UF_COMPILED)
1145 {
1146 // The return type will now be known.
1147 set_function_type(ufunc);
1148 return OK;
1149 }
1150 clear_tv(rettv);
1151 return FAIL;
1152}
1153
1154/*
1155 * parse a dict: {key: val, [key]: val}
1156 * "*arg" points to the '{'.
1157 * ppconst->pp_is_const is set if all item values are a constant.
1158 */
1159 static int
1160compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1161{
1162 garray_T *instr = &cctx->ctx_instr;
1163 int count = 0;
1164 dict_T *d = dict_alloc();
1165 dictitem_T *item;
1166 char_u *whitep = *arg + 1;
1167 char_u *p;
1168 int is_const;
1169 int is_all_const = TRUE; // reset when non-const encountered
1170
1171 if (d == NULL)
1172 return FAIL;
1173 if (generate_ppconst(cctx, ppconst) == FAIL)
1174 return FAIL;
1175 for (;;)
1176 {
1177 char_u *key = NULL;
1178
1179 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1180 {
1181 *arg = NULL;
1182 goto failret;
1183 }
1184
1185 if (**arg == '}')
1186 break;
1187
1188 if (**arg == '[')
1189 {
1190 isn_T *isn;
1191
1192 // {[expr]: value} uses an evaluated key.
1193 *arg = skipwhite(*arg + 1);
1194 if (compile_expr0(arg, cctx) == FAIL)
1195 return FAIL;
1196 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1197 if (isn->isn_type == ISN_PUSHNR)
1198 {
1199 char buf[NUMBUFLEN];
1200
1201 // Convert to string at compile time.
1202 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1203 isn->isn_type = ISN_PUSHS;
1204 isn->isn_arg.string = vim_strsave((char_u *)buf);
1205 }
1206 if (isn->isn_type == ISN_PUSHS)
1207 key = isn->isn_arg.string;
1208 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1209 return FAIL;
1210 *arg = skipwhite(*arg);
1211 if (**arg != ']')
1212 {
1213 emsg(_(e_missing_matching_bracket_after_dict_key));
1214 return FAIL;
1215 }
1216 ++*arg;
1217 }
1218 else
1219 {
1220 // {"name": value},
1221 // {'name': value},
1222 // {name: value} use "name" as a literal key
1223 key = get_literal_key(arg);
1224 if (key == NULL)
1225 return FAIL;
1226 if (generate_PUSHS(cctx, &key) == FAIL)
1227 return FAIL;
1228 }
1229
1230 // Check for duplicate keys, if using string keys.
1231 if (key != NULL)
1232 {
1233 item = dict_find(d, key, -1);
1234 if (item != NULL)
1235 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001236 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001237 goto failret;
1238 }
1239 item = dictitem_alloc(key);
1240 if (item != NULL)
1241 {
1242 item->di_tv.v_type = VAR_UNKNOWN;
1243 item->di_tv.v_lock = 0;
1244 if (dict_add(d, item) == FAIL)
1245 dictitem_free(item);
1246 }
1247 }
1248
1249 if (**arg != ':')
1250 {
1251 if (*skipwhite(*arg) == ':')
1252 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1253 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001254 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255 return FAIL;
1256 }
1257 whitep = *arg + 1;
1258 if (!IS_WHITE_OR_NUL(*whitep))
1259 {
1260 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1261 return FAIL;
1262 }
1263
1264 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1265 {
1266 *arg = NULL;
1267 goto failret;
1268 }
1269
1270 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1271 return FAIL;
1272 if (!is_const)
1273 is_all_const = FALSE;
1274 ++count;
1275
1276 whitep = *arg;
1277 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1278 {
1279 *arg = NULL;
1280 goto failret;
1281 }
1282 if (**arg == '}')
1283 break;
1284 if (**arg != ',')
1285 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001286 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287 goto failret;
1288 }
1289 if (IS_WHITE_OR_NUL(*whitep))
1290 {
1291 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1292 return FAIL;
1293 }
1294 whitep = *arg + 1;
1295 if (!IS_WHITE_OR_NUL(*whitep))
1296 {
1297 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1298 return FAIL;
1299 }
1300 *arg = skipwhite(whitep);
1301 }
1302
1303 *arg = *arg + 1;
1304
1305 // Allow for following comment, after at least one space.
1306 p = skipwhite(*arg);
1307 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1308 *arg += STRLEN(*arg);
1309
1310 dict_unref(d);
1311 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001312 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001313
1314failret:
1315 if (*arg == NULL)
1316 {
1317 semsg(_(e_missing_dict_end), _("[end of lines]"));
1318 *arg = (char_u *)"";
1319 }
1320 dict_unref(d);
1321 return FAIL;
1322}
1323
1324/*
1325 * Compile "&option".
1326 */
1327 static int
1328compile_get_option(char_u **arg, cctx_T *cctx)
1329{
1330 typval_T rettv;
1331 char_u *start = *arg;
1332 int ret;
1333
1334 // parse the option and get the current value to get the type.
1335 rettv.v_type = VAR_UNKNOWN;
1336 ret = eval_option(arg, &rettv, TRUE);
1337 if (ret == OK)
1338 {
1339 // include the '&' in the name, eval_option() expects it.
1340 char_u *name = vim_strnsave(start, *arg - start);
1341 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1342 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1343
1344 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1345 vim_free(name);
1346 }
1347 clear_tv(&rettv);
1348
1349 return ret;
1350}
1351
1352/*
1353 * Compile "$VAR".
1354 */
1355 static int
1356compile_get_env(char_u **arg, cctx_T *cctx)
1357{
1358 char_u *start = *arg;
1359 int len;
1360 int ret;
1361 char_u *name;
1362
1363 ++*arg;
1364 len = get_env_len(arg);
1365 if (len == 0)
1366 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001367 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001368 return FAIL;
1369 }
1370
1371 // include the '$' in the name, eval_env_var() expects it.
1372 name = vim_strnsave(start, len + 1);
1373 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1374 vim_free(name);
1375 return ret;
1376}
1377
1378/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001379 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001380 */
1381 static int
1382compile_interp_string(char_u **arg, cctx_T *cctx)
1383{
1384 typval_T tv;
1385 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001386 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001387 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001388 int count = 0;
1389 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001390
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001391 // *arg is on the '$' character, move it to the first string character.
1392 ++*arg;
1393 quote = **arg;
1394 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001395
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001396 for (;;)
1397 {
1398 // Get the string up to the matching quote or to a single '{'.
1399 // "arg" is advanced to either the quote or the '{'.
1400 if (quote == '"')
1401 ret = eval_string(arg, &tv, evaluate, TRUE);
1402 else
1403 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1404 if (ret == FAIL)
1405 break;
1406 if (evaluate)
1407 {
1408 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1409 || (**arg != '{' && count == 0))
1410 {
1411 // generate non-empty string or empty string if it's the only
1412 // one
1413 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1414 return FAIL;
1415 tv.vval.v_string = NULL; // don't free it now
1416 ++count;
1417 }
1418 clear_tv(&tv);
1419 }
1420
1421 if (**arg != '{')
1422 {
1423 // found terminating quote
1424 ++*arg;
1425 break;
1426 }
1427
1428 p = compile_one_expr_in_str(*arg, cctx);
1429 if (p == NULL)
1430 {
1431 ret = FAIL;
1432 break;
1433 }
1434 ++count;
1435 *arg = p;
1436 }
LemonBoy2eaef102022-05-06 13:14:50 +01001437
1438 if (ret == FAIL || !evaluate)
1439 return ret;
1440
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001441 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1442 if (count > 1)
1443 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001444
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001445 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001446}
1447
1448/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001449 * Compile "@r".
1450 */
1451 static int
1452compile_get_register(char_u **arg, cctx_T *cctx)
1453{
1454 int ret;
1455
1456 ++*arg;
1457 if (**arg == NUL)
1458 {
1459 semsg(_(e_syntax_error_at_str), *arg - 1);
1460 return FAIL;
1461 }
1462 if (!valid_yank_reg(**arg, FALSE))
1463 {
1464 emsg_invreg(**arg);
1465 return FAIL;
1466 }
1467 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1468 ++*arg;
1469 return ret;
1470}
1471
1472/*
1473 * Apply leading '!', '-' and '+' to constant "rettv".
1474 * When "numeric_only" is TRUE do not apply '!'.
1475 */
1476 static int
1477apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1478{
1479 char_u *p = *end;
1480
1481 // this works from end to start
1482 while (p > start)
1483 {
1484 --p;
1485 if (*p == '-' || *p == '+')
1486 {
1487 // only '-' has an effect, for '+' we only check the type
1488#ifdef FEAT_FLOAT
1489 if (rettv->v_type == VAR_FLOAT)
1490 {
1491 if (*p == '-')
1492 rettv->vval.v_float = -rettv->vval.v_float;
1493 }
1494 else
1495#endif
1496 {
1497 varnumber_T val;
1498 int error = FALSE;
1499
1500 // tv_get_number_chk() accepts a string, but we don't want that
1501 // here
1502 if (check_not_string(rettv) == FAIL)
1503 return FAIL;
1504 val = tv_get_number_chk(rettv, &error);
1505 clear_tv(rettv);
1506 if (error)
1507 return FAIL;
1508 if (*p == '-')
1509 val = -val;
1510 rettv->v_type = VAR_NUMBER;
1511 rettv->vval.v_number = val;
1512 }
1513 }
1514 else if (numeric_only)
1515 {
1516 ++p;
1517 break;
1518 }
1519 else if (*p == '!')
1520 {
1521 int v = tv2bool(rettv);
1522
1523 // '!' is permissive in the type.
1524 clear_tv(rettv);
1525 rettv->v_type = VAR_BOOL;
1526 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1527 }
1528 }
1529 *end = p;
1530 return OK;
1531}
1532
1533/*
1534 * Recognize v: variables that are constants and set "rettv".
1535 */
1536 static void
1537get_vim_constant(char_u **arg, typval_T *rettv)
1538{
1539 if (STRNCMP(*arg, "v:true", 6) == 0)
1540 {
1541 rettv->v_type = VAR_BOOL;
1542 rettv->vval.v_number = VVAL_TRUE;
1543 *arg += 6;
1544 }
1545 else if (STRNCMP(*arg, "v:false", 7) == 0)
1546 {
1547 rettv->v_type = VAR_BOOL;
1548 rettv->vval.v_number = VVAL_FALSE;
1549 *arg += 7;
1550 }
1551 else if (STRNCMP(*arg, "v:null", 6) == 0)
1552 {
1553 rettv->v_type = VAR_SPECIAL;
1554 rettv->vval.v_number = VVAL_NULL;
1555 *arg += 6;
1556 }
1557 else if (STRNCMP(*arg, "v:none", 6) == 0)
1558 {
1559 rettv->v_type = VAR_SPECIAL;
1560 rettv->vval.v_number = VVAL_NONE;
1561 *arg += 6;
1562 }
1563}
1564
1565 exprtype_T
1566get_compare_type(char_u *p, int *len, int *type_is)
1567{
1568 exprtype_T type = EXPR_UNKNOWN;
1569 int i;
1570
1571 switch (p[0])
1572 {
1573 case '=': if (p[1] == '=')
1574 type = EXPR_EQUAL;
1575 else if (p[1] == '~')
1576 type = EXPR_MATCH;
1577 break;
1578 case '!': if (p[1] == '=')
1579 type = EXPR_NEQUAL;
1580 else if (p[1] == '~')
1581 type = EXPR_NOMATCH;
1582 break;
1583 case '>': if (p[1] != '=')
1584 {
1585 type = EXPR_GREATER;
1586 *len = 1;
1587 }
1588 else
1589 type = EXPR_GEQUAL;
1590 break;
1591 case '<': if (p[1] != '=')
1592 {
1593 type = EXPR_SMALLER;
1594 *len = 1;
1595 }
1596 else
1597 type = EXPR_SEQUAL;
1598 break;
1599 case 'i': if (p[1] == 's')
1600 {
1601 // "is" and "isnot"; but not a prefix of a name
1602 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1603 *len = 5;
1604 i = p[*len];
1605 if (!isalnum(i) && i != '_')
1606 {
1607 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1608 *type_is = TRUE;
1609 }
1610 }
1611 break;
1612 }
1613 return type;
1614}
1615
1616/*
1617 * Skip over an expression, ignoring most errors.
1618 */
1619 void
1620skip_expr_cctx(char_u **arg, cctx_T *cctx)
1621{
1622 evalarg_T evalarg;
1623
1624 init_evalarg(&evalarg);
1625 evalarg.eval_cctx = cctx;
1626 skip_expr(arg, &evalarg);
1627 clear_evalarg(&evalarg, NULL);
1628}
1629
1630/*
1631 * Check that the top of the type stack has a type that can be used as a
1632 * condition. Give an error and return FAIL if not.
1633 */
1634 int
1635bool_on_stack(cctx_T *cctx)
1636{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001637 type_T *type;
1638
Bram Moolenaar078a4612022-01-04 15:17:03 +00001639 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001640 if (type == &t_bool)
1641 return OK;
1642
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001643 if (type == &t_any
1644 || type == &t_unknown
1645 || type == &t_number
1646 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001647 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1648 // This requires a runtime type check.
1649 return generate_COND2BOOL(cctx);
1650
1651 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1652}
1653
1654/*
1655 * Give the "white on both sides" error, taking the operator from "p[len]".
1656 */
1657 void
1658error_white_both(char_u *op, int len)
1659{
1660 char_u buf[10];
1661
1662 vim_strncpy(buf, op, len);
1663 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1664}
1665
1666/*
1667 * Compile code to apply '-', '+' and '!'.
1668 * When "numeric_only" is TRUE do not apply '!'.
1669 */
1670 static int
1671compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1672{
1673 char_u *p = *end;
1674
1675 // this works from end to start
1676 while (p > start)
1677 {
1678 --p;
1679 while (VIM_ISWHITE(*p))
1680 --p;
1681 if (*p == '-' || *p == '+')
1682 {
1683 int negate = *p == '-';
1684 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001685 type_T *type;
1686
Bram Moolenaar078a4612022-01-04 15:17:03 +00001687 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 if (type != &t_float && need_type(type, &t_number,
1689 -1, 0, cctx, FALSE, FALSE) == FAIL)
1690 return FAIL;
1691
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001692 // only '-' has an effect, for '+' we only check the type
1693 if (negate)
1694 {
1695 isn = generate_instr(cctx, ISN_NEGATENR);
1696 if (isn == NULL)
1697 return FAIL;
1698 }
1699 }
1700 else if (numeric_only)
1701 {
1702 ++p;
1703 break;
1704 }
1705 else
1706 {
1707 int invert = *p == '!';
1708
1709 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1710 {
1711 if (p[-1] == '!')
1712 invert = !invert;
1713 --p;
1714 }
1715 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1716 return FAIL;
1717 }
1718 }
1719 *end = p;
1720 return OK;
1721}
1722
1723/*
1724 * Compile "(expression)": recursive!
1725 * Return FAIL/OK.
1726 */
1727 static int
1728compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1729{
1730 int ret;
1731 char_u *p = *arg + 1;
1732
1733 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1734 return FAIL;
1735 if (ppconst->pp_used <= PPSIZE - 10)
1736 {
1737 ret = compile_expr1(arg, cctx, ppconst);
1738 }
1739 else
1740 {
1741 // Not enough space in ppconst, flush constants.
1742 if (generate_ppconst(cctx, ppconst) == FAIL)
1743 return FAIL;
1744 ret = compile_expr0(arg, cctx);
1745 }
1746 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1747 return FAIL;
1748 if (**arg == ')')
1749 ++*arg;
1750 else if (ret == OK)
1751 {
1752 emsg(_(e_missing_closing_paren));
1753 ret = FAIL;
1754 }
1755 return ret;
1756}
1757
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001758static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001759
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001760/*
1761 * Compile whatever comes after "name" or "name()".
1762 * Advances "*arg" only when something was recognized.
1763 */
1764 static int
1765compile_subscript(
1766 char_u **arg,
1767 cctx_T *cctx,
1768 char_u *start_leader,
1769 char_u **end_leader,
1770 ppconst_T *ppconst)
1771{
1772 char_u *name_start = *end_leader;
1773 int keeping_dict = FALSE;
1774
1775 for (;;)
1776 {
1777 char_u *p = skipwhite(*arg);
1778
1779 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1780 {
1781 char_u *next = peek_next_line_from_context(cctx);
1782
1783 // If a following line starts with "->{" or "->X" advance to that
1784 // line, so that a line break before "->" is allowed.
1785 // Also if a following line starts with ".x".
1786 if (next != NULL &&
1787 ((next[0] == '-' && next[1] == '>'
1788 && (next[2] == '{'
1789 || ASCII_ISALPHA(*skipwhite(next + 2))))
1790 || (next[0] == '.' && eval_isdictc(next[1]))))
1791 {
1792 next = next_line_from_context(cctx, TRUE);
1793 if (next == NULL)
1794 return FAIL;
1795 *arg = next;
1796 p = skipwhite(*arg);
1797 }
1798 }
1799
1800 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1801 // is not a function call.
1802 if (**arg == '(')
1803 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804 type_T *type;
1805 int argcount = 0;
1806
1807 if (generate_ppconst(cctx, ppconst) == FAIL)
1808 return FAIL;
1809 ppconst->pp_is_const = FALSE;
1810
1811 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001812 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813
1814 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001815 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001816 return FAIL;
1817 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1818 return FAIL;
1819 if (keeping_dict)
1820 {
1821 keeping_dict = FALSE;
1822 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1823 return FAIL;
1824 }
1825 }
1826 else if (*p == '-' && p[1] == '>')
1827 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001828 char_u *pstart = p;
1829 int alt;
1830 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831
Bram Moolenaarc7349932022-01-16 20:59:39 +00001832 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001833 if (generate_ppconst(cctx, ppconst) == FAIL)
1834 return FAIL;
1835 ppconst->pp_is_const = FALSE;
1836
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837 // Apply the '!', '-' and '+' first:
1838 // -1.0->func() works like (-1.0)->func()
1839 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1840 return FAIL;
1841
1842 p += 2;
1843 *arg = skipwhite(p);
1844 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001845
1846 // Three alternatives handled here:
1847 // 1. "base->name(" only a name, use compile_call()
1848 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1849 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001851 alt = 2;
1852 else
1853 {
1854 // alternative 1 or 3
1855 p = *arg;
1856 if (!eval_isnamec1(*p))
1857 {
1858 semsg(_(e_trailing_characters_str), pstart);
1859 return FAIL;
1860 }
1861 if (ASCII_ISALPHA(*p) && p[1] == ':')
1862 p += 2;
1863 for ( ; eval_isnamec(*p); ++p)
1864 ;
1865 if (*p == '(')
1866 {
1867 // alternative 1
1868 alt = 1;
1869 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1870 return FAIL;
1871 }
1872 else
1873 {
1874 // Must be alternative 3, find the "(". Only works within
1875 // one line.
1876 alt = 3;
1877 paren = vim_strchr(p, '(');
1878 if (paren == NULL)
1879 {
1880 semsg(_(e_missing_parenthesis_str), *arg);
1881 return FAIL;
1882 }
1883 }
1884 }
1885
1886 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001887 {
1888 int argcount = 1;
1889 garray_T *stack = &cctx->ctx_type_stack;
1890 int type_idx_start = stack->ga_len;
1891 type_T *type;
1892 int expr_isn_start = cctx->ctx_instr.ga_len;
1893 int expr_isn_end;
1894 int arg_isn_count;
1895
Bram Moolenaarc7349932022-01-16 20:59:39 +00001896 if (alt == 2)
1897 {
1898 // Funcref call: list->(Refs[2])(arg)
1899 // or lambda: list->((arg) => expr)(arg)
1900 //
1901 // Fist compile the function expression.
1902 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1903 return FAIL;
1904 }
1905 else
1906 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001907 int fail;
1908 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001909 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001910
Bram Moolenaarc7349932022-01-16 20:59:39 +00001911 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001912
1913 // instead of using LOADG for "import.Func" use PUSHFUNC
1914 ++paren_follows_after_expr;
1915
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001916 // do not look in the next line
1917 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001918
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001919 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001920 || *skipwhite(*arg) != NUL;
1921 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001922 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001923 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001924
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001925 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001926 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001927 if (did_emsg == prev_did_emsg)
1928 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001929 return FAIL;
1930 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001931 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001932
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001933 // Compile the arguments.
1934 if (**arg != '(')
1935 {
1936 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001937 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 else
1939 semsg(_(e_missing_parenthesis_str), *arg);
1940 return FAIL;
1941 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001942
1943 // Remember the next instruction index, where the instructions
1944 // for arguments are being written.
1945 expr_isn_end = cctx->ctx_instr.ga_len;
1946
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001947 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001948 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1949 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001950 return FAIL;
1951
1952 // Move the instructions for the arguments to before the
1953 // instructions of the expression and move the type of the
1954 // expression after the argument types. This is what ISN_PCALL
1955 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1957 if (arg_isn_count > 0)
1958 {
1959 int expr_isn_count = expr_isn_end - expr_isn_start;
1960 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001961 type_T *decl_type;
1962 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001963
1964 if (isn == NULL)
1965 return FAIL;
1966 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1967 + expr_isn_start,
1968 sizeof(isn_T) * expr_isn_count);
1969 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1970 + expr_isn_start,
1971 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1972 sizeof(isn_T) * arg_isn_count);
1973 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1974 + expr_isn_start + arg_isn_count,
1975 isn, sizeof(isn_T) * expr_isn_count);
1976 vim_free(isn);
1977
Bram Moolenaar078a4612022-01-04 15:17:03 +00001978 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1979 type = typep->type_curr;
1980 decl_type = typep->type_decl;
1981 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1982 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1983 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001985 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1986 typep->type_curr = type;
1987 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 }
1989
Bram Moolenaar078a4612022-01-04 15:17:03 +00001990 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001991 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1992 return FAIL;
1993 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001994
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 if (keeping_dict)
1996 {
1997 keeping_dict = FALSE;
1998 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1999 return FAIL;
2000 }
2001 }
2002 else if (**arg == '[')
2003 {
2004 int is_slice = FALSE;
2005
2006 // list index: list[123]
2007 // dict member: dict[key]
2008 // string index: text[123]
2009 // blob index: blob[123]
2010 if (generate_ppconst(cctx, ppconst) == FAIL)
2011 return FAIL;
2012 ppconst->pp_is_const = FALSE;
2013
2014 ++p;
2015 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2016 return FAIL;
2017 if (**arg == ':')
2018 {
2019 // missing first index is equal to zero
2020 generate_PUSHNR(cctx, 0);
2021 }
2022 else
2023 {
2024 if (compile_expr0(arg, cctx) == FAIL)
2025 return FAIL;
2026 if (**arg == ':')
2027 {
2028 semsg(_(e_white_space_required_before_and_after_str_at_str),
2029 ":", *arg);
2030 return FAIL;
2031 }
2032 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2033 return FAIL;
2034 *arg = skipwhite(*arg);
2035 }
2036 if (**arg == ':')
2037 {
2038 is_slice = TRUE;
2039 ++*arg;
2040 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2041 {
2042 semsg(_(e_white_space_required_before_and_after_str_at_str),
2043 ":", *arg);
2044 return FAIL;
2045 }
2046 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2047 return FAIL;
2048 if (**arg == ']')
2049 // missing second index is equal to end of string
2050 generate_PUSHNR(cctx, -1);
2051 else
2052 {
2053 if (compile_expr0(arg, cctx) == FAIL)
2054 return FAIL;
2055 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2056 return FAIL;
2057 *arg = skipwhite(*arg);
2058 }
2059 }
2060
2061 if (**arg != ']')
2062 {
2063 emsg(_(e_missing_closing_square_brace));
2064 return FAIL;
2065 }
2066 *arg = *arg + 1;
2067
2068 if (keeping_dict)
2069 {
2070 keeping_dict = FALSE;
2071 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2072 return FAIL;
2073 }
2074 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2075 return FAIL;
2076 }
2077 else if (*p == '.' && p[1] != '.')
2078 {
2079 // dictionary member: dict.name
2080 if (generate_ppconst(cctx, ppconst) == FAIL)
2081 return FAIL;
2082 ppconst->pp_is_const = FALSE;
2083
2084 *arg = p + 1;
2085 if (IS_WHITE_OR_NUL(**arg))
2086 {
2087 emsg(_(e_missing_name_after_dot));
2088 return FAIL;
2089 }
2090 p = *arg;
2091 if (eval_isdictc(*p))
2092 while (eval_isnamec(*p))
2093 MB_PTR_ADV(p);
2094 if (p == *arg)
2095 {
2096 semsg(_(e_syntax_error_at_str), *arg);
2097 return FAIL;
2098 }
2099 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2100 return FAIL;
2101 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2102 return FAIL;
2103 keeping_dict = TRUE;
2104 *arg = p;
2105 }
2106 else
2107 break;
2108 }
2109
2110 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2111 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002112 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2113 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002114 return FAIL;
2115
2116 return OK;
2117}
2118
2119/*
2120 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2121 * "arg" is advanced until after the expression, skipping white space.
2122 *
2123 * If the value is a constant "ppconst->pp_used" will be non-zero.
2124 * Before instructions are generated, any values in "ppconst" will generated.
2125 *
2126 * This is the compiling equivalent of eval1(), eval2(), etc.
2127 */
2128
2129/*
2130 * number number constant
2131 * 0zFFFFFFFF Blob constant
2132 * "string" string constant
2133 * 'string' literal string constant
2134 * &option-name option value
2135 * @r register contents
2136 * identifier variable value
2137 * function() function call
2138 * $VAR environment variable
2139 * (expression) nested expression
2140 * [expr, expr] List
2141 * {key: val, [key]: val} Dictionary
2142 *
2143 * Also handle:
2144 * ! in front logical NOT
2145 * - in front unary minus
2146 * + in front unary plus (ignored)
2147 * trailing (arg) funcref/partial call
2148 * trailing [] subscript in String or List
2149 * trailing .name entry in Dictionary
2150 * trailing ->name() method call
2151 */
2152 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002153compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002154 char_u **arg,
2155 cctx_T *cctx,
2156 ppconst_T *ppconst)
2157{
2158 char_u *start_leader, *end_leader;
2159 int ret = OK;
2160 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2161 int used_before = ppconst->pp_used;
2162
2163 ppconst->pp_is_const = FALSE;
2164
2165 /*
2166 * Skip '!', '-' and '+' characters. They are handled later.
2167 */
2168 start_leader = *arg;
2169 if (eval_leader(arg, TRUE) == FAIL)
2170 return FAIL;
2171 end_leader = *arg;
2172
2173 rettv->v_type = VAR_UNKNOWN;
2174 switch (**arg)
2175 {
2176 /*
2177 * Number constant.
2178 */
2179 case '0': // also for blob starting with 0z
2180 case '1':
2181 case '2':
2182 case '3':
2183 case '4':
2184 case '5':
2185 case '6':
2186 case '7':
2187 case '8':
2188 case '9':
2189 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2190 return FAIL;
2191 // Apply "-" and "+" just before the number now, right to
2192 // left. Matters especially when "->" follows. Stops at
2193 // '!'.
2194 if (apply_leader(rettv, TRUE,
2195 start_leader, &end_leader) == FAIL)
2196 {
2197 clear_tv(rettv);
2198 return FAIL;
2199 }
2200 break;
2201
2202 /*
2203 * String constant: "string".
2204 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002205 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 return FAIL;
2207 break;
2208
2209 /*
2210 * Literal string constant: 'str''ing'.
2211 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002212 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002213 return FAIL;
2214 break;
2215
2216 /*
2217 * Constant Vim variable.
2218 */
2219 case 'v': get_vim_constant(arg, rettv);
2220 ret = NOTDONE;
2221 break;
2222
2223 /*
2224 * "true" constant
2225 */
2226 case 't': if (STRNCMP(*arg, "true", 4) == 0
2227 && !eval_isnamec((*arg)[4]))
2228 {
2229 *arg += 4;
2230 rettv->v_type = VAR_BOOL;
2231 rettv->vval.v_number = VVAL_TRUE;
2232 }
2233 else
2234 ret = NOTDONE;
2235 break;
2236
2237 /*
2238 * "false" constant
2239 */
2240 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2241 && !eval_isnamec((*arg)[5]))
2242 {
2243 *arg += 5;
2244 rettv->v_type = VAR_BOOL;
2245 rettv->vval.v_number = VVAL_FALSE;
2246 }
2247 else
2248 ret = NOTDONE;
2249 break;
2250
2251 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002252 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002253 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002254 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002255 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002256 char_u *p = *arg + 4;
2257 int len;
2258
2259 for (len = 0; eval_isnamec(p[len]); ++len)
2260 ;
2261 ret = handle_predefined(*arg, len + 4, rettv);
2262 if (ret == FAIL)
2263 ret = NOTDONE;
2264 else
2265 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002266 }
2267 else
2268 ret = NOTDONE;
2269 break;
2270
2271 /*
2272 * List: [expr, expr]
2273 */
2274 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2275 return FAIL;
2276 ret = compile_list(arg, cctx, ppconst);
2277 break;
2278
2279 /*
2280 * Dictionary: {'key': val, 'key': val}
2281 */
2282 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2283 return FAIL;
2284 ret = compile_dict(arg, cctx, ppconst);
2285 break;
2286
2287 /*
2288 * Option value: &name
2289 */
2290 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2291 return FAIL;
2292 ret = compile_get_option(arg, cctx);
2293 break;
2294
2295 /*
2296 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002297 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002298 */
2299 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2300 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002301 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2302 ret = compile_interp_string(arg, cctx);
2303 else
2304 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002305 break;
2306
2307 /*
2308 * Register contents: @r.
2309 */
2310 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2311 return FAIL;
2312 ret = compile_get_register(arg, cctx);
2313 break;
2314 /*
2315 * nested expression: (expression).
2316 * lambda: (arg, arg) => expr
2317 * funcref: (arg, arg) => { statement }
2318 */
2319 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2320 ret = compile_lambda(arg, cctx);
2321 if (ret == NOTDONE)
2322 ret = compile_parenthesis(arg, cctx, ppconst);
2323 break;
2324
2325 default: ret = NOTDONE;
2326 break;
2327 }
2328 if (ret == FAIL)
2329 return FAIL;
2330
2331 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2332 {
2333 if (cctx->ctx_skip == SKIP_YES)
2334 clear_tv(rettv);
2335 else
2336 // A constant expression can possibly be handled compile time,
2337 // return the value instead of generating code.
2338 ++ppconst->pp_used;
2339 }
2340 else if (ret == NOTDONE)
2341 {
2342 char_u *p;
2343 int r;
2344
2345 if (!eval_isnamec1(**arg))
2346 {
2347 if (!vim9_bad_comment(*arg))
2348 {
2349 if (ends_excmd(*skipwhite(*arg)))
2350 semsg(_(e_empty_expression_str), *arg);
2351 else
2352 semsg(_(e_name_expected_str), *arg);
2353 }
2354 return FAIL;
2355 }
2356
2357 // "name" or "name()"
2358 p = to_name_end(*arg, TRUE);
2359 if (p - *arg == (size_t)1 && **arg == '_')
2360 {
2361 emsg(_(e_cannot_use_underscore_here));
2362 return FAIL;
2363 }
2364
2365 if (*p == '(')
2366 {
2367 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2368 }
2369 else
2370 {
2371 if (cctx->ctx_skip != SKIP_YES
2372 && generate_ppconst(cctx, ppconst) == FAIL)
2373 return FAIL;
2374 r = compile_load(arg, p, cctx, TRUE, TRUE);
2375 }
2376 if (r == FAIL)
2377 return FAIL;
2378 }
2379
2380 // Handle following "[]", ".member", etc.
2381 // Then deal with prefixed '-', '+' and '!', if not done already.
2382 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2383 ppconst) == FAIL)
2384 return FAIL;
2385 if (ppconst->pp_used > 0)
2386 {
2387 // apply the '!', '-' and '+' before the constant
2388 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2389 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2390 return FAIL;
2391 return OK;
2392 }
2393 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2394 return FAIL;
2395 return OK;
2396}
2397
2398/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002399 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002400 */
2401 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002402compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002403{
2404 type_T *want_type = NULL;
2405
2406 // Recognize <type>
2407 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2408 {
2409 ++*arg;
2410 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2411 if (want_type == NULL)
2412 return FAIL;
2413
2414 if (**arg != '>')
2415 {
2416 if (*skipwhite(*arg) == '>')
2417 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2418 else
2419 emsg(_(e_missing_gt));
2420 return FAIL;
2421 }
2422 ++*arg;
2423 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2424 return FAIL;
2425 }
2426
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002427 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002428 return FAIL;
2429
2430 if (want_type != NULL)
2431 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 type_T *actual;
2433 where_T where = WHERE_INIT;
2434
2435 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002436 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002437 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002438 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002439 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2440 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 return FAIL;
2442 }
2443 }
2444
2445 return OK;
2446}
2447
2448/*
2449 * * number multiplication
2450 * / number division
2451 * % number modulo
2452 */
2453 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002454compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002455{
2456 char_u *op;
2457 char_u *next;
2458 int ppconst_used = ppconst->pp_used;
2459
2460 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002461 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 return FAIL;
2463
2464 /*
2465 * Repeat computing, until no "*", "/" or "%" is following.
2466 */
2467 for (;;)
2468 {
2469 op = may_peek_next_line(cctx, *arg, &next);
2470 if (*op != '*' && *op != '/' && *op != '%')
2471 break;
2472 if (next != NULL)
2473 {
2474 *arg = next_line_from_context(cctx, TRUE);
2475 op = skipwhite(*arg);
2476 }
2477
2478 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2479 {
2480 error_white_both(op, 1);
2481 return FAIL;
2482 }
2483 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2484 return FAIL;
2485
2486 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002487 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488 return FAIL;
2489
2490 if (ppconst->pp_used == ppconst_used + 2
2491 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2492 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2493 {
2494 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2495 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2496 varnumber_T res = 0;
2497 int failed = FALSE;
2498
2499 // both are numbers: compute the result
2500 switch (*op)
2501 {
2502 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2503 break;
2504 case '/': res = num_divide(tv1->vval.v_number,
2505 tv2->vval.v_number, &failed);
2506 break;
2507 case '%': res = num_modulus(tv1->vval.v_number,
2508 tv2->vval.v_number, &failed);
2509 break;
2510 }
2511 if (failed)
2512 return FAIL;
2513 tv1->vval.v_number = res;
2514 --ppconst->pp_used;
2515 }
2516 else
2517 {
2518 generate_ppconst(cctx, ppconst);
2519 generate_two_op(cctx, op);
2520 }
2521 }
2522
2523 return OK;
2524}
2525
2526/*
2527 * + number addition or list/blobl concatenation
2528 * - number subtraction
2529 * .. string concatenation
2530 */
2531 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002532compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002533{
2534 char_u *op;
2535 char_u *next;
2536 int oplen;
2537 int ppconst_used = ppconst->pp_used;
2538
2539 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002540 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002541 return FAIL;
2542
2543 /*
2544 * Repeat computing, until no "+", "-" or ".." is following.
2545 */
2546 for (;;)
2547 {
2548 op = may_peek_next_line(cctx, *arg, &next);
2549 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2550 break;
2551 if (op[0] == op[1] && *op != '.' && next)
2552 // Finding "++" or "--" on the next line is a separate command.
2553 // But ".." is concatenation.
2554 break;
2555 oplen = (*op == '.' ? 2 : 1);
2556 if (next != NULL)
2557 {
2558 *arg = next_line_from_context(cctx, TRUE);
2559 op = skipwhite(*arg);
2560 }
2561
2562 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2563 {
2564 error_white_both(op, oplen);
2565 return FAIL;
2566 }
2567
2568 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2569 return FAIL;
2570
2571 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002572 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002573 return FAIL;
2574
2575 if (ppconst->pp_used == ppconst_used + 2
2576 && (*op == '.'
2577 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2578 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2579 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2580 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2581 {
2582 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2583 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2584
2585 // concat/subtract/add constant numbers
2586 if (*op == '+')
2587 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2588 else if (*op == '-')
2589 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2590 else
2591 {
2592 // concatenate constant strings
2593 char_u *s1 = tv1->vval.v_string;
2594 char_u *s2 = tv2->vval.v_string;
2595 size_t len1 = STRLEN(s1);
2596
2597 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2598 if (tv1->vval.v_string == NULL)
2599 {
2600 clear_ppconst(ppconst);
2601 return FAIL;
2602 }
2603 mch_memmove(tv1->vval.v_string, s1, len1);
2604 STRCPY(tv1->vval.v_string + len1, s2);
2605 vim_free(s1);
2606 vim_free(s2);
2607 }
2608 --ppconst->pp_used;
2609 }
2610 else
2611 {
2612 generate_ppconst(cctx, ppconst);
2613 ppconst->pp_is_const = FALSE;
2614 if (*op == '.')
2615 {
2616 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2617 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2618 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002619 if (generate_CONCAT(cctx, 2) == FAIL)
2620 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002621 }
2622 else
2623 generate_two_op(cctx, op);
2624 }
2625 }
2626
2627 return OK;
2628}
2629
2630/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002631 * expr6a >> expr6b
2632 * expr6a << expr6b
2633 *
2634 * Produces instructions:
2635 * OPNR bitwise left or right shift
2636 */
2637 static int
2638compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2639{
2640 exprtype_T type = EXPR_UNKNOWN;
2641 char_u *p;
2642 char_u *next;
2643 int len = 2;
2644 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002645 isn_T *isn;
2646
2647 // get the first variable
2648 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2649 return FAIL;
2650
2651 /*
2652 * Repeat computing, until no "+", "-" or ".." is following.
2653 */
2654 for (;;)
2655 {
2656 type = EXPR_UNKNOWN;
2657
2658 p = may_peek_next_line(cctx, *arg, &next);
2659 if (p[0] == '<' && p[1] == '<')
2660 type = EXPR_LSHIFT;
2661 else if (p[0] == '>' && p[1] == '>')
2662 type = EXPR_RSHIFT;
2663
2664 if (type == EXPR_UNKNOWN)
2665 return OK;
2666
2667 // Handle a bitwise left or right shift operator
2668 if (ppconst->pp_used == ppconst_used + 1)
2669 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002670 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002671 {
2672 // left operand should be a number
2673 emsg(_(e_bitshift_ops_must_be_number));
2674 return FAIL;
2675 }
2676 }
2677 else
2678 {
2679 type_T *t = get_type_on_stack(cctx, 0);
2680
2681 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2682 {
2683 emsg(_(e_bitshift_ops_must_be_number));
2684 return FAIL;
2685 }
2686 }
2687
2688 if (next != NULL)
2689 {
2690 *arg = next_line_from_context(cctx, TRUE);
2691 p = skipwhite(*arg);
2692 }
2693
2694 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2695 {
2696 error_white_both(p, len);
2697 return FAIL;
2698 }
2699
2700 // get the second variable
2701 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2702 return FAIL;
2703
2704 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2705 return FAIL;
2706
2707 if (ppconst->pp_used == ppconst_used + 2)
2708 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002709 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2710 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2711
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002712 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002713 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2714 {
2715 // right operand should be a positive number
2716 if (tv2->v_type != VAR_NUMBER)
2717 emsg(_(e_bitshift_ops_must_be_number));
2718 else
2719 emsg(_(e_bitshift_ops_must_be_postive));
2720 return FAIL;
2721 }
2722
2723 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2724 tv1->vval.v_number = 0;
2725 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002726 tv1->vval.v_number =
2727 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002728 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002729 tv1->vval.v_number =
2730 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002731 clear_tv(tv2);
2732 --ppconst->pp_used;
2733 }
2734 else
2735 {
2736 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2737 FALSE, FALSE) == FAIL)
2738 {
2739 emsg(_(e_bitshift_ops_must_be_number));
2740 return FAIL;
2741 }
2742
2743 generate_ppconst(cctx, ppconst);
2744
2745 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2746 if (isn == NULL)
2747 return FAIL;
2748
2749 if (isn != NULL)
2750 isn->isn_arg.op.op_type = type;
2751 }
2752 }
2753
2754 return OK;
2755}
2756
2757/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002758 * expr5a == expr5b
2759 * expr5a =~ expr5b
2760 * expr5a != expr5b
2761 * expr5a !~ expr5b
2762 * expr5a > expr5b
2763 * expr5a >= expr5b
2764 * expr5a < expr5b
2765 * expr5a <= expr5b
2766 * expr5a is expr5b
2767 * expr5a isnot expr5b
2768 *
2769 * Produces instructions:
2770 * EVAL expr5a Push result of "expr5a"
2771 * EVAL expr5b Push result of "expr5b"
2772 * COMPARE one of the compare instructions
2773 */
2774 static int
2775compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2776{
2777 exprtype_T type = EXPR_UNKNOWN;
2778 char_u *p;
2779 char_u *next;
2780 int len = 2;
2781 int type_is = FALSE;
2782 int ppconst_used = ppconst->pp_used;
2783
2784 // get the first variable
2785 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2786 return FAIL;
2787
2788 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002789
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002790 type = get_compare_type(p, &len, &type_is);
2791
2792 /*
2793 * If there is a comparative operator, use it.
2794 */
2795 if (type != EXPR_UNKNOWN)
2796 {
2797 int ic = FALSE; // Default: do not ignore case
2798
2799 if (next != NULL)
2800 {
2801 *arg = next_line_from_context(cctx, TRUE);
2802 p = skipwhite(*arg);
2803 }
2804 if (type_is && (p[len] == '?' || p[len] == '#'))
2805 {
2806 semsg(_(e_invalid_expression_str), *arg);
2807 return FAIL;
2808 }
2809 // extra question mark appended: ignore case
2810 if (p[len] == '?')
2811 {
2812 ic = TRUE;
2813 ++len;
2814 }
2815 // extra '#' appended: match case (ignored)
2816 else if (p[len] == '#')
2817 ++len;
2818 // nothing appended: match case
2819
2820 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2821 {
2822 error_white_both(p, len);
2823 return FAIL;
2824 }
2825
2826 // get the second variable
2827 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2828 return FAIL;
2829
2830 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2831 return FAIL;
2832
2833 if (ppconst->pp_used == ppconst_used + 2)
2834 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002835 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002836 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2837 int ret;
2838
2839 // Both sides are a constant, compute the result now.
2840 // First check for a valid combination of types, this is more
2841 // strict than typval_compare().
2842 if (check_compare_types(type, tv1, tv2) == FAIL)
2843 ret = FAIL;
2844 else
2845 {
2846 ret = typval_compare(tv1, tv2, type, ic);
2847 tv1->v_type = VAR_BOOL;
2848 tv1->vval.v_number = tv1->vval.v_number
2849 ? VVAL_TRUE : VVAL_FALSE;
2850 clear_tv(tv2);
2851 --ppconst->pp_used;
2852 }
2853 return ret;
2854 }
2855
2856 generate_ppconst(cctx, ppconst);
2857 return generate_COMPARE(cctx, type, ic);
2858 }
2859
2860 return OK;
2861}
2862
2863static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2864
2865/*
2866 * Compile || or &&.
2867 */
2868 static int
2869compile_and_or(
2870 char_u **arg,
2871 cctx_T *cctx,
2872 char *op,
2873 ppconst_T *ppconst,
2874 int ppconst_used UNUSED)
2875{
2876 char_u *next;
2877 char_u *p = may_peek_next_line(cctx, *arg, &next);
2878 int opchar = *op;
2879
2880 if (p[0] == opchar && p[1] == opchar)
2881 {
2882 garray_T *instr = &cctx->ctx_instr;
2883 garray_T end_ga;
2884 int save_skip = cctx->ctx_skip;
2885
2886 /*
2887 * Repeat until there is no following "||" or "&&"
2888 */
2889 ga_init2(&end_ga, sizeof(int), 10);
2890 while (p[0] == opchar && p[1] == opchar)
2891 {
2892 long start_lnum = SOURCING_LNUM;
2893 long save_sourcing_lnum;
2894 int start_ctx_lnum = cctx->ctx_lnum;
2895 int save_lnum;
2896 int const_used;
2897 int status;
2898 jumpwhen_T jump_when = opchar == '|'
2899 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2900
2901 if (next != NULL)
2902 {
2903 *arg = next_line_from_context(cctx, TRUE);
2904 p = skipwhite(*arg);
2905 }
2906
2907 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2908 {
2909 semsg(_(e_white_space_required_before_and_after_str_at_str),
2910 op, p);
2911 ga_clear(&end_ga);
2912 return FAIL;
2913 }
2914
2915 save_sourcing_lnum = SOURCING_LNUM;
2916 SOURCING_LNUM = start_lnum;
2917 save_lnum = cctx->ctx_lnum;
2918 cctx->ctx_lnum = start_ctx_lnum;
2919
2920 status = check_ppconst_bool(ppconst);
2921 if (status != FAIL)
2922 {
2923 // Use the last ppconst if possible.
2924 if (ppconst->pp_used > 0)
2925 {
2926 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2927 int is_true = tv2bool(tv);
2928
2929 if ((is_true && opchar == '|')
2930 || (!is_true && opchar == '&'))
2931 {
2932 // For "false && expr" and "true || expr" the "expr"
2933 // does not need to be evaluated.
2934 cctx->ctx_skip = SKIP_YES;
2935 clear_tv(tv);
2936 tv->v_type = VAR_BOOL;
2937 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2938 }
2939 else
2940 {
2941 // For "true && expr" and "false || expr" only "expr"
2942 // needs to be evaluated.
2943 --ppconst->pp_used;
2944 jump_when = JUMP_NEVER;
2945 }
2946 }
2947 else
2948 {
2949 // Every part must evaluate to a bool.
2950 status = bool_on_stack(cctx);
2951 }
2952 }
2953 if (status != FAIL)
2954 status = ga_grow(&end_ga, 1);
2955 cctx->ctx_lnum = save_lnum;
2956 if (status == FAIL)
2957 {
2958 ga_clear(&end_ga);
2959 return FAIL;
2960 }
2961
2962 if (jump_when != JUMP_NEVER)
2963 {
2964 if (cctx->ctx_skip != SKIP_YES)
2965 {
2966 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2967 ++end_ga.ga_len;
2968 }
2969 generate_JUMP(cctx, jump_when, 0);
2970 }
2971
2972 // eval the next expression
2973 SOURCING_LNUM = save_sourcing_lnum;
2974 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2975 {
2976 ga_clear(&end_ga);
2977 return FAIL;
2978 }
2979
2980 const_used = ppconst->pp_used;
2981 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2982 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2983 {
2984 ga_clear(&end_ga);
2985 return FAIL;
2986 }
2987
2988 // "0 || 1" results in true, "1 && 0" results in false.
2989 if (ppconst->pp_used == const_used + 1)
2990 {
2991 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2992
2993 if (tv->v_type == VAR_NUMBER
2994 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2995 {
2996 tv->vval.v_number = tv->vval.v_number == 1
2997 ? VVAL_TRUE : VVAL_FALSE;
2998 tv->v_type = VAR_BOOL;
2999 }
3000 }
3001
3002 p = may_peek_next_line(cctx, *arg, &next);
3003 }
3004
3005 if (check_ppconst_bool(ppconst) == FAIL)
3006 {
3007 ga_clear(&end_ga);
3008 return FAIL;
3009 }
3010
3011 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3012 // Every part must evaluate to a bool.
3013 if (bool_on_stack(cctx) == FAIL)
3014 {
3015 ga_clear(&end_ga);
3016 return FAIL;
3017 }
3018
3019 if (end_ga.ga_len > 0)
3020 {
3021 // Fill in the end label in all jumps.
3022 generate_ppconst(cctx, ppconst);
3023 while (end_ga.ga_len > 0)
3024 {
3025 isn_T *isn;
3026
3027 --end_ga.ga_len;
3028 isn = ((isn_T *)instr->ga_data)
3029 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3030 isn->isn_arg.jump.jump_where = instr->ga_len;
3031 }
3032 }
3033 ga_clear(&end_ga);
3034
3035 cctx->ctx_skip = save_skip;
3036 }
3037
3038 return OK;
3039}
3040
3041/*
3042 * expr4a && expr4a && expr4a logical AND
3043 *
3044 * Produces instructions:
3045 * EVAL expr4a Push result of "expr4a"
3046 * COND2BOOL convert to bool if needed
3047 * JUMP_IF_COND_FALSE end
3048 * EVAL expr4b Push result of "expr4b"
3049 * JUMP_IF_COND_FALSE end
3050 * EVAL expr4c Push result of "expr4c"
3051 * end:
3052 */
3053 static int
3054compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3055{
3056 int ppconst_used = ppconst->pp_used;
3057
3058 // get the first variable
3059 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3060 return FAIL;
3061
3062 // || and && work almost the same
3063 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3064}
3065
3066/*
3067 * expr3a || expr3b || expr3c logical OR
3068 *
3069 * Produces instructions:
3070 * EVAL expr3a Push result of "expr3a"
3071 * COND2BOOL convert to bool if needed
3072 * JUMP_IF_COND_TRUE end
3073 * EVAL expr3b Push result of "expr3b"
3074 * JUMP_IF_COND_TRUE end
3075 * EVAL expr3c Push result of "expr3c"
3076 * end:
3077 */
3078 static int
3079compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3080{
3081 int ppconst_used = ppconst->pp_used;
3082
3083 // eval the first expression
3084 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3085 return FAIL;
3086
3087 // || and && work almost the same
3088 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3089}
3090
3091/*
3092 * Toplevel expression: expr2 ? expr1a : expr1b
3093 * Produces instructions:
3094 * EVAL expr2 Push result of "expr2"
3095 * JUMP_IF_FALSE alt jump if false
3096 * EVAL expr1a
3097 * JUMP_ALWAYS end
3098 * alt: EVAL expr1b
3099 * end:
3100 *
3101 * Toplevel expression: expr2 ?? expr1
3102 * Produces instructions:
3103 * EVAL expr2 Push result of "expr2"
3104 * JUMP_AND_KEEP_IF_TRUE end jump if true
3105 * EVAL expr1
3106 * end:
3107 */
3108 int
3109compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3110{
3111 char_u *p;
3112 int ppconst_used = ppconst->pp_used;
3113 char_u *next;
3114
3115 // Ignore all kinds of errors when not producing code.
3116 if (cctx->ctx_skip == SKIP_YES)
3117 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003118 int prev_did_emsg = did_emsg;
3119
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003120 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003121 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003122 }
3123
3124 // Evaluate the first expression.
3125 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3126 return FAIL;
3127
3128 p = may_peek_next_line(cctx, *arg, &next);
3129 if (*p == '?')
3130 {
3131 int op_falsy = p[1] == '?';
3132 garray_T *instr = &cctx->ctx_instr;
3133 garray_T *stack = &cctx->ctx_type_stack;
3134 int alt_idx = instr->ga_len;
3135 int end_idx = 0;
3136 isn_T *isn;
3137 type_T *type1 = NULL;
3138 int has_const_expr = FALSE;
3139 int const_value = FALSE;
3140 int save_skip = cctx->ctx_skip;
3141
3142 if (next != NULL)
3143 {
3144 *arg = next_line_from_context(cctx, TRUE);
3145 p = skipwhite(*arg);
3146 }
3147
3148 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3149 {
3150 semsg(_(e_white_space_required_before_and_after_str_at_str),
3151 op_falsy ? "??" : "?", p);
3152 return FAIL;
3153 }
3154
3155 if (ppconst->pp_used == ppconst_used + 1)
3156 {
3157 // the condition is a constant, we know whether the ? or the :
3158 // expression is to be evaluated.
3159 has_const_expr = TRUE;
3160 if (op_falsy)
3161 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3162 else
3163 {
3164 int error = FALSE;
3165
3166 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3167 &error);
3168 if (error)
3169 return FAIL;
3170 }
3171 cctx->ctx_skip = save_skip == SKIP_YES ||
3172 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3173
3174 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3175 // "left ?? right" and "left" is truthy: produce "left"
3176 generate_ppconst(cctx, ppconst);
3177 else
3178 {
3179 clear_tv(&ppconst->pp_tv[ppconst_used]);
3180 --ppconst->pp_used;
3181 }
3182 }
3183 else
3184 {
3185 generate_ppconst(cctx, ppconst);
3186 if (op_falsy)
3187 end_idx = instr->ga_len;
3188 generate_JUMP(cctx, op_falsy
3189 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3190 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003191 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003192 }
3193
3194 // evaluate the second expression; any type is accepted
3195 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3196 return FAIL;
3197 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3198 return FAIL;
3199
3200 if (!has_const_expr)
3201 {
3202 generate_ppconst(cctx, ppconst);
3203
3204 if (!op_falsy)
3205 {
3206 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003207 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003208 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003209
3210 end_idx = instr->ga_len;
3211 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3212
3213 // jump here from JUMP_IF_FALSE
3214 isn = ((isn_T *)instr->ga_data) + alt_idx;
3215 isn->isn_arg.jump.jump_where = instr->ga_len;
3216 }
3217 }
3218
3219 if (!op_falsy)
3220 {
3221 // Check for the ":".
3222 p = may_peek_next_line(cctx, *arg, &next);
3223 if (*p != ':')
3224 {
3225 emsg(_(e_missing_colon_after_questionmark));
3226 return FAIL;
3227 }
3228 if (next != NULL)
3229 {
3230 *arg = next_line_from_context(cctx, TRUE);
3231 p = skipwhite(*arg);
3232 }
3233
3234 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3235 {
3236 semsg(_(e_white_space_required_before_and_after_str_at_str),
3237 ":", p);
3238 return FAIL;
3239 }
3240
3241 // evaluate the third expression
3242 if (has_const_expr)
3243 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3244 ? SKIP_YES : SKIP_NOT;
3245 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3246 return FAIL;
3247 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3248 return FAIL;
3249 }
3250
3251 if (!has_const_expr)
3252 {
3253 type_T **typep;
3254
3255 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003256 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003257
3258 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003259 typep = &((((type2_T *)stack->ga_data)
3260 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003261 common_type(type1, *typep, typep, cctx->ctx_type_list);
3262
3263 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3264 isn = ((isn_T *)instr->ga_data) + end_idx;
3265 isn->isn_arg.jump.jump_where = instr->ga_len;
3266 }
3267
3268 cctx->ctx_skip = save_skip;
3269 }
3270 return OK;
3271}
3272
3273/*
3274 * Toplevel expression.
3275 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3276 * Returns OK or FAIL.
3277 */
3278 int
3279compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3280{
3281 ppconst_T ppconst;
3282
3283 CLEAR_FIELD(ppconst);
3284 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3285 {
3286 clear_ppconst(&ppconst);
3287 return FAIL;
3288 }
3289 if (is_const != NULL)
3290 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3291 if (generate_ppconst(cctx, &ppconst) == FAIL)
3292 return FAIL;
3293 return OK;
3294}
3295
3296/*
3297 * Toplevel expression.
3298 */
3299 int
3300compile_expr0(char_u **arg, cctx_T *cctx)
3301{
3302 return compile_expr0_ext(arg, cctx, NULL);
3303}
3304
3305
3306#endif // defined(FEAT_EVAL)