blob: 7f5f4c8b0d94cfe29e070fe4ede425591c8cd7ef [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
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000266 if (type->tt_type == VAR_CLASS)
267 {
268 garray_T *instr = &cctx->ctx_instr;
269 if (instr->ga_len > 0)
270 {
271 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
272 if (isn->isn_type == ISN_LOADSCRIPT)
273 {
274 // The class was recognized as a script item. We only need
275 // to know what class it is, drop the instruction.
276 --instr->ga_len;
277 vim_free(isn->isn_arg.script.scriptref);
278 }
279 }
280 }
281
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000282 ++*arg;
283 char_u *name = *arg;
284 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
285 if (name_end == name)
286 return FAIL;
287 size_t len = name_end - name;
288
289 class_T *cl = (class_T *)type->tt_member;
290 if (*name_end == '(')
291 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000292 int function_count;
293 ufunc_T **functions;
294
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000295 if (type->tt_type == VAR_CLASS)
296 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000297 function_count = cl->class_class_function_count;
298 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000299 }
300 else
301 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000302 // type->tt_type == VAR_OBJECT: method call
303 function_count = cl->class_obj_method_count;
304 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000305 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000306
307 ufunc_T *ufunc = NULL;
308 for (int i = 0; i < function_count; ++i)
309 {
310 ufunc_T *fp = functions[i];
311 // Use a separate pointer to avoid that ASAN complains about
312 // uf_name[] only being 4 characters.
313 char_u *ufname = (char_u *)fp->uf_name;
314 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
315 {
316 ufunc = fp;
317 break;
318 }
319 }
320 if (ufunc == NULL)
321 {
322 // TODO: different error for object method?
323 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
324 return FAIL;
325 }
326
327 // Compile the arguments and call the class function or object method.
328 // The object method will know that the object is on the stack, just
329 // before the arguments.
330 *arg = skipwhite(name_end + 1);
331 int argcount = 0;
332 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
333 return FAIL;
334 return generate_CALL(cctx, ufunc, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000335 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000336
337 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000338 {
339 for (int i = 0; i < cl->class_obj_member_count; ++i)
340 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000341 ocmember_T *m = &cl->class_obj_members[i];
342 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000343 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000344 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
345 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000346 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000347 return FAIL;
348 }
349
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000350 *arg = name_end;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000351 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000352 }
353 }
354
355 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
356 }
357 else
358 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000359 // load class member
360 int idx;
361 for (idx = 0; idx < cl->class_class_member_count; ++idx)
362 {
363 ocmember_T *m = &cl->class_class_members[idx];
364 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
365 break;
366 }
367 if (idx < cl->class_class_member_count)
368 {
369 *arg = name_end;
370 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
371 }
372 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000373 }
374
375 return FAIL;
376}
377
378/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 * Generate an instruction to load script-local variable "name", without the
380 * leading "s:".
381 * Also finds imported variables.
382 */
383 int
384compile_load_scriptvar(
385 cctx_T *cctx,
386 char_u *name, // variable NUL terminated
387 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100388 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000389{
390 scriptitem_T *si;
391 int idx;
392 imported_T *import;
393
394 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
395 return FAIL;
396 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000397 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000398 if (idx >= 0)
399 {
400 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
401
402 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
403 current_sctx.sc_sid, idx, sv->sv_type);
404 return OK;
405 }
406
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000407 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 if (import != NULL)
409 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000410 char_u *p = skipwhite(*end);
411 char_u *exp_name;
412 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100413 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000414 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000415 int done = FALSE;
416 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000417
418 // Need to lookup the member.
419 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000420 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000421 semsg(_(e_expected_dot_after_name_str), start);
422 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000424 ++p;
425 if (VIM_ISWHITE(*p))
426 {
427 emsg(_(e_no_white_space_allowed_after_dot));
428 return FAIL;
429 }
430
431 // isolate one name
432 exp_name = p;
433 while (eval_isnamec(*p))
434 ++p;
435 cc = *p;
436 *p = NUL;
437
Bram Moolenaard041f422022-01-12 19:54:00 +0000438 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100439 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
440 // "import autoload './dir/script.vim'" or
441 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100442 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100443
444 if (res == OK)
445 {
446 if (si->sn_autoload_prefix != NULL
447 && si->sn_state == SN_STATE_NOT_LOADED)
448 {
449 char_u *auto_name =
450 concat_str(si->sn_autoload_prefix, exp_name);
451
452 // autoload script must be loaded later, access by the autoload
453 // name. If a '(' follows it must be a function. Otherwise we
454 // don't know, it can be "script.Func".
455 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100456 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100457 else
458 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
459 vim_free(auto_name);
460 done = TRUE;
461 }
462 else if (si->sn_import_autoload
463 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100464 {
465 // If a '(' follows it must be a function. Otherwise we don't
466 // know, it can be "script.Func".
467 if (cc == '(' || paren_follows_after_expr)
468 {
469 char_u sid_name[MAX_FUNC_NAME_LEN];
470
471 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100472 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100473 }
474 else
475 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
476 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100477 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100478 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100479 else
480 {
481 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
482 cctx, NULL, TRUE);
483 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100484 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100485
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000486 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000487 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000488 if (done)
489 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000490
491 if (idx < 0)
492 {
493 if (ufunc != NULL)
494 {
495 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100496 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000497 return OK;
498 }
499 return FAIL;
500 }
501
502 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
503 import->imp_sid,
504 idx,
505 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000506 return OK;
507 }
508
Bram Moolenaard0132f42022-05-12 11:05:40 +0100509 // Can only get here if we know "name" is a script variable and not in a
510 // Vim9 script (variable is not in sn_var_vals): old style script.
511 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000512 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000513}
514
515 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000516generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000517{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000518 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000519 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000520
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000521 // Reject a global non-autoload function found without the "g:" prefix.
522 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000523 return FAIL;
524
525 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000526 compile_type = get_compile_type(ufunc);
527 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000528 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100530 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531}
532
533/*
534 * Compile a variable name into a load instruction.
535 * "end" points to just after the name.
536 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
537 * When "error" is FALSE do not give an error when not found.
538 */
539 int
540compile_load(
541 char_u **arg,
542 char_u *end_arg,
543 cctx_T *cctx,
544 int is_expr,
545 int error)
546{
547 type_T *type;
548 char_u *name = NULL;
549 char_u *end = end_arg;
550 int res = FAIL;
551 int prev_called_emsg = called_emsg;
552
553 if (*(*arg + 1) == ':')
554 {
555 if (end <= *arg + 2)
556 {
557 isntype_T isn_type;
558
559 // load dictionary of namespace
560 switch (**arg)
561 {
562 case 'g': isn_type = ISN_LOADGDICT; break;
563 case 'w': isn_type = ISN_LOADWDICT; break;
564 case 't': isn_type = ISN_LOADTDICT; break;
565 case 'b': isn_type = ISN_LOADBDICT; break;
566 default:
567 semsg(_(e_namespace_not_supported_str), *arg);
568 goto theend;
569 }
570 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
571 goto theend;
572 res = OK;
573 }
574 else
575 {
576 isntype_T isn_type = ISN_DROP;
577
578 // load namespaced variable
579 name = vim_strnsave(*arg + 2, end - (*arg + 2));
580 if (name == NULL)
581 return FAIL;
582
583 switch (**arg)
584 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100585 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000586 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000587 case 's': if (current_script_is_vim9())
588 {
589 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
590 *arg);
591 vim_free(name);
592 return FAIL;
593 }
Kota Kato948a3892022-08-16 16:09:59 +0100594 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000595 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000596 else
597 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100598 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000599 break;
600 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
601 {
602 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000603 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000604 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605 else
606 isn_type = ISN_LOADG;
607 }
608 else
609 {
610 isn_type = ISN_LOADAUTO;
611 vim_free(name);
612 name = vim_strnsave(*arg, end - *arg);
613 if (name == NULL)
614 return FAIL;
615 }
616 break;
617 case 'w': isn_type = ISN_LOADW; break;
618 case 't': isn_type = ISN_LOADT; break;
619 case 'b': isn_type = ISN_LOADB; break;
620 default: // cannot happen, just in case
621 semsg(_(e_namespace_not_supported_str), *arg);
622 goto theend;
623 }
624 if (isn_type != ISN_DROP)
625 {
626 // Global, Buffer-local, Window-local and Tabpage-local
627 // variables can be defined later, thus we don't check if it
628 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000629 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000630 }
631 }
632 }
633 else
634 {
635 size_t len = end - *arg;
636 int idx;
637 int gen_load = FALSE;
638 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100639 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100640 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000641
642 name = vim_strnsave(*arg, end - *arg);
643 if (name == NULL)
644 return FAIL;
645
646 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
647 {
648 script_autoload(name, FALSE);
649 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
650 }
651 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
652 == OK)
653 {
654 if (gen_load_outer == 0)
655 gen_load = TRUE;
656 }
657 else
658 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000659 lvar_T lvar;
660 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000661
662 if (lookup_local(*arg, len, &lvar, cctx) == OK)
663 {
664 type = lvar.lv_type;
665 idx = lvar.lv_idx;
666 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100667 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000668 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100669 outer_loop_depth = lvar.lv_loop_depth;
670 outer_loop_idx = lvar.lv_loop_idx;
671 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000672 else
673 gen_load = TRUE;
674 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000675 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000676 {
677 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
678 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000679 else
680 {
681 // "var" can be script-local even without using "s:" if it
682 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000683 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000684 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100685 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686
687 // When evaluating an expression and the name starts with an
688 // uppercase letter it can be a user defined function.
689 // generate_funcref() will fail if the function can't be found.
690 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000691 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692 }
693 }
694 if (gen_load)
695 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
696 if (gen_load_outer > 0)
697 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100698 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
699 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000700 cctx->ctx_outer_used = TRUE;
701 }
702 }
703
704 *arg = end;
705
706theend:
707 if (res == FAIL && error && called_emsg == prev_called_emsg)
708 semsg(_(e_variable_not_found_str), name);
709 vim_free(name);
710 return res;
711}
712
713/*
714 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100715 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 * Returns FAIL if compilation fails.
717 */
718 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100719compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000720{
LemonBoyf3b48952022-05-05 13:53:03 +0100721 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722 garray_T save_ga = cctx->ctx_instr;
723 int expr_res;
724 int trailing_error;
725 int instr_count;
726 isn_T *instr = NULL;
727
728 // Remove the string type from the stack.
729 --cctx->ctx_type_stack.ga_len;
730
731 // Temporarily reset the list of instructions so that the jump labels are
732 // correct.
733 cctx->ctx_instr.ga_len = 0;
734 cctx->ctx_instr.ga_maxlen = 0;
735 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000736
737 // avoid peeking a next line
738 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
739 cctx->ctx_ufunc->uf_lines.ga_len = 0;
740
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000742
743 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
744
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 s = skipwhite(s);
746 trailing_error = *s != NUL;
747
748 if (expr_res == FAIL || trailing_error
749 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
750 {
751 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000752 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753 clear_instr_ga(&cctx->ctx_instr);
754 cctx->ctx_instr = save_ga;
755 ++cctx->ctx_type_stack.ga_len;
756 return FAIL;
757 }
758
759 // Move the generated instructions into the ISN_INSTR instruction, then
760 // restore the list of instructions.
761 instr_count = cctx->ctx_instr.ga_len;
762 instr = cctx->ctx_instr.ga_data;
763 instr[instr_count].isn_type = ISN_FINISH;
764
765 cctx->ctx_instr = save_ga;
766 vim_free(isn->isn_arg.string);
767 isn->isn_type = ISN_INSTR;
768 isn->isn_arg.instr = instr;
769 return OK;
770}
771
772/*
773 * Compile the argument expressions.
774 * "arg" points to just after the "(" and is advanced to after the ")"
775 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100776 int
LemonBoyf3b48952022-05-05 13:53:03 +0100777compile_arguments(
778 char_u **arg,
779 cctx_T *cctx,
780 int *argcount,
781 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782{
783 char_u *p = *arg;
784 char_u *whitep = *arg;
785 int must_end = FALSE;
786 int instr_count;
787
788 for (;;)
789 {
790 if (may_get_next_line(whitep, &p, cctx) == FAIL)
791 goto failret;
792 if (*p == ')')
793 {
794 *arg = p + 1;
795 return OK;
796 }
797 if (must_end)
798 {
799 semsg(_(e_missing_comma_before_argument_str), p);
800 return FAIL;
801 }
802
803 instr_count = cctx->ctx_instr.ga_len;
804 if (compile_expr0(&p, cctx) == FAIL)
805 return FAIL;
806 ++*argcount;
807
LemonBoyf3b48952022-05-05 13:53:03 +0100808 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809 && cctx->ctx_instr.ga_len == instr_count + 1)
810 {
811 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
812
813 // {skip} argument of searchpair() can be compiled if not empty
814 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100815 compile_string(isn, cctx, 0);
816 }
817 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
818 && cctx->ctx_instr.ga_len == instr_count + 1)
819 {
820 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
821
822 // {sub} argument of substitute() can be compiled if it starts
823 // with \=
824 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100825 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100826 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 }
828
829 if (*p != ',' && *skipwhite(p) == ',')
830 {
831 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
832 p = skipwhite(p);
833 }
834 if (*p == ',')
835 {
836 ++p;
837 if (*p != NUL && !VIM_ISWHITE(*p))
838 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
839 }
840 else
841 must_end = TRUE;
842 whitep = p;
843 p = skipwhite(p);
844 }
845failret:
846 emsg(_(e_missing_closing_paren));
847 return FAIL;
848}
849
850/*
851 * Compile a function call: name(arg1, arg2)
852 * "arg" points to "name", "arg + varlen" to the "(".
853 * "argcount_init" is 1 for "value->method()"
854 * Instructions:
855 * EVAL arg1
856 * EVAL arg2
857 * BCALL / DCALL / UCALL
858 */
859 static int
860compile_call(
861 char_u **arg,
862 size_t varlen,
863 cctx_T *cctx,
864 ppconst_T *ppconst,
865 int argcount_init)
866{
867 char_u *name = *arg;
868 char_u *p;
869 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100870 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871 char_u fname_buf[FLEN_FIXED + 1];
872 char_u *tofree = NULL;
873 int error = FCERR_NONE;
874 ufunc_T *ufunc = NULL;
875 int res = FAIL;
876 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000877 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100878 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000879 imported_T *import;
880
881 if (varlen >= sizeof(namebuf))
882 {
883 semsg(_(e_name_too_long_str), name);
884 return FAIL;
885 }
886 vim_strncpy(namebuf, *arg, varlen);
887
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000888 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000889 if (import != NULL)
890 {
891 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
892 return FAIL;
893 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894
895 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100896 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100898 if ((varlen == 3
899 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
901 {
902 char_u *s = skipwhite(*arg + varlen + 1);
903 typval_T argvars[2];
904 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100905 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906
907 argvars[0].v_type = VAR_UNKNOWN;
908 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100909 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100911 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000912 s = skipwhite(s);
913 if (*s == ')' && argvars[0].v_type == VAR_STRING
914 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
915 || !is_has))
916 {
917 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
918
919 *arg = s + 1;
920 argvars[1].v_type = VAR_UNKNOWN;
921 tv->v_type = VAR_NUMBER;
922 tv->vval.v_number = 0;
923 if (is_has)
924 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100925 else if (is_len)
926 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 else
928 f_exists(argvars, tv);
929 clear_tv(&argvars[0]);
930 ++ppconst->pp_used;
931 return OK;
932 }
933 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100934 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000935 {
936 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
937 return FAIL;
938 }
939 }
940
941 if (generate_ppconst(cctx, ppconst) == FAIL)
942 return FAIL;
943
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000944 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
945
946 // We handle the "skip" argument of searchpair() and searchpairpos()
947 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100948 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
949 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
950 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
951 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
952 special_fn = CA_SEARCHPAIR;
953 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
954 special_fn = CA_SUBSTITUTE;
955 else
956 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000957
958 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100959 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000960 goto theend;
961
962 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
963 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
964 {
965 int idx;
966
967 // builtin function
968 idx = find_internal_func(name);
969 if (idx >= 0)
970 {
971 if (STRCMP(name, "flatten") == 0)
972 {
973 emsg(_(e_cannot_use_flatten_in_vim9_script));
974 goto theend;
975 }
976
977 if (STRCMP(name, "add") == 0 && argcount == 2)
978 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000979 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000980
981 // add() can be compiled to instructions if we know the type
982 if (type->tt_type == VAR_LIST)
983 {
984 // inline "add(list, item)" so that the type can be checked
985 res = generate_LISTAPPEND(cctx);
986 idx = -1;
987 }
988 else if (type->tt_type == VAR_BLOB)
989 {
990 // inline "add(blob, nr)" so that the type can be checked
991 res = generate_BLOBAPPEND(cctx);
992 idx = -1;
993 }
994 }
995
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100996 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
997 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100998 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100999 // May have the "D" or "R" flag, reserve a variable for a
1000 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001001 if (get_defer_var_idx(cctx) == 0)
1002 idx = -1;
1003 }
1004
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001005 if (idx >= 0)
1006 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1007 }
1008 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001009 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001010 goto theend;
1011 }
1012
Bram Moolenaar62aec932022-01-29 21:45:34 +00001013 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1014
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001015 // An argument or local variable can be a function reference, this
1016 // overrules a function name.
1017 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1018 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1019 {
1020 // If we can find the function by name generate the right call.
1021 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001022 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001023 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001024 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001025 if (!func_is_global(ufunc))
1026 {
1027 res = generate_CALL(cctx, ufunc, argcount);
1028 goto theend;
1029 }
1030 if (!has_g_namespace
1031 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1032 {
1033 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001034 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001035 goto theend;
1036 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001037 }
1038 }
1039
1040 // If the name is a variable, load it and use PCALL.
1041 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001042 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001043 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001044 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001045 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1046 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001047 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001048
1049 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1050 goto theend;
1051 }
1052
1053 // If we can find a global function by name generate the right call.
1054 if (ufunc != NULL)
1055 {
1056 res = generate_CALL(cctx, ufunc, argcount);
1057 goto theend;
1058 }
1059
1060 // A global function may be defined only later. Need to figure out at
1061 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001062 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001063 res = generate_UCALL(cctx, name, argcount);
1064 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001065 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001066
1067theend:
1068 vim_free(tofree);
1069 return res;
1070}
1071
1072// like NAMESPACE_CHAR but with 'a' and 'l'.
1073#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1074
1075/*
1076 * Find the end of a variable or function name. Unlike find_name_end() this
1077 * does not recognize magic braces.
1078 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1079 * Return a pointer to just after the name. Equal to "arg" if there is no
1080 * valid name.
1081 */
1082 char_u *
1083to_name_end(char_u *arg, int use_namespace)
1084{
1085 char_u *p;
1086
1087 // Quick check for valid starting character.
1088 if (!eval_isnamec1(*arg))
1089 return arg;
1090
1091 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1092 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1093 // and can be used in slice "[n:]".
1094 if (*p == ':' && (p != arg + 1
1095 || !use_namespace
1096 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1097 break;
1098 return p;
1099}
1100
1101/*
1102 * Like to_name_end() but also skip over a list or dict constant.
1103 * Also accept "<SNR>123_Func".
1104 * This intentionally does not handle line continuation.
1105 */
1106 char_u *
1107to_name_const_end(char_u *arg)
1108{
1109 char_u *p = arg;
1110 typval_T rettv;
1111
1112 if (STRNCMP(p, "<SNR>", 5) == 0)
1113 p = skipdigits(p + 5);
1114 p = to_name_end(p, TRUE);
1115 if (p == arg && *arg == '[')
1116 {
1117
1118 // Can be "[1, 2, 3]->Func()".
1119 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1120 p = arg;
1121 }
1122 return p;
1123}
1124
1125/*
1126 * parse a list: [expr, expr]
1127 * "*arg" points to the '['.
1128 * ppconst->pp_is_const is set if all items are a constant.
1129 */
1130 static int
1131compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1132{
1133 char_u *p = skipwhite(*arg + 1);
1134 char_u *whitep = *arg + 1;
1135 int count = 0;
1136 int is_const;
1137 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001138 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139
1140 for (;;)
1141 {
1142 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1143 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001144 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145 return FAIL;
1146 }
1147 if (*p == ',')
1148 {
1149 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1150 return FAIL;
1151 }
1152 if (*p == ']')
1153 {
1154 ++p;
1155 break;
1156 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001157 if (must_end)
1158 {
1159 semsg(_(e_missing_comma_in_list_str), p);
1160 return FAIL;
1161 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1163 return FAIL;
1164 if (!is_const)
1165 is_all_const = FALSE;
1166 ++count;
1167 if (*p == ',')
1168 {
1169 ++p;
1170 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1171 {
1172 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1173 return FAIL;
1174 }
1175 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001176 else
1177 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 whitep = p;
1179 p = skipwhite(p);
1180 }
1181 *arg = p;
1182
1183 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001184 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185}
1186
1187/*
1188 * Parse a lambda: "(arg, arg) => expr"
1189 * "*arg" points to the '('.
1190 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1191 */
1192 static int
1193compile_lambda(char_u **arg, cctx_T *cctx)
1194{
1195 int r;
1196 typval_T rettv;
1197 ufunc_T *ufunc;
1198 evalarg_T evalarg;
1199
1200 init_evalarg(&evalarg);
1201 evalarg.eval_flags = EVAL_EVALUATE;
1202 evalarg.eval_cctx = cctx;
1203
1204 // Get the funcref in "rettv".
1205 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1206 if (r != OK)
1207 {
1208 clear_evalarg(&evalarg, NULL);
1209 return r;
1210 }
1211
1212 // "rettv" will now be a partial referencing the function.
1213 ufunc = rettv.vval.v_partial->pt_func;
1214 ++ufunc->uf_refcount;
1215 clear_tv(&rettv);
1216
1217 // Compile it here to get the return type. The return type is optional,
1218 // when it's missing use t_unknown. This is recognized in
1219 // compile_return().
1220 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1221 ufunc->uf_ret_type = &t_unknown;
1222 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1223
1224 // When the outer function is compiled for profiling or debugging, the
1225 // lambda may be called without profiling or debugging. Compile it here in
1226 // the right context.
1227 if (cctx->ctx_compile_type == CT_DEBUG
1228#ifdef FEAT_PROFILE
1229 || cctx->ctx_compile_type == CT_PROFILE
1230#endif
1231 )
1232 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1233
Bram Moolenaar139575d2022-03-15 19:29:30 +00001234 // if the outer function is not compiled for debugging or profiling, this
1235 // one might be
1236 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001237 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001238 compiletype_T compile_type = get_compile_type(ufunc);
1239
1240 if (compile_type != CT_NONE)
1241 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001242 }
1243
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1245 // "*arg" may point into it. Point into the original line to avoid a
1246 // dangling pointer.
1247 if (evalarg.eval_using_cmdline)
1248 {
1249 garray_T *gap = &evalarg.eval_tofree_ga;
1250 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1251
1252 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1253 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001254 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255 }
1256
1257 clear_evalarg(&evalarg, NULL);
1258
1259 if (ufunc->uf_def_status == UF_COMPILED)
1260 {
1261 // The return type will now be known.
1262 set_function_type(ufunc);
1263
1264 // The function reference count will be 1. When the ISN_FUNCREF
1265 // instruction is deleted the reference count is decremented and the
1266 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001267 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001268 }
1269
1270 func_ptr_unref(ufunc);
1271 return FAIL;
1272}
1273
1274/*
1275 * Get a lambda and compile it. Uses Vim9 syntax.
1276 */
1277 int
1278get_lambda_tv_and_compile(
1279 char_u **arg,
1280 typval_T *rettv,
1281 int types_optional,
1282 evalarg_T *evalarg)
1283{
1284 int r;
1285 ufunc_T *ufunc;
1286 int save_sc_version = current_sctx.sc_version;
1287
1288 // Get the funcref in "rettv".
1289 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1290 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1291 current_sctx.sc_version = save_sc_version;
1292 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001293 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294
1295 // "rettv" will now be a partial referencing the function.
1296 ufunc = rettv->vval.v_partial->pt_func;
1297
1298 // Compile it here to get the return type. The return type is optional,
1299 // when it's missing use t_unknown. This is recognized in
1300 // compile_return().
1301 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1302 ufunc->uf_ret_type = &t_unknown;
1303 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1304
1305 if (ufunc->uf_def_status == UF_COMPILED)
1306 {
1307 // The return type will now be known.
1308 set_function_type(ufunc);
1309 return OK;
1310 }
1311 clear_tv(rettv);
1312 return FAIL;
1313}
1314
1315/*
1316 * parse a dict: {key: val, [key]: val}
1317 * "*arg" points to the '{'.
1318 * ppconst->pp_is_const is set if all item values are a constant.
1319 */
1320 static int
1321compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1322{
1323 garray_T *instr = &cctx->ctx_instr;
1324 int count = 0;
1325 dict_T *d = dict_alloc();
1326 dictitem_T *item;
1327 char_u *whitep = *arg + 1;
1328 char_u *p;
1329 int is_const;
1330 int is_all_const = TRUE; // reset when non-const encountered
1331
1332 if (d == NULL)
1333 return FAIL;
1334 if (generate_ppconst(cctx, ppconst) == FAIL)
1335 return FAIL;
1336 for (;;)
1337 {
1338 char_u *key = NULL;
1339
1340 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1341 {
1342 *arg = NULL;
1343 goto failret;
1344 }
1345
1346 if (**arg == '}')
1347 break;
1348
1349 if (**arg == '[')
1350 {
1351 isn_T *isn;
1352
1353 // {[expr]: value} uses an evaluated key.
1354 *arg = skipwhite(*arg + 1);
1355 if (compile_expr0(arg, cctx) == FAIL)
1356 return FAIL;
1357 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1358 if (isn->isn_type == ISN_PUSHNR)
1359 {
1360 char buf[NUMBUFLEN];
1361
1362 // Convert to string at compile time.
1363 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1364 isn->isn_type = ISN_PUSHS;
1365 isn->isn_arg.string = vim_strsave((char_u *)buf);
1366 }
1367 if (isn->isn_type == ISN_PUSHS)
1368 key = isn->isn_arg.string;
1369 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1370 return FAIL;
1371 *arg = skipwhite(*arg);
1372 if (**arg != ']')
1373 {
1374 emsg(_(e_missing_matching_bracket_after_dict_key));
1375 return FAIL;
1376 }
1377 ++*arg;
1378 }
1379 else
1380 {
1381 // {"name": value},
1382 // {'name': value},
1383 // {name: value} use "name" as a literal key
1384 key = get_literal_key(arg);
1385 if (key == NULL)
1386 return FAIL;
1387 if (generate_PUSHS(cctx, &key) == FAIL)
1388 return FAIL;
1389 }
1390
1391 // Check for duplicate keys, if using string keys.
1392 if (key != NULL)
1393 {
1394 item = dict_find(d, key, -1);
1395 if (item != NULL)
1396 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001397 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398 goto failret;
1399 }
1400 item = dictitem_alloc(key);
1401 if (item != NULL)
1402 {
1403 item->di_tv.v_type = VAR_UNKNOWN;
1404 item->di_tv.v_lock = 0;
1405 if (dict_add(d, item) == FAIL)
1406 dictitem_free(item);
1407 }
1408 }
1409
1410 if (**arg != ':')
1411 {
1412 if (*skipwhite(*arg) == ':')
1413 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1414 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001415 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001416 return FAIL;
1417 }
1418 whitep = *arg + 1;
1419 if (!IS_WHITE_OR_NUL(*whitep))
1420 {
1421 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1422 return FAIL;
1423 }
1424
1425 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1426 {
1427 *arg = NULL;
1428 goto failret;
1429 }
1430
1431 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1432 return FAIL;
1433 if (!is_const)
1434 is_all_const = FALSE;
1435 ++count;
1436
1437 whitep = *arg;
1438 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1439 {
1440 *arg = NULL;
1441 goto failret;
1442 }
1443 if (**arg == '}')
1444 break;
1445 if (**arg != ',')
1446 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001447 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448 goto failret;
1449 }
1450 if (IS_WHITE_OR_NUL(*whitep))
1451 {
1452 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1453 return FAIL;
1454 }
1455 whitep = *arg + 1;
1456 if (!IS_WHITE_OR_NUL(*whitep))
1457 {
1458 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1459 return FAIL;
1460 }
1461 *arg = skipwhite(whitep);
1462 }
1463
1464 *arg = *arg + 1;
1465
1466 // Allow for following comment, after at least one space.
1467 p = skipwhite(*arg);
1468 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1469 *arg += STRLEN(*arg);
1470
1471 dict_unref(d);
1472 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001473 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001474
1475failret:
1476 if (*arg == NULL)
1477 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001478 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001479 *arg = (char_u *)"";
1480 }
1481 dict_unref(d);
1482 return FAIL;
1483}
1484
1485/*
1486 * Compile "&option".
1487 */
1488 static int
1489compile_get_option(char_u **arg, cctx_T *cctx)
1490{
1491 typval_T rettv;
1492 char_u *start = *arg;
1493 int ret;
1494
1495 // parse the option and get the current value to get the type.
1496 rettv.v_type = VAR_UNKNOWN;
1497 ret = eval_option(arg, &rettv, TRUE);
1498 if (ret == OK)
1499 {
1500 // include the '&' in the name, eval_option() expects it.
1501 char_u *name = vim_strnsave(start, *arg - start);
1502 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1503 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1504
1505 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1506 vim_free(name);
1507 }
1508 clear_tv(&rettv);
1509
1510 return ret;
1511}
1512
1513/*
1514 * Compile "$VAR".
1515 */
1516 static int
1517compile_get_env(char_u **arg, cctx_T *cctx)
1518{
1519 char_u *start = *arg;
1520 int len;
1521 int ret;
1522 char_u *name;
1523
1524 ++*arg;
1525 len = get_env_len(arg);
1526 if (len == 0)
1527 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001528 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001529 return FAIL;
1530 }
1531
1532 // include the '$' in the name, eval_env_var() expects it.
1533 name = vim_strnsave(start, len + 1);
1534 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1535 vim_free(name);
1536 return ret;
1537}
1538
1539/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001540 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001541 */
1542 static int
1543compile_interp_string(char_u **arg, cctx_T *cctx)
1544{
1545 typval_T tv;
1546 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001547 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001548 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001549 int count = 0;
1550 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001551
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001552 // *arg is on the '$' character, move it to the first string character.
1553 ++*arg;
1554 quote = **arg;
1555 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001556
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001557 for (;;)
1558 {
1559 // Get the string up to the matching quote or to a single '{'.
1560 // "arg" is advanced to either the quote or the '{'.
1561 if (quote == '"')
1562 ret = eval_string(arg, &tv, evaluate, TRUE);
1563 else
1564 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1565 if (ret == FAIL)
1566 break;
1567 if (evaluate)
1568 {
1569 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1570 || (**arg != '{' && count == 0))
1571 {
1572 // generate non-empty string or empty string if it's the only
1573 // one
1574 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1575 return FAIL;
1576 tv.vval.v_string = NULL; // don't free it now
1577 ++count;
1578 }
1579 clear_tv(&tv);
1580 }
1581
1582 if (**arg != '{')
1583 {
1584 // found terminating quote
1585 ++*arg;
1586 break;
1587 }
1588
1589 p = compile_one_expr_in_str(*arg, cctx);
1590 if (p == NULL)
1591 {
1592 ret = FAIL;
1593 break;
1594 }
1595 ++count;
1596 *arg = p;
1597 }
LemonBoy2eaef102022-05-06 13:14:50 +01001598
1599 if (ret == FAIL || !evaluate)
1600 return ret;
1601
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001602 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1603 if (count > 1)
1604 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001605
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001606 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001607}
1608
1609/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001610 * Compile "@r".
1611 */
1612 static int
1613compile_get_register(char_u **arg, cctx_T *cctx)
1614{
1615 int ret;
1616
1617 ++*arg;
1618 if (**arg == NUL)
1619 {
1620 semsg(_(e_syntax_error_at_str), *arg - 1);
1621 return FAIL;
1622 }
1623 if (!valid_yank_reg(**arg, FALSE))
1624 {
1625 emsg_invreg(**arg);
1626 return FAIL;
1627 }
1628 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1629 ++*arg;
1630 return ret;
1631}
1632
1633/*
1634 * Apply leading '!', '-' and '+' to constant "rettv".
1635 * When "numeric_only" is TRUE do not apply '!'.
1636 */
1637 static int
1638apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1639{
1640 char_u *p = *end;
1641
1642 // this works from end to start
1643 while (p > start)
1644 {
1645 --p;
1646 if (*p == '-' || *p == '+')
1647 {
1648 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001649 if (rettv->v_type == VAR_FLOAT)
1650 {
1651 if (*p == '-')
1652 rettv->vval.v_float = -rettv->vval.v_float;
1653 }
1654 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001655 {
1656 varnumber_T val;
1657 int error = FALSE;
1658
1659 // tv_get_number_chk() accepts a string, but we don't want that
1660 // here
1661 if (check_not_string(rettv) == FAIL)
1662 return FAIL;
1663 val = tv_get_number_chk(rettv, &error);
1664 clear_tv(rettv);
1665 if (error)
1666 return FAIL;
1667 if (*p == '-')
1668 val = -val;
1669 rettv->v_type = VAR_NUMBER;
1670 rettv->vval.v_number = val;
1671 }
1672 }
1673 else if (numeric_only)
1674 {
1675 ++p;
1676 break;
1677 }
1678 else if (*p == '!')
1679 {
1680 int v = tv2bool(rettv);
1681
1682 // '!' is permissive in the type.
1683 clear_tv(rettv);
1684 rettv->v_type = VAR_BOOL;
1685 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1686 }
1687 }
1688 *end = p;
1689 return OK;
1690}
1691
1692/*
1693 * Recognize v: variables that are constants and set "rettv".
1694 */
1695 static void
1696get_vim_constant(char_u **arg, typval_T *rettv)
1697{
1698 if (STRNCMP(*arg, "v:true", 6) == 0)
1699 {
1700 rettv->v_type = VAR_BOOL;
1701 rettv->vval.v_number = VVAL_TRUE;
1702 *arg += 6;
1703 }
1704 else if (STRNCMP(*arg, "v:false", 7) == 0)
1705 {
1706 rettv->v_type = VAR_BOOL;
1707 rettv->vval.v_number = VVAL_FALSE;
1708 *arg += 7;
1709 }
1710 else if (STRNCMP(*arg, "v:null", 6) == 0)
1711 {
1712 rettv->v_type = VAR_SPECIAL;
1713 rettv->vval.v_number = VVAL_NULL;
1714 *arg += 6;
1715 }
1716 else if (STRNCMP(*arg, "v:none", 6) == 0)
1717 {
1718 rettv->v_type = VAR_SPECIAL;
1719 rettv->vval.v_number = VVAL_NONE;
1720 *arg += 6;
1721 }
1722}
1723
1724 exprtype_T
1725get_compare_type(char_u *p, int *len, int *type_is)
1726{
1727 exprtype_T type = EXPR_UNKNOWN;
1728 int i;
1729
1730 switch (p[0])
1731 {
1732 case '=': if (p[1] == '=')
1733 type = EXPR_EQUAL;
1734 else if (p[1] == '~')
1735 type = EXPR_MATCH;
1736 break;
1737 case '!': if (p[1] == '=')
1738 type = EXPR_NEQUAL;
1739 else if (p[1] == '~')
1740 type = EXPR_NOMATCH;
1741 break;
1742 case '>': if (p[1] != '=')
1743 {
1744 type = EXPR_GREATER;
1745 *len = 1;
1746 }
1747 else
1748 type = EXPR_GEQUAL;
1749 break;
1750 case '<': if (p[1] != '=')
1751 {
1752 type = EXPR_SMALLER;
1753 *len = 1;
1754 }
1755 else
1756 type = EXPR_SEQUAL;
1757 break;
1758 case 'i': if (p[1] == 's')
1759 {
1760 // "is" and "isnot"; but not a prefix of a name
1761 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1762 *len = 5;
1763 i = p[*len];
1764 if (!isalnum(i) && i != '_')
1765 {
1766 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1767 *type_is = TRUE;
1768 }
1769 }
1770 break;
1771 }
1772 return type;
1773}
1774
1775/*
1776 * Skip over an expression, ignoring most errors.
1777 */
1778 void
1779skip_expr_cctx(char_u **arg, cctx_T *cctx)
1780{
1781 evalarg_T evalarg;
1782
1783 init_evalarg(&evalarg);
1784 evalarg.eval_cctx = cctx;
1785 skip_expr(arg, &evalarg);
1786 clear_evalarg(&evalarg, NULL);
1787}
1788
1789/*
1790 * Check that the top of the type stack has a type that can be used as a
1791 * condition. Give an error and return FAIL if not.
1792 */
1793 int
1794bool_on_stack(cctx_T *cctx)
1795{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001796 type_T *type;
1797
Bram Moolenaar078a4612022-01-04 15:17:03 +00001798 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799 if (type == &t_bool)
1800 return OK;
1801
Bram Moolenaar4913d422022-10-17 13:13:32 +01001802 if (type->tt_type == VAR_ANY
1803 || type->tt_type == VAR_UNKNOWN
1804 || type->tt_type == VAR_NUMBER
1805 || type == &t_number_bool
1806 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001807 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1808 // This requires a runtime type check.
1809 return generate_COND2BOOL(cctx);
1810
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001811 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812}
1813
1814/*
1815 * Give the "white on both sides" error, taking the operator from "p[len]".
1816 */
1817 void
1818error_white_both(char_u *op, int len)
1819{
1820 char_u buf[10];
1821
1822 vim_strncpy(buf, op, len);
1823 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1824}
1825
1826/*
1827 * Compile code to apply '-', '+' and '!'.
1828 * When "numeric_only" is TRUE do not apply '!'.
1829 */
1830 static int
1831compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1832{
1833 char_u *p = *end;
1834
1835 // this works from end to start
1836 while (p > start)
1837 {
1838 --p;
1839 while (VIM_ISWHITE(*p))
1840 --p;
1841 if (*p == '-' || *p == '+')
1842 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001843 type_T *type = get_type_on_stack(cctx, 0);
1844 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001845 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846 return FAIL;
1847
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001848 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001849 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1850 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851 }
1852 else if (numeric_only)
1853 {
1854 ++p;
1855 break;
1856 }
1857 else
1858 {
1859 int invert = *p == '!';
1860
1861 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1862 {
1863 if (p[-1] == '!')
1864 invert = !invert;
1865 --p;
1866 }
1867 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1868 return FAIL;
1869 }
1870 }
1871 *end = p;
1872 return OK;
1873}
1874
1875/*
1876 * Compile "(expression)": recursive!
1877 * Return FAIL/OK.
1878 */
1879 static int
1880compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1881{
1882 int ret;
1883 char_u *p = *arg + 1;
1884
1885 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1886 return FAIL;
1887 if (ppconst->pp_used <= PPSIZE - 10)
1888 {
1889 ret = compile_expr1(arg, cctx, ppconst);
1890 }
1891 else
1892 {
1893 // Not enough space in ppconst, flush constants.
1894 if (generate_ppconst(cctx, ppconst) == FAIL)
1895 return FAIL;
1896 ret = compile_expr0(arg, cctx);
1897 }
1898 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1899 return FAIL;
1900 if (**arg == ')')
1901 ++*arg;
1902 else if (ret == OK)
1903 {
1904 emsg(_(e_missing_closing_paren));
1905 ret = FAIL;
1906 }
1907 return ret;
1908}
1909
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001910static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001911
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001912/*
1913 * Compile whatever comes after "name" or "name()".
1914 * Advances "*arg" only when something was recognized.
1915 */
1916 static int
1917compile_subscript(
1918 char_u **arg,
1919 cctx_T *cctx,
1920 char_u *start_leader,
1921 char_u **end_leader,
1922 ppconst_T *ppconst)
1923{
1924 char_u *name_start = *end_leader;
1925 int keeping_dict = FALSE;
1926
1927 for (;;)
1928 {
1929 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001930 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001931
1932 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1933 {
1934 char_u *next = peek_next_line_from_context(cctx);
1935
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001936 // If a following line starts with "->{", "->(" or "->X" advance to
1937 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 // Also if a following line starts with ".x".
1939 if (next != NULL &&
1940 ((next[0] == '-' && next[1] == '>'
1941 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001942 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001943 || ASCII_ISALPHA(*skipwhite(next + 2))))
1944 || (next[0] == '.' && eval_isdictc(next[1]))))
1945 {
1946 next = next_line_from_context(cctx, TRUE);
1947 if (next == NULL)
1948 return FAIL;
1949 *arg = next;
1950 p = skipwhite(*arg);
1951 }
1952 }
1953
1954 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1955 // is not a function call.
1956 if (**arg == '(')
1957 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001958 int argcount = 0;
1959
1960 if (generate_ppconst(cctx, ppconst) == FAIL)
1961 return FAIL;
1962 ppconst->pp_is_const = FALSE;
1963
1964 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001965 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001966
1967 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001968 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 return FAIL;
1970 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1971 return FAIL;
1972 if (keeping_dict)
1973 {
1974 keeping_dict = FALSE;
1975 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1976 return FAIL;
1977 }
1978 }
1979 else if (*p == '-' && p[1] == '>')
1980 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001981 char_u *pstart = p;
1982 int alt;
1983 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984
Bram Moolenaarc7349932022-01-16 20:59:39 +00001985 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001986 if (generate_ppconst(cctx, ppconst) == FAIL)
1987 return FAIL;
1988 ppconst->pp_is_const = FALSE;
1989
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990 // Apply the '!', '-' and '+' first:
1991 // -1.0->func() works like (-1.0)->func()
1992 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1993 return FAIL;
1994
1995 p += 2;
1996 *arg = skipwhite(p);
1997 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001998
1999 // Three alternatives handled here:
2000 // 1. "base->name(" only a name, use compile_call()
2001 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2002 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002004 alt = 2;
2005 else
2006 {
2007 // alternative 1 or 3
2008 p = *arg;
2009 if (!eval_isnamec1(*p))
2010 {
2011 semsg(_(e_trailing_characters_str), pstart);
2012 return FAIL;
2013 }
2014 if (ASCII_ISALPHA(*p) && p[1] == ':')
2015 p += 2;
2016 for ( ; eval_isnamec(*p); ++p)
2017 ;
2018 if (*p == '(')
2019 {
2020 // alternative 1
2021 alt = 1;
2022 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2023 return FAIL;
2024 }
2025 else
2026 {
2027 // Must be alternative 3, find the "(". Only works within
2028 // one line.
2029 alt = 3;
2030 paren = vim_strchr(p, '(');
2031 if (paren == NULL)
2032 {
2033 semsg(_(e_missing_parenthesis_str), *arg);
2034 return FAIL;
2035 }
2036 }
2037 }
2038
2039 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 {
2041 int argcount = 1;
2042 garray_T *stack = &cctx->ctx_type_stack;
2043 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002044 int expr_isn_start = cctx->ctx_instr.ga_len;
2045 int expr_isn_end;
2046 int arg_isn_count;
2047
Bram Moolenaarc7349932022-01-16 20:59:39 +00002048 if (alt == 2)
2049 {
2050 // Funcref call: list->(Refs[2])(arg)
2051 // or lambda: list->((arg) => expr)(arg)
2052 //
2053 // Fist compile the function expression.
2054 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2055 return FAIL;
2056 }
2057 else
2058 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002059 int fail;
2060 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002061 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002062
Bram Moolenaarc7349932022-01-16 20:59:39 +00002063 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002064
2065 // instead of using LOADG for "import.Func" use PUSHFUNC
2066 ++paren_follows_after_expr;
2067
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002068 // do not look in the next line
2069 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002070
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002071 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002072 || *skipwhite(*arg) != NUL;
2073 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002074 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002075 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002076
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002077 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002078 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002079 if (did_emsg == prev_did_emsg)
2080 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002081 return FAIL;
2082 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002083 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002084
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085 // Compile the arguments.
2086 if (**arg != '(')
2087 {
2088 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002089 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002090 else
2091 semsg(_(e_missing_parenthesis_str), *arg);
2092 return FAIL;
2093 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002094
2095 // Remember the next instruction index, where the instructions
2096 // for arguments are being written.
2097 expr_isn_end = cctx->ctx_instr.ga_len;
2098
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002099 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002100 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2101 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002102 return FAIL;
2103
2104 // Move the instructions for the arguments to before the
2105 // instructions of the expression and move the type of the
2106 // expression after the argument types. This is what ISN_PCALL
2107 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002108 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2109 if (arg_isn_count > 0)
2110 {
2111 int expr_isn_count = expr_isn_end - expr_isn_start;
2112 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002113 type_T *decl_type;
2114 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115
2116 if (isn == NULL)
2117 return FAIL;
2118 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2119 + expr_isn_start,
2120 sizeof(isn_T) * expr_isn_count);
2121 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2122 + expr_isn_start,
2123 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2124 sizeof(isn_T) * arg_isn_count);
2125 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2126 + expr_isn_start + arg_isn_count,
2127 isn, sizeof(isn_T) * expr_isn_count);
2128 vim_free(isn);
2129
Bram Moolenaar078a4612022-01-04 15:17:03 +00002130 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2131 type = typep->type_curr;
2132 decl_type = typep->type_decl;
2133 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2134 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2135 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002136 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002137 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2138 typep->type_curr = type;
2139 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002140 }
2141
Bram Moolenaar078a4612022-01-04 15:17:03 +00002142 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002143 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2144 return FAIL;
2145 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002146
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002147 if (keeping_dict)
2148 {
2149 keeping_dict = FALSE;
2150 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2151 return FAIL;
2152 }
2153 }
2154 else if (**arg == '[')
2155 {
2156 int is_slice = FALSE;
2157
2158 // list index: list[123]
2159 // dict member: dict[key]
2160 // string index: text[123]
2161 // blob index: blob[123]
2162 if (generate_ppconst(cctx, ppconst) == FAIL)
2163 return FAIL;
2164 ppconst->pp_is_const = FALSE;
2165
2166 ++p;
2167 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2168 return FAIL;
2169 if (**arg == ':')
2170 {
2171 // missing first index is equal to zero
2172 generate_PUSHNR(cctx, 0);
2173 }
2174 else
2175 {
2176 if (compile_expr0(arg, cctx) == FAIL)
2177 return FAIL;
2178 if (**arg == ':')
2179 {
2180 semsg(_(e_white_space_required_before_and_after_str_at_str),
2181 ":", *arg);
2182 return FAIL;
2183 }
2184 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2185 return FAIL;
2186 *arg = skipwhite(*arg);
2187 }
2188 if (**arg == ':')
2189 {
2190 is_slice = TRUE;
2191 ++*arg;
2192 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2193 {
2194 semsg(_(e_white_space_required_before_and_after_str_at_str),
2195 ":", *arg);
2196 return FAIL;
2197 }
2198 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2199 return FAIL;
2200 if (**arg == ']')
2201 // missing second index is equal to end of string
2202 generate_PUSHNR(cctx, -1);
2203 else
2204 {
2205 if (compile_expr0(arg, cctx) == FAIL)
2206 return FAIL;
2207 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2208 return FAIL;
2209 *arg = skipwhite(*arg);
2210 }
2211 }
2212
2213 if (**arg != ']')
2214 {
2215 emsg(_(e_missing_closing_square_brace));
2216 return FAIL;
2217 }
2218 *arg = *arg + 1;
2219
2220 if (keeping_dict)
2221 {
2222 keeping_dict = FALSE;
2223 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2224 return FAIL;
2225 }
2226 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2227 return FAIL;
2228 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002229 else if (*p == '.'
2230 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2231 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2232 {
2233 // class member: SomeClass.varname
2234 // class method: SomeClass.SomeMethod()
2235 // class constructor: SomeClass.new()
2236 // object member: someObject.varname, this.varname
2237 // object method: someObject.SomeMethod(), this.SomeMethod()
2238 if (compile_class_object_index(cctx, arg, type) == FAIL)
2239 return FAIL;
2240 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241 else if (*p == '.' && p[1] != '.')
2242 {
2243 // dictionary member: dict.name
2244 if (generate_ppconst(cctx, ppconst) == FAIL)
2245 return FAIL;
2246 ppconst->pp_is_const = FALSE;
2247
2248 *arg = p + 1;
2249 if (IS_WHITE_OR_NUL(**arg))
2250 {
2251 emsg(_(e_missing_name_after_dot));
2252 return FAIL;
2253 }
2254 p = *arg;
2255 if (eval_isdictc(*p))
2256 while (eval_isnamec(*p))
2257 MB_PTR_ADV(p);
2258 if (p == *arg)
2259 {
2260 semsg(_(e_syntax_error_at_str), *arg);
2261 return FAIL;
2262 }
2263 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2264 return FAIL;
2265 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2266 return FAIL;
2267 keeping_dict = TRUE;
2268 *arg = p;
2269 }
2270 else
2271 break;
2272 }
2273
2274 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2275 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002276 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2277 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278 return FAIL;
2279
2280 return OK;
2281}
2282
2283/*
2284 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2285 * "arg" is advanced until after the expression, skipping white space.
2286 *
2287 * If the value is a constant "ppconst->pp_used" will be non-zero.
2288 * Before instructions are generated, any values in "ppconst" will generated.
2289 *
2290 * This is the compiling equivalent of eval1(), eval2(), etc.
2291 */
2292
2293/*
2294 * number number constant
2295 * 0zFFFFFFFF Blob constant
2296 * "string" string constant
2297 * 'string' literal string constant
2298 * &option-name option value
2299 * @r register contents
2300 * identifier variable value
2301 * function() function call
2302 * $VAR environment variable
2303 * (expression) nested expression
2304 * [expr, expr] List
2305 * {key: val, [key]: val} Dictionary
2306 *
2307 * Also handle:
2308 * ! in front logical NOT
2309 * - in front unary minus
2310 * + in front unary plus (ignored)
2311 * trailing (arg) funcref/partial call
2312 * trailing [] subscript in String or List
2313 * trailing .name entry in Dictionary
2314 * trailing ->name() method call
2315 */
2316 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002317compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002318 char_u **arg,
2319 cctx_T *cctx,
2320 ppconst_T *ppconst)
2321{
2322 char_u *start_leader, *end_leader;
2323 int ret = OK;
2324 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2325 int used_before = ppconst->pp_used;
2326
2327 ppconst->pp_is_const = FALSE;
2328
2329 /*
2330 * Skip '!', '-' and '+' characters. They are handled later.
2331 */
2332 start_leader = *arg;
2333 if (eval_leader(arg, TRUE) == FAIL)
2334 return FAIL;
2335 end_leader = *arg;
2336
2337 rettv->v_type = VAR_UNKNOWN;
2338 switch (**arg)
2339 {
2340 /*
2341 * Number constant.
2342 */
2343 case '0': // also for blob starting with 0z
2344 case '1':
2345 case '2':
2346 case '3':
2347 case '4':
2348 case '5':
2349 case '6':
2350 case '7':
2351 case '8':
2352 case '9':
2353 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2354 return FAIL;
2355 // Apply "-" and "+" just before the number now, right to
2356 // left. Matters especially when "->" follows. Stops at
2357 // '!'.
2358 if (apply_leader(rettv, TRUE,
2359 start_leader, &end_leader) == FAIL)
2360 {
2361 clear_tv(rettv);
2362 return FAIL;
2363 }
2364 break;
2365
2366 /*
2367 * String constant: "string".
2368 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002369 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370 return FAIL;
2371 break;
2372
2373 /*
2374 * Literal string constant: 'str''ing'.
2375 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002376 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002377 return FAIL;
2378 break;
2379
2380 /*
2381 * Constant Vim variable.
2382 */
2383 case 'v': get_vim_constant(arg, rettv);
2384 ret = NOTDONE;
2385 break;
2386
2387 /*
2388 * "true" constant
2389 */
2390 case 't': if (STRNCMP(*arg, "true", 4) == 0
2391 && !eval_isnamec((*arg)[4]))
2392 {
2393 *arg += 4;
2394 rettv->v_type = VAR_BOOL;
2395 rettv->vval.v_number = VVAL_TRUE;
2396 }
2397 else
2398 ret = NOTDONE;
2399 break;
2400
2401 /*
2402 * "false" constant
2403 */
2404 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2405 && !eval_isnamec((*arg)[5]))
2406 {
2407 *arg += 5;
2408 rettv->v_type = VAR_BOOL;
2409 rettv->vval.v_number = VVAL_FALSE;
2410 }
2411 else
2412 ret = NOTDONE;
2413 break;
2414
2415 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002416 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002417 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002418 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002419 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002420 char_u *p = *arg + 4;
2421 int len;
2422
2423 for (len = 0; eval_isnamec(p[len]); ++len)
2424 ;
2425 ret = handle_predefined(*arg, len + 4, rettv);
2426 if (ret == FAIL)
2427 ret = NOTDONE;
2428 else
2429 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 }
2431 else
2432 ret = NOTDONE;
2433 break;
2434
2435 /*
2436 * List: [expr, expr]
2437 */
2438 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2439 return FAIL;
2440 ret = compile_list(arg, cctx, ppconst);
2441 break;
2442
2443 /*
2444 * Dictionary: {'key': val, 'key': val}
2445 */
2446 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2447 return FAIL;
2448 ret = compile_dict(arg, cctx, ppconst);
2449 break;
2450
2451 /*
2452 * Option value: &name
2453 */
2454 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2455 return FAIL;
2456 ret = compile_get_option(arg, cctx);
2457 break;
2458
2459 /*
2460 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002461 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 */
2463 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2464 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002465 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2466 ret = compile_interp_string(arg, cctx);
2467 else
2468 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002469 break;
2470
2471 /*
2472 * Register contents: @r.
2473 */
2474 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2475 return FAIL;
2476 ret = compile_get_register(arg, cctx);
2477 break;
2478 /*
2479 * nested expression: (expression).
2480 * lambda: (arg, arg) => expr
2481 * funcref: (arg, arg) => { statement }
2482 */
2483 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2484 ret = compile_lambda(arg, cctx);
2485 if (ret == NOTDONE)
2486 ret = compile_parenthesis(arg, cctx, ppconst);
2487 break;
2488
2489 default: ret = NOTDONE;
2490 break;
2491 }
2492 if (ret == FAIL)
2493 return FAIL;
2494
2495 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2496 {
2497 if (cctx->ctx_skip == SKIP_YES)
2498 clear_tv(rettv);
2499 else
2500 // A constant expression can possibly be handled compile time,
2501 // return the value instead of generating code.
2502 ++ppconst->pp_used;
2503 }
2504 else if (ret == NOTDONE)
2505 {
2506 char_u *p;
2507 int r;
2508
2509 if (!eval_isnamec1(**arg))
2510 {
2511 if (!vim9_bad_comment(*arg))
2512 {
2513 if (ends_excmd(*skipwhite(*arg)))
2514 semsg(_(e_empty_expression_str), *arg);
2515 else
2516 semsg(_(e_name_expected_str), *arg);
2517 }
2518 return FAIL;
2519 }
2520
2521 // "name" or "name()"
2522 p = to_name_end(*arg, TRUE);
2523 if (p - *arg == (size_t)1 && **arg == '_')
2524 {
2525 emsg(_(e_cannot_use_underscore_here));
2526 return FAIL;
2527 }
2528
2529 if (*p == '(')
2530 {
2531 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2532 }
2533 else
2534 {
2535 if (cctx->ctx_skip != SKIP_YES
2536 && generate_ppconst(cctx, ppconst) == FAIL)
2537 return FAIL;
2538 r = compile_load(arg, p, cctx, TRUE, TRUE);
2539 }
2540 if (r == FAIL)
2541 return FAIL;
2542 }
2543
2544 // Handle following "[]", ".member", etc.
2545 // Then deal with prefixed '-', '+' and '!', if not done already.
2546 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2547 ppconst) == FAIL)
2548 return FAIL;
2549 if (ppconst->pp_used > 0)
2550 {
2551 // apply the '!', '-' and '+' before the constant
2552 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2553 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2554 return FAIL;
2555 return OK;
2556 }
2557 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2558 return FAIL;
2559 return OK;
2560}
2561
2562/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002563 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002564 */
2565 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002566compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002567{
2568 type_T *want_type = NULL;
2569
2570 // Recognize <type>
2571 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2572 {
2573 ++*arg;
2574 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2575 if (want_type == NULL)
2576 return FAIL;
2577
2578 if (**arg != '>')
2579 {
2580 if (*skipwhite(*arg) == '>')
2581 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2582 else
2583 emsg(_(e_missing_gt));
2584 return FAIL;
2585 }
2586 ++*arg;
2587 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2588 return FAIL;
2589 }
2590
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002591 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002592 return FAIL;
2593
2594 if (want_type != NULL)
2595 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002596 type_T *actual;
2597 where_T where = WHERE_INIT;
2598
2599 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002600 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002601 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002602 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002603 if (need_type(actual, want_type, FALSE,
2604 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002605 return FAIL;
2606 }
2607 }
2608
2609 return OK;
2610}
2611
2612/*
2613 * * number multiplication
2614 * / number division
2615 * % number modulo
2616 */
2617 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002618compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002619{
2620 char_u *op;
2621 char_u *next;
2622 int ppconst_used = ppconst->pp_used;
2623
2624 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002625 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002626 return FAIL;
2627
2628 /*
2629 * Repeat computing, until no "*", "/" or "%" is following.
2630 */
2631 for (;;)
2632 {
2633 op = may_peek_next_line(cctx, *arg, &next);
2634 if (*op != '*' && *op != '/' && *op != '%')
2635 break;
2636 if (next != NULL)
2637 {
2638 *arg = next_line_from_context(cctx, TRUE);
2639 op = skipwhite(*arg);
2640 }
2641
2642 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2643 {
2644 error_white_both(op, 1);
2645 return FAIL;
2646 }
2647 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2648 return FAIL;
2649
2650 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002651 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002652 return FAIL;
2653
2654 if (ppconst->pp_used == ppconst_used + 2
2655 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2656 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2657 {
2658 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2659 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2660 varnumber_T res = 0;
2661 int failed = FALSE;
2662
2663 // both are numbers: compute the result
2664 switch (*op)
2665 {
2666 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2667 break;
2668 case '/': res = num_divide(tv1->vval.v_number,
2669 tv2->vval.v_number, &failed);
2670 break;
2671 case '%': res = num_modulus(tv1->vval.v_number,
2672 tv2->vval.v_number, &failed);
2673 break;
2674 }
2675 if (failed)
2676 return FAIL;
2677 tv1->vval.v_number = res;
2678 --ppconst->pp_used;
2679 }
2680 else
2681 {
2682 generate_ppconst(cctx, ppconst);
2683 generate_two_op(cctx, op);
2684 }
2685 }
2686
2687 return OK;
2688}
2689
2690/*
2691 * + number addition or list/blobl concatenation
2692 * - number subtraction
2693 * .. string concatenation
2694 */
2695 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002696compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697{
2698 char_u *op;
2699 char_u *next;
2700 int oplen;
2701 int ppconst_used = ppconst->pp_used;
2702
2703 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002704 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002705 return FAIL;
2706
2707 /*
2708 * Repeat computing, until no "+", "-" or ".." is following.
2709 */
2710 for (;;)
2711 {
2712 op = may_peek_next_line(cctx, *arg, &next);
2713 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2714 break;
2715 if (op[0] == op[1] && *op != '.' && next)
2716 // Finding "++" or "--" on the next line is a separate command.
2717 // But ".." is concatenation.
2718 break;
2719 oplen = (*op == '.' ? 2 : 1);
2720 if (next != NULL)
2721 {
2722 *arg = next_line_from_context(cctx, TRUE);
2723 op = skipwhite(*arg);
2724 }
2725
2726 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2727 {
2728 error_white_both(op, oplen);
2729 return FAIL;
2730 }
2731
2732 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2733 return FAIL;
2734
2735 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002736 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002737 return FAIL;
2738
2739 if (ppconst->pp_used == ppconst_used + 2
2740 && (*op == '.'
2741 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2742 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2743 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2744 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2745 {
2746 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2747 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2748
2749 // concat/subtract/add constant numbers
2750 if (*op == '+')
2751 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2752 else if (*op == '-')
2753 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2754 else
2755 {
2756 // concatenate constant strings
2757 char_u *s1 = tv1->vval.v_string;
2758 char_u *s2 = tv2->vval.v_string;
2759 size_t len1 = STRLEN(s1);
2760
2761 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2762 if (tv1->vval.v_string == NULL)
2763 {
2764 clear_ppconst(ppconst);
2765 return FAIL;
2766 }
2767 mch_memmove(tv1->vval.v_string, s1, len1);
2768 STRCPY(tv1->vval.v_string + len1, s2);
2769 vim_free(s1);
2770 vim_free(s2);
2771 }
2772 --ppconst->pp_used;
2773 }
2774 else
2775 {
2776 generate_ppconst(cctx, ppconst);
2777 ppconst->pp_is_const = FALSE;
2778 if (*op == '.')
2779 {
2780 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2781 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2782 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002783 if (generate_CONCAT(cctx, 2) == FAIL)
2784 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002785 }
2786 else
2787 generate_two_op(cctx, op);
2788 }
2789 }
2790
2791 return OK;
2792}
2793
2794/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002795 * expr6a >> expr6b
2796 * expr6a << expr6b
2797 *
2798 * Produces instructions:
2799 * OPNR bitwise left or right shift
2800 */
2801 static int
2802compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2803{
2804 exprtype_T type = EXPR_UNKNOWN;
2805 char_u *p;
2806 char_u *next;
2807 int len = 2;
2808 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002809 isn_T *isn;
2810
2811 // get the first variable
2812 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2813 return FAIL;
2814
2815 /*
2816 * Repeat computing, until no "+", "-" or ".." is following.
2817 */
2818 for (;;)
2819 {
2820 type = EXPR_UNKNOWN;
2821
2822 p = may_peek_next_line(cctx, *arg, &next);
2823 if (p[0] == '<' && p[1] == '<')
2824 type = EXPR_LSHIFT;
2825 else if (p[0] == '>' && p[1] == '>')
2826 type = EXPR_RSHIFT;
2827
2828 if (type == EXPR_UNKNOWN)
2829 return OK;
2830
2831 // Handle a bitwise left or right shift operator
2832 if (ppconst->pp_used == ppconst_used + 1)
2833 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002834 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002835 {
2836 // left operand should be a number
2837 emsg(_(e_bitshift_ops_must_be_number));
2838 return FAIL;
2839 }
2840 }
2841 else
2842 {
2843 type_T *t = get_type_on_stack(cctx, 0);
2844
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002845 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002846 {
2847 emsg(_(e_bitshift_ops_must_be_number));
2848 return FAIL;
2849 }
2850 }
2851
2852 if (next != NULL)
2853 {
2854 *arg = next_line_from_context(cctx, TRUE);
2855 p = skipwhite(*arg);
2856 }
2857
2858 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2859 {
2860 error_white_both(p, len);
2861 return FAIL;
2862 }
2863
2864 // get the second variable
2865 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2866 return FAIL;
2867
2868 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2869 return FAIL;
2870
2871 if (ppconst->pp_used == ppconst_used + 2)
2872 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002873 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2874 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2875
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002876 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002877 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2878 {
2879 // right operand should be a positive number
2880 if (tv2->v_type != VAR_NUMBER)
2881 emsg(_(e_bitshift_ops_must_be_number));
2882 else
dundargocc57b5bc2022-11-02 13:30:51 +00002883 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002884 return FAIL;
2885 }
2886
2887 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2888 tv1->vval.v_number = 0;
2889 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002890 tv1->vval.v_number =
2891 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002892 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002893 tv1->vval.v_number =
2894 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002895 clear_tv(tv2);
2896 --ppconst->pp_used;
2897 }
2898 else
2899 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002900 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2901 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002902 {
2903 emsg(_(e_bitshift_ops_must_be_number));
2904 return FAIL;
2905 }
2906
2907 generate_ppconst(cctx, ppconst);
2908
2909 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2910 if (isn == NULL)
2911 return FAIL;
2912
2913 if (isn != NULL)
2914 isn->isn_arg.op.op_type = type;
2915 }
2916 }
2917
2918 return OK;
2919}
2920
2921/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002922 * expr5a == expr5b
2923 * expr5a =~ expr5b
2924 * expr5a != expr5b
2925 * expr5a !~ expr5b
2926 * expr5a > expr5b
2927 * expr5a >= expr5b
2928 * expr5a < expr5b
2929 * expr5a <= expr5b
2930 * expr5a is expr5b
2931 * expr5a isnot expr5b
2932 *
2933 * Produces instructions:
2934 * EVAL expr5a Push result of "expr5a"
2935 * EVAL expr5b Push result of "expr5b"
2936 * COMPARE one of the compare instructions
2937 */
2938 static int
2939compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2940{
2941 exprtype_T type = EXPR_UNKNOWN;
2942 char_u *p;
2943 char_u *next;
2944 int len = 2;
2945 int type_is = FALSE;
2946 int ppconst_used = ppconst->pp_used;
2947
2948 // get the first variable
2949 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2950 return FAIL;
2951
2952 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002953
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002954 type = get_compare_type(p, &len, &type_is);
2955
2956 /*
2957 * If there is a comparative operator, use it.
2958 */
2959 if (type != EXPR_UNKNOWN)
2960 {
2961 int ic = FALSE; // Default: do not ignore case
2962
2963 if (next != NULL)
2964 {
2965 *arg = next_line_from_context(cctx, TRUE);
2966 p = skipwhite(*arg);
2967 }
2968 if (type_is && (p[len] == '?' || p[len] == '#'))
2969 {
2970 semsg(_(e_invalid_expression_str), *arg);
2971 return FAIL;
2972 }
2973 // extra question mark appended: ignore case
2974 if (p[len] == '?')
2975 {
2976 ic = TRUE;
2977 ++len;
2978 }
2979 // extra '#' appended: match case (ignored)
2980 else if (p[len] == '#')
2981 ++len;
2982 // nothing appended: match case
2983
2984 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2985 {
2986 error_white_both(p, len);
2987 return FAIL;
2988 }
2989
2990 // get the second variable
2991 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2992 return FAIL;
2993
2994 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2995 return FAIL;
2996
2997 if (ppconst->pp_used == ppconst_used + 2)
2998 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002999 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003000 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3001 int ret;
3002
3003 // Both sides are a constant, compute the result now.
3004 // First check for a valid combination of types, this is more
3005 // strict than typval_compare().
3006 if (check_compare_types(type, tv1, tv2) == FAIL)
3007 ret = FAIL;
3008 else
3009 {
3010 ret = typval_compare(tv1, tv2, type, ic);
3011 tv1->v_type = VAR_BOOL;
3012 tv1->vval.v_number = tv1->vval.v_number
3013 ? VVAL_TRUE : VVAL_FALSE;
3014 clear_tv(tv2);
3015 --ppconst->pp_used;
3016 }
3017 return ret;
3018 }
3019
3020 generate_ppconst(cctx, ppconst);
3021 return generate_COMPARE(cctx, type, ic);
3022 }
3023
3024 return OK;
3025}
3026
3027static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3028
3029/*
3030 * Compile || or &&.
3031 */
3032 static int
3033compile_and_or(
3034 char_u **arg,
3035 cctx_T *cctx,
3036 char *op,
3037 ppconst_T *ppconst,
3038 int ppconst_used UNUSED)
3039{
3040 char_u *next;
3041 char_u *p = may_peek_next_line(cctx, *arg, &next);
3042 int opchar = *op;
3043
3044 if (p[0] == opchar && p[1] == opchar)
3045 {
3046 garray_T *instr = &cctx->ctx_instr;
3047 garray_T end_ga;
3048 int save_skip = cctx->ctx_skip;
3049
3050 /*
3051 * Repeat until there is no following "||" or "&&"
3052 */
3053 ga_init2(&end_ga, sizeof(int), 10);
3054 while (p[0] == opchar && p[1] == opchar)
3055 {
3056 long start_lnum = SOURCING_LNUM;
3057 long save_sourcing_lnum;
3058 int start_ctx_lnum = cctx->ctx_lnum;
3059 int save_lnum;
3060 int const_used;
3061 int status;
3062 jumpwhen_T jump_when = opchar == '|'
3063 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3064
3065 if (next != NULL)
3066 {
3067 *arg = next_line_from_context(cctx, TRUE);
3068 p = skipwhite(*arg);
3069 }
3070
3071 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3072 {
3073 semsg(_(e_white_space_required_before_and_after_str_at_str),
3074 op, p);
3075 ga_clear(&end_ga);
3076 return FAIL;
3077 }
3078
3079 save_sourcing_lnum = SOURCING_LNUM;
3080 SOURCING_LNUM = start_lnum;
3081 save_lnum = cctx->ctx_lnum;
3082 cctx->ctx_lnum = start_ctx_lnum;
3083
3084 status = check_ppconst_bool(ppconst);
3085 if (status != FAIL)
3086 {
3087 // Use the last ppconst if possible.
3088 if (ppconst->pp_used > 0)
3089 {
3090 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3091 int is_true = tv2bool(tv);
3092
3093 if ((is_true && opchar == '|')
3094 || (!is_true && opchar == '&'))
3095 {
3096 // For "false && expr" and "true || expr" the "expr"
3097 // does not need to be evaluated.
3098 cctx->ctx_skip = SKIP_YES;
3099 clear_tv(tv);
3100 tv->v_type = VAR_BOOL;
3101 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3102 }
3103 else
3104 {
3105 // For "true && expr" and "false || expr" only "expr"
3106 // needs to be evaluated.
3107 --ppconst->pp_used;
3108 jump_when = JUMP_NEVER;
3109 }
3110 }
3111 else
3112 {
3113 // Every part must evaluate to a bool.
3114 status = bool_on_stack(cctx);
3115 }
3116 }
3117 if (status != FAIL)
3118 status = ga_grow(&end_ga, 1);
3119 cctx->ctx_lnum = save_lnum;
3120 if (status == FAIL)
3121 {
3122 ga_clear(&end_ga);
3123 return FAIL;
3124 }
3125
3126 if (jump_when != JUMP_NEVER)
3127 {
3128 if (cctx->ctx_skip != SKIP_YES)
3129 {
3130 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3131 ++end_ga.ga_len;
3132 }
3133 generate_JUMP(cctx, jump_when, 0);
3134 }
3135
3136 // eval the next expression
3137 SOURCING_LNUM = save_sourcing_lnum;
3138 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3139 {
3140 ga_clear(&end_ga);
3141 return FAIL;
3142 }
3143
3144 const_used = ppconst->pp_used;
3145 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3146 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3147 {
3148 ga_clear(&end_ga);
3149 return FAIL;
3150 }
3151
3152 // "0 || 1" results in true, "1 && 0" results in false.
3153 if (ppconst->pp_used == const_used + 1)
3154 {
3155 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3156
3157 if (tv->v_type == VAR_NUMBER
3158 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3159 {
3160 tv->vval.v_number = tv->vval.v_number == 1
3161 ? VVAL_TRUE : VVAL_FALSE;
3162 tv->v_type = VAR_BOOL;
3163 }
3164 }
3165
3166 p = may_peek_next_line(cctx, *arg, &next);
3167 }
3168
3169 if (check_ppconst_bool(ppconst) == FAIL)
3170 {
3171 ga_clear(&end_ga);
3172 return FAIL;
3173 }
3174
3175 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3176 // Every part must evaluate to a bool.
3177 if (bool_on_stack(cctx) == FAIL)
3178 {
3179 ga_clear(&end_ga);
3180 return FAIL;
3181 }
3182
3183 if (end_ga.ga_len > 0)
3184 {
3185 // Fill in the end label in all jumps.
3186 generate_ppconst(cctx, ppconst);
3187 while (end_ga.ga_len > 0)
3188 {
3189 isn_T *isn;
3190
3191 --end_ga.ga_len;
3192 isn = ((isn_T *)instr->ga_data)
3193 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3194 isn->isn_arg.jump.jump_where = instr->ga_len;
3195 }
3196 }
3197 ga_clear(&end_ga);
3198
3199 cctx->ctx_skip = save_skip;
3200 }
3201
3202 return OK;
3203}
3204
3205/*
3206 * expr4a && expr4a && expr4a logical AND
3207 *
3208 * Produces instructions:
3209 * EVAL expr4a Push result of "expr4a"
3210 * COND2BOOL convert to bool if needed
3211 * JUMP_IF_COND_FALSE end
3212 * EVAL expr4b Push result of "expr4b"
3213 * JUMP_IF_COND_FALSE end
3214 * EVAL expr4c Push result of "expr4c"
3215 * end:
3216 */
3217 static int
3218compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3219{
3220 int ppconst_used = ppconst->pp_used;
3221
3222 // get the first variable
3223 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3224 return FAIL;
3225
3226 // || and && work almost the same
3227 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3228}
3229
3230/*
3231 * expr3a || expr3b || expr3c logical OR
3232 *
3233 * Produces instructions:
3234 * EVAL expr3a Push result of "expr3a"
3235 * COND2BOOL convert to bool if needed
3236 * JUMP_IF_COND_TRUE end
3237 * EVAL expr3b Push result of "expr3b"
3238 * JUMP_IF_COND_TRUE end
3239 * EVAL expr3c Push result of "expr3c"
3240 * end:
3241 */
3242 static int
3243compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3244{
3245 int ppconst_used = ppconst->pp_used;
3246
3247 // eval the first expression
3248 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3249 return FAIL;
3250
3251 // || and && work almost the same
3252 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3253}
3254
3255/*
3256 * Toplevel expression: expr2 ? expr1a : expr1b
3257 * Produces instructions:
3258 * EVAL expr2 Push result of "expr2"
3259 * JUMP_IF_FALSE alt jump if false
3260 * EVAL expr1a
3261 * JUMP_ALWAYS end
3262 * alt: EVAL expr1b
3263 * end:
3264 *
3265 * Toplevel expression: expr2 ?? expr1
3266 * Produces instructions:
3267 * EVAL expr2 Push result of "expr2"
3268 * JUMP_AND_KEEP_IF_TRUE end jump if true
3269 * EVAL expr1
3270 * end:
3271 */
3272 int
3273compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3274{
3275 char_u *p;
3276 int ppconst_used = ppconst->pp_used;
3277 char_u *next;
3278
3279 // Ignore all kinds of errors when not producing code.
3280 if (cctx->ctx_skip == SKIP_YES)
3281 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003282 int prev_did_emsg = did_emsg;
3283
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003284 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003285 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003286 }
3287
3288 // Evaluate the first expression.
3289 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3290 return FAIL;
3291
3292 p = may_peek_next_line(cctx, *arg, &next);
3293 if (*p == '?')
3294 {
3295 int op_falsy = p[1] == '?';
3296 garray_T *instr = &cctx->ctx_instr;
3297 garray_T *stack = &cctx->ctx_type_stack;
3298 int alt_idx = instr->ga_len;
3299 int end_idx = 0;
3300 isn_T *isn;
3301 type_T *type1 = NULL;
3302 int has_const_expr = FALSE;
3303 int const_value = FALSE;
3304 int save_skip = cctx->ctx_skip;
3305
3306 if (next != NULL)
3307 {
3308 *arg = next_line_from_context(cctx, TRUE);
3309 p = skipwhite(*arg);
3310 }
3311
3312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3313 {
3314 semsg(_(e_white_space_required_before_and_after_str_at_str),
3315 op_falsy ? "??" : "?", p);
3316 return FAIL;
3317 }
3318
3319 if (ppconst->pp_used == ppconst_used + 1)
3320 {
3321 // the condition is a constant, we know whether the ? or the :
3322 // expression is to be evaluated.
3323 has_const_expr = TRUE;
3324 if (op_falsy)
3325 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3326 else
3327 {
3328 int error = FALSE;
3329
3330 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3331 &error);
3332 if (error)
3333 return FAIL;
3334 }
3335 cctx->ctx_skip = save_skip == SKIP_YES ||
3336 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3337
3338 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3339 // "left ?? right" and "left" is truthy: produce "left"
3340 generate_ppconst(cctx, ppconst);
3341 else
3342 {
3343 clear_tv(&ppconst->pp_tv[ppconst_used]);
3344 --ppconst->pp_used;
3345 }
3346 }
3347 else
3348 {
3349 generate_ppconst(cctx, ppconst);
3350 if (op_falsy)
3351 end_idx = instr->ga_len;
3352 generate_JUMP(cctx, op_falsy
3353 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3354 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003355 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003356 }
3357
3358 // evaluate the second expression; any type is accepted
3359 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3360 return FAIL;
3361 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3362 return FAIL;
3363
3364 if (!has_const_expr)
3365 {
3366 generate_ppconst(cctx, ppconst);
3367
3368 if (!op_falsy)
3369 {
3370 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003371 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003372 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003373
3374 end_idx = instr->ga_len;
3375 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3376
3377 // jump here from JUMP_IF_FALSE
3378 isn = ((isn_T *)instr->ga_data) + alt_idx;
3379 isn->isn_arg.jump.jump_where = instr->ga_len;
3380 }
3381 }
3382
3383 if (!op_falsy)
3384 {
3385 // Check for the ":".
3386 p = may_peek_next_line(cctx, *arg, &next);
3387 if (*p != ':')
3388 {
3389 emsg(_(e_missing_colon_after_questionmark));
3390 return FAIL;
3391 }
3392 if (next != NULL)
3393 {
3394 *arg = next_line_from_context(cctx, TRUE);
3395 p = skipwhite(*arg);
3396 }
3397
3398 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3399 {
3400 semsg(_(e_white_space_required_before_and_after_str_at_str),
3401 ":", p);
3402 return FAIL;
3403 }
3404
3405 // evaluate the third expression
3406 if (has_const_expr)
3407 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3408 ? SKIP_YES : SKIP_NOT;
3409 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3410 return FAIL;
3411 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3412 return FAIL;
3413 }
3414
3415 if (!has_const_expr)
3416 {
3417 type_T **typep;
3418
3419 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003420 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003421
3422 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003423 typep = &((((type2_T *)stack->ga_data)
3424 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003425 common_type(type1, *typep, typep, cctx->ctx_type_list);
3426
3427 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3428 isn = ((isn_T *)instr->ga_data) + end_idx;
3429 isn->isn_arg.jump.jump_where = instr->ga_len;
3430 }
3431
3432 cctx->ctx_skip = save_skip;
3433 }
3434 return OK;
3435}
3436
3437/*
3438 * Toplevel expression.
3439 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3440 * Returns OK or FAIL.
3441 */
3442 int
3443compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3444{
3445 ppconst_T ppconst;
3446
3447 CLEAR_FIELD(ppconst);
3448 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3449 {
3450 clear_ppconst(&ppconst);
3451 return FAIL;
3452 }
3453 if (is_const != NULL)
3454 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3455 if (generate_ppconst(cctx, &ppconst) == FAIL)
3456 return FAIL;
3457 return OK;
3458}
3459
3460/*
3461 * Toplevel expression.
3462 */
3463 int
3464compile_expr0(char_u **arg, cctx_T *cctx)
3465{
3466 return compile_expr0_ext(arg, cctx, NULL);
3467}
3468
3469
3470#endif // defined(FEAT_EVAL)