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