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