blob: 5a6b1108322029925f38afa9816462f26b673530 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000102 if (need_type(idxtype, &t_number, FALSE,
103 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000104 return FAIL;
105 if (is_slice)
106 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000107 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000108 if (need_type(idxtype, &t_number, FALSE,
109 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000110 return FAIL;
111 }
112 }
113
114 if (vartype == VAR_DICT)
115 {
116 if (is_slice)
117 {
118 emsg(_(e_cannot_slice_dictionary));
119 return FAIL;
120 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000122 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000123 typep->type_curr = typep->type_curr->tt_member;
124 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000125 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 typep->type_curr = &t_any;
127 if (typep->type_decl->tt_type == VAR_DICT)
128 {
129 typep->type_decl = typep->type_decl->tt_member;
130 if (typep->type_decl == &t_unknown)
131 // empty dict was used
132 typep->type_decl = &t_any;
133 }
134 else
135 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000136 }
137 else
138 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000139 if (need_type(typep->type_curr, &t_dict_any, FALSE,
140 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000141 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000142 typep->type_curr = &t_any;
143 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100145 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
146 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147 return FAIL;
148 if (keeping_dict != NULL)
149 *keeping_dict = TRUE;
150 }
151 else if (vartype == VAR_STRING)
152 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000153 typep->type_curr = &t_string;
154 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000155 if ((is_slice
156 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
157 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
158 return FAIL;
159 }
160 else if (vartype == VAR_BLOB)
161 {
162 if (is_slice)
163 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 typep->type_curr = &t_blob;
165 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
167 return FAIL;
168 }
169 else
170 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000171 typep->type_curr = &t_number;
172 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
174 return FAIL;
175 }
176 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100177 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
178 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000179 {
180 if (is_slice)
181 {
182 if (generate_instr_drop(cctx,
183 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
184 2) == FAIL)
185 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000186 // a copy is made so the member type is no longer declared
187 if (typep->type_decl->tt_type == VAR_LIST)
188 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000189
190 // a copy is made, the composite is no longer "const"
191 if (typep->type_curr->tt_flags & TTFLAG_CONST)
192 {
193 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
194
195 if (type != typep->type_curr) // did get a copy
196 {
197 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
198 typep->type_curr = type;
199 }
200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201 }
202 else
203 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 typep->type_curr = typep->type_curr->tt_member;
207 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000209 typep->type_curr = &t_any;
210 if (typep->type_decl->tt_type == VAR_LIST)
211 {
212 typep->type_decl = typep->type_decl->tt_member;
213 if (typep->type_decl == &t_unknown)
214 // empty list was used
215 typep->type_decl = &t_any;
216 }
217 else
218 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 }
220 if (generate_instr_drop(cctx,
221 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
222 == FAIL)
223 return FAIL;
224 }
225 }
226 else
227 {
228 switch (vartype)
229 {
230 case VAR_FUNC:
231 case VAR_PARTIAL:
232 emsg(_(e_cannot_index_a_funcref));
233 break;
234 case VAR_BOOL:
235 case VAR_SPECIAL:
236 case VAR_JOB:
237 case VAR_CHANNEL:
238 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 case VAR_CLASS:
240 case VAR_OBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000241 case VAR_UNKNOWN:
242 case VAR_ANY:
243 case VAR_VOID:
244 emsg(_(e_cannot_index_special_variable));
245 break;
246 default:
247 emsg(_(e_string_list_dict_or_blob_required));
248 }
249 return FAIL;
250 }
251 return OK;
252}
253
254/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000255 * Compile ".member" coming after an object or class.
256 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000257 static int
258compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
259{
260 if (VIM_ISWHITE((*arg)[1]))
261 {
262 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
263 return FAIL;
264 }
265
266 ++*arg;
267 char_u *name = *arg;
268 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
269 if (name_end == name)
270 return FAIL;
271 size_t len = name_end - name;
272
273 class_T *cl = (class_T *)type->tt_member;
274 if (*name_end == '(')
275 {
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000276 if (type->tt_type == VAR_CLASS)
277 {
278 garray_T *instr = &cctx->ctx_instr;
279 if (instr->ga_len > 0)
280 {
281 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
282 if (isn->isn_type == ISN_LOADSCRIPT)
283 {
284 // The class was recognized as a script item. We only need
285 // to know what class it is, drop the instruction.
286 --instr->ga_len;
287 vim_free(isn->isn_arg.script.scriptref);
288 }
289 }
290
291 for (int i = 0; i < cl->class_class_function_count; ++i)
292 {
293 ufunc_T *fp = cl->class_class_functions[i];
294 // Use a separate pointer to avoid that ASAN complains about
295 // uf_name[] only being 4 characters.
296 char_u *ufname = (char_u *)fp->uf_name;
297 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
298 {
299 *arg = skipwhite(name_end + 1);
300 int argcount = 0;
301 if (compile_arguments(arg, cctx, &argcount,
302 CA_NOT_SPECIAL) == FAIL)
303 return FAIL;
304 return generate_CALL(cctx, fp, argcount);
305 }
306 }
307
308 semsg(_(e_method_not_found_on_class_str_str),
309 cl->class_name, name);
310 return FAIL;
311 }
312 else
313 {
314 // TODO: method call
315 emsg("compile_class_object_index(): object call not handled yet");
316 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000317 }
318 else if (type->tt_type == VAR_OBJECT)
319 {
320 for (int i = 0; i < cl->class_obj_member_count; ++i)
321 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000322 ocmember_T *m = &cl->class_obj_members[i];
323 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000324 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000325 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
326 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000327 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000328 return FAIL;
329 }
330
Bram Moolenaard505d172022-12-18 21:42:55 +0000331 generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000332
333 *arg = name_end;
334 return OK;
335 }
336 }
337
338 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
339 }
340 else
341 {
342 // TODO: class member
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000343 emsg("compile_class_object_index(): class member not handled yet");
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000344 }
345
346 return FAIL;
347}
348
349/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000350 * Generate an instruction to load script-local variable "name", without the
351 * leading "s:".
352 * Also finds imported variables.
353 */
354 int
355compile_load_scriptvar(
356 cctx_T *cctx,
357 char_u *name, // variable NUL terminated
358 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100359 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000360{
361 scriptitem_T *si;
362 int idx;
363 imported_T *import;
364
365 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
366 return FAIL;
367 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000368 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000369 if (idx >= 0)
370 {
371 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
372
373 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
374 current_sctx.sc_sid, idx, sv->sv_type);
375 return OK;
376 }
377
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000378 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 if (import != NULL)
380 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000381 char_u *p = skipwhite(*end);
382 char_u *exp_name;
383 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100384 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000385 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000386 int done = FALSE;
387 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000388
389 // Need to lookup the member.
390 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000391 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000392 semsg(_(e_expected_dot_after_name_str), start);
393 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000394 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000395 ++p;
396 if (VIM_ISWHITE(*p))
397 {
398 emsg(_(e_no_white_space_allowed_after_dot));
399 return FAIL;
400 }
401
402 // isolate one name
403 exp_name = p;
404 while (eval_isnamec(*p))
405 ++p;
406 cc = *p;
407 *p = NUL;
408
Bram Moolenaard041f422022-01-12 19:54:00 +0000409 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100410 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
411 // "import autoload './dir/script.vim'" or
412 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100413 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100414
415 if (res == OK)
416 {
417 if (si->sn_autoload_prefix != NULL
418 && si->sn_state == SN_STATE_NOT_LOADED)
419 {
420 char_u *auto_name =
421 concat_str(si->sn_autoload_prefix, exp_name);
422
423 // autoload script must be loaded later, access by the autoload
424 // name. If a '(' follows it must be a function. Otherwise we
425 // don't know, it can be "script.Func".
426 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100427 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100428 else
429 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
430 vim_free(auto_name);
431 done = TRUE;
432 }
433 else if (si->sn_import_autoload
434 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100435 {
436 // If a '(' follows it must be a function. Otherwise we don't
437 // know, it can be "script.Func".
438 if (cc == '(' || paren_follows_after_expr)
439 {
440 char_u sid_name[MAX_FUNC_NAME_LEN];
441
442 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100443 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100444 }
445 else
446 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
447 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100448 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100449 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100450 else
451 {
452 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
453 cctx, NULL, TRUE);
454 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100455 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100456
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000457 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000458 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000459 if (done)
460 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000461
462 if (idx < 0)
463 {
464 if (ufunc != NULL)
465 {
466 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100467 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000468 return OK;
469 }
470 return FAIL;
471 }
472
473 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
474 import->imp_sid,
475 idx,
476 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000477 return OK;
478 }
479
Bram Moolenaard0132f42022-05-12 11:05:40 +0100480 // Can only get here if we know "name" is a script variable and not in a
481 // Vim9 script (variable is not in sn_var_vals): old style script.
482 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000483 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000484}
485
486 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000487generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000488{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000489 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000490 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000492 // Reject a global non-autoload function found without the "g:" prefix.
493 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000494 return FAIL;
495
496 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000497 compile_type = get_compile_type(ufunc);
498 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000499 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100501 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502}
503
504/*
505 * Compile a variable name into a load instruction.
506 * "end" points to just after the name.
507 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
508 * When "error" is FALSE do not give an error when not found.
509 */
510 int
511compile_load(
512 char_u **arg,
513 char_u *end_arg,
514 cctx_T *cctx,
515 int is_expr,
516 int error)
517{
518 type_T *type;
519 char_u *name = NULL;
520 char_u *end = end_arg;
521 int res = FAIL;
522 int prev_called_emsg = called_emsg;
523
524 if (*(*arg + 1) == ':')
525 {
526 if (end <= *arg + 2)
527 {
528 isntype_T isn_type;
529
530 // load dictionary of namespace
531 switch (**arg)
532 {
533 case 'g': isn_type = ISN_LOADGDICT; break;
534 case 'w': isn_type = ISN_LOADWDICT; break;
535 case 't': isn_type = ISN_LOADTDICT; break;
536 case 'b': isn_type = ISN_LOADBDICT; break;
537 default:
538 semsg(_(e_namespace_not_supported_str), *arg);
539 goto theend;
540 }
541 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
542 goto theend;
543 res = OK;
544 }
545 else
546 {
547 isntype_T isn_type = ISN_DROP;
548
549 // load namespaced variable
550 name = vim_strnsave(*arg + 2, end - (*arg + 2));
551 if (name == NULL)
552 return FAIL;
553
554 switch (**arg)
555 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100556 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000557 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000558 case 's': if (current_script_is_vim9())
559 {
560 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
561 *arg);
562 vim_free(name);
563 return FAIL;
564 }
Kota Kato948a3892022-08-16 16:09:59 +0100565 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000566 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000567 else
568 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100569 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570 break;
571 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
572 {
573 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000574 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000575 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576 else
577 isn_type = ISN_LOADG;
578 }
579 else
580 {
581 isn_type = ISN_LOADAUTO;
582 vim_free(name);
583 name = vim_strnsave(*arg, end - *arg);
584 if (name == NULL)
585 return FAIL;
586 }
587 break;
588 case 'w': isn_type = ISN_LOADW; break;
589 case 't': isn_type = ISN_LOADT; break;
590 case 'b': isn_type = ISN_LOADB; break;
591 default: // cannot happen, just in case
592 semsg(_(e_namespace_not_supported_str), *arg);
593 goto theend;
594 }
595 if (isn_type != ISN_DROP)
596 {
597 // Global, Buffer-local, Window-local and Tabpage-local
598 // variables can be defined later, thus we don't check if it
599 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000600 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601 }
602 }
603 }
604 else
605 {
606 size_t len = end - *arg;
607 int idx;
608 int gen_load = FALSE;
609 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100610 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100611 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612
613 name = vim_strnsave(*arg, end - *arg);
614 if (name == NULL)
615 return FAIL;
616
617 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
618 {
619 script_autoload(name, FALSE);
620 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
621 }
622 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
623 == OK)
624 {
625 if (gen_load_outer == 0)
626 gen_load = TRUE;
627 }
628 else
629 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000630 lvar_T lvar;
631 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000632
633 if (lookup_local(*arg, len, &lvar, cctx) == OK)
634 {
635 type = lvar.lv_type;
636 idx = lvar.lv_idx;
637 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100638 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000639 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100640 outer_loop_depth = lvar.lv_loop_depth;
641 outer_loop_idx = lvar.lv_loop_idx;
642 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000643 else
644 gen_load = TRUE;
645 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000646 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000647 {
648 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
649 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000650 else
651 {
652 // "var" can be script-local even without using "s:" if it
653 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000654 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000655 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100656 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657
658 // When evaluating an expression and the name starts with an
659 // uppercase letter it can be a user defined function.
660 // generate_funcref() will fail if the function can't be found.
661 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000662 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000663 }
664 }
665 if (gen_load)
666 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
667 if (gen_load_outer > 0)
668 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100669 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
670 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000671 cctx->ctx_outer_used = TRUE;
672 }
673 }
674
675 *arg = end;
676
677theend:
678 if (res == FAIL && error && called_emsg == prev_called_emsg)
679 semsg(_(e_variable_not_found_str), name);
680 vim_free(name);
681 return res;
682}
683
684/*
685 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100686 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 * Returns FAIL if compilation fails.
688 */
689 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100690compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000691{
LemonBoyf3b48952022-05-05 13:53:03 +0100692 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000693 garray_T save_ga = cctx->ctx_instr;
694 int expr_res;
695 int trailing_error;
696 int instr_count;
697 isn_T *instr = NULL;
698
699 // Remove the string type from the stack.
700 --cctx->ctx_type_stack.ga_len;
701
702 // Temporarily reset the list of instructions so that the jump labels are
703 // correct.
704 cctx->ctx_instr.ga_len = 0;
705 cctx->ctx_instr.ga_maxlen = 0;
706 cctx->ctx_instr.ga_data = NULL;
707 expr_res = compile_expr0(&s, cctx);
708 s = skipwhite(s);
709 trailing_error = *s != NUL;
710
711 if (expr_res == FAIL || trailing_error
712 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
713 {
714 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000715 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 clear_instr_ga(&cctx->ctx_instr);
717 cctx->ctx_instr = save_ga;
718 ++cctx->ctx_type_stack.ga_len;
719 return FAIL;
720 }
721
722 // Move the generated instructions into the ISN_INSTR instruction, then
723 // restore the list of instructions.
724 instr_count = cctx->ctx_instr.ga_len;
725 instr = cctx->ctx_instr.ga_data;
726 instr[instr_count].isn_type = ISN_FINISH;
727
728 cctx->ctx_instr = save_ga;
729 vim_free(isn->isn_arg.string);
730 isn->isn_type = ISN_INSTR;
731 isn->isn_arg.instr = instr;
732 return OK;
733}
734
735/*
736 * Compile the argument expressions.
737 * "arg" points to just after the "(" and is advanced to after the ")"
738 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100739 int
LemonBoyf3b48952022-05-05 13:53:03 +0100740compile_arguments(
741 char_u **arg,
742 cctx_T *cctx,
743 int *argcount,
744 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745{
746 char_u *p = *arg;
747 char_u *whitep = *arg;
748 int must_end = FALSE;
749 int instr_count;
750
751 for (;;)
752 {
753 if (may_get_next_line(whitep, &p, cctx) == FAIL)
754 goto failret;
755 if (*p == ')')
756 {
757 *arg = p + 1;
758 return OK;
759 }
760 if (must_end)
761 {
762 semsg(_(e_missing_comma_before_argument_str), p);
763 return FAIL;
764 }
765
766 instr_count = cctx->ctx_instr.ga_len;
767 if (compile_expr0(&p, cctx) == FAIL)
768 return FAIL;
769 ++*argcount;
770
LemonBoyf3b48952022-05-05 13:53:03 +0100771 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 && cctx->ctx_instr.ga_len == instr_count + 1)
773 {
774 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
775
776 // {skip} argument of searchpair() can be compiled if not empty
777 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100778 compile_string(isn, cctx, 0);
779 }
780 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
781 && cctx->ctx_instr.ga_len == instr_count + 1)
782 {
783 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
784
785 // {sub} argument of substitute() can be compiled if it starts
786 // with \=
787 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100788 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100789 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 }
791
792 if (*p != ',' && *skipwhite(p) == ',')
793 {
794 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
795 p = skipwhite(p);
796 }
797 if (*p == ',')
798 {
799 ++p;
800 if (*p != NUL && !VIM_ISWHITE(*p))
801 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
802 }
803 else
804 must_end = TRUE;
805 whitep = p;
806 p = skipwhite(p);
807 }
808failret:
809 emsg(_(e_missing_closing_paren));
810 return FAIL;
811}
812
813/*
814 * Compile a function call: name(arg1, arg2)
815 * "arg" points to "name", "arg + varlen" to the "(".
816 * "argcount_init" is 1 for "value->method()"
817 * Instructions:
818 * EVAL arg1
819 * EVAL arg2
820 * BCALL / DCALL / UCALL
821 */
822 static int
823compile_call(
824 char_u **arg,
825 size_t varlen,
826 cctx_T *cctx,
827 ppconst_T *ppconst,
828 int argcount_init)
829{
830 char_u *name = *arg;
831 char_u *p;
832 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100833 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000834 char_u fname_buf[FLEN_FIXED + 1];
835 char_u *tofree = NULL;
836 int error = FCERR_NONE;
837 ufunc_T *ufunc = NULL;
838 int res = FAIL;
839 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000840 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100841 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000842 imported_T *import;
843
844 if (varlen >= sizeof(namebuf))
845 {
846 semsg(_(e_name_too_long_str), name);
847 return FAIL;
848 }
849 vim_strncpy(namebuf, *arg, varlen);
850
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000851 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000852 if (import != NULL)
853 {
854 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
855 return FAIL;
856 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857
858 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100859 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100861 if ((varlen == 3
862 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
864 {
865 char_u *s = skipwhite(*arg + varlen + 1);
866 typval_T argvars[2];
867 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100868 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869
870 argvars[0].v_type = VAR_UNKNOWN;
871 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100872 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100874 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 s = skipwhite(s);
876 if (*s == ')' && argvars[0].v_type == VAR_STRING
877 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
878 || !is_has))
879 {
880 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
881
882 *arg = s + 1;
883 argvars[1].v_type = VAR_UNKNOWN;
884 tv->v_type = VAR_NUMBER;
885 tv->vval.v_number = 0;
886 if (is_has)
887 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100888 else if (is_len)
889 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000890 else
891 f_exists(argvars, tv);
892 clear_tv(&argvars[0]);
893 ++ppconst->pp_used;
894 return OK;
895 }
896 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100897 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898 {
899 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
900 return FAIL;
901 }
902 }
903
904 if (generate_ppconst(cctx, ppconst) == FAIL)
905 return FAIL;
906
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000907 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
908
909 // We handle the "skip" argument of searchpair() and searchpairpos()
910 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100911 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
912 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
913 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
914 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
915 special_fn = CA_SEARCHPAIR;
916 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
917 special_fn = CA_SUBSTITUTE;
918 else
919 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920
921 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100922 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923 goto theend;
924
925 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
926 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
927 {
928 int idx;
929
930 // builtin function
931 idx = find_internal_func(name);
932 if (idx >= 0)
933 {
934 if (STRCMP(name, "flatten") == 0)
935 {
936 emsg(_(e_cannot_use_flatten_in_vim9_script));
937 goto theend;
938 }
939
940 if (STRCMP(name, "add") == 0 && argcount == 2)
941 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000942 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000943
944 // add() can be compiled to instructions if we know the type
945 if (type->tt_type == VAR_LIST)
946 {
947 // inline "add(list, item)" so that the type can be checked
948 res = generate_LISTAPPEND(cctx);
949 idx = -1;
950 }
951 else if (type->tt_type == VAR_BLOB)
952 {
953 // inline "add(blob, nr)" so that the type can be checked
954 res = generate_BLOBAPPEND(cctx);
955 idx = -1;
956 }
957 }
958
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100959 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
960 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100961 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100962 // May have the "D" or "R" flag, reserve a variable for a
963 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 if (get_defer_var_idx(cctx) == 0)
965 idx = -1;
966 }
967
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000968 if (idx >= 0)
969 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
970 }
971 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100972 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000973 goto theend;
974 }
975
Bram Moolenaar62aec932022-01-29 21:45:34 +0000976 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
977
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978 // An argument or local variable can be a function reference, this
979 // overrules a function name.
980 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
981 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
982 {
983 // If we can find the function by name generate the right call.
984 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000985 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000986 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000988 if (!func_is_global(ufunc))
989 {
990 res = generate_CALL(cctx, ufunc, argcount);
991 goto theend;
992 }
993 if (!has_g_namespace
994 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
995 {
996 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100997 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000998 goto theend;
999 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001000 }
1001 }
1002
1003 // If the name is a variable, load it and use PCALL.
1004 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001005 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001006 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001007 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1009 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001010 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011
1012 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1013 goto theend;
1014 }
1015
1016 // If we can find a global function by name generate the right call.
1017 if (ufunc != NULL)
1018 {
1019 res = generate_CALL(cctx, ufunc, argcount);
1020 goto theend;
1021 }
1022
1023 // A global function may be defined only later. Need to figure out at
1024 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001025 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001026 res = generate_UCALL(cctx, name, argcount);
1027 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001028 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001029
1030theend:
1031 vim_free(tofree);
1032 return res;
1033}
1034
1035// like NAMESPACE_CHAR but with 'a' and 'l'.
1036#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1037
1038/*
1039 * Find the end of a variable or function name. Unlike find_name_end() this
1040 * does not recognize magic braces.
1041 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1042 * Return a pointer to just after the name. Equal to "arg" if there is no
1043 * valid name.
1044 */
1045 char_u *
1046to_name_end(char_u *arg, int use_namespace)
1047{
1048 char_u *p;
1049
1050 // Quick check for valid starting character.
1051 if (!eval_isnamec1(*arg))
1052 return arg;
1053
1054 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1055 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1056 // and can be used in slice "[n:]".
1057 if (*p == ':' && (p != arg + 1
1058 || !use_namespace
1059 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1060 break;
1061 return p;
1062}
1063
1064/*
1065 * Like to_name_end() but also skip over a list or dict constant.
1066 * Also accept "<SNR>123_Func".
1067 * This intentionally does not handle line continuation.
1068 */
1069 char_u *
1070to_name_const_end(char_u *arg)
1071{
1072 char_u *p = arg;
1073 typval_T rettv;
1074
1075 if (STRNCMP(p, "<SNR>", 5) == 0)
1076 p = skipdigits(p + 5);
1077 p = to_name_end(p, TRUE);
1078 if (p == arg && *arg == '[')
1079 {
1080
1081 // Can be "[1, 2, 3]->Func()".
1082 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1083 p = arg;
1084 }
1085 return p;
1086}
1087
1088/*
1089 * parse a list: [expr, expr]
1090 * "*arg" points to the '['.
1091 * ppconst->pp_is_const is set if all items are a constant.
1092 */
1093 static int
1094compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1095{
1096 char_u *p = skipwhite(*arg + 1);
1097 char_u *whitep = *arg + 1;
1098 int count = 0;
1099 int is_const;
1100 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001101 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102
1103 for (;;)
1104 {
1105 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1106 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001107 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001108 return FAIL;
1109 }
1110 if (*p == ',')
1111 {
1112 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1113 return FAIL;
1114 }
1115 if (*p == ']')
1116 {
1117 ++p;
1118 break;
1119 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001120 if (must_end)
1121 {
1122 semsg(_(e_missing_comma_in_list_str), p);
1123 return FAIL;
1124 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1126 return FAIL;
1127 if (!is_const)
1128 is_all_const = FALSE;
1129 ++count;
1130 if (*p == ',')
1131 {
1132 ++p;
1133 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1134 {
1135 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1136 return FAIL;
1137 }
1138 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001139 else
1140 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 whitep = p;
1142 p = skipwhite(p);
1143 }
1144 *arg = p;
1145
1146 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001147 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001148}
1149
1150/*
1151 * Parse a lambda: "(arg, arg) => expr"
1152 * "*arg" points to the '('.
1153 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1154 */
1155 static int
1156compile_lambda(char_u **arg, cctx_T *cctx)
1157{
1158 int r;
1159 typval_T rettv;
1160 ufunc_T *ufunc;
1161 evalarg_T evalarg;
1162
1163 init_evalarg(&evalarg);
1164 evalarg.eval_flags = EVAL_EVALUATE;
1165 evalarg.eval_cctx = cctx;
1166
1167 // Get the funcref in "rettv".
1168 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1169 if (r != OK)
1170 {
1171 clear_evalarg(&evalarg, NULL);
1172 return r;
1173 }
1174
1175 // "rettv" will now be a partial referencing the function.
1176 ufunc = rettv.vval.v_partial->pt_func;
1177 ++ufunc->uf_refcount;
1178 clear_tv(&rettv);
1179
1180 // Compile it here to get the return type. The return type is optional,
1181 // when it's missing use t_unknown. This is recognized in
1182 // compile_return().
1183 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1184 ufunc->uf_ret_type = &t_unknown;
1185 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1186
1187 // When the outer function is compiled for profiling or debugging, the
1188 // lambda may be called without profiling or debugging. Compile it here in
1189 // the right context.
1190 if (cctx->ctx_compile_type == CT_DEBUG
1191#ifdef FEAT_PROFILE
1192 || cctx->ctx_compile_type == CT_PROFILE
1193#endif
1194 )
1195 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1196
Bram Moolenaar139575d2022-03-15 19:29:30 +00001197 // if the outer function is not compiled for debugging or profiling, this
1198 // one might be
1199 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001200 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001201 compiletype_T compile_type = get_compile_type(ufunc);
1202
1203 if (compile_type != CT_NONE)
1204 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001205 }
1206
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001207 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1208 // "*arg" may point into it. Point into the original line to avoid a
1209 // dangling pointer.
1210 if (evalarg.eval_using_cmdline)
1211 {
1212 garray_T *gap = &evalarg.eval_tofree_ga;
1213 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1214
1215 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1216 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001217 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 }
1219
1220 clear_evalarg(&evalarg, NULL);
1221
1222 if (ufunc->uf_def_status == UF_COMPILED)
1223 {
1224 // The return type will now be known.
1225 set_function_type(ufunc);
1226
1227 // The function reference count will be 1. When the ISN_FUNCREF
1228 // instruction is deleted the reference count is decremented and the
1229 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001230 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231 }
1232
1233 func_ptr_unref(ufunc);
1234 return FAIL;
1235}
1236
1237/*
1238 * Get a lambda and compile it. Uses Vim9 syntax.
1239 */
1240 int
1241get_lambda_tv_and_compile(
1242 char_u **arg,
1243 typval_T *rettv,
1244 int types_optional,
1245 evalarg_T *evalarg)
1246{
1247 int r;
1248 ufunc_T *ufunc;
1249 int save_sc_version = current_sctx.sc_version;
1250
1251 // Get the funcref in "rettv".
1252 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1253 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1254 current_sctx.sc_version = save_sc_version;
1255 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001256 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001257
1258 // "rettv" will now be a partial referencing the function.
1259 ufunc = rettv->vval.v_partial->pt_func;
1260
1261 // Compile it here to get the return type. The return type is optional,
1262 // when it's missing use t_unknown. This is recognized in
1263 // compile_return().
1264 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1265 ufunc->uf_ret_type = &t_unknown;
1266 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1267
1268 if (ufunc->uf_def_status == UF_COMPILED)
1269 {
1270 // The return type will now be known.
1271 set_function_type(ufunc);
1272 return OK;
1273 }
1274 clear_tv(rettv);
1275 return FAIL;
1276}
1277
1278/*
1279 * parse a dict: {key: val, [key]: val}
1280 * "*arg" points to the '{'.
1281 * ppconst->pp_is_const is set if all item values are a constant.
1282 */
1283 static int
1284compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1285{
1286 garray_T *instr = &cctx->ctx_instr;
1287 int count = 0;
1288 dict_T *d = dict_alloc();
1289 dictitem_T *item;
1290 char_u *whitep = *arg + 1;
1291 char_u *p;
1292 int is_const;
1293 int is_all_const = TRUE; // reset when non-const encountered
1294
1295 if (d == NULL)
1296 return FAIL;
1297 if (generate_ppconst(cctx, ppconst) == FAIL)
1298 return FAIL;
1299 for (;;)
1300 {
1301 char_u *key = NULL;
1302
1303 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1304 {
1305 *arg = NULL;
1306 goto failret;
1307 }
1308
1309 if (**arg == '}')
1310 break;
1311
1312 if (**arg == '[')
1313 {
1314 isn_T *isn;
1315
1316 // {[expr]: value} uses an evaluated key.
1317 *arg = skipwhite(*arg + 1);
1318 if (compile_expr0(arg, cctx) == FAIL)
1319 return FAIL;
1320 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1321 if (isn->isn_type == ISN_PUSHNR)
1322 {
1323 char buf[NUMBUFLEN];
1324
1325 // Convert to string at compile time.
1326 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1327 isn->isn_type = ISN_PUSHS;
1328 isn->isn_arg.string = vim_strsave((char_u *)buf);
1329 }
1330 if (isn->isn_type == ISN_PUSHS)
1331 key = isn->isn_arg.string;
1332 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1333 return FAIL;
1334 *arg = skipwhite(*arg);
1335 if (**arg != ']')
1336 {
1337 emsg(_(e_missing_matching_bracket_after_dict_key));
1338 return FAIL;
1339 }
1340 ++*arg;
1341 }
1342 else
1343 {
1344 // {"name": value},
1345 // {'name': value},
1346 // {name: value} use "name" as a literal key
1347 key = get_literal_key(arg);
1348 if (key == NULL)
1349 return FAIL;
1350 if (generate_PUSHS(cctx, &key) == FAIL)
1351 return FAIL;
1352 }
1353
1354 // Check for duplicate keys, if using string keys.
1355 if (key != NULL)
1356 {
1357 item = dict_find(d, key, -1);
1358 if (item != NULL)
1359 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001360 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361 goto failret;
1362 }
1363 item = dictitem_alloc(key);
1364 if (item != NULL)
1365 {
1366 item->di_tv.v_type = VAR_UNKNOWN;
1367 item->di_tv.v_lock = 0;
1368 if (dict_add(d, item) == FAIL)
1369 dictitem_free(item);
1370 }
1371 }
1372
1373 if (**arg != ':')
1374 {
1375 if (*skipwhite(*arg) == ':')
1376 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1377 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001378 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379 return FAIL;
1380 }
1381 whitep = *arg + 1;
1382 if (!IS_WHITE_OR_NUL(*whitep))
1383 {
1384 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1385 return FAIL;
1386 }
1387
1388 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1389 {
1390 *arg = NULL;
1391 goto failret;
1392 }
1393
1394 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1395 return FAIL;
1396 if (!is_const)
1397 is_all_const = FALSE;
1398 ++count;
1399
1400 whitep = *arg;
1401 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1402 {
1403 *arg = NULL;
1404 goto failret;
1405 }
1406 if (**arg == '}')
1407 break;
1408 if (**arg != ',')
1409 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001410 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001411 goto failret;
1412 }
1413 if (IS_WHITE_OR_NUL(*whitep))
1414 {
1415 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1416 return FAIL;
1417 }
1418 whitep = *arg + 1;
1419 if (!IS_WHITE_OR_NUL(*whitep))
1420 {
1421 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1422 return FAIL;
1423 }
1424 *arg = skipwhite(whitep);
1425 }
1426
1427 *arg = *arg + 1;
1428
1429 // Allow for following comment, after at least one space.
1430 p = skipwhite(*arg);
1431 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1432 *arg += STRLEN(*arg);
1433
1434 dict_unref(d);
1435 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001436 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437
1438failret:
1439 if (*arg == NULL)
1440 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001441 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 *arg = (char_u *)"";
1443 }
1444 dict_unref(d);
1445 return FAIL;
1446}
1447
1448/*
1449 * Compile "&option".
1450 */
1451 static int
1452compile_get_option(char_u **arg, cctx_T *cctx)
1453{
1454 typval_T rettv;
1455 char_u *start = *arg;
1456 int ret;
1457
1458 // parse the option and get the current value to get the type.
1459 rettv.v_type = VAR_UNKNOWN;
1460 ret = eval_option(arg, &rettv, TRUE);
1461 if (ret == OK)
1462 {
1463 // include the '&' in the name, eval_option() expects it.
1464 char_u *name = vim_strnsave(start, *arg - start);
1465 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1466 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1467
1468 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1469 vim_free(name);
1470 }
1471 clear_tv(&rettv);
1472
1473 return ret;
1474}
1475
1476/*
1477 * Compile "$VAR".
1478 */
1479 static int
1480compile_get_env(char_u **arg, cctx_T *cctx)
1481{
1482 char_u *start = *arg;
1483 int len;
1484 int ret;
1485 char_u *name;
1486
1487 ++*arg;
1488 len = get_env_len(arg);
1489 if (len == 0)
1490 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001491 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492 return FAIL;
1493 }
1494
1495 // include the '$' in the name, eval_env_var() expects it.
1496 name = vim_strnsave(start, len + 1);
1497 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1498 vim_free(name);
1499 return ret;
1500}
1501
1502/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001503 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001504 */
1505 static int
1506compile_interp_string(char_u **arg, cctx_T *cctx)
1507{
1508 typval_T tv;
1509 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001510 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001511 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001512 int count = 0;
1513 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001514
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001515 // *arg is on the '$' character, move it to the first string character.
1516 ++*arg;
1517 quote = **arg;
1518 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001519
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001520 for (;;)
1521 {
1522 // Get the string up to the matching quote or to a single '{'.
1523 // "arg" is advanced to either the quote or the '{'.
1524 if (quote == '"')
1525 ret = eval_string(arg, &tv, evaluate, TRUE);
1526 else
1527 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1528 if (ret == FAIL)
1529 break;
1530 if (evaluate)
1531 {
1532 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1533 || (**arg != '{' && count == 0))
1534 {
1535 // generate non-empty string or empty string if it's the only
1536 // one
1537 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1538 return FAIL;
1539 tv.vval.v_string = NULL; // don't free it now
1540 ++count;
1541 }
1542 clear_tv(&tv);
1543 }
1544
1545 if (**arg != '{')
1546 {
1547 // found terminating quote
1548 ++*arg;
1549 break;
1550 }
1551
1552 p = compile_one_expr_in_str(*arg, cctx);
1553 if (p == NULL)
1554 {
1555 ret = FAIL;
1556 break;
1557 }
1558 ++count;
1559 *arg = p;
1560 }
LemonBoy2eaef102022-05-06 13:14:50 +01001561
1562 if (ret == FAIL || !evaluate)
1563 return ret;
1564
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001565 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1566 if (count > 1)
1567 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001568
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001569 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001570}
1571
1572/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573 * Compile "@r".
1574 */
1575 static int
1576compile_get_register(char_u **arg, cctx_T *cctx)
1577{
1578 int ret;
1579
1580 ++*arg;
1581 if (**arg == NUL)
1582 {
1583 semsg(_(e_syntax_error_at_str), *arg - 1);
1584 return FAIL;
1585 }
1586 if (!valid_yank_reg(**arg, FALSE))
1587 {
1588 emsg_invreg(**arg);
1589 return FAIL;
1590 }
1591 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1592 ++*arg;
1593 return ret;
1594}
1595
1596/*
1597 * Apply leading '!', '-' and '+' to constant "rettv".
1598 * When "numeric_only" is TRUE do not apply '!'.
1599 */
1600 static int
1601apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1602{
1603 char_u *p = *end;
1604
1605 // this works from end to start
1606 while (p > start)
1607 {
1608 --p;
1609 if (*p == '-' || *p == '+')
1610 {
1611 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001612 if (rettv->v_type == VAR_FLOAT)
1613 {
1614 if (*p == '-')
1615 rettv->vval.v_float = -rettv->vval.v_float;
1616 }
1617 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001618 {
1619 varnumber_T val;
1620 int error = FALSE;
1621
1622 // tv_get_number_chk() accepts a string, but we don't want that
1623 // here
1624 if (check_not_string(rettv) == FAIL)
1625 return FAIL;
1626 val = tv_get_number_chk(rettv, &error);
1627 clear_tv(rettv);
1628 if (error)
1629 return FAIL;
1630 if (*p == '-')
1631 val = -val;
1632 rettv->v_type = VAR_NUMBER;
1633 rettv->vval.v_number = val;
1634 }
1635 }
1636 else if (numeric_only)
1637 {
1638 ++p;
1639 break;
1640 }
1641 else if (*p == '!')
1642 {
1643 int v = tv2bool(rettv);
1644
1645 // '!' is permissive in the type.
1646 clear_tv(rettv);
1647 rettv->v_type = VAR_BOOL;
1648 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1649 }
1650 }
1651 *end = p;
1652 return OK;
1653}
1654
1655/*
1656 * Recognize v: variables that are constants and set "rettv".
1657 */
1658 static void
1659get_vim_constant(char_u **arg, typval_T *rettv)
1660{
1661 if (STRNCMP(*arg, "v:true", 6) == 0)
1662 {
1663 rettv->v_type = VAR_BOOL;
1664 rettv->vval.v_number = VVAL_TRUE;
1665 *arg += 6;
1666 }
1667 else if (STRNCMP(*arg, "v:false", 7) == 0)
1668 {
1669 rettv->v_type = VAR_BOOL;
1670 rettv->vval.v_number = VVAL_FALSE;
1671 *arg += 7;
1672 }
1673 else if (STRNCMP(*arg, "v:null", 6) == 0)
1674 {
1675 rettv->v_type = VAR_SPECIAL;
1676 rettv->vval.v_number = VVAL_NULL;
1677 *arg += 6;
1678 }
1679 else if (STRNCMP(*arg, "v:none", 6) == 0)
1680 {
1681 rettv->v_type = VAR_SPECIAL;
1682 rettv->vval.v_number = VVAL_NONE;
1683 *arg += 6;
1684 }
1685}
1686
1687 exprtype_T
1688get_compare_type(char_u *p, int *len, int *type_is)
1689{
1690 exprtype_T type = EXPR_UNKNOWN;
1691 int i;
1692
1693 switch (p[0])
1694 {
1695 case '=': if (p[1] == '=')
1696 type = EXPR_EQUAL;
1697 else if (p[1] == '~')
1698 type = EXPR_MATCH;
1699 break;
1700 case '!': if (p[1] == '=')
1701 type = EXPR_NEQUAL;
1702 else if (p[1] == '~')
1703 type = EXPR_NOMATCH;
1704 break;
1705 case '>': if (p[1] != '=')
1706 {
1707 type = EXPR_GREATER;
1708 *len = 1;
1709 }
1710 else
1711 type = EXPR_GEQUAL;
1712 break;
1713 case '<': if (p[1] != '=')
1714 {
1715 type = EXPR_SMALLER;
1716 *len = 1;
1717 }
1718 else
1719 type = EXPR_SEQUAL;
1720 break;
1721 case 'i': if (p[1] == 's')
1722 {
1723 // "is" and "isnot"; but not a prefix of a name
1724 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1725 *len = 5;
1726 i = p[*len];
1727 if (!isalnum(i) && i != '_')
1728 {
1729 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1730 *type_is = TRUE;
1731 }
1732 }
1733 break;
1734 }
1735 return type;
1736}
1737
1738/*
1739 * Skip over an expression, ignoring most errors.
1740 */
1741 void
1742skip_expr_cctx(char_u **arg, cctx_T *cctx)
1743{
1744 evalarg_T evalarg;
1745
1746 init_evalarg(&evalarg);
1747 evalarg.eval_cctx = cctx;
1748 skip_expr(arg, &evalarg);
1749 clear_evalarg(&evalarg, NULL);
1750}
1751
1752/*
1753 * Check that the top of the type stack has a type that can be used as a
1754 * condition. Give an error and return FAIL if not.
1755 */
1756 int
1757bool_on_stack(cctx_T *cctx)
1758{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001759 type_T *type;
1760
Bram Moolenaar078a4612022-01-04 15:17:03 +00001761 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762 if (type == &t_bool)
1763 return OK;
1764
Bram Moolenaar4913d422022-10-17 13:13:32 +01001765 if (type->tt_type == VAR_ANY
1766 || type->tt_type == VAR_UNKNOWN
1767 || type->tt_type == VAR_NUMBER
1768 || type == &t_number_bool
1769 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001770 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1771 // This requires a runtime type check.
1772 return generate_COND2BOOL(cctx);
1773
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001774 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775}
1776
1777/*
1778 * Give the "white on both sides" error, taking the operator from "p[len]".
1779 */
1780 void
1781error_white_both(char_u *op, int len)
1782{
1783 char_u buf[10];
1784
1785 vim_strncpy(buf, op, len);
1786 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1787}
1788
1789/*
1790 * Compile code to apply '-', '+' and '!'.
1791 * When "numeric_only" is TRUE do not apply '!'.
1792 */
1793 static int
1794compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1795{
1796 char_u *p = *end;
1797
1798 // this works from end to start
1799 while (p > start)
1800 {
1801 --p;
1802 while (VIM_ISWHITE(*p))
1803 --p;
1804 if (*p == '-' || *p == '+')
1805 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001806 type_T *type = get_type_on_stack(cctx, 0);
1807 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001808 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001809 return FAIL;
1810
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001811 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001812 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1813 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001814 }
1815 else if (numeric_only)
1816 {
1817 ++p;
1818 break;
1819 }
1820 else
1821 {
1822 int invert = *p == '!';
1823
1824 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1825 {
1826 if (p[-1] == '!')
1827 invert = !invert;
1828 --p;
1829 }
1830 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1831 return FAIL;
1832 }
1833 }
1834 *end = p;
1835 return OK;
1836}
1837
1838/*
1839 * Compile "(expression)": recursive!
1840 * Return FAIL/OK.
1841 */
1842 static int
1843compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1844{
1845 int ret;
1846 char_u *p = *arg + 1;
1847
1848 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1849 return FAIL;
1850 if (ppconst->pp_used <= PPSIZE - 10)
1851 {
1852 ret = compile_expr1(arg, cctx, ppconst);
1853 }
1854 else
1855 {
1856 // Not enough space in ppconst, flush constants.
1857 if (generate_ppconst(cctx, ppconst) == FAIL)
1858 return FAIL;
1859 ret = compile_expr0(arg, cctx);
1860 }
1861 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1862 return FAIL;
1863 if (**arg == ')')
1864 ++*arg;
1865 else if (ret == OK)
1866 {
1867 emsg(_(e_missing_closing_paren));
1868 ret = FAIL;
1869 }
1870 return ret;
1871}
1872
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001873static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001874
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875/*
1876 * Compile whatever comes after "name" or "name()".
1877 * Advances "*arg" only when something was recognized.
1878 */
1879 static int
1880compile_subscript(
1881 char_u **arg,
1882 cctx_T *cctx,
1883 char_u *start_leader,
1884 char_u **end_leader,
1885 ppconst_T *ppconst)
1886{
1887 char_u *name_start = *end_leader;
1888 int keeping_dict = FALSE;
1889
1890 for (;;)
1891 {
1892 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001893 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001894
1895 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1896 {
1897 char_u *next = peek_next_line_from_context(cctx);
1898
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001899 // If a following line starts with "->{", "->(" or "->X" advance to
1900 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001901 // Also if a following line starts with ".x".
1902 if (next != NULL &&
1903 ((next[0] == '-' && next[1] == '>'
1904 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001905 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001906 || ASCII_ISALPHA(*skipwhite(next + 2))))
1907 || (next[0] == '.' && eval_isdictc(next[1]))))
1908 {
1909 next = next_line_from_context(cctx, TRUE);
1910 if (next == NULL)
1911 return FAIL;
1912 *arg = next;
1913 p = skipwhite(*arg);
1914 }
1915 }
1916
1917 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1918 // is not a function call.
1919 if (**arg == '(')
1920 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001921 int argcount = 0;
1922
1923 if (generate_ppconst(cctx, ppconst) == FAIL)
1924 return FAIL;
1925 ppconst->pp_is_const = FALSE;
1926
1927 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001928 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001929
1930 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001931 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001932 return FAIL;
1933 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1934 return FAIL;
1935 if (keeping_dict)
1936 {
1937 keeping_dict = FALSE;
1938 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1939 return FAIL;
1940 }
1941 }
1942 else if (*p == '-' && p[1] == '>')
1943 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001944 char_u *pstart = p;
1945 int alt;
1946 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001947
Bram Moolenaarc7349932022-01-16 20:59:39 +00001948 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001949 if (generate_ppconst(cctx, ppconst) == FAIL)
1950 return FAIL;
1951 ppconst->pp_is_const = FALSE;
1952
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001953 // Apply the '!', '-' and '+' first:
1954 // -1.0->func() works like (-1.0)->func()
1955 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1956 return FAIL;
1957
1958 p += 2;
1959 *arg = skipwhite(p);
1960 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001961
1962 // Three alternatives handled here:
1963 // 1. "base->name(" only a name, use compile_call()
1964 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1965 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001966 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001967 alt = 2;
1968 else
1969 {
1970 // alternative 1 or 3
1971 p = *arg;
1972 if (!eval_isnamec1(*p))
1973 {
1974 semsg(_(e_trailing_characters_str), pstart);
1975 return FAIL;
1976 }
1977 if (ASCII_ISALPHA(*p) && p[1] == ':')
1978 p += 2;
1979 for ( ; eval_isnamec(*p); ++p)
1980 ;
1981 if (*p == '(')
1982 {
1983 // alternative 1
1984 alt = 1;
1985 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1986 return FAIL;
1987 }
1988 else
1989 {
1990 // Must be alternative 3, find the "(". Only works within
1991 // one line.
1992 alt = 3;
1993 paren = vim_strchr(p, '(');
1994 if (paren == NULL)
1995 {
1996 semsg(_(e_missing_parenthesis_str), *arg);
1997 return FAIL;
1998 }
1999 }
2000 }
2001
2002 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 {
2004 int argcount = 1;
2005 garray_T *stack = &cctx->ctx_type_stack;
2006 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007 int expr_isn_start = cctx->ctx_instr.ga_len;
2008 int expr_isn_end;
2009 int arg_isn_count;
2010
Bram Moolenaarc7349932022-01-16 20:59:39 +00002011 if (alt == 2)
2012 {
2013 // Funcref call: list->(Refs[2])(arg)
2014 // or lambda: list->((arg) => expr)(arg)
2015 //
2016 // Fist compile the function expression.
2017 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2018 return FAIL;
2019 }
2020 else
2021 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002022 int fail;
2023 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002024 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002025
Bram Moolenaarc7349932022-01-16 20:59:39 +00002026 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002027
2028 // instead of using LOADG for "import.Func" use PUSHFUNC
2029 ++paren_follows_after_expr;
2030
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002031 // do not look in the next line
2032 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002033
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002034 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002035 || *skipwhite(*arg) != NUL;
2036 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002037 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002038 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002039
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002040 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002041 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002042 if (did_emsg == prev_did_emsg)
2043 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002044 return FAIL;
2045 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002046 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002048 // Compile the arguments.
2049 if (**arg != '(')
2050 {
2051 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002052 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002053 else
2054 semsg(_(e_missing_parenthesis_str), *arg);
2055 return FAIL;
2056 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002057
2058 // Remember the next instruction index, where the instructions
2059 // for arguments are being written.
2060 expr_isn_end = cctx->ctx_instr.ga_len;
2061
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002062 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002063 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2064 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 return FAIL;
2066
2067 // Move the instructions for the arguments to before the
2068 // instructions of the expression and move the type of the
2069 // expression after the argument types. This is what ISN_PCALL
2070 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002071 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2072 if (arg_isn_count > 0)
2073 {
2074 int expr_isn_count = expr_isn_end - expr_isn_start;
2075 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002076 type_T *decl_type;
2077 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002078
2079 if (isn == NULL)
2080 return FAIL;
2081 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2082 + expr_isn_start,
2083 sizeof(isn_T) * expr_isn_count);
2084 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2085 + expr_isn_start,
2086 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2087 sizeof(isn_T) * arg_isn_count);
2088 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2089 + expr_isn_start + arg_isn_count,
2090 isn, sizeof(isn_T) * expr_isn_count);
2091 vim_free(isn);
2092
Bram Moolenaar078a4612022-01-04 15:17:03 +00002093 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2094 type = typep->type_curr;
2095 decl_type = typep->type_decl;
2096 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2097 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2098 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002099 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002100 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2101 typep->type_curr = type;
2102 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002103 }
2104
Bram Moolenaar078a4612022-01-04 15:17:03 +00002105 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2107 return FAIL;
2108 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002109
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002110 if (keeping_dict)
2111 {
2112 keeping_dict = FALSE;
2113 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2114 return FAIL;
2115 }
2116 }
2117 else if (**arg == '[')
2118 {
2119 int is_slice = FALSE;
2120
2121 // list index: list[123]
2122 // dict member: dict[key]
2123 // string index: text[123]
2124 // blob index: blob[123]
2125 if (generate_ppconst(cctx, ppconst) == FAIL)
2126 return FAIL;
2127 ppconst->pp_is_const = FALSE;
2128
2129 ++p;
2130 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2131 return FAIL;
2132 if (**arg == ':')
2133 {
2134 // missing first index is equal to zero
2135 generate_PUSHNR(cctx, 0);
2136 }
2137 else
2138 {
2139 if (compile_expr0(arg, cctx) == FAIL)
2140 return FAIL;
2141 if (**arg == ':')
2142 {
2143 semsg(_(e_white_space_required_before_and_after_str_at_str),
2144 ":", *arg);
2145 return FAIL;
2146 }
2147 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2148 return FAIL;
2149 *arg = skipwhite(*arg);
2150 }
2151 if (**arg == ':')
2152 {
2153 is_slice = TRUE;
2154 ++*arg;
2155 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2156 {
2157 semsg(_(e_white_space_required_before_and_after_str_at_str),
2158 ":", *arg);
2159 return FAIL;
2160 }
2161 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2162 return FAIL;
2163 if (**arg == ']')
2164 // missing second index is equal to end of string
2165 generate_PUSHNR(cctx, -1);
2166 else
2167 {
2168 if (compile_expr0(arg, cctx) == FAIL)
2169 return FAIL;
2170 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2171 return FAIL;
2172 *arg = skipwhite(*arg);
2173 }
2174 }
2175
2176 if (**arg != ']')
2177 {
2178 emsg(_(e_missing_closing_square_brace));
2179 return FAIL;
2180 }
2181 *arg = *arg + 1;
2182
2183 if (keeping_dict)
2184 {
2185 keeping_dict = FALSE;
2186 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2187 return FAIL;
2188 }
2189 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2190 return FAIL;
2191 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002192 else if (*p == '.'
2193 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2194 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2195 {
2196 // class member: SomeClass.varname
2197 // class method: SomeClass.SomeMethod()
2198 // class constructor: SomeClass.new()
2199 // object member: someObject.varname, this.varname
2200 // object method: someObject.SomeMethod(), this.SomeMethod()
2201 if (compile_class_object_index(cctx, arg, type) == FAIL)
2202 return FAIL;
2203 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002204 else if (*p == '.' && p[1] != '.')
2205 {
2206 // dictionary member: dict.name
2207 if (generate_ppconst(cctx, ppconst) == FAIL)
2208 return FAIL;
2209 ppconst->pp_is_const = FALSE;
2210
2211 *arg = p + 1;
2212 if (IS_WHITE_OR_NUL(**arg))
2213 {
2214 emsg(_(e_missing_name_after_dot));
2215 return FAIL;
2216 }
2217 p = *arg;
2218 if (eval_isdictc(*p))
2219 while (eval_isnamec(*p))
2220 MB_PTR_ADV(p);
2221 if (p == *arg)
2222 {
2223 semsg(_(e_syntax_error_at_str), *arg);
2224 return FAIL;
2225 }
2226 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2227 return FAIL;
2228 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2229 return FAIL;
2230 keeping_dict = TRUE;
2231 *arg = p;
2232 }
2233 else
2234 break;
2235 }
2236
2237 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2238 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002239 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2240 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241 return FAIL;
2242
2243 return OK;
2244}
2245
2246/*
2247 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2248 * "arg" is advanced until after the expression, skipping white space.
2249 *
2250 * If the value is a constant "ppconst->pp_used" will be non-zero.
2251 * Before instructions are generated, any values in "ppconst" will generated.
2252 *
2253 * This is the compiling equivalent of eval1(), eval2(), etc.
2254 */
2255
2256/*
2257 * number number constant
2258 * 0zFFFFFFFF Blob constant
2259 * "string" string constant
2260 * 'string' literal string constant
2261 * &option-name option value
2262 * @r register contents
2263 * identifier variable value
2264 * function() function call
2265 * $VAR environment variable
2266 * (expression) nested expression
2267 * [expr, expr] List
2268 * {key: val, [key]: val} Dictionary
2269 *
2270 * Also handle:
2271 * ! in front logical NOT
2272 * - in front unary minus
2273 * + in front unary plus (ignored)
2274 * trailing (arg) funcref/partial call
2275 * trailing [] subscript in String or List
2276 * trailing .name entry in Dictionary
2277 * trailing ->name() method call
2278 */
2279 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002280compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002281 char_u **arg,
2282 cctx_T *cctx,
2283 ppconst_T *ppconst)
2284{
2285 char_u *start_leader, *end_leader;
2286 int ret = OK;
2287 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2288 int used_before = ppconst->pp_used;
2289
2290 ppconst->pp_is_const = FALSE;
2291
2292 /*
2293 * Skip '!', '-' and '+' characters. They are handled later.
2294 */
2295 start_leader = *arg;
2296 if (eval_leader(arg, TRUE) == FAIL)
2297 return FAIL;
2298 end_leader = *arg;
2299
2300 rettv->v_type = VAR_UNKNOWN;
2301 switch (**arg)
2302 {
2303 /*
2304 * Number constant.
2305 */
2306 case '0': // also for blob starting with 0z
2307 case '1':
2308 case '2':
2309 case '3':
2310 case '4':
2311 case '5':
2312 case '6':
2313 case '7':
2314 case '8':
2315 case '9':
2316 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2317 return FAIL;
2318 // Apply "-" and "+" just before the number now, right to
2319 // left. Matters especially when "->" follows. Stops at
2320 // '!'.
2321 if (apply_leader(rettv, TRUE,
2322 start_leader, &end_leader) == FAIL)
2323 {
2324 clear_tv(rettv);
2325 return FAIL;
2326 }
2327 break;
2328
2329 /*
2330 * String constant: "string".
2331 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002332 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002333 return FAIL;
2334 break;
2335
2336 /*
2337 * Literal string constant: 'str''ing'.
2338 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002339 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002340 return FAIL;
2341 break;
2342
2343 /*
2344 * Constant Vim variable.
2345 */
2346 case 'v': get_vim_constant(arg, rettv);
2347 ret = NOTDONE;
2348 break;
2349
2350 /*
2351 * "true" constant
2352 */
2353 case 't': if (STRNCMP(*arg, "true", 4) == 0
2354 && !eval_isnamec((*arg)[4]))
2355 {
2356 *arg += 4;
2357 rettv->v_type = VAR_BOOL;
2358 rettv->vval.v_number = VVAL_TRUE;
2359 }
2360 else
2361 ret = NOTDONE;
2362 break;
2363
2364 /*
2365 * "false" constant
2366 */
2367 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2368 && !eval_isnamec((*arg)[5]))
2369 {
2370 *arg += 5;
2371 rettv->v_type = VAR_BOOL;
2372 rettv->vval.v_number = VVAL_FALSE;
2373 }
2374 else
2375 ret = NOTDONE;
2376 break;
2377
2378 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002379 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002380 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002381 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002383 char_u *p = *arg + 4;
2384 int len;
2385
2386 for (len = 0; eval_isnamec(p[len]); ++len)
2387 ;
2388 ret = handle_predefined(*arg, len + 4, rettv);
2389 if (ret == FAIL)
2390 ret = NOTDONE;
2391 else
2392 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 }
2394 else
2395 ret = NOTDONE;
2396 break;
2397
2398 /*
2399 * List: [expr, expr]
2400 */
2401 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2402 return FAIL;
2403 ret = compile_list(arg, cctx, ppconst);
2404 break;
2405
2406 /*
2407 * Dictionary: {'key': val, 'key': val}
2408 */
2409 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2410 return FAIL;
2411 ret = compile_dict(arg, cctx, ppconst);
2412 break;
2413
2414 /*
2415 * Option value: &name
2416 */
2417 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2418 return FAIL;
2419 ret = compile_get_option(arg, cctx);
2420 break;
2421
2422 /*
2423 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002424 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002425 */
2426 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2427 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002428 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2429 ret = compile_interp_string(arg, cctx);
2430 else
2431 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 break;
2433
2434 /*
2435 * Register contents: @r.
2436 */
2437 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2438 return FAIL;
2439 ret = compile_get_register(arg, cctx);
2440 break;
2441 /*
2442 * nested expression: (expression).
2443 * lambda: (arg, arg) => expr
2444 * funcref: (arg, arg) => { statement }
2445 */
2446 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2447 ret = compile_lambda(arg, cctx);
2448 if (ret == NOTDONE)
2449 ret = compile_parenthesis(arg, cctx, ppconst);
2450 break;
2451
2452 default: ret = NOTDONE;
2453 break;
2454 }
2455 if (ret == FAIL)
2456 return FAIL;
2457
2458 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2459 {
2460 if (cctx->ctx_skip == SKIP_YES)
2461 clear_tv(rettv);
2462 else
2463 // A constant expression can possibly be handled compile time,
2464 // return the value instead of generating code.
2465 ++ppconst->pp_used;
2466 }
2467 else if (ret == NOTDONE)
2468 {
2469 char_u *p;
2470 int r;
2471
2472 if (!eval_isnamec1(**arg))
2473 {
2474 if (!vim9_bad_comment(*arg))
2475 {
2476 if (ends_excmd(*skipwhite(*arg)))
2477 semsg(_(e_empty_expression_str), *arg);
2478 else
2479 semsg(_(e_name_expected_str), *arg);
2480 }
2481 return FAIL;
2482 }
2483
2484 // "name" or "name()"
2485 p = to_name_end(*arg, TRUE);
2486 if (p - *arg == (size_t)1 && **arg == '_')
2487 {
2488 emsg(_(e_cannot_use_underscore_here));
2489 return FAIL;
2490 }
2491
2492 if (*p == '(')
2493 {
2494 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2495 }
2496 else
2497 {
2498 if (cctx->ctx_skip != SKIP_YES
2499 && generate_ppconst(cctx, ppconst) == FAIL)
2500 return FAIL;
2501 r = compile_load(arg, p, cctx, TRUE, TRUE);
2502 }
2503 if (r == FAIL)
2504 return FAIL;
2505 }
2506
2507 // Handle following "[]", ".member", etc.
2508 // Then deal with prefixed '-', '+' and '!', if not done already.
2509 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2510 ppconst) == FAIL)
2511 return FAIL;
2512 if (ppconst->pp_used > 0)
2513 {
2514 // apply the '!', '-' and '+' before the constant
2515 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2516 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2517 return FAIL;
2518 return OK;
2519 }
2520 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2521 return FAIL;
2522 return OK;
2523}
2524
2525/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002526 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002527 */
2528 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002529compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002530{
2531 type_T *want_type = NULL;
2532
2533 // Recognize <type>
2534 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2535 {
2536 ++*arg;
2537 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2538 if (want_type == NULL)
2539 return FAIL;
2540
2541 if (**arg != '>')
2542 {
2543 if (*skipwhite(*arg) == '>')
2544 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2545 else
2546 emsg(_(e_missing_gt));
2547 return FAIL;
2548 }
2549 ++*arg;
2550 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2551 return FAIL;
2552 }
2553
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002554 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002555 return FAIL;
2556
2557 if (want_type != NULL)
2558 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559 type_T *actual;
2560 where_T where = WHERE_INIT;
2561
2562 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002563 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002564 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002565 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002566 if (need_type(actual, want_type, FALSE,
2567 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002568 return FAIL;
2569 }
2570 }
2571
2572 return OK;
2573}
2574
2575/*
2576 * * number multiplication
2577 * / number division
2578 * % number modulo
2579 */
2580 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002581compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002582{
2583 char_u *op;
2584 char_u *next;
2585 int ppconst_used = ppconst->pp_used;
2586
2587 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002588 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002589 return FAIL;
2590
2591 /*
2592 * Repeat computing, until no "*", "/" or "%" is following.
2593 */
2594 for (;;)
2595 {
2596 op = may_peek_next_line(cctx, *arg, &next);
2597 if (*op != '*' && *op != '/' && *op != '%')
2598 break;
2599 if (next != NULL)
2600 {
2601 *arg = next_line_from_context(cctx, TRUE);
2602 op = skipwhite(*arg);
2603 }
2604
2605 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2606 {
2607 error_white_both(op, 1);
2608 return FAIL;
2609 }
2610 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2611 return FAIL;
2612
2613 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002614 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615 return FAIL;
2616
2617 if (ppconst->pp_used == ppconst_used + 2
2618 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2619 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2620 {
2621 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2622 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2623 varnumber_T res = 0;
2624 int failed = FALSE;
2625
2626 // both are numbers: compute the result
2627 switch (*op)
2628 {
2629 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2630 break;
2631 case '/': res = num_divide(tv1->vval.v_number,
2632 tv2->vval.v_number, &failed);
2633 break;
2634 case '%': res = num_modulus(tv1->vval.v_number,
2635 tv2->vval.v_number, &failed);
2636 break;
2637 }
2638 if (failed)
2639 return FAIL;
2640 tv1->vval.v_number = res;
2641 --ppconst->pp_used;
2642 }
2643 else
2644 {
2645 generate_ppconst(cctx, ppconst);
2646 generate_two_op(cctx, op);
2647 }
2648 }
2649
2650 return OK;
2651}
2652
2653/*
2654 * + number addition or list/blobl concatenation
2655 * - number subtraction
2656 * .. string concatenation
2657 */
2658 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002659compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002660{
2661 char_u *op;
2662 char_u *next;
2663 int oplen;
2664 int ppconst_used = ppconst->pp_used;
2665
2666 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002667 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002668 return FAIL;
2669
2670 /*
2671 * Repeat computing, until no "+", "-" or ".." is following.
2672 */
2673 for (;;)
2674 {
2675 op = may_peek_next_line(cctx, *arg, &next);
2676 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2677 break;
2678 if (op[0] == op[1] && *op != '.' && next)
2679 // Finding "++" or "--" on the next line is a separate command.
2680 // But ".." is concatenation.
2681 break;
2682 oplen = (*op == '.' ? 2 : 1);
2683 if (next != NULL)
2684 {
2685 *arg = next_line_from_context(cctx, TRUE);
2686 op = skipwhite(*arg);
2687 }
2688
2689 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2690 {
2691 error_white_both(op, oplen);
2692 return FAIL;
2693 }
2694
2695 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2696 return FAIL;
2697
2698 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002699 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002700 return FAIL;
2701
2702 if (ppconst->pp_used == ppconst_used + 2
2703 && (*op == '.'
2704 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2705 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2706 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2707 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2708 {
2709 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2710 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2711
2712 // concat/subtract/add constant numbers
2713 if (*op == '+')
2714 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2715 else if (*op == '-')
2716 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2717 else
2718 {
2719 // concatenate constant strings
2720 char_u *s1 = tv1->vval.v_string;
2721 char_u *s2 = tv2->vval.v_string;
2722 size_t len1 = STRLEN(s1);
2723
2724 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2725 if (tv1->vval.v_string == NULL)
2726 {
2727 clear_ppconst(ppconst);
2728 return FAIL;
2729 }
2730 mch_memmove(tv1->vval.v_string, s1, len1);
2731 STRCPY(tv1->vval.v_string + len1, s2);
2732 vim_free(s1);
2733 vim_free(s2);
2734 }
2735 --ppconst->pp_used;
2736 }
2737 else
2738 {
2739 generate_ppconst(cctx, ppconst);
2740 ppconst->pp_is_const = FALSE;
2741 if (*op == '.')
2742 {
2743 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2744 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2745 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002746 if (generate_CONCAT(cctx, 2) == FAIL)
2747 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002748 }
2749 else
2750 generate_two_op(cctx, op);
2751 }
2752 }
2753
2754 return OK;
2755}
2756
2757/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002758 * expr6a >> expr6b
2759 * expr6a << expr6b
2760 *
2761 * Produces instructions:
2762 * OPNR bitwise left or right shift
2763 */
2764 static int
2765compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2766{
2767 exprtype_T type = EXPR_UNKNOWN;
2768 char_u *p;
2769 char_u *next;
2770 int len = 2;
2771 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002772 isn_T *isn;
2773
2774 // get the first variable
2775 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2776 return FAIL;
2777
2778 /*
2779 * Repeat computing, until no "+", "-" or ".." is following.
2780 */
2781 for (;;)
2782 {
2783 type = EXPR_UNKNOWN;
2784
2785 p = may_peek_next_line(cctx, *arg, &next);
2786 if (p[0] == '<' && p[1] == '<')
2787 type = EXPR_LSHIFT;
2788 else if (p[0] == '>' && p[1] == '>')
2789 type = EXPR_RSHIFT;
2790
2791 if (type == EXPR_UNKNOWN)
2792 return OK;
2793
2794 // Handle a bitwise left or right shift operator
2795 if (ppconst->pp_used == ppconst_used + 1)
2796 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002797 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002798 {
2799 // left operand should be a number
2800 emsg(_(e_bitshift_ops_must_be_number));
2801 return FAIL;
2802 }
2803 }
2804 else
2805 {
2806 type_T *t = get_type_on_stack(cctx, 0);
2807
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002808 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002809 {
2810 emsg(_(e_bitshift_ops_must_be_number));
2811 return FAIL;
2812 }
2813 }
2814
2815 if (next != NULL)
2816 {
2817 *arg = next_line_from_context(cctx, TRUE);
2818 p = skipwhite(*arg);
2819 }
2820
2821 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2822 {
2823 error_white_both(p, len);
2824 return FAIL;
2825 }
2826
2827 // get the second variable
2828 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2829 return FAIL;
2830
2831 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2832 return FAIL;
2833
2834 if (ppconst->pp_used == ppconst_used + 2)
2835 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002836 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2837 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2838
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002839 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002840 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2841 {
2842 // right operand should be a positive number
2843 if (tv2->v_type != VAR_NUMBER)
2844 emsg(_(e_bitshift_ops_must_be_number));
2845 else
dundargocc57b5bc2022-11-02 13:30:51 +00002846 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002847 return FAIL;
2848 }
2849
2850 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2851 tv1->vval.v_number = 0;
2852 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002853 tv1->vval.v_number =
2854 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002855 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002856 tv1->vval.v_number =
2857 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002858 clear_tv(tv2);
2859 --ppconst->pp_used;
2860 }
2861 else
2862 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002863 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2864 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002865 {
2866 emsg(_(e_bitshift_ops_must_be_number));
2867 return FAIL;
2868 }
2869
2870 generate_ppconst(cctx, ppconst);
2871
2872 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2873 if (isn == NULL)
2874 return FAIL;
2875
2876 if (isn != NULL)
2877 isn->isn_arg.op.op_type = type;
2878 }
2879 }
2880
2881 return OK;
2882}
2883
2884/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002885 * expr5a == expr5b
2886 * expr5a =~ expr5b
2887 * expr5a != expr5b
2888 * expr5a !~ expr5b
2889 * expr5a > expr5b
2890 * expr5a >= expr5b
2891 * expr5a < expr5b
2892 * expr5a <= expr5b
2893 * expr5a is expr5b
2894 * expr5a isnot expr5b
2895 *
2896 * Produces instructions:
2897 * EVAL expr5a Push result of "expr5a"
2898 * EVAL expr5b Push result of "expr5b"
2899 * COMPARE one of the compare instructions
2900 */
2901 static int
2902compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2903{
2904 exprtype_T type = EXPR_UNKNOWN;
2905 char_u *p;
2906 char_u *next;
2907 int len = 2;
2908 int type_is = FALSE;
2909 int ppconst_used = ppconst->pp_used;
2910
2911 // get the first variable
2912 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2913 return FAIL;
2914
2915 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002916
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002917 type = get_compare_type(p, &len, &type_is);
2918
2919 /*
2920 * If there is a comparative operator, use it.
2921 */
2922 if (type != EXPR_UNKNOWN)
2923 {
2924 int ic = FALSE; // Default: do not ignore case
2925
2926 if (next != NULL)
2927 {
2928 *arg = next_line_from_context(cctx, TRUE);
2929 p = skipwhite(*arg);
2930 }
2931 if (type_is && (p[len] == '?' || p[len] == '#'))
2932 {
2933 semsg(_(e_invalid_expression_str), *arg);
2934 return FAIL;
2935 }
2936 // extra question mark appended: ignore case
2937 if (p[len] == '?')
2938 {
2939 ic = TRUE;
2940 ++len;
2941 }
2942 // extra '#' appended: match case (ignored)
2943 else if (p[len] == '#')
2944 ++len;
2945 // nothing appended: match case
2946
2947 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2948 {
2949 error_white_both(p, len);
2950 return FAIL;
2951 }
2952
2953 // get the second variable
2954 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2955 return FAIL;
2956
2957 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2958 return FAIL;
2959
2960 if (ppconst->pp_used == ppconst_used + 2)
2961 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002962 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002963 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2964 int ret;
2965
2966 // Both sides are a constant, compute the result now.
2967 // First check for a valid combination of types, this is more
2968 // strict than typval_compare().
2969 if (check_compare_types(type, tv1, tv2) == FAIL)
2970 ret = FAIL;
2971 else
2972 {
2973 ret = typval_compare(tv1, tv2, type, ic);
2974 tv1->v_type = VAR_BOOL;
2975 tv1->vval.v_number = tv1->vval.v_number
2976 ? VVAL_TRUE : VVAL_FALSE;
2977 clear_tv(tv2);
2978 --ppconst->pp_used;
2979 }
2980 return ret;
2981 }
2982
2983 generate_ppconst(cctx, ppconst);
2984 return generate_COMPARE(cctx, type, ic);
2985 }
2986
2987 return OK;
2988}
2989
2990static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2991
2992/*
2993 * Compile || or &&.
2994 */
2995 static int
2996compile_and_or(
2997 char_u **arg,
2998 cctx_T *cctx,
2999 char *op,
3000 ppconst_T *ppconst,
3001 int ppconst_used UNUSED)
3002{
3003 char_u *next;
3004 char_u *p = may_peek_next_line(cctx, *arg, &next);
3005 int opchar = *op;
3006
3007 if (p[0] == opchar && p[1] == opchar)
3008 {
3009 garray_T *instr = &cctx->ctx_instr;
3010 garray_T end_ga;
3011 int save_skip = cctx->ctx_skip;
3012
3013 /*
3014 * Repeat until there is no following "||" or "&&"
3015 */
3016 ga_init2(&end_ga, sizeof(int), 10);
3017 while (p[0] == opchar && p[1] == opchar)
3018 {
3019 long start_lnum = SOURCING_LNUM;
3020 long save_sourcing_lnum;
3021 int start_ctx_lnum = cctx->ctx_lnum;
3022 int save_lnum;
3023 int const_used;
3024 int status;
3025 jumpwhen_T jump_when = opchar == '|'
3026 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3027
3028 if (next != NULL)
3029 {
3030 *arg = next_line_from_context(cctx, TRUE);
3031 p = skipwhite(*arg);
3032 }
3033
3034 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3035 {
3036 semsg(_(e_white_space_required_before_and_after_str_at_str),
3037 op, p);
3038 ga_clear(&end_ga);
3039 return FAIL;
3040 }
3041
3042 save_sourcing_lnum = SOURCING_LNUM;
3043 SOURCING_LNUM = start_lnum;
3044 save_lnum = cctx->ctx_lnum;
3045 cctx->ctx_lnum = start_ctx_lnum;
3046
3047 status = check_ppconst_bool(ppconst);
3048 if (status != FAIL)
3049 {
3050 // Use the last ppconst if possible.
3051 if (ppconst->pp_used > 0)
3052 {
3053 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3054 int is_true = tv2bool(tv);
3055
3056 if ((is_true && opchar == '|')
3057 || (!is_true && opchar == '&'))
3058 {
3059 // For "false && expr" and "true || expr" the "expr"
3060 // does not need to be evaluated.
3061 cctx->ctx_skip = SKIP_YES;
3062 clear_tv(tv);
3063 tv->v_type = VAR_BOOL;
3064 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3065 }
3066 else
3067 {
3068 // For "true && expr" and "false || expr" only "expr"
3069 // needs to be evaluated.
3070 --ppconst->pp_used;
3071 jump_when = JUMP_NEVER;
3072 }
3073 }
3074 else
3075 {
3076 // Every part must evaluate to a bool.
3077 status = bool_on_stack(cctx);
3078 }
3079 }
3080 if (status != FAIL)
3081 status = ga_grow(&end_ga, 1);
3082 cctx->ctx_lnum = save_lnum;
3083 if (status == FAIL)
3084 {
3085 ga_clear(&end_ga);
3086 return FAIL;
3087 }
3088
3089 if (jump_when != JUMP_NEVER)
3090 {
3091 if (cctx->ctx_skip != SKIP_YES)
3092 {
3093 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3094 ++end_ga.ga_len;
3095 }
3096 generate_JUMP(cctx, jump_when, 0);
3097 }
3098
3099 // eval the next expression
3100 SOURCING_LNUM = save_sourcing_lnum;
3101 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3102 {
3103 ga_clear(&end_ga);
3104 return FAIL;
3105 }
3106
3107 const_used = ppconst->pp_used;
3108 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3109 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3110 {
3111 ga_clear(&end_ga);
3112 return FAIL;
3113 }
3114
3115 // "0 || 1" results in true, "1 && 0" results in false.
3116 if (ppconst->pp_used == const_used + 1)
3117 {
3118 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3119
3120 if (tv->v_type == VAR_NUMBER
3121 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3122 {
3123 tv->vval.v_number = tv->vval.v_number == 1
3124 ? VVAL_TRUE : VVAL_FALSE;
3125 tv->v_type = VAR_BOOL;
3126 }
3127 }
3128
3129 p = may_peek_next_line(cctx, *arg, &next);
3130 }
3131
3132 if (check_ppconst_bool(ppconst) == FAIL)
3133 {
3134 ga_clear(&end_ga);
3135 return FAIL;
3136 }
3137
3138 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3139 // Every part must evaluate to a bool.
3140 if (bool_on_stack(cctx) == FAIL)
3141 {
3142 ga_clear(&end_ga);
3143 return FAIL;
3144 }
3145
3146 if (end_ga.ga_len > 0)
3147 {
3148 // Fill in the end label in all jumps.
3149 generate_ppconst(cctx, ppconst);
3150 while (end_ga.ga_len > 0)
3151 {
3152 isn_T *isn;
3153
3154 --end_ga.ga_len;
3155 isn = ((isn_T *)instr->ga_data)
3156 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3157 isn->isn_arg.jump.jump_where = instr->ga_len;
3158 }
3159 }
3160 ga_clear(&end_ga);
3161
3162 cctx->ctx_skip = save_skip;
3163 }
3164
3165 return OK;
3166}
3167
3168/*
3169 * expr4a && expr4a && expr4a logical AND
3170 *
3171 * Produces instructions:
3172 * EVAL expr4a Push result of "expr4a"
3173 * COND2BOOL convert to bool if needed
3174 * JUMP_IF_COND_FALSE end
3175 * EVAL expr4b Push result of "expr4b"
3176 * JUMP_IF_COND_FALSE end
3177 * EVAL expr4c Push result of "expr4c"
3178 * end:
3179 */
3180 static int
3181compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3182{
3183 int ppconst_used = ppconst->pp_used;
3184
3185 // get the first variable
3186 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3187 return FAIL;
3188
3189 // || and && work almost the same
3190 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3191}
3192
3193/*
3194 * expr3a || expr3b || expr3c logical OR
3195 *
3196 * Produces instructions:
3197 * EVAL expr3a Push result of "expr3a"
3198 * COND2BOOL convert to bool if needed
3199 * JUMP_IF_COND_TRUE end
3200 * EVAL expr3b Push result of "expr3b"
3201 * JUMP_IF_COND_TRUE end
3202 * EVAL expr3c Push result of "expr3c"
3203 * end:
3204 */
3205 static int
3206compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3207{
3208 int ppconst_used = ppconst->pp_used;
3209
3210 // eval the first expression
3211 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3212 return FAIL;
3213
3214 // || and && work almost the same
3215 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3216}
3217
3218/*
3219 * Toplevel expression: expr2 ? expr1a : expr1b
3220 * Produces instructions:
3221 * EVAL expr2 Push result of "expr2"
3222 * JUMP_IF_FALSE alt jump if false
3223 * EVAL expr1a
3224 * JUMP_ALWAYS end
3225 * alt: EVAL expr1b
3226 * end:
3227 *
3228 * Toplevel expression: expr2 ?? expr1
3229 * Produces instructions:
3230 * EVAL expr2 Push result of "expr2"
3231 * JUMP_AND_KEEP_IF_TRUE end jump if true
3232 * EVAL expr1
3233 * end:
3234 */
3235 int
3236compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3237{
3238 char_u *p;
3239 int ppconst_used = ppconst->pp_used;
3240 char_u *next;
3241
3242 // Ignore all kinds of errors when not producing code.
3243 if (cctx->ctx_skip == SKIP_YES)
3244 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003245 int prev_did_emsg = did_emsg;
3246
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003247 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003248 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003249 }
3250
3251 // Evaluate the first expression.
3252 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3253 return FAIL;
3254
3255 p = may_peek_next_line(cctx, *arg, &next);
3256 if (*p == '?')
3257 {
3258 int op_falsy = p[1] == '?';
3259 garray_T *instr = &cctx->ctx_instr;
3260 garray_T *stack = &cctx->ctx_type_stack;
3261 int alt_idx = instr->ga_len;
3262 int end_idx = 0;
3263 isn_T *isn;
3264 type_T *type1 = NULL;
3265 int has_const_expr = FALSE;
3266 int const_value = FALSE;
3267 int save_skip = cctx->ctx_skip;
3268
3269 if (next != NULL)
3270 {
3271 *arg = next_line_from_context(cctx, TRUE);
3272 p = skipwhite(*arg);
3273 }
3274
3275 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3276 {
3277 semsg(_(e_white_space_required_before_and_after_str_at_str),
3278 op_falsy ? "??" : "?", p);
3279 return FAIL;
3280 }
3281
3282 if (ppconst->pp_used == ppconst_used + 1)
3283 {
3284 // the condition is a constant, we know whether the ? or the :
3285 // expression is to be evaluated.
3286 has_const_expr = TRUE;
3287 if (op_falsy)
3288 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3289 else
3290 {
3291 int error = FALSE;
3292
3293 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3294 &error);
3295 if (error)
3296 return FAIL;
3297 }
3298 cctx->ctx_skip = save_skip == SKIP_YES ||
3299 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3300
3301 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3302 // "left ?? right" and "left" is truthy: produce "left"
3303 generate_ppconst(cctx, ppconst);
3304 else
3305 {
3306 clear_tv(&ppconst->pp_tv[ppconst_used]);
3307 --ppconst->pp_used;
3308 }
3309 }
3310 else
3311 {
3312 generate_ppconst(cctx, ppconst);
3313 if (op_falsy)
3314 end_idx = instr->ga_len;
3315 generate_JUMP(cctx, op_falsy
3316 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3317 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003318 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003319 }
3320
3321 // evaluate the second expression; any type is accepted
3322 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3323 return FAIL;
3324 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3325 return FAIL;
3326
3327 if (!has_const_expr)
3328 {
3329 generate_ppconst(cctx, ppconst);
3330
3331 if (!op_falsy)
3332 {
3333 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003334 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003335 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003336
3337 end_idx = instr->ga_len;
3338 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3339
3340 // jump here from JUMP_IF_FALSE
3341 isn = ((isn_T *)instr->ga_data) + alt_idx;
3342 isn->isn_arg.jump.jump_where = instr->ga_len;
3343 }
3344 }
3345
3346 if (!op_falsy)
3347 {
3348 // Check for the ":".
3349 p = may_peek_next_line(cctx, *arg, &next);
3350 if (*p != ':')
3351 {
3352 emsg(_(e_missing_colon_after_questionmark));
3353 return FAIL;
3354 }
3355 if (next != NULL)
3356 {
3357 *arg = next_line_from_context(cctx, TRUE);
3358 p = skipwhite(*arg);
3359 }
3360
3361 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3362 {
3363 semsg(_(e_white_space_required_before_and_after_str_at_str),
3364 ":", p);
3365 return FAIL;
3366 }
3367
3368 // evaluate the third expression
3369 if (has_const_expr)
3370 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3371 ? SKIP_YES : SKIP_NOT;
3372 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3373 return FAIL;
3374 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3375 return FAIL;
3376 }
3377
3378 if (!has_const_expr)
3379 {
3380 type_T **typep;
3381
3382 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003383 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003384
3385 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003386 typep = &((((type2_T *)stack->ga_data)
3387 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003388 common_type(type1, *typep, typep, cctx->ctx_type_list);
3389
3390 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3391 isn = ((isn_T *)instr->ga_data) + end_idx;
3392 isn->isn_arg.jump.jump_where = instr->ga_len;
3393 }
3394
3395 cctx->ctx_skip = save_skip;
3396 }
3397 return OK;
3398}
3399
3400/*
3401 * Toplevel expression.
3402 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3403 * Returns OK or FAIL.
3404 */
3405 int
3406compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3407{
3408 ppconst_T ppconst;
3409
3410 CLEAR_FIELD(ppconst);
3411 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3412 {
3413 clear_ppconst(&ppconst);
3414 return FAIL;
3415 }
3416 if (is_const != NULL)
3417 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3418 if (generate_ppconst(cctx, &ppconst) == FAIL)
3419 return FAIL;
3420 return OK;
3421}
3422
3423/*
3424 * Toplevel expression.
3425 */
3426 int
3427compile_expr0(char_u **arg, cctx_T *cctx)
3428{
3429 return compile_expr0_ext(arg, cctx, NULL);
3430}
3431
3432
3433#endif // defined(FEAT_EVAL)