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