blob: daf014f84bf3825aae3b253f51d39575a9e1d512 [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 Moolenaar574950d2023-01-03 19:08:50 +0000276 int function_count;
277 ufunc_T **functions;
278
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000279 if (type->tt_type == VAR_CLASS)
280 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000281 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000282 if (instr->ga_len > 0)
283 {
284 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
285 if (isn->isn_type == ISN_LOADSCRIPT)
286 {
287 // The class was recognized as a script item. We only need
288 // to know what class it is, drop the instruction.
289 --instr->ga_len;
290 vim_free(isn->isn_arg.script.scriptref);
291 }
292 }
293
Bram Moolenaar574950d2023-01-03 19:08:50 +0000294 function_count = cl->class_class_function_count;
295 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000296 }
297 else
298 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000299 // type->tt_type == VAR_OBJECT: method call
300 function_count = cl->class_obj_method_count;
301 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000302 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000303
304 ufunc_T *ufunc = NULL;
305 for (int i = 0; i < function_count; ++i)
306 {
307 ufunc_T *fp = functions[i];
308 // Use a separate pointer to avoid that ASAN complains about
309 // uf_name[] only being 4 characters.
310 char_u *ufname = (char_u *)fp->uf_name;
311 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
312 {
313 ufunc = fp;
314 break;
315 }
316 }
317 if (ufunc == NULL)
318 {
319 // TODO: different error for object method?
320 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
321 return FAIL;
322 }
323
324 // Compile the arguments and call the class function or object method.
325 // The object method will know that the object is on the stack, just
326 // before the arguments.
327 *arg = skipwhite(name_end + 1);
328 int argcount = 0;
329 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
330 return FAIL;
331 return generate_CALL(cctx, ufunc, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000332 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000333
334 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000335 {
336 for (int i = 0; i < cl->class_obj_member_count; ++i)
337 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000338 ocmember_T *m = &cl->class_obj_members[i];
339 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000340 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000341 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
342 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000343 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000344 return FAIL;
345 }
346
Bram Moolenaard505d172022-12-18 21:42:55 +0000347 generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000348
349 *arg = name_end;
350 return OK;
351 }
352 }
353
354 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
355 }
356 else
357 {
358 // TODO: class member
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000359 emsg("compile_class_object_index(): class member not handled yet");
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000360 }
361
362 return FAIL;
363}
364
365/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366 * Generate an instruction to load script-local variable "name", without the
367 * leading "s:".
368 * Also finds imported variables.
369 */
370 int
371compile_load_scriptvar(
372 cctx_T *cctx,
373 char_u *name, // variable NUL terminated
374 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100375 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000376{
377 scriptitem_T *si;
378 int idx;
379 imported_T *import;
380
381 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
382 return FAIL;
383 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000384 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000385 if (idx >= 0)
386 {
387 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
388
389 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
390 current_sctx.sc_sid, idx, sv->sv_type);
391 return OK;
392 }
393
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000394 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000395 if (import != NULL)
396 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000397 char_u *p = skipwhite(*end);
398 char_u *exp_name;
399 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100400 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000401 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000402 int done = FALSE;
403 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000404
405 // Need to lookup the member.
406 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000407 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000408 semsg(_(e_expected_dot_after_name_str), start);
409 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000410 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000411 ++p;
412 if (VIM_ISWHITE(*p))
413 {
414 emsg(_(e_no_white_space_allowed_after_dot));
415 return FAIL;
416 }
417
418 // isolate one name
419 exp_name = p;
420 while (eval_isnamec(*p))
421 ++p;
422 cc = *p;
423 *p = NUL;
424
Bram Moolenaard041f422022-01-12 19:54:00 +0000425 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100426 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
427 // "import autoload './dir/script.vim'" or
428 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100429 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100430
431 if (res == OK)
432 {
433 if (si->sn_autoload_prefix != NULL
434 && si->sn_state == SN_STATE_NOT_LOADED)
435 {
436 char_u *auto_name =
437 concat_str(si->sn_autoload_prefix, exp_name);
438
439 // autoload script must be loaded later, access by the autoload
440 // name. If a '(' follows it must be a function. Otherwise we
441 // don't know, it can be "script.Func".
442 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100443 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100444 else
445 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
446 vim_free(auto_name);
447 done = TRUE;
448 }
449 else if (si->sn_import_autoload
450 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100451 {
452 // If a '(' follows it must be a function. Otherwise we don't
453 // know, it can be "script.Func".
454 if (cc == '(' || paren_follows_after_expr)
455 {
456 char_u sid_name[MAX_FUNC_NAME_LEN];
457
458 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100459 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100460 }
461 else
462 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
463 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100464 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100465 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100466 else
467 {
468 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
469 cctx, NULL, TRUE);
470 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100471 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100472
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000473 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000474 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000475 if (done)
476 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000477
478 if (idx < 0)
479 {
480 if (ufunc != NULL)
481 {
482 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100483 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000484 return OK;
485 }
486 return FAIL;
487 }
488
489 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
490 import->imp_sid,
491 idx,
492 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000493 return OK;
494 }
495
Bram Moolenaard0132f42022-05-12 11:05:40 +0100496 // Can only get here if we know "name" is a script variable and not in a
497 // Vim9 script (variable is not in sn_var_vals): old style script.
498 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000499 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500}
501
502 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000503generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000504{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000505 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000506 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000507
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000508 // Reject a global non-autoload function found without the "g:" prefix.
509 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000510 return FAIL;
511
512 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000513 compile_type = get_compile_type(ufunc);
514 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000515 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000516 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100517 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000518}
519
520/*
521 * Compile a variable name into a load instruction.
522 * "end" points to just after the name.
523 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
524 * When "error" is FALSE do not give an error when not found.
525 */
526 int
527compile_load(
528 char_u **arg,
529 char_u *end_arg,
530 cctx_T *cctx,
531 int is_expr,
532 int error)
533{
534 type_T *type;
535 char_u *name = NULL;
536 char_u *end = end_arg;
537 int res = FAIL;
538 int prev_called_emsg = called_emsg;
539
540 if (*(*arg + 1) == ':')
541 {
542 if (end <= *arg + 2)
543 {
544 isntype_T isn_type;
545
546 // load dictionary of namespace
547 switch (**arg)
548 {
549 case 'g': isn_type = ISN_LOADGDICT; break;
550 case 'w': isn_type = ISN_LOADWDICT; break;
551 case 't': isn_type = ISN_LOADTDICT; break;
552 case 'b': isn_type = ISN_LOADBDICT; break;
553 default:
554 semsg(_(e_namespace_not_supported_str), *arg);
555 goto theend;
556 }
557 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
558 goto theend;
559 res = OK;
560 }
561 else
562 {
563 isntype_T isn_type = ISN_DROP;
564
565 // load namespaced variable
566 name = vim_strnsave(*arg + 2, end - (*arg + 2));
567 if (name == NULL)
568 return FAIL;
569
570 switch (**arg)
571 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100572 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000574 case 's': if (current_script_is_vim9())
575 {
576 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
577 *arg);
578 vim_free(name);
579 return FAIL;
580 }
Kota Kato948a3892022-08-16 16:09:59 +0100581 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000582 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000583 else
584 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100585 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000586 break;
587 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
588 {
589 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000590 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000591 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000592 else
593 isn_type = ISN_LOADG;
594 }
595 else
596 {
597 isn_type = ISN_LOADAUTO;
598 vim_free(name);
599 name = vim_strnsave(*arg, end - *arg);
600 if (name == NULL)
601 return FAIL;
602 }
603 break;
604 case 'w': isn_type = ISN_LOADW; break;
605 case 't': isn_type = ISN_LOADT; break;
606 case 'b': isn_type = ISN_LOADB; break;
607 default: // cannot happen, just in case
608 semsg(_(e_namespace_not_supported_str), *arg);
609 goto theend;
610 }
611 if (isn_type != ISN_DROP)
612 {
613 // Global, Buffer-local, Window-local and Tabpage-local
614 // variables can be defined later, thus we don't check if it
615 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000616 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000617 }
618 }
619 }
620 else
621 {
622 size_t len = end - *arg;
623 int idx;
624 int gen_load = FALSE;
625 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100626 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100627 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000628
629 name = vim_strnsave(*arg, end - *arg);
630 if (name == NULL)
631 return FAIL;
632
633 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
634 {
635 script_autoload(name, FALSE);
636 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
637 }
638 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
639 == OK)
640 {
641 if (gen_load_outer == 0)
642 gen_load = TRUE;
643 }
644 else
645 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000646 lvar_T lvar;
647 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648
649 if (lookup_local(*arg, len, &lvar, cctx) == OK)
650 {
651 type = lvar.lv_type;
652 idx = lvar.lv_idx;
653 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100654 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000655 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100656 outer_loop_depth = lvar.lv_loop_depth;
657 outer_loop_idx = lvar.lv_loop_idx;
658 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659 else
660 gen_load = TRUE;
661 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000662 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000663 {
664 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
665 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666 else
667 {
668 // "var" can be script-local even without using "s:" if it
669 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000670 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000671 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100672 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000673
674 // When evaluating an expression and the name starts with an
675 // uppercase letter it can be a user defined function.
676 // generate_funcref() will fail if the function can't be found.
677 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000678 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000679 }
680 }
681 if (gen_load)
682 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
683 if (gen_load_outer > 0)
684 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100685 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
686 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 cctx->ctx_outer_used = TRUE;
688 }
689 }
690
691 *arg = end;
692
693theend:
694 if (res == FAIL && error && called_emsg == prev_called_emsg)
695 semsg(_(e_variable_not_found_str), name);
696 vim_free(name);
697 return res;
698}
699
700/*
701 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100702 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000703 * Returns FAIL if compilation fails.
704 */
705 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100706compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000707{
LemonBoyf3b48952022-05-05 13:53:03 +0100708 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000709 garray_T save_ga = cctx->ctx_instr;
710 int expr_res;
711 int trailing_error;
712 int instr_count;
713 isn_T *instr = NULL;
714
715 // Remove the string type from the stack.
716 --cctx->ctx_type_stack.ga_len;
717
718 // Temporarily reset the list of instructions so that the jump labels are
719 // correct.
720 cctx->ctx_instr.ga_len = 0;
721 cctx->ctx_instr.ga_maxlen = 0;
722 cctx->ctx_instr.ga_data = NULL;
723 expr_res = compile_expr0(&s, cctx);
724 s = skipwhite(s);
725 trailing_error = *s != NUL;
726
727 if (expr_res == FAIL || trailing_error
728 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
729 {
730 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000731 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000732 clear_instr_ga(&cctx->ctx_instr);
733 cctx->ctx_instr = save_ga;
734 ++cctx->ctx_type_stack.ga_len;
735 return FAIL;
736 }
737
738 // Move the generated instructions into the ISN_INSTR instruction, then
739 // restore the list of instructions.
740 instr_count = cctx->ctx_instr.ga_len;
741 instr = cctx->ctx_instr.ga_data;
742 instr[instr_count].isn_type = ISN_FINISH;
743
744 cctx->ctx_instr = save_ga;
745 vim_free(isn->isn_arg.string);
746 isn->isn_type = ISN_INSTR;
747 isn->isn_arg.instr = instr;
748 return OK;
749}
750
751/*
752 * Compile the argument expressions.
753 * "arg" points to just after the "(" and is advanced to after the ")"
754 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100755 int
LemonBoyf3b48952022-05-05 13:53:03 +0100756compile_arguments(
757 char_u **arg,
758 cctx_T *cctx,
759 int *argcount,
760 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761{
762 char_u *p = *arg;
763 char_u *whitep = *arg;
764 int must_end = FALSE;
765 int instr_count;
766
767 for (;;)
768 {
769 if (may_get_next_line(whitep, &p, cctx) == FAIL)
770 goto failret;
771 if (*p == ')')
772 {
773 *arg = p + 1;
774 return OK;
775 }
776 if (must_end)
777 {
778 semsg(_(e_missing_comma_before_argument_str), p);
779 return FAIL;
780 }
781
782 instr_count = cctx->ctx_instr.ga_len;
783 if (compile_expr0(&p, cctx) == FAIL)
784 return FAIL;
785 ++*argcount;
786
LemonBoyf3b48952022-05-05 13:53:03 +0100787 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 && cctx->ctx_instr.ga_len == instr_count + 1)
789 {
790 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
791
792 // {skip} argument of searchpair() can be compiled if not empty
793 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100794 compile_string(isn, cctx, 0);
795 }
796 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
797 && cctx->ctx_instr.ga_len == instr_count + 1)
798 {
799 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
800
801 // {sub} argument of substitute() can be compiled if it starts
802 // with \=
803 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100804 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100805 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 }
807
808 if (*p != ',' && *skipwhite(p) == ',')
809 {
810 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
811 p = skipwhite(p);
812 }
813 if (*p == ',')
814 {
815 ++p;
816 if (*p != NUL && !VIM_ISWHITE(*p))
817 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
818 }
819 else
820 must_end = TRUE;
821 whitep = p;
822 p = skipwhite(p);
823 }
824failret:
825 emsg(_(e_missing_closing_paren));
826 return FAIL;
827}
828
829/*
830 * Compile a function call: name(arg1, arg2)
831 * "arg" points to "name", "arg + varlen" to the "(".
832 * "argcount_init" is 1 for "value->method()"
833 * Instructions:
834 * EVAL arg1
835 * EVAL arg2
836 * BCALL / DCALL / UCALL
837 */
838 static int
839compile_call(
840 char_u **arg,
841 size_t varlen,
842 cctx_T *cctx,
843 ppconst_T *ppconst,
844 int argcount_init)
845{
846 char_u *name = *arg;
847 char_u *p;
848 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100849 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 char_u fname_buf[FLEN_FIXED + 1];
851 char_u *tofree = NULL;
852 int error = FCERR_NONE;
853 ufunc_T *ufunc = NULL;
854 int res = FAIL;
855 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000856 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100857 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000858 imported_T *import;
859
860 if (varlen >= sizeof(namebuf))
861 {
862 semsg(_(e_name_too_long_str), name);
863 return FAIL;
864 }
865 vim_strncpy(namebuf, *arg, varlen);
866
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000867 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000868 if (import != NULL)
869 {
870 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
871 return FAIL;
872 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873
874 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100875 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100877 if ((varlen == 3
878 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
880 {
881 char_u *s = skipwhite(*arg + varlen + 1);
882 typval_T argvars[2];
883 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100884 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885
886 argvars[0].v_type = VAR_UNKNOWN;
887 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100888 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100890 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891 s = skipwhite(s);
892 if (*s == ')' && argvars[0].v_type == VAR_STRING
893 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
894 || !is_has))
895 {
896 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
897
898 *arg = s + 1;
899 argvars[1].v_type = VAR_UNKNOWN;
900 tv->v_type = VAR_NUMBER;
901 tv->vval.v_number = 0;
902 if (is_has)
903 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100904 else if (is_len)
905 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906 else
907 f_exists(argvars, tv);
908 clear_tv(&argvars[0]);
909 ++ppconst->pp_used;
910 return OK;
911 }
912 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100913 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000914 {
915 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
916 return FAIL;
917 }
918 }
919
920 if (generate_ppconst(cctx, ppconst) == FAIL)
921 return FAIL;
922
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
924
925 // We handle the "skip" argument of searchpair() and searchpairpos()
926 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100927 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
928 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
929 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
930 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
931 special_fn = CA_SEARCHPAIR;
932 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
933 special_fn = CA_SUBSTITUTE;
934 else
935 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936
937 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100938 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000939 goto theend;
940
941 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
942 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
943 {
944 int idx;
945
946 // builtin function
947 idx = find_internal_func(name);
948 if (idx >= 0)
949 {
950 if (STRCMP(name, "flatten") == 0)
951 {
952 emsg(_(e_cannot_use_flatten_in_vim9_script));
953 goto theend;
954 }
955
956 if (STRCMP(name, "add") == 0 && argcount == 2)
957 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000958 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000959
960 // add() can be compiled to instructions if we know the type
961 if (type->tt_type == VAR_LIST)
962 {
963 // inline "add(list, item)" so that the type can be checked
964 res = generate_LISTAPPEND(cctx);
965 idx = -1;
966 }
967 else if (type->tt_type == VAR_BLOB)
968 {
969 // inline "add(blob, nr)" so that the type can be checked
970 res = generate_BLOBAPPEND(cctx);
971 idx = -1;
972 }
973 }
974
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100975 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
976 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100978 // May have the "D" or "R" flag, reserve a variable for a
979 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100980 if (get_defer_var_idx(cctx) == 0)
981 idx = -1;
982 }
983
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984 if (idx >= 0)
985 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
986 }
987 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100988 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000989 goto theend;
990 }
991
Bram Moolenaar62aec932022-01-29 21:45:34 +0000992 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
993
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000994 // An argument or local variable can be a function reference, this
995 // overrules a function name.
996 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
997 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
998 {
999 // If we can find the function by name generate the right call.
1000 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001001 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001002 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001003 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001004 if (!func_is_global(ufunc))
1005 {
1006 res = generate_CALL(cctx, ufunc, argcount);
1007 goto theend;
1008 }
1009 if (!has_g_namespace
1010 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1011 {
1012 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001013 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001014 goto theend;
1015 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001016 }
1017 }
1018
1019 // If the name is a variable, load it and use PCALL.
1020 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001021 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001022 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001023 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001024 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1025 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001026 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001027
1028 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1029 goto theend;
1030 }
1031
1032 // If we can find a global function by name generate the right call.
1033 if (ufunc != NULL)
1034 {
1035 res = generate_CALL(cctx, ufunc, argcount);
1036 goto theend;
1037 }
1038
1039 // A global function may be defined only later. Need to figure out at
1040 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001041 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001042 res = generate_UCALL(cctx, name, argcount);
1043 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001044 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001045
1046theend:
1047 vim_free(tofree);
1048 return res;
1049}
1050
1051// like NAMESPACE_CHAR but with 'a' and 'l'.
1052#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1053
1054/*
1055 * Find the end of a variable or function name. Unlike find_name_end() this
1056 * does not recognize magic braces.
1057 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1058 * Return a pointer to just after the name. Equal to "arg" if there is no
1059 * valid name.
1060 */
1061 char_u *
1062to_name_end(char_u *arg, int use_namespace)
1063{
1064 char_u *p;
1065
1066 // Quick check for valid starting character.
1067 if (!eval_isnamec1(*arg))
1068 return arg;
1069
1070 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1071 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1072 // and can be used in slice "[n:]".
1073 if (*p == ':' && (p != arg + 1
1074 || !use_namespace
1075 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1076 break;
1077 return p;
1078}
1079
1080/*
1081 * Like to_name_end() but also skip over a list or dict constant.
1082 * Also accept "<SNR>123_Func".
1083 * This intentionally does not handle line continuation.
1084 */
1085 char_u *
1086to_name_const_end(char_u *arg)
1087{
1088 char_u *p = arg;
1089 typval_T rettv;
1090
1091 if (STRNCMP(p, "<SNR>", 5) == 0)
1092 p = skipdigits(p + 5);
1093 p = to_name_end(p, TRUE);
1094 if (p == arg && *arg == '[')
1095 {
1096
1097 // Can be "[1, 2, 3]->Func()".
1098 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1099 p = arg;
1100 }
1101 return p;
1102}
1103
1104/*
1105 * parse a list: [expr, expr]
1106 * "*arg" points to the '['.
1107 * ppconst->pp_is_const is set if all items are a constant.
1108 */
1109 static int
1110compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1111{
1112 char_u *p = skipwhite(*arg + 1);
1113 char_u *whitep = *arg + 1;
1114 int count = 0;
1115 int is_const;
1116 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001117 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001118
1119 for (;;)
1120 {
1121 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1122 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001123 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124 return FAIL;
1125 }
1126 if (*p == ',')
1127 {
1128 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1129 return FAIL;
1130 }
1131 if (*p == ']')
1132 {
1133 ++p;
1134 break;
1135 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001136 if (must_end)
1137 {
1138 semsg(_(e_missing_comma_in_list_str), p);
1139 return FAIL;
1140 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1142 return FAIL;
1143 if (!is_const)
1144 is_all_const = FALSE;
1145 ++count;
1146 if (*p == ',')
1147 {
1148 ++p;
1149 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1150 {
1151 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1152 return FAIL;
1153 }
1154 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001155 else
1156 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157 whitep = p;
1158 p = skipwhite(p);
1159 }
1160 *arg = p;
1161
1162 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001163 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164}
1165
1166/*
1167 * Parse a lambda: "(arg, arg) => expr"
1168 * "*arg" points to the '('.
1169 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1170 */
1171 static int
1172compile_lambda(char_u **arg, cctx_T *cctx)
1173{
1174 int r;
1175 typval_T rettv;
1176 ufunc_T *ufunc;
1177 evalarg_T evalarg;
1178
1179 init_evalarg(&evalarg);
1180 evalarg.eval_flags = EVAL_EVALUATE;
1181 evalarg.eval_cctx = cctx;
1182
1183 // Get the funcref in "rettv".
1184 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1185 if (r != OK)
1186 {
1187 clear_evalarg(&evalarg, NULL);
1188 return r;
1189 }
1190
1191 // "rettv" will now be a partial referencing the function.
1192 ufunc = rettv.vval.v_partial->pt_func;
1193 ++ufunc->uf_refcount;
1194 clear_tv(&rettv);
1195
1196 // Compile it here to get the return type. The return type is optional,
1197 // when it's missing use t_unknown. This is recognized in
1198 // compile_return().
1199 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1200 ufunc->uf_ret_type = &t_unknown;
1201 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1202
1203 // When the outer function is compiled for profiling or debugging, the
1204 // lambda may be called without profiling or debugging. Compile it here in
1205 // the right context.
1206 if (cctx->ctx_compile_type == CT_DEBUG
1207#ifdef FEAT_PROFILE
1208 || cctx->ctx_compile_type == CT_PROFILE
1209#endif
1210 )
1211 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1212
Bram Moolenaar139575d2022-03-15 19:29:30 +00001213 // if the outer function is not compiled for debugging or profiling, this
1214 // one might be
1215 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001216 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001217 compiletype_T compile_type = get_compile_type(ufunc);
1218
1219 if (compile_type != CT_NONE)
1220 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001221 }
1222
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1224 // "*arg" may point into it. Point into the original line to avoid a
1225 // dangling pointer.
1226 if (evalarg.eval_using_cmdline)
1227 {
1228 garray_T *gap = &evalarg.eval_tofree_ga;
1229 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1230
1231 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1232 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001233 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234 }
1235
1236 clear_evalarg(&evalarg, NULL);
1237
1238 if (ufunc->uf_def_status == UF_COMPILED)
1239 {
1240 // The return type will now be known.
1241 set_function_type(ufunc);
1242
1243 // The function reference count will be 1. When the ISN_FUNCREF
1244 // instruction is deleted the reference count is decremented and the
1245 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001246 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247 }
1248
1249 func_ptr_unref(ufunc);
1250 return FAIL;
1251}
1252
1253/*
1254 * Get a lambda and compile it. Uses Vim9 syntax.
1255 */
1256 int
1257get_lambda_tv_and_compile(
1258 char_u **arg,
1259 typval_T *rettv,
1260 int types_optional,
1261 evalarg_T *evalarg)
1262{
1263 int r;
1264 ufunc_T *ufunc;
1265 int save_sc_version = current_sctx.sc_version;
1266
1267 // Get the funcref in "rettv".
1268 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1269 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1270 current_sctx.sc_version = save_sc_version;
1271 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001272 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001273
1274 // "rettv" will now be a partial referencing the function.
1275 ufunc = rettv->vval.v_partial->pt_func;
1276
1277 // Compile it here to get the return type. The return type is optional,
1278 // when it's missing use t_unknown. This is recognized in
1279 // compile_return().
1280 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1281 ufunc->uf_ret_type = &t_unknown;
1282 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1283
1284 if (ufunc->uf_def_status == UF_COMPILED)
1285 {
1286 // The return type will now be known.
1287 set_function_type(ufunc);
1288 return OK;
1289 }
1290 clear_tv(rettv);
1291 return FAIL;
1292}
1293
1294/*
1295 * parse a dict: {key: val, [key]: val}
1296 * "*arg" points to the '{'.
1297 * ppconst->pp_is_const is set if all item values are a constant.
1298 */
1299 static int
1300compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1301{
1302 garray_T *instr = &cctx->ctx_instr;
1303 int count = 0;
1304 dict_T *d = dict_alloc();
1305 dictitem_T *item;
1306 char_u *whitep = *arg + 1;
1307 char_u *p;
1308 int is_const;
1309 int is_all_const = TRUE; // reset when non-const encountered
1310
1311 if (d == NULL)
1312 return FAIL;
1313 if (generate_ppconst(cctx, ppconst) == FAIL)
1314 return FAIL;
1315 for (;;)
1316 {
1317 char_u *key = NULL;
1318
1319 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1320 {
1321 *arg = NULL;
1322 goto failret;
1323 }
1324
1325 if (**arg == '}')
1326 break;
1327
1328 if (**arg == '[')
1329 {
1330 isn_T *isn;
1331
1332 // {[expr]: value} uses an evaluated key.
1333 *arg = skipwhite(*arg + 1);
1334 if (compile_expr0(arg, cctx) == FAIL)
1335 return FAIL;
1336 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1337 if (isn->isn_type == ISN_PUSHNR)
1338 {
1339 char buf[NUMBUFLEN];
1340
1341 // Convert to string at compile time.
1342 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1343 isn->isn_type = ISN_PUSHS;
1344 isn->isn_arg.string = vim_strsave((char_u *)buf);
1345 }
1346 if (isn->isn_type == ISN_PUSHS)
1347 key = isn->isn_arg.string;
1348 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1349 return FAIL;
1350 *arg = skipwhite(*arg);
1351 if (**arg != ']')
1352 {
1353 emsg(_(e_missing_matching_bracket_after_dict_key));
1354 return FAIL;
1355 }
1356 ++*arg;
1357 }
1358 else
1359 {
1360 // {"name": value},
1361 // {'name': value},
1362 // {name: value} use "name" as a literal key
1363 key = get_literal_key(arg);
1364 if (key == NULL)
1365 return FAIL;
1366 if (generate_PUSHS(cctx, &key) == FAIL)
1367 return FAIL;
1368 }
1369
1370 // Check for duplicate keys, if using string keys.
1371 if (key != NULL)
1372 {
1373 item = dict_find(d, key, -1);
1374 if (item != NULL)
1375 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001376 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 goto failret;
1378 }
1379 item = dictitem_alloc(key);
1380 if (item != NULL)
1381 {
1382 item->di_tv.v_type = VAR_UNKNOWN;
1383 item->di_tv.v_lock = 0;
1384 if (dict_add(d, item) == FAIL)
1385 dictitem_free(item);
1386 }
1387 }
1388
1389 if (**arg != ':')
1390 {
1391 if (*skipwhite(*arg) == ':')
1392 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1393 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001394 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395 return FAIL;
1396 }
1397 whitep = *arg + 1;
1398 if (!IS_WHITE_OR_NUL(*whitep))
1399 {
1400 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1401 return FAIL;
1402 }
1403
1404 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1405 {
1406 *arg = NULL;
1407 goto failret;
1408 }
1409
1410 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1411 return FAIL;
1412 if (!is_const)
1413 is_all_const = FALSE;
1414 ++count;
1415
1416 whitep = *arg;
1417 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1418 {
1419 *arg = NULL;
1420 goto failret;
1421 }
1422 if (**arg == '}')
1423 break;
1424 if (**arg != ',')
1425 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001426 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001427 goto failret;
1428 }
1429 if (IS_WHITE_OR_NUL(*whitep))
1430 {
1431 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1432 return FAIL;
1433 }
1434 whitep = *arg + 1;
1435 if (!IS_WHITE_OR_NUL(*whitep))
1436 {
1437 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1438 return FAIL;
1439 }
1440 *arg = skipwhite(whitep);
1441 }
1442
1443 *arg = *arg + 1;
1444
1445 // Allow for following comment, after at least one space.
1446 p = skipwhite(*arg);
1447 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1448 *arg += STRLEN(*arg);
1449
1450 dict_unref(d);
1451 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001452 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001453
1454failret:
1455 if (*arg == NULL)
1456 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001457 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 *arg = (char_u *)"";
1459 }
1460 dict_unref(d);
1461 return FAIL;
1462}
1463
1464/*
1465 * Compile "&option".
1466 */
1467 static int
1468compile_get_option(char_u **arg, cctx_T *cctx)
1469{
1470 typval_T rettv;
1471 char_u *start = *arg;
1472 int ret;
1473
1474 // parse the option and get the current value to get the type.
1475 rettv.v_type = VAR_UNKNOWN;
1476 ret = eval_option(arg, &rettv, TRUE);
1477 if (ret == OK)
1478 {
1479 // include the '&' in the name, eval_option() expects it.
1480 char_u *name = vim_strnsave(start, *arg - start);
1481 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1482 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1483
1484 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1485 vim_free(name);
1486 }
1487 clear_tv(&rettv);
1488
1489 return ret;
1490}
1491
1492/*
1493 * Compile "$VAR".
1494 */
1495 static int
1496compile_get_env(char_u **arg, cctx_T *cctx)
1497{
1498 char_u *start = *arg;
1499 int len;
1500 int ret;
1501 char_u *name;
1502
1503 ++*arg;
1504 len = get_env_len(arg);
1505 if (len == 0)
1506 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001507 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001508 return FAIL;
1509 }
1510
1511 // include the '$' in the name, eval_env_var() expects it.
1512 name = vim_strnsave(start, len + 1);
1513 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1514 vim_free(name);
1515 return ret;
1516}
1517
1518/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001519 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001520 */
1521 static int
1522compile_interp_string(char_u **arg, cctx_T *cctx)
1523{
1524 typval_T tv;
1525 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001526 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001527 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001528 int count = 0;
1529 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001530
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001531 // *arg is on the '$' character, move it to the first string character.
1532 ++*arg;
1533 quote = **arg;
1534 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001535
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001536 for (;;)
1537 {
1538 // Get the string up to the matching quote or to a single '{'.
1539 // "arg" is advanced to either the quote or the '{'.
1540 if (quote == '"')
1541 ret = eval_string(arg, &tv, evaluate, TRUE);
1542 else
1543 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1544 if (ret == FAIL)
1545 break;
1546 if (evaluate)
1547 {
1548 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1549 || (**arg != '{' && count == 0))
1550 {
1551 // generate non-empty string or empty string if it's the only
1552 // one
1553 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1554 return FAIL;
1555 tv.vval.v_string = NULL; // don't free it now
1556 ++count;
1557 }
1558 clear_tv(&tv);
1559 }
1560
1561 if (**arg != '{')
1562 {
1563 // found terminating quote
1564 ++*arg;
1565 break;
1566 }
1567
1568 p = compile_one_expr_in_str(*arg, cctx);
1569 if (p == NULL)
1570 {
1571 ret = FAIL;
1572 break;
1573 }
1574 ++count;
1575 *arg = p;
1576 }
LemonBoy2eaef102022-05-06 13:14:50 +01001577
1578 if (ret == FAIL || !evaluate)
1579 return ret;
1580
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001581 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1582 if (count > 1)
1583 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001584
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001585 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001586}
1587
1588/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001589 * Compile "@r".
1590 */
1591 static int
1592compile_get_register(char_u **arg, cctx_T *cctx)
1593{
1594 int ret;
1595
1596 ++*arg;
1597 if (**arg == NUL)
1598 {
1599 semsg(_(e_syntax_error_at_str), *arg - 1);
1600 return FAIL;
1601 }
1602 if (!valid_yank_reg(**arg, FALSE))
1603 {
1604 emsg_invreg(**arg);
1605 return FAIL;
1606 }
1607 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1608 ++*arg;
1609 return ret;
1610}
1611
1612/*
1613 * Apply leading '!', '-' and '+' to constant "rettv".
1614 * When "numeric_only" is TRUE do not apply '!'.
1615 */
1616 static int
1617apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1618{
1619 char_u *p = *end;
1620
1621 // this works from end to start
1622 while (p > start)
1623 {
1624 --p;
1625 if (*p == '-' || *p == '+')
1626 {
1627 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628 if (rettv->v_type == VAR_FLOAT)
1629 {
1630 if (*p == '-')
1631 rettv->vval.v_float = -rettv->vval.v_float;
1632 }
1633 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001634 {
1635 varnumber_T val;
1636 int error = FALSE;
1637
1638 // tv_get_number_chk() accepts a string, but we don't want that
1639 // here
1640 if (check_not_string(rettv) == FAIL)
1641 return FAIL;
1642 val = tv_get_number_chk(rettv, &error);
1643 clear_tv(rettv);
1644 if (error)
1645 return FAIL;
1646 if (*p == '-')
1647 val = -val;
1648 rettv->v_type = VAR_NUMBER;
1649 rettv->vval.v_number = val;
1650 }
1651 }
1652 else if (numeric_only)
1653 {
1654 ++p;
1655 break;
1656 }
1657 else if (*p == '!')
1658 {
1659 int v = tv2bool(rettv);
1660
1661 // '!' is permissive in the type.
1662 clear_tv(rettv);
1663 rettv->v_type = VAR_BOOL;
1664 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1665 }
1666 }
1667 *end = p;
1668 return OK;
1669}
1670
1671/*
1672 * Recognize v: variables that are constants and set "rettv".
1673 */
1674 static void
1675get_vim_constant(char_u **arg, typval_T *rettv)
1676{
1677 if (STRNCMP(*arg, "v:true", 6) == 0)
1678 {
1679 rettv->v_type = VAR_BOOL;
1680 rettv->vval.v_number = VVAL_TRUE;
1681 *arg += 6;
1682 }
1683 else if (STRNCMP(*arg, "v:false", 7) == 0)
1684 {
1685 rettv->v_type = VAR_BOOL;
1686 rettv->vval.v_number = VVAL_FALSE;
1687 *arg += 7;
1688 }
1689 else if (STRNCMP(*arg, "v:null", 6) == 0)
1690 {
1691 rettv->v_type = VAR_SPECIAL;
1692 rettv->vval.v_number = VVAL_NULL;
1693 *arg += 6;
1694 }
1695 else if (STRNCMP(*arg, "v:none", 6) == 0)
1696 {
1697 rettv->v_type = VAR_SPECIAL;
1698 rettv->vval.v_number = VVAL_NONE;
1699 *arg += 6;
1700 }
1701}
1702
1703 exprtype_T
1704get_compare_type(char_u *p, int *len, int *type_is)
1705{
1706 exprtype_T type = EXPR_UNKNOWN;
1707 int i;
1708
1709 switch (p[0])
1710 {
1711 case '=': if (p[1] == '=')
1712 type = EXPR_EQUAL;
1713 else if (p[1] == '~')
1714 type = EXPR_MATCH;
1715 break;
1716 case '!': if (p[1] == '=')
1717 type = EXPR_NEQUAL;
1718 else if (p[1] == '~')
1719 type = EXPR_NOMATCH;
1720 break;
1721 case '>': if (p[1] != '=')
1722 {
1723 type = EXPR_GREATER;
1724 *len = 1;
1725 }
1726 else
1727 type = EXPR_GEQUAL;
1728 break;
1729 case '<': if (p[1] != '=')
1730 {
1731 type = EXPR_SMALLER;
1732 *len = 1;
1733 }
1734 else
1735 type = EXPR_SEQUAL;
1736 break;
1737 case 'i': if (p[1] == 's')
1738 {
1739 // "is" and "isnot"; but not a prefix of a name
1740 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1741 *len = 5;
1742 i = p[*len];
1743 if (!isalnum(i) && i != '_')
1744 {
1745 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1746 *type_is = TRUE;
1747 }
1748 }
1749 break;
1750 }
1751 return type;
1752}
1753
1754/*
1755 * Skip over an expression, ignoring most errors.
1756 */
1757 void
1758skip_expr_cctx(char_u **arg, cctx_T *cctx)
1759{
1760 evalarg_T evalarg;
1761
1762 init_evalarg(&evalarg);
1763 evalarg.eval_cctx = cctx;
1764 skip_expr(arg, &evalarg);
1765 clear_evalarg(&evalarg, NULL);
1766}
1767
1768/*
1769 * Check that the top of the type stack has a type that can be used as a
1770 * condition. Give an error and return FAIL if not.
1771 */
1772 int
1773bool_on_stack(cctx_T *cctx)
1774{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 type_T *type;
1776
Bram Moolenaar078a4612022-01-04 15:17:03 +00001777 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001778 if (type == &t_bool)
1779 return OK;
1780
Bram Moolenaar4913d422022-10-17 13:13:32 +01001781 if (type->tt_type == VAR_ANY
1782 || type->tt_type == VAR_UNKNOWN
1783 || type->tt_type == VAR_NUMBER
1784 || type == &t_number_bool
1785 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001786 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1787 // This requires a runtime type check.
1788 return generate_COND2BOOL(cctx);
1789
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001790 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791}
1792
1793/*
1794 * Give the "white on both sides" error, taking the operator from "p[len]".
1795 */
1796 void
1797error_white_both(char_u *op, int len)
1798{
1799 char_u buf[10];
1800
1801 vim_strncpy(buf, op, len);
1802 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1803}
1804
1805/*
1806 * Compile code to apply '-', '+' and '!'.
1807 * When "numeric_only" is TRUE do not apply '!'.
1808 */
1809 static int
1810compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1811{
1812 char_u *p = *end;
1813
1814 // this works from end to start
1815 while (p > start)
1816 {
1817 --p;
1818 while (VIM_ISWHITE(*p))
1819 --p;
1820 if (*p == '-' || *p == '+')
1821 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001822 type_T *type = get_type_on_stack(cctx, 0);
1823 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001824 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 return FAIL;
1826
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001827 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001828 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1829 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001830 }
1831 else if (numeric_only)
1832 {
1833 ++p;
1834 break;
1835 }
1836 else
1837 {
1838 int invert = *p == '!';
1839
1840 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1841 {
1842 if (p[-1] == '!')
1843 invert = !invert;
1844 --p;
1845 }
1846 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1847 return FAIL;
1848 }
1849 }
1850 *end = p;
1851 return OK;
1852}
1853
1854/*
1855 * Compile "(expression)": recursive!
1856 * Return FAIL/OK.
1857 */
1858 static int
1859compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1860{
1861 int ret;
1862 char_u *p = *arg + 1;
1863
1864 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1865 return FAIL;
1866 if (ppconst->pp_used <= PPSIZE - 10)
1867 {
1868 ret = compile_expr1(arg, cctx, ppconst);
1869 }
1870 else
1871 {
1872 // Not enough space in ppconst, flush constants.
1873 if (generate_ppconst(cctx, ppconst) == FAIL)
1874 return FAIL;
1875 ret = compile_expr0(arg, cctx);
1876 }
1877 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1878 return FAIL;
1879 if (**arg == ')')
1880 ++*arg;
1881 else if (ret == OK)
1882 {
1883 emsg(_(e_missing_closing_paren));
1884 ret = FAIL;
1885 }
1886 return ret;
1887}
1888
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001889static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001890
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001891/*
1892 * Compile whatever comes after "name" or "name()".
1893 * Advances "*arg" only when something was recognized.
1894 */
1895 static int
1896compile_subscript(
1897 char_u **arg,
1898 cctx_T *cctx,
1899 char_u *start_leader,
1900 char_u **end_leader,
1901 ppconst_T *ppconst)
1902{
1903 char_u *name_start = *end_leader;
1904 int keeping_dict = FALSE;
1905
1906 for (;;)
1907 {
1908 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001909 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910
1911 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1912 {
1913 char_u *next = peek_next_line_from_context(cctx);
1914
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001915 // If a following line starts with "->{", "->(" or "->X" advance to
1916 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001917 // Also if a following line starts with ".x".
1918 if (next != NULL &&
1919 ((next[0] == '-' && next[1] == '>'
1920 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001921 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001922 || ASCII_ISALPHA(*skipwhite(next + 2))))
1923 || (next[0] == '.' && eval_isdictc(next[1]))))
1924 {
1925 next = next_line_from_context(cctx, TRUE);
1926 if (next == NULL)
1927 return FAIL;
1928 *arg = next;
1929 p = skipwhite(*arg);
1930 }
1931 }
1932
1933 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1934 // is not a function call.
1935 if (**arg == '(')
1936 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001937 int argcount = 0;
1938
1939 if (generate_ppconst(cctx, ppconst) == FAIL)
1940 return FAIL;
1941 ppconst->pp_is_const = FALSE;
1942
1943 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001944 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001945
1946 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001947 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948 return FAIL;
1949 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1950 return FAIL;
1951 if (keeping_dict)
1952 {
1953 keeping_dict = FALSE;
1954 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1955 return FAIL;
1956 }
1957 }
1958 else if (*p == '-' && p[1] == '>')
1959 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001960 char_u *pstart = p;
1961 int alt;
1962 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001963
Bram Moolenaarc7349932022-01-16 20:59:39 +00001964 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001965 if (generate_ppconst(cctx, ppconst) == FAIL)
1966 return FAIL;
1967 ppconst->pp_is_const = FALSE;
1968
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 // Apply the '!', '-' and '+' first:
1970 // -1.0->func() works like (-1.0)->func()
1971 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1972 return FAIL;
1973
1974 p += 2;
1975 *arg = skipwhite(p);
1976 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001977
1978 // Three alternatives handled here:
1979 // 1. "base->name(" only a name, use compile_call()
1980 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1981 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001983 alt = 2;
1984 else
1985 {
1986 // alternative 1 or 3
1987 p = *arg;
1988 if (!eval_isnamec1(*p))
1989 {
1990 semsg(_(e_trailing_characters_str), pstart);
1991 return FAIL;
1992 }
1993 if (ASCII_ISALPHA(*p) && p[1] == ':')
1994 p += 2;
1995 for ( ; eval_isnamec(*p); ++p)
1996 ;
1997 if (*p == '(')
1998 {
1999 // alternative 1
2000 alt = 1;
2001 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2002 return FAIL;
2003 }
2004 else
2005 {
2006 // Must be alternative 3, find the "(". Only works within
2007 // one line.
2008 alt = 3;
2009 paren = vim_strchr(p, '(');
2010 if (paren == NULL)
2011 {
2012 semsg(_(e_missing_parenthesis_str), *arg);
2013 return FAIL;
2014 }
2015 }
2016 }
2017
2018 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002019 {
2020 int argcount = 1;
2021 garray_T *stack = &cctx->ctx_type_stack;
2022 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002023 int expr_isn_start = cctx->ctx_instr.ga_len;
2024 int expr_isn_end;
2025 int arg_isn_count;
2026
Bram Moolenaarc7349932022-01-16 20:59:39 +00002027 if (alt == 2)
2028 {
2029 // Funcref call: list->(Refs[2])(arg)
2030 // or lambda: list->((arg) => expr)(arg)
2031 //
2032 // Fist compile the function expression.
2033 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2034 return FAIL;
2035 }
2036 else
2037 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002038 int fail;
2039 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002040 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002041
Bram Moolenaarc7349932022-01-16 20:59:39 +00002042 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002043
2044 // instead of using LOADG for "import.Func" use PUSHFUNC
2045 ++paren_follows_after_expr;
2046
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002047 // do not look in the next line
2048 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002049
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002050 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002051 || *skipwhite(*arg) != NUL;
2052 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002053 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002054 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002055
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002056 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002057 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002058 if (did_emsg == prev_did_emsg)
2059 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002060 return FAIL;
2061 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002062 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 // Compile the arguments.
2065 if (**arg != '(')
2066 {
2067 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002068 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002069 else
2070 semsg(_(e_missing_parenthesis_str), *arg);
2071 return FAIL;
2072 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002073
2074 // Remember the next instruction index, where the instructions
2075 // for arguments are being written.
2076 expr_isn_end = cctx->ctx_instr.ga_len;
2077
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002078 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002079 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2080 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002081 return FAIL;
2082
2083 // Move the instructions for the arguments to before the
2084 // instructions of the expression and move the type of the
2085 // expression after the argument types. This is what ISN_PCALL
2086 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002087 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2088 if (arg_isn_count > 0)
2089 {
2090 int expr_isn_count = expr_isn_end - expr_isn_start;
2091 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002092 type_T *decl_type;
2093 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002094
2095 if (isn == NULL)
2096 return FAIL;
2097 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2098 + expr_isn_start,
2099 sizeof(isn_T) * expr_isn_count);
2100 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2101 + expr_isn_start,
2102 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2103 sizeof(isn_T) * arg_isn_count);
2104 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2105 + expr_isn_start + arg_isn_count,
2106 isn, sizeof(isn_T) * expr_isn_count);
2107 vim_free(isn);
2108
Bram Moolenaar078a4612022-01-04 15:17:03 +00002109 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2110 type = typep->type_curr;
2111 decl_type = typep->type_decl;
2112 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2113 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2114 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002116 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2117 typep->type_curr = type;
2118 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002119 }
2120
Bram Moolenaar078a4612022-01-04 15:17:03 +00002121 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002122 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2123 return FAIL;
2124 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002125
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002126 if (keeping_dict)
2127 {
2128 keeping_dict = FALSE;
2129 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2130 return FAIL;
2131 }
2132 }
2133 else if (**arg == '[')
2134 {
2135 int is_slice = FALSE;
2136
2137 // list index: list[123]
2138 // dict member: dict[key]
2139 // string index: text[123]
2140 // blob index: blob[123]
2141 if (generate_ppconst(cctx, ppconst) == FAIL)
2142 return FAIL;
2143 ppconst->pp_is_const = FALSE;
2144
2145 ++p;
2146 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2147 return FAIL;
2148 if (**arg == ':')
2149 {
2150 // missing first index is equal to zero
2151 generate_PUSHNR(cctx, 0);
2152 }
2153 else
2154 {
2155 if (compile_expr0(arg, cctx) == FAIL)
2156 return FAIL;
2157 if (**arg == ':')
2158 {
2159 semsg(_(e_white_space_required_before_and_after_str_at_str),
2160 ":", *arg);
2161 return FAIL;
2162 }
2163 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2164 return FAIL;
2165 *arg = skipwhite(*arg);
2166 }
2167 if (**arg == ':')
2168 {
2169 is_slice = TRUE;
2170 ++*arg;
2171 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2172 {
2173 semsg(_(e_white_space_required_before_and_after_str_at_str),
2174 ":", *arg);
2175 return FAIL;
2176 }
2177 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2178 return FAIL;
2179 if (**arg == ']')
2180 // missing second index is equal to end of string
2181 generate_PUSHNR(cctx, -1);
2182 else
2183 {
2184 if (compile_expr0(arg, cctx) == FAIL)
2185 return FAIL;
2186 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2187 return FAIL;
2188 *arg = skipwhite(*arg);
2189 }
2190 }
2191
2192 if (**arg != ']')
2193 {
2194 emsg(_(e_missing_closing_square_brace));
2195 return FAIL;
2196 }
2197 *arg = *arg + 1;
2198
2199 if (keeping_dict)
2200 {
2201 keeping_dict = FALSE;
2202 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2203 return FAIL;
2204 }
2205 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2206 return FAIL;
2207 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002208 else if (*p == '.'
2209 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2210 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2211 {
2212 // class member: SomeClass.varname
2213 // class method: SomeClass.SomeMethod()
2214 // class constructor: SomeClass.new()
2215 // object member: someObject.varname, this.varname
2216 // object method: someObject.SomeMethod(), this.SomeMethod()
2217 if (compile_class_object_index(cctx, arg, type) == FAIL)
2218 return FAIL;
2219 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002220 else if (*p == '.' && p[1] != '.')
2221 {
2222 // dictionary member: dict.name
2223 if (generate_ppconst(cctx, ppconst) == FAIL)
2224 return FAIL;
2225 ppconst->pp_is_const = FALSE;
2226
2227 *arg = p + 1;
2228 if (IS_WHITE_OR_NUL(**arg))
2229 {
2230 emsg(_(e_missing_name_after_dot));
2231 return FAIL;
2232 }
2233 p = *arg;
2234 if (eval_isdictc(*p))
2235 while (eval_isnamec(*p))
2236 MB_PTR_ADV(p);
2237 if (p == *arg)
2238 {
2239 semsg(_(e_syntax_error_at_str), *arg);
2240 return FAIL;
2241 }
2242 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2243 return FAIL;
2244 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2245 return FAIL;
2246 keeping_dict = TRUE;
2247 *arg = p;
2248 }
2249 else
2250 break;
2251 }
2252
2253 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2254 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002255 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2256 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 return FAIL;
2258
2259 return OK;
2260}
2261
2262/*
2263 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2264 * "arg" is advanced until after the expression, skipping white space.
2265 *
2266 * If the value is a constant "ppconst->pp_used" will be non-zero.
2267 * Before instructions are generated, any values in "ppconst" will generated.
2268 *
2269 * This is the compiling equivalent of eval1(), eval2(), etc.
2270 */
2271
2272/*
2273 * number number constant
2274 * 0zFFFFFFFF Blob constant
2275 * "string" string constant
2276 * 'string' literal string constant
2277 * &option-name option value
2278 * @r register contents
2279 * identifier variable value
2280 * function() function call
2281 * $VAR environment variable
2282 * (expression) nested expression
2283 * [expr, expr] List
2284 * {key: val, [key]: val} Dictionary
2285 *
2286 * Also handle:
2287 * ! in front logical NOT
2288 * - in front unary minus
2289 * + in front unary plus (ignored)
2290 * trailing (arg) funcref/partial call
2291 * trailing [] subscript in String or List
2292 * trailing .name entry in Dictionary
2293 * trailing ->name() method call
2294 */
2295 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002296compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 char_u **arg,
2298 cctx_T *cctx,
2299 ppconst_T *ppconst)
2300{
2301 char_u *start_leader, *end_leader;
2302 int ret = OK;
2303 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2304 int used_before = ppconst->pp_used;
2305
2306 ppconst->pp_is_const = FALSE;
2307
2308 /*
2309 * Skip '!', '-' and '+' characters. They are handled later.
2310 */
2311 start_leader = *arg;
2312 if (eval_leader(arg, TRUE) == FAIL)
2313 return FAIL;
2314 end_leader = *arg;
2315
2316 rettv->v_type = VAR_UNKNOWN;
2317 switch (**arg)
2318 {
2319 /*
2320 * Number constant.
2321 */
2322 case '0': // also for blob starting with 0z
2323 case '1':
2324 case '2':
2325 case '3':
2326 case '4':
2327 case '5':
2328 case '6':
2329 case '7':
2330 case '8':
2331 case '9':
2332 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2333 return FAIL;
2334 // Apply "-" and "+" just before the number now, right to
2335 // left. Matters especially when "->" follows. Stops at
2336 // '!'.
2337 if (apply_leader(rettv, TRUE,
2338 start_leader, &end_leader) == FAIL)
2339 {
2340 clear_tv(rettv);
2341 return FAIL;
2342 }
2343 break;
2344
2345 /*
2346 * String constant: "string".
2347 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002348 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349 return FAIL;
2350 break;
2351
2352 /*
2353 * Literal string constant: 'str''ing'.
2354 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002355 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002356 return FAIL;
2357 break;
2358
2359 /*
2360 * Constant Vim variable.
2361 */
2362 case 'v': get_vim_constant(arg, rettv);
2363 ret = NOTDONE;
2364 break;
2365
2366 /*
2367 * "true" constant
2368 */
2369 case 't': if (STRNCMP(*arg, "true", 4) == 0
2370 && !eval_isnamec((*arg)[4]))
2371 {
2372 *arg += 4;
2373 rettv->v_type = VAR_BOOL;
2374 rettv->vval.v_number = VVAL_TRUE;
2375 }
2376 else
2377 ret = NOTDONE;
2378 break;
2379
2380 /*
2381 * "false" constant
2382 */
2383 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2384 && !eval_isnamec((*arg)[5]))
2385 {
2386 *arg += 5;
2387 rettv->v_type = VAR_BOOL;
2388 rettv->vval.v_number = VVAL_FALSE;
2389 }
2390 else
2391 ret = NOTDONE;
2392 break;
2393
2394 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002395 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002396 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002397 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002398 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002399 char_u *p = *arg + 4;
2400 int len;
2401
2402 for (len = 0; eval_isnamec(p[len]); ++len)
2403 ;
2404 ret = handle_predefined(*arg, len + 4, rettv);
2405 if (ret == FAIL)
2406 ret = NOTDONE;
2407 else
2408 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409 }
2410 else
2411 ret = NOTDONE;
2412 break;
2413
2414 /*
2415 * List: [expr, expr]
2416 */
2417 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2418 return FAIL;
2419 ret = compile_list(arg, cctx, ppconst);
2420 break;
2421
2422 /*
2423 * Dictionary: {'key': val, 'key': val}
2424 */
2425 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2426 return FAIL;
2427 ret = compile_dict(arg, cctx, ppconst);
2428 break;
2429
2430 /*
2431 * Option value: &name
2432 */
2433 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2434 return FAIL;
2435 ret = compile_get_option(arg, cctx);
2436 break;
2437
2438 /*
2439 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002440 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 */
2442 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2443 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002444 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2445 ret = compile_interp_string(arg, cctx);
2446 else
2447 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002448 break;
2449
2450 /*
2451 * Register contents: @r.
2452 */
2453 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2454 return FAIL;
2455 ret = compile_get_register(arg, cctx);
2456 break;
2457 /*
2458 * nested expression: (expression).
2459 * lambda: (arg, arg) => expr
2460 * funcref: (arg, arg) => { statement }
2461 */
2462 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2463 ret = compile_lambda(arg, cctx);
2464 if (ret == NOTDONE)
2465 ret = compile_parenthesis(arg, cctx, ppconst);
2466 break;
2467
2468 default: ret = NOTDONE;
2469 break;
2470 }
2471 if (ret == FAIL)
2472 return FAIL;
2473
2474 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2475 {
2476 if (cctx->ctx_skip == SKIP_YES)
2477 clear_tv(rettv);
2478 else
2479 // A constant expression can possibly be handled compile time,
2480 // return the value instead of generating code.
2481 ++ppconst->pp_used;
2482 }
2483 else if (ret == NOTDONE)
2484 {
2485 char_u *p;
2486 int r;
2487
2488 if (!eval_isnamec1(**arg))
2489 {
2490 if (!vim9_bad_comment(*arg))
2491 {
2492 if (ends_excmd(*skipwhite(*arg)))
2493 semsg(_(e_empty_expression_str), *arg);
2494 else
2495 semsg(_(e_name_expected_str), *arg);
2496 }
2497 return FAIL;
2498 }
2499
2500 // "name" or "name()"
2501 p = to_name_end(*arg, TRUE);
2502 if (p - *arg == (size_t)1 && **arg == '_')
2503 {
2504 emsg(_(e_cannot_use_underscore_here));
2505 return FAIL;
2506 }
2507
2508 if (*p == '(')
2509 {
2510 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2511 }
2512 else
2513 {
2514 if (cctx->ctx_skip != SKIP_YES
2515 && generate_ppconst(cctx, ppconst) == FAIL)
2516 return FAIL;
2517 r = compile_load(arg, p, cctx, TRUE, TRUE);
2518 }
2519 if (r == FAIL)
2520 return FAIL;
2521 }
2522
2523 // Handle following "[]", ".member", etc.
2524 // Then deal with prefixed '-', '+' and '!', if not done already.
2525 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2526 ppconst) == FAIL)
2527 return FAIL;
2528 if (ppconst->pp_used > 0)
2529 {
2530 // apply the '!', '-' and '+' before the constant
2531 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2532 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2533 return FAIL;
2534 return OK;
2535 }
2536 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2537 return FAIL;
2538 return OK;
2539}
2540
2541/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002542 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 */
2544 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002545compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002546{
2547 type_T *want_type = NULL;
2548
2549 // Recognize <type>
2550 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2551 {
2552 ++*arg;
2553 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2554 if (want_type == NULL)
2555 return FAIL;
2556
2557 if (**arg != '>')
2558 {
2559 if (*skipwhite(*arg) == '>')
2560 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2561 else
2562 emsg(_(e_missing_gt));
2563 return FAIL;
2564 }
2565 ++*arg;
2566 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2567 return FAIL;
2568 }
2569
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002570 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571 return FAIL;
2572
2573 if (want_type != NULL)
2574 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002575 type_T *actual;
2576 where_T where = WHERE_INIT;
2577
2578 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002579 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002580 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002581 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002582 if (need_type(actual, want_type, FALSE,
2583 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002584 return FAIL;
2585 }
2586 }
2587
2588 return OK;
2589}
2590
2591/*
2592 * * number multiplication
2593 * / number division
2594 * % number modulo
2595 */
2596 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002597compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002598{
2599 char_u *op;
2600 char_u *next;
2601 int ppconst_used = ppconst->pp_used;
2602
2603 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002604 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002605 return FAIL;
2606
2607 /*
2608 * Repeat computing, until no "*", "/" or "%" is following.
2609 */
2610 for (;;)
2611 {
2612 op = may_peek_next_line(cctx, *arg, &next);
2613 if (*op != '*' && *op != '/' && *op != '%')
2614 break;
2615 if (next != NULL)
2616 {
2617 *arg = next_line_from_context(cctx, TRUE);
2618 op = skipwhite(*arg);
2619 }
2620
2621 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2622 {
2623 error_white_both(op, 1);
2624 return FAIL;
2625 }
2626 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2627 return FAIL;
2628
2629 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002630 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002631 return FAIL;
2632
2633 if (ppconst->pp_used == ppconst_used + 2
2634 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2635 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2636 {
2637 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2638 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2639 varnumber_T res = 0;
2640 int failed = FALSE;
2641
2642 // both are numbers: compute the result
2643 switch (*op)
2644 {
2645 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2646 break;
2647 case '/': res = num_divide(tv1->vval.v_number,
2648 tv2->vval.v_number, &failed);
2649 break;
2650 case '%': res = num_modulus(tv1->vval.v_number,
2651 tv2->vval.v_number, &failed);
2652 break;
2653 }
2654 if (failed)
2655 return FAIL;
2656 tv1->vval.v_number = res;
2657 --ppconst->pp_used;
2658 }
2659 else
2660 {
2661 generate_ppconst(cctx, ppconst);
2662 generate_two_op(cctx, op);
2663 }
2664 }
2665
2666 return OK;
2667}
2668
2669/*
2670 * + number addition or list/blobl concatenation
2671 * - number subtraction
2672 * .. string concatenation
2673 */
2674 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002675compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002676{
2677 char_u *op;
2678 char_u *next;
2679 int oplen;
2680 int ppconst_used = ppconst->pp_used;
2681
2682 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002683 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002684 return FAIL;
2685
2686 /*
2687 * Repeat computing, until no "+", "-" or ".." is following.
2688 */
2689 for (;;)
2690 {
2691 op = may_peek_next_line(cctx, *arg, &next);
2692 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2693 break;
2694 if (op[0] == op[1] && *op != '.' && next)
2695 // Finding "++" or "--" on the next line is a separate command.
2696 // But ".." is concatenation.
2697 break;
2698 oplen = (*op == '.' ? 2 : 1);
2699 if (next != NULL)
2700 {
2701 *arg = next_line_from_context(cctx, TRUE);
2702 op = skipwhite(*arg);
2703 }
2704
2705 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2706 {
2707 error_white_both(op, oplen);
2708 return FAIL;
2709 }
2710
2711 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2712 return FAIL;
2713
2714 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002715 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716 return FAIL;
2717
2718 if (ppconst->pp_used == ppconst_used + 2
2719 && (*op == '.'
2720 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2721 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2722 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2723 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2724 {
2725 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2726 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2727
2728 // concat/subtract/add constant numbers
2729 if (*op == '+')
2730 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2731 else if (*op == '-')
2732 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2733 else
2734 {
2735 // concatenate constant strings
2736 char_u *s1 = tv1->vval.v_string;
2737 char_u *s2 = tv2->vval.v_string;
2738 size_t len1 = STRLEN(s1);
2739
2740 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2741 if (tv1->vval.v_string == NULL)
2742 {
2743 clear_ppconst(ppconst);
2744 return FAIL;
2745 }
2746 mch_memmove(tv1->vval.v_string, s1, len1);
2747 STRCPY(tv1->vval.v_string + len1, s2);
2748 vim_free(s1);
2749 vim_free(s2);
2750 }
2751 --ppconst->pp_used;
2752 }
2753 else
2754 {
2755 generate_ppconst(cctx, ppconst);
2756 ppconst->pp_is_const = FALSE;
2757 if (*op == '.')
2758 {
2759 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2760 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2761 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002762 if (generate_CONCAT(cctx, 2) == FAIL)
2763 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002764 }
2765 else
2766 generate_two_op(cctx, op);
2767 }
2768 }
2769
2770 return OK;
2771}
2772
2773/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002774 * expr6a >> expr6b
2775 * expr6a << expr6b
2776 *
2777 * Produces instructions:
2778 * OPNR bitwise left or right shift
2779 */
2780 static int
2781compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2782{
2783 exprtype_T type = EXPR_UNKNOWN;
2784 char_u *p;
2785 char_u *next;
2786 int len = 2;
2787 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002788 isn_T *isn;
2789
2790 // get the first variable
2791 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2792 return FAIL;
2793
2794 /*
2795 * Repeat computing, until no "+", "-" or ".." is following.
2796 */
2797 for (;;)
2798 {
2799 type = EXPR_UNKNOWN;
2800
2801 p = may_peek_next_line(cctx, *arg, &next);
2802 if (p[0] == '<' && p[1] == '<')
2803 type = EXPR_LSHIFT;
2804 else if (p[0] == '>' && p[1] == '>')
2805 type = EXPR_RSHIFT;
2806
2807 if (type == EXPR_UNKNOWN)
2808 return OK;
2809
2810 // Handle a bitwise left or right shift operator
2811 if (ppconst->pp_used == ppconst_used + 1)
2812 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002813 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002814 {
2815 // left operand should be a number
2816 emsg(_(e_bitshift_ops_must_be_number));
2817 return FAIL;
2818 }
2819 }
2820 else
2821 {
2822 type_T *t = get_type_on_stack(cctx, 0);
2823
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002824 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002825 {
2826 emsg(_(e_bitshift_ops_must_be_number));
2827 return FAIL;
2828 }
2829 }
2830
2831 if (next != NULL)
2832 {
2833 *arg = next_line_from_context(cctx, TRUE);
2834 p = skipwhite(*arg);
2835 }
2836
2837 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2838 {
2839 error_white_both(p, len);
2840 return FAIL;
2841 }
2842
2843 // get the second variable
2844 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2845 return FAIL;
2846
2847 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2848 return FAIL;
2849
2850 if (ppconst->pp_used == ppconst_used + 2)
2851 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002852 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2853 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2854
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002855 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002856 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2857 {
2858 // right operand should be a positive number
2859 if (tv2->v_type != VAR_NUMBER)
2860 emsg(_(e_bitshift_ops_must_be_number));
2861 else
dundargocc57b5bc2022-11-02 13:30:51 +00002862 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002863 return FAIL;
2864 }
2865
2866 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2867 tv1->vval.v_number = 0;
2868 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002869 tv1->vval.v_number =
2870 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002871 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002872 tv1->vval.v_number =
2873 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002874 clear_tv(tv2);
2875 --ppconst->pp_used;
2876 }
2877 else
2878 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002879 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2880 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002881 {
2882 emsg(_(e_bitshift_ops_must_be_number));
2883 return FAIL;
2884 }
2885
2886 generate_ppconst(cctx, ppconst);
2887
2888 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2889 if (isn == NULL)
2890 return FAIL;
2891
2892 if (isn != NULL)
2893 isn->isn_arg.op.op_type = type;
2894 }
2895 }
2896
2897 return OK;
2898}
2899
2900/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002901 * expr5a == expr5b
2902 * expr5a =~ expr5b
2903 * expr5a != expr5b
2904 * expr5a !~ expr5b
2905 * expr5a > expr5b
2906 * expr5a >= expr5b
2907 * expr5a < expr5b
2908 * expr5a <= expr5b
2909 * expr5a is expr5b
2910 * expr5a isnot expr5b
2911 *
2912 * Produces instructions:
2913 * EVAL expr5a Push result of "expr5a"
2914 * EVAL expr5b Push result of "expr5b"
2915 * COMPARE one of the compare instructions
2916 */
2917 static int
2918compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2919{
2920 exprtype_T type = EXPR_UNKNOWN;
2921 char_u *p;
2922 char_u *next;
2923 int len = 2;
2924 int type_is = FALSE;
2925 int ppconst_used = ppconst->pp_used;
2926
2927 // get the first variable
2928 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2929 return FAIL;
2930
2931 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002932
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002933 type = get_compare_type(p, &len, &type_is);
2934
2935 /*
2936 * If there is a comparative operator, use it.
2937 */
2938 if (type != EXPR_UNKNOWN)
2939 {
2940 int ic = FALSE; // Default: do not ignore case
2941
2942 if (next != NULL)
2943 {
2944 *arg = next_line_from_context(cctx, TRUE);
2945 p = skipwhite(*arg);
2946 }
2947 if (type_is && (p[len] == '?' || p[len] == '#'))
2948 {
2949 semsg(_(e_invalid_expression_str), *arg);
2950 return FAIL;
2951 }
2952 // extra question mark appended: ignore case
2953 if (p[len] == '?')
2954 {
2955 ic = TRUE;
2956 ++len;
2957 }
2958 // extra '#' appended: match case (ignored)
2959 else if (p[len] == '#')
2960 ++len;
2961 // nothing appended: match case
2962
2963 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2964 {
2965 error_white_both(p, len);
2966 return FAIL;
2967 }
2968
2969 // get the second variable
2970 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2971 return FAIL;
2972
2973 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2974 return FAIL;
2975
2976 if (ppconst->pp_used == ppconst_used + 2)
2977 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002978 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002979 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2980 int ret;
2981
2982 // Both sides are a constant, compute the result now.
2983 // First check for a valid combination of types, this is more
2984 // strict than typval_compare().
2985 if (check_compare_types(type, tv1, tv2) == FAIL)
2986 ret = FAIL;
2987 else
2988 {
2989 ret = typval_compare(tv1, tv2, type, ic);
2990 tv1->v_type = VAR_BOOL;
2991 tv1->vval.v_number = tv1->vval.v_number
2992 ? VVAL_TRUE : VVAL_FALSE;
2993 clear_tv(tv2);
2994 --ppconst->pp_used;
2995 }
2996 return ret;
2997 }
2998
2999 generate_ppconst(cctx, ppconst);
3000 return generate_COMPARE(cctx, type, ic);
3001 }
3002
3003 return OK;
3004}
3005
3006static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3007
3008/*
3009 * Compile || or &&.
3010 */
3011 static int
3012compile_and_or(
3013 char_u **arg,
3014 cctx_T *cctx,
3015 char *op,
3016 ppconst_T *ppconst,
3017 int ppconst_used UNUSED)
3018{
3019 char_u *next;
3020 char_u *p = may_peek_next_line(cctx, *arg, &next);
3021 int opchar = *op;
3022
3023 if (p[0] == opchar && p[1] == opchar)
3024 {
3025 garray_T *instr = &cctx->ctx_instr;
3026 garray_T end_ga;
3027 int save_skip = cctx->ctx_skip;
3028
3029 /*
3030 * Repeat until there is no following "||" or "&&"
3031 */
3032 ga_init2(&end_ga, sizeof(int), 10);
3033 while (p[0] == opchar && p[1] == opchar)
3034 {
3035 long start_lnum = SOURCING_LNUM;
3036 long save_sourcing_lnum;
3037 int start_ctx_lnum = cctx->ctx_lnum;
3038 int save_lnum;
3039 int const_used;
3040 int status;
3041 jumpwhen_T jump_when = opchar == '|'
3042 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3043
3044 if (next != NULL)
3045 {
3046 *arg = next_line_from_context(cctx, TRUE);
3047 p = skipwhite(*arg);
3048 }
3049
3050 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3051 {
3052 semsg(_(e_white_space_required_before_and_after_str_at_str),
3053 op, p);
3054 ga_clear(&end_ga);
3055 return FAIL;
3056 }
3057
3058 save_sourcing_lnum = SOURCING_LNUM;
3059 SOURCING_LNUM = start_lnum;
3060 save_lnum = cctx->ctx_lnum;
3061 cctx->ctx_lnum = start_ctx_lnum;
3062
3063 status = check_ppconst_bool(ppconst);
3064 if (status != FAIL)
3065 {
3066 // Use the last ppconst if possible.
3067 if (ppconst->pp_used > 0)
3068 {
3069 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3070 int is_true = tv2bool(tv);
3071
3072 if ((is_true && opchar == '|')
3073 || (!is_true && opchar == '&'))
3074 {
3075 // For "false && expr" and "true || expr" the "expr"
3076 // does not need to be evaluated.
3077 cctx->ctx_skip = SKIP_YES;
3078 clear_tv(tv);
3079 tv->v_type = VAR_BOOL;
3080 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3081 }
3082 else
3083 {
3084 // For "true && expr" and "false || expr" only "expr"
3085 // needs to be evaluated.
3086 --ppconst->pp_used;
3087 jump_when = JUMP_NEVER;
3088 }
3089 }
3090 else
3091 {
3092 // Every part must evaluate to a bool.
3093 status = bool_on_stack(cctx);
3094 }
3095 }
3096 if (status != FAIL)
3097 status = ga_grow(&end_ga, 1);
3098 cctx->ctx_lnum = save_lnum;
3099 if (status == FAIL)
3100 {
3101 ga_clear(&end_ga);
3102 return FAIL;
3103 }
3104
3105 if (jump_when != JUMP_NEVER)
3106 {
3107 if (cctx->ctx_skip != SKIP_YES)
3108 {
3109 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3110 ++end_ga.ga_len;
3111 }
3112 generate_JUMP(cctx, jump_when, 0);
3113 }
3114
3115 // eval the next expression
3116 SOURCING_LNUM = save_sourcing_lnum;
3117 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3118 {
3119 ga_clear(&end_ga);
3120 return FAIL;
3121 }
3122
3123 const_used = ppconst->pp_used;
3124 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3125 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3126 {
3127 ga_clear(&end_ga);
3128 return FAIL;
3129 }
3130
3131 // "0 || 1" results in true, "1 && 0" results in false.
3132 if (ppconst->pp_used == const_used + 1)
3133 {
3134 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3135
3136 if (tv->v_type == VAR_NUMBER
3137 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3138 {
3139 tv->vval.v_number = tv->vval.v_number == 1
3140 ? VVAL_TRUE : VVAL_FALSE;
3141 tv->v_type = VAR_BOOL;
3142 }
3143 }
3144
3145 p = may_peek_next_line(cctx, *arg, &next);
3146 }
3147
3148 if (check_ppconst_bool(ppconst) == FAIL)
3149 {
3150 ga_clear(&end_ga);
3151 return FAIL;
3152 }
3153
3154 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3155 // Every part must evaluate to a bool.
3156 if (bool_on_stack(cctx) == FAIL)
3157 {
3158 ga_clear(&end_ga);
3159 return FAIL;
3160 }
3161
3162 if (end_ga.ga_len > 0)
3163 {
3164 // Fill in the end label in all jumps.
3165 generate_ppconst(cctx, ppconst);
3166 while (end_ga.ga_len > 0)
3167 {
3168 isn_T *isn;
3169
3170 --end_ga.ga_len;
3171 isn = ((isn_T *)instr->ga_data)
3172 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3173 isn->isn_arg.jump.jump_where = instr->ga_len;
3174 }
3175 }
3176 ga_clear(&end_ga);
3177
3178 cctx->ctx_skip = save_skip;
3179 }
3180
3181 return OK;
3182}
3183
3184/*
3185 * expr4a && expr4a && expr4a logical AND
3186 *
3187 * Produces instructions:
3188 * EVAL expr4a Push result of "expr4a"
3189 * COND2BOOL convert to bool if needed
3190 * JUMP_IF_COND_FALSE end
3191 * EVAL expr4b Push result of "expr4b"
3192 * JUMP_IF_COND_FALSE end
3193 * EVAL expr4c Push result of "expr4c"
3194 * end:
3195 */
3196 static int
3197compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3198{
3199 int ppconst_used = ppconst->pp_used;
3200
3201 // get the first variable
3202 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3203 return FAIL;
3204
3205 // || and && work almost the same
3206 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3207}
3208
3209/*
3210 * expr3a || expr3b || expr3c logical OR
3211 *
3212 * Produces instructions:
3213 * EVAL expr3a Push result of "expr3a"
3214 * COND2BOOL convert to bool if needed
3215 * JUMP_IF_COND_TRUE end
3216 * EVAL expr3b Push result of "expr3b"
3217 * JUMP_IF_COND_TRUE end
3218 * EVAL expr3c Push result of "expr3c"
3219 * end:
3220 */
3221 static int
3222compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3223{
3224 int ppconst_used = ppconst->pp_used;
3225
3226 // eval the first expression
3227 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3228 return FAIL;
3229
3230 // || and && work almost the same
3231 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3232}
3233
3234/*
3235 * Toplevel expression: expr2 ? expr1a : expr1b
3236 * Produces instructions:
3237 * EVAL expr2 Push result of "expr2"
3238 * JUMP_IF_FALSE alt jump if false
3239 * EVAL expr1a
3240 * JUMP_ALWAYS end
3241 * alt: EVAL expr1b
3242 * end:
3243 *
3244 * Toplevel expression: expr2 ?? expr1
3245 * Produces instructions:
3246 * EVAL expr2 Push result of "expr2"
3247 * JUMP_AND_KEEP_IF_TRUE end jump if true
3248 * EVAL expr1
3249 * end:
3250 */
3251 int
3252compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3253{
3254 char_u *p;
3255 int ppconst_used = ppconst->pp_used;
3256 char_u *next;
3257
3258 // Ignore all kinds of errors when not producing code.
3259 if (cctx->ctx_skip == SKIP_YES)
3260 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003261 int prev_did_emsg = did_emsg;
3262
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003263 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003264 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003265 }
3266
3267 // Evaluate the first expression.
3268 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3269 return FAIL;
3270
3271 p = may_peek_next_line(cctx, *arg, &next);
3272 if (*p == '?')
3273 {
3274 int op_falsy = p[1] == '?';
3275 garray_T *instr = &cctx->ctx_instr;
3276 garray_T *stack = &cctx->ctx_type_stack;
3277 int alt_idx = instr->ga_len;
3278 int end_idx = 0;
3279 isn_T *isn;
3280 type_T *type1 = NULL;
3281 int has_const_expr = FALSE;
3282 int const_value = FALSE;
3283 int save_skip = cctx->ctx_skip;
3284
3285 if (next != NULL)
3286 {
3287 *arg = next_line_from_context(cctx, TRUE);
3288 p = skipwhite(*arg);
3289 }
3290
3291 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3292 {
3293 semsg(_(e_white_space_required_before_and_after_str_at_str),
3294 op_falsy ? "??" : "?", p);
3295 return FAIL;
3296 }
3297
3298 if (ppconst->pp_used == ppconst_used + 1)
3299 {
3300 // the condition is a constant, we know whether the ? or the :
3301 // expression is to be evaluated.
3302 has_const_expr = TRUE;
3303 if (op_falsy)
3304 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3305 else
3306 {
3307 int error = FALSE;
3308
3309 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3310 &error);
3311 if (error)
3312 return FAIL;
3313 }
3314 cctx->ctx_skip = save_skip == SKIP_YES ||
3315 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3316
3317 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3318 // "left ?? right" and "left" is truthy: produce "left"
3319 generate_ppconst(cctx, ppconst);
3320 else
3321 {
3322 clear_tv(&ppconst->pp_tv[ppconst_used]);
3323 --ppconst->pp_used;
3324 }
3325 }
3326 else
3327 {
3328 generate_ppconst(cctx, ppconst);
3329 if (op_falsy)
3330 end_idx = instr->ga_len;
3331 generate_JUMP(cctx, op_falsy
3332 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3333 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003334 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003335 }
3336
3337 // evaluate the second expression; any type is accepted
3338 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3339 return FAIL;
3340 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3341 return FAIL;
3342
3343 if (!has_const_expr)
3344 {
3345 generate_ppconst(cctx, ppconst);
3346
3347 if (!op_falsy)
3348 {
3349 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003350 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003351 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003352
3353 end_idx = instr->ga_len;
3354 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3355
3356 // jump here from JUMP_IF_FALSE
3357 isn = ((isn_T *)instr->ga_data) + alt_idx;
3358 isn->isn_arg.jump.jump_where = instr->ga_len;
3359 }
3360 }
3361
3362 if (!op_falsy)
3363 {
3364 // Check for the ":".
3365 p = may_peek_next_line(cctx, *arg, &next);
3366 if (*p != ':')
3367 {
3368 emsg(_(e_missing_colon_after_questionmark));
3369 return FAIL;
3370 }
3371 if (next != NULL)
3372 {
3373 *arg = next_line_from_context(cctx, TRUE);
3374 p = skipwhite(*arg);
3375 }
3376
3377 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3378 {
3379 semsg(_(e_white_space_required_before_and_after_str_at_str),
3380 ":", p);
3381 return FAIL;
3382 }
3383
3384 // evaluate the third expression
3385 if (has_const_expr)
3386 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3387 ? SKIP_YES : SKIP_NOT;
3388 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3389 return FAIL;
3390 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3391 return FAIL;
3392 }
3393
3394 if (!has_const_expr)
3395 {
3396 type_T **typep;
3397
3398 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003399 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003400
3401 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003402 typep = &((((type2_T *)stack->ga_data)
3403 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003404 common_type(type1, *typep, typep, cctx->ctx_type_list);
3405
3406 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3407 isn = ((isn_T *)instr->ga_data) + end_idx;
3408 isn->isn_arg.jump.jump_where = instr->ga_len;
3409 }
3410
3411 cctx->ctx_skip = save_skip;
3412 }
3413 return OK;
3414}
3415
3416/*
3417 * Toplevel expression.
3418 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3419 * Returns OK or FAIL.
3420 */
3421 int
3422compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3423{
3424 ppconst_T ppconst;
3425
3426 CLEAR_FIELD(ppconst);
3427 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3428 {
3429 clear_ppconst(&ppconst);
3430 return FAIL;
3431 }
3432 if (is_const != NULL)
3433 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3434 if (generate_ppconst(cctx, &ppconst) == FAIL)
3435 return FAIL;
3436 return OK;
3437}
3438
3439/*
3440 * Toplevel expression.
3441 */
3442 int
3443compile_expr0(char_u **arg, cctx_T *cctx)
3444{
3445 return compile_expr0_ext(arg, cctx, NULL);
3446}
3447
3448
3449#endif // defined(FEAT_EVAL)