blob: 3acb97bef6c491cebd747dc28a3d9c1a3293bc67 [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;
736 expr_res = compile_expr0(&s, cctx);
737 s = skipwhite(s);
738 trailing_error = *s != NUL;
739
740 if (expr_res == FAIL || trailing_error
741 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
742 {
743 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000744 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 clear_instr_ga(&cctx->ctx_instr);
746 cctx->ctx_instr = save_ga;
747 ++cctx->ctx_type_stack.ga_len;
748 return FAIL;
749 }
750
751 // Move the generated instructions into the ISN_INSTR instruction, then
752 // restore the list of instructions.
753 instr_count = cctx->ctx_instr.ga_len;
754 instr = cctx->ctx_instr.ga_data;
755 instr[instr_count].isn_type = ISN_FINISH;
756
757 cctx->ctx_instr = save_ga;
758 vim_free(isn->isn_arg.string);
759 isn->isn_type = ISN_INSTR;
760 isn->isn_arg.instr = instr;
761 return OK;
762}
763
764/*
765 * Compile the argument expressions.
766 * "arg" points to just after the "(" and is advanced to after the ")"
767 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100768 int
LemonBoyf3b48952022-05-05 13:53:03 +0100769compile_arguments(
770 char_u **arg,
771 cctx_T *cctx,
772 int *argcount,
773 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774{
775 char_u *p = *arg;
776 char_u *whitep = *arg;
777 int must_end = FALSE;
778 int instr_count;
779
780 for (;;)
781 {
782 if (may_get_next_line(whitep, &p, cctx) == FAIL)
783 goto failret;
784 if (*p == ')')
785 {
786 *arg = p + 1;
787 return OK;
788 }
789 if (must_end)
790 {
791 semsg(_(e_missing_comma_before_argument_str), p);
792 return FAIL;
793 }
794
795 instr_count = cctx->ctx_instr.ga_len;
796 if (compile_expr0(&p, cctx) == FAIL)
797 return FAIL;
798 ++*argcount;
799
LemonBoyf3b48952022-05-05 13:53:03 +0100800 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 && cctx->ctx_instr.ga_len == instr_count + 1)
802 {
803 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
804
805 // {skip} argument of searchpair() can be compiled if not empty
806 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100807 compile_string(isn, cctx, 0);
808 }
809 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
810 && cctx->ctx_instr.ga_len == instr_count + 1)
811 {
812 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
813
814 // {sub} argument of substitute() can be compiled if it starts
815 // with \=
816 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100817 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100818 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 }
820
821 if (*p != ',' && *skipwhite(p) == ',')
822 {
823 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
824 p = skipwhite(p);
825 }
826 if (*p == ',')
827 {
828 ++p;
829 if (*p != NUL && !VIM_ISWHITE(*p))
830 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
831 }
832 else
833 must_end = TRUE;
834 whitep = p;
835 p = skipwhite(p);
836 }
837failret:
838 emsg(_(e_missing_closing_paren));
839 return FAIL;
840}
841
842/*
843 * Compile a function call: name(arg1, arg2)
844 * "arg" points to "name", "arg + varlen" to the "(".
845 * "argcount_init" is 1 for "value->method()"
846 * Instructions:
847 * EVAL arg1
848 * EVAL arg2
849 * BCALL / DCALL / UCALL
850 */
851 static int
852compile_call(
853 char_u **arg,
854 size_t varlen,
855 cctx_T *cctx,
856 ppconst_T *ppconst,
857 int argcount_init)
858{
859 char_u *name = *arg;
860 char_u *p;
861 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100862 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863 char_u fname_buf[FLEN_FIXED + 1];
864 char_u *tofree = NULL;
865 int error = FCERR_NONE;
866 ufunc_T *ufunc = NULL;
867 int res = FAIL;
868 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000869 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100870 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000871 imported_T *import;
872
873 if (varlen >= sizeof(namebuf))
874 {
875 semsg(_(e_name_too_long_str), name);
876 return FAIL;
877 }
878 vim_strncpy(namebuf, *arg, varlen);
879
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000880 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000881 if (import != NULL)
882 {
883 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
884 return FAIL;
885 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886
887 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100888 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100890 if ((varlen == 3
891 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
893 {
894 char_u *s = skipwhite(*arg + varlen + 1);
895 typval_T argvars[2];
896 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100897 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898
899 argvars[0].v_type = VAR_UNKNOWN;
900 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100901 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000902 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100903 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904 s = skipwhite(s);
905 if (*s == ')' && argvars[0].v_type == VAR_STRING
906 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
907 || !is_has))
908 {
909 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
910
911 *arg = s + 1;
912 argvars[1].v_type = VAR_UNKNOWN;
913 tv->v_type = VAR_NUMBER;
914 tv->vval.v_number = 0;
915 if (is_has)
916 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100917 else if (is_len)
918 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000919 else
920 f_exists(argvars, tv);
921 clear_tv(&argvars[0]);
922 ++ppconst->pp_used;
923 return OK;
924 }
925 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100926 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 {
928 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
929 return FAIL;
930 }
931 }
932
933 if (generate_ppconst(cctx, ppconst) == FAIL)
934 return FAIL;
935
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
937
938 // We handle the "skip" argument of searchpair() and searchpairpos()
939 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100940 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
941 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
942 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
943 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
944 special_fn = CA_SEARCHPAIR;
945 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
946 special_fn = CA_SUBSTITUTE;
947 else
948 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949
950 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100951 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000952 goto theend;
953
954 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
955 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
956 {
957 int idx;
958
959 // builtin function
960 idx = find_internal_func(name);
961 if (idx >= 0)
962 {
963 if (STRCMP(name, "flatten") == 0)
964 {
965 emsg(_(e_cannot_use_flatten_in_vim9_script));
966 goto theend;
967 }
968
969 if (STRCMP(name, "add") == 0 && argcount == 2)
970 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000971 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972
973 // add() can be compiled to instructions if we know the type
974 if (type->tt_type == VAR_LIST)
975 {
976 // inline "add(list, item)" so that the type can be checked
977 res = generate_LISTAPPEND(cctx);
978 idx = -1;
979 }
980 else if (type->tt_type == VAR_BLOB)
981 {
982 // inline "add(blob, nr)" so that the type can be checked
983 res = generate_BLOBAPPEND(cctx);
984 idx = -1;
985 }
986 }
987
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100988 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
989 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100990 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100991 // May have the "D" or "R" flag, reserve a variable for a
992 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100993 if (get_defer_var_idx(cctx) == 0)
994 idx = -1;
995 }
996
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000997 if (idx >= 0)
998 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
999 }
1000 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001001 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001002 goto theend;
1003 }
1004
Bram Moolenaar62aec932022-01-29 21:45:34 +00001005 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1006
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 // An argument or local variable can be a function reference, this
1008 // overrules a function name.
1009 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1010 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1011 {
1012 // If we can find the function by name generate the right call.
1013 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001014 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001015 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001016 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001017 if (!func_is_global(ufunc))
1018 {
1019 res = generate_CALL(cctx, ufunc, argcount);
1020 goto theend;
1021 }
1022 if (!has_g_namespace
1023 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1024 {
1025 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001026 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001027 goto theend;
1028 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001029 }
1030 }
1031
1032 // If the name is a variable, load it and use PCALL.
1033 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001034 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001035 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001036 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001037 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1038 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001039 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001040
1041 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1042 goto theend;
1043 }
1044
1045 // If we can find a global function by name generate the right call.
1046 if (ufunc != NULL)
1047 {
1048 res = generate_CALL(cctx, ufunc, argcount);
1049 goto theend;
1050 }
1051
1052 // A global function may be defined only later. Need to figure out at
1053 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001054 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001055 res = generate_UCALL(cctx, name, argcount);
1056 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001057 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001058
1059theend:
1060 vim_free(tofree);
1061 return res;
1062}
1063
1064// like NAMESPACE_CHAR but with 'a' and 'l'.
1065#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1066
1067/*
1068 * Find the end of a variable or function name. Unlike find_name_end() this
1069 * does not recognize magic braces.
1070 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1071 * Return a pointer to just after the name. Equal to "arg" if there is no
1072 * valid name.
1073 */
1074 char_u *
1075to_name_end(char_u *arg, int use_namespace)
1076{
1077 char_u *p;
1078
1079 // Quick check for valid starting character.
1080 if (!eval_isnamec1(*arg))
1081 return arg;
1082
1083 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1084 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1085 // and can be used in slice "[n:]".
1086 if (*p == ':' && (p != arg + 1
1087 || !use_namespace
1088 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1089 break;
1090 return p;
1091}
1092
1093/*
1094 * Like to_name_end() but also skip over a list or dict constant.
1095 * Also accept "<SNR>123_Func".
1096 * This intentionally does not handle line continuation.
1097 */
1098 char_u *
1099to_name_const_end(char_u *arg)
1100{
1101 char_u *p = arg;
1102 typval_T rettv;
1103
1104 if (STRNCMP(p, "<SNR>", 5) == 0)
1105 p = skipdigits(p + 5);
1106 p = to_name_end(p, TRUE);
1107 if (p == arg && *arg == '[')
1108 {
1109
1110 // Can be "[1, 2, 3]->Func()".
1111 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1112 p = arg;
1113 }
1114 return p;
1115}
1116
1117/*
1118 * parse a list: [expr, expr]
1119 * "*arg" points to the '['.
1120 * ppconst->pp_is_const is set if all items are a constant.
1121 */
1122 static int
1123compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1124{
1125 char_u *p = skipwhite(*arg + 1);
1126 char_u *whitep = *arg + 1;
1127 int count = 0;
1128 int is_const;
1129 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001130 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131
1132 for (;;)
1133 {
1134 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1135 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001136 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001137 return FAIL;
1138 }
1139 if (*p == ',')
1140 {
1141 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1142 return FAIL;
1143 }
1144 if (*p == ']')
1145 {
1146 ++p;
1147 break;
1148 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001149 if (must_end)
1150 {
1151 semsg(_(e_missing_comma_in_list_str), p);
1152 return FAIL;
1153 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1155 return FAIL;
1156 if (!is_const)
1157 is_all_const = FALSE;
1158 ++count;
1159 if (*p == ',')
1160 {
1161 ++p;
1162 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1163 {
1164 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1165 return FAIL;
1166 }
1167 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001168 else
1169 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170 whitep = p;
1171 p = skipwhite(p);
1172 }
1173 *arg = p;
1174
1175 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001176 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001177}
1178
1179/*
1180 * Parse a lambda: "(arg, arg) => expr"
1181 * "*arg" points to the '('.
1182 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1183 */
1184 static int
1185compile_lambda(char_u **arg, cctx_T *cctx)
1186{
1187 int r;
1188 typval_T rettv;
1189 ufunc_T *ufunc;
1190 evalarg_T evalarg;
1191
1192 init_evalarg(&evalarg);
1193 evalarg.eval_flags = EVAL_EVALUATE;
1194 evalarg.eval_cctx = cctx;
1195
1196 // Get the funcref in "rettv".
1197 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1198 if (r != OK)
1199 {
1200 clear_evalarg(&evalarg, NULL);
1201 return r;
1202 }
1203
1204 // "rettv" will now be a partial referencing the function.
1205 ufunc = rettv.vval.v_partial->pt_func;
1206 ++ufunc->uf_refcount;
1207 clear_tv(&rettv);
1208
1209 // Compile it here to get the return type. The return type is optional,
1210 // when it's missing use t_unknown. This is recognized in
1211 // compile_return().
1212 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1213 ufunc->uf_ret_type = &t_unknown;
1214 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1215
1216 // When the outer function is compiled for profiling or debugging, the
1217 // lambda may be called without profiling or debugging. Compile it here in
1218 // the right context.
1219 if (cctx->ctx_compile_type == CT_DEBUG
1220#ifdef FEAT_PROFILE
1221 || cctx->ctx_compile_type == CT_PROFILE
1222#endif
1223 )
1224 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1225
Bram Moolenaar139575d2022-03-15 19:29:30 +00001226 // if the outer function is not compiled for debugging or profiling, this
1227 // one might be
1228 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001229 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001230 compiletype_T compile_type = get_compile_type(ufunc);
1231
1232 if (compile_type != CT_NONE)
1233 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001234 }
1235
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001236 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1237 // "*arg" may point into it. Point into the original line to avoid a
1238 // dangling pointer.
1239 if (evalarg.eval_using_cmdline)
1240 {
1241 garray_T *gap = &evalarg.eval_tofree_ga;
1242 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1243
1244 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1245 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001246 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247 }
1248
1249 clear_evalarg(&evalarg, NULL);
1250
1251 if (ufunc->uf_def_status == UF_COMPILED)
1252 {
1253 // The return type will now be known.
1254 set_function_type(ufunc);
1255
1256 // The function reference count will be 1. When the ISN_FUNCREF
1257 // instruction is deleted the reference count is decremented and the
1258 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001259 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001260 }
1261
1262 func_ptr_unref(ufunc);
1263 return FAIL;
1264}
1265
1266/*
1267 * Get a lambda and compile it. Uses Vim9 syntax.
1268 */
1269 int
1270get_lambda_tv_and_compile(
1271 char_u **arg,
1272 typval_T *rettv,
1273 int types_optional,
1274 evalarg_T *evalarg)
1275{
1276 int r;
1277 ufunc_T *ufunc;
1278 int save_sc_version = current_sctx.sc_version;
1279
1280 // Get the funcref in "rettv".
1281 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1282 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1283 current_sctx.sc_version = save_sc_version;
1284 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001285 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001286
1287 // "rettv" will now be a partial referencing the function.
1288 ufunc = rettv->vval.v_partial->pt_func;
1289
1290 // Compile it here to get the return type. The return type is optional,
1291 // when it's missing use t_unknown. This is recognized in
1292 // compile_return().
1293 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1294 ufunc->uf_ret_type = &t_unknown;
1295 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1296
1297 if (ufunc->uf_def_status == UF_COMPILED)
1298 {
1299 // The return type will now be known.
1300 set_function_type(ufunc);
1301 return OK;
1302 }
1303 clear_tv(rettv);
1304 return FAIL;
1305}
1306
1307/*
1308 * parse a dict: {key: val, [key]: val}
1309 * "*arg" points to the '{'.
1310 * ppconst->pp_is_const is set if all item values are a constant.
1311 */
1312 static int
1313compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1314{
1315 garray_T *instr = &cctx->ctx_instr;
1316 int count = 0;
1317 dict_T *d = dict_alloc();
1318 dictitem_T *item;
1319 char_u *whitep = *arg + 1;
1320 char_u *p;
1321 int is_const;
1322 int is_all_const = TRUE; // reset when non-const encountered
1323
1324 if (d == NULL)
1325 return FAIL;
1326 if (generate_ppconst(cctx, ppconst) == FAIL)
1327 return FAIL;
1328 for (;;)
1329 {
1330 char_u *key = NULL;
1331
1332 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1333 {
1334 *arg = NULL;
1335 goto failret;
1336 }
1337
1338 if (**arg == '}')
1339 break;
1340
1341 if (**arg == '[')
1342 {
1343 isn_T *isn;
1344
1345 // {[expr]: value} uses an evaluated key.
1346 *arg = skipwhite(*arg + 1);
1347 if (compile_expr0(arg, cctx) == FAIL)
1348 return FAIL;
1349 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1350 if (isn->isn_type == ISN_PUSHNR)
1351 {
1352 char buf[NUMBUFLEN];
1353
1354 // Convert to string at compile time.
1355 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1356 isn->isn_type = ISN_PUSHS;
1357 isn->isn_arg.string = vim_strsave((char_u *)buf);
1358 }
1359 if (isn->isn_type == ISN_PUSHS)
1360 key = isn->isn_arg.string;
1361 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1362 return FAIL;
1363 *arg = skipwhite(*arg);
1364 if (**arg != ']')
1365 {
1366 emsg(_(e_missing_matching_bracket_after_dict_key));
1367 return FAIL;
1368 }
1369 ++*arg;
1370 }
1371 else
1372 {
1373 // {"name": value},
1374 // {'name': value},
1375 // {name: value} use "name" as a literal key
1376 key = get_literal_key(arg);
1377 if (key == NULL)
1378 return FAIL;
1379 if (generate_PUSHS(cctx, &key) == FAIL)
1380 return FAIL;
1381 }
1382
1383 // Check for duplicate keys, if using string keys.
1384 if (key != NULL)
1385 {
1386 item = dict_find(d, key, -1);
1387 if (item != NULL)
1388 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001389 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390 goto failret;
1391 }
1392 item = dictitem_alloc(key);
1393 if (item != NULL)
1394 {
1395 item->di_tv.v_type = VAR_UNKNOWN;
1396 item->di_tv.v_lock = 0;
1397 if (dict_add(d, item) == FAIL)
1398 dictitem_free(item);
1399 }
1400 }
1401
1402 if (**arg != ':')
1403 {
1404 if (*skipwhite(*arg) == ':')
1405 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1406 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001407 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001408 return FAIL;
1409 }
1410 whitep = *arg + 1;
1411 if (!IS_WHITE_OR_NUL(*whitep))
1412 {
1413 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1414 return FAIL;
1415 }
1416
1417 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1418 {
1419 *arg = NULL;
1420 goto failret;
1421 }
1422
1423 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1424 return FAIL;
1425 if (!is_const)
1426 is_all_const = FALSE;
1427 ++count;
1428
1429 whitep = *arg;
1430 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1431 {
1432 *arg = NULL;
1433 goto failret;
1434 }
1435 if (**arg == '}')
1436 break;
1437 if (**arg != ',')
1438 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001439 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 goto failret;
1441 }
1442 if (IS_WHITE_OR_NUL(*whitep))
1443 {
1444 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1445 return FAIL;
1446 }
1447 whitep = *arg + 1;
1448 if (!IS_WHITE_OR_NUL(*whitep))
1449 {
1450 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1451 return FAIL;
1452 }
1453 *arg = skipwhite(whitep);
1454 }
1455
1456 *arg = *arg + 1;
1457
1458 // Allow for following comment, after at least one space.
1459 p = skipwhite(*arg);
1460 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1461 *arg += STRLEN(*arg);
1462
1463 dict_unref(d);
1464 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001465 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001466
1467failret:
1468 if (*arg == NULL)
1469 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001470 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001471 *arg = (char_u *)"";
1472 }
1473 dict_unref(d);
1474 return FAIL;
1475}
1476
1477/*
1478 * Compile "&option".
1479 */
1480 static int
1481compile_get_option(char_u **arg, cctx_T *cctx)
1482{
1483 typval_T rettv;
1484 char_u *start = *arg;
1485 int ret;
1486
1487 // parse the option and get the current value to get the type.
1488 rettv.v_type = VAR_UNKNOWN;
1489 ret = eval_option(arg, &rettv, TRUE);
1490 if (ret == OK)
1491 {
1492 // include the '&' in the name, eval_option() expects it.
1493 char_u *name = vim_strnsave(start, *arg - start);
1494 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1495 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1496
1497 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1498 vim_free(name);
1499 }
1500 clear_tv(&rettv);
1501
1502 return ret;
1503}
1504
1505/*
1506 * Compile "$VAR".
1507 */
1508 static int
1509compile_get_env(char_u **arg, cctx_T *cctx)
1510{
1511 char_u *start = *arg;
1512 int len;
1513 int ret;
1514 char_u *name;
1515
1516 ++*arg;
1517 len = get_env_len(arg);
1518 if (len == 0)
1519 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001520 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001521 return FAIL;
1522 }
1523
1524 // include the '$' in the name, eval_env_var() expects it.
1525 name = vim_strnsave(start, len + 1);
1526 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1527 vim_free(name);
1528 return ret;
1529}
1530
1531/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001532 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001533 */
1534 static int
1535compile_interp_string(char_u **arg, cctx_T *cctx)
1536{
1537 typval_T tv;
1538 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001539 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001540 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001541 int count = 0;
1542 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001543
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001544 // *arg is on the '$' character, move it to the first string character.
1545 ++*arg;
1546 quote = **arg;
1547 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001548
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001549 for (;;)
1550 {
1551 // Get the string up to the matching quote or to a single '{'.
1552 // "arg" is advanced to either the quote or the '{'.
1553 if (quote == '"')
1554 ret = eval_string(arg, &tv, evaluate, TRUE);
1555 else
1556 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1557 if (ret == FAIL)
1558 break;
1559 if (evaluate)
1560 {
1561 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1562 || (**arg != '{' && count == 0))
1563 {
1564 // generate non-empty string or empty string if it's the only
1565 // one
1566 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1567 return FAIL;
1568 tv.vval.v_string = NULL; // don't free it now
1569 ++count;
1570 }
1571 clear_tv(&tv);
1572 }
1573
1574 if (**arg != '{')
1575 {
1576 // found terminating quote
1577 ++*arg;
1578 break;
1579 }
1580
1581 p = compile_one_expr_in_str(*arg, cctx);
1582 if (p == NULL)
1583 {
1584 ret = FAIL;
1585 break;
1586 }
1587 ++count;
1588 *arg = p;
1589 }
LemonBoy2eaef102022-05-06 13:14:50 +01001590
1591 if (ret == FAIL || !evaluate)
1592 return ret;
1593
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001594 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1595 if (count > 1)
1596 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001597
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001598 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001599}
1600
1601/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001602 * Compile "@r".
1603 */
1604 static int
1605compile_get_register(char_u **arg, cctx_T *cctx)
1606{
1607 int ret;
1608
1609 ++*arg;
1610 if (**arg == NUL)
1611 {
1612 semsg(_(e_syntax_error_at_str), *arg - 1);
1613 return FAIL;
1614 }
1615 if (!valid_yank_reg(**arg, FALSE))
1616 {
1617 emsg_invreg(**arg);
1618 return FAIL;
1619 }
1620 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1621 ++*arg;
1622 return ret;
1623}
1624
1625/*
1626 * Apply leading '!', '-' and '+' to constant "rettv".
1627 * When "numeric_only" is TRUE do not apply '!'.
1628 */
1629 static int
1630apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1631{
1632 char_u *p = *end;
1633
1634 // this works from end to start
1635 while (p > start)
1636 {
1637 --p;
1638 if (*p == '-' || *p == '+')
1639 {
1640 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001641 if (rettv->v_type == VAR_FLOAT)
1642 {
1643 if (*p == '-')
1644 rettv->vval.v_float = -rettv->vval.v_float;
1645 }
1646 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001647 {
1648 varnumber_T val;
1649 int error = FALSE;
1650
1651 // tv_get_number_chk() accepts a string, but we don't want that
1652 // here
1653 if (check_not_string(rettv) == FAIL)
1654 return FAIL;
1655 val = tv_get_number_chk(rettv, &error);
1656 clear_tv(rettv);
1657 if (error)
1658 return FAIL;
1659 if (*p == '-')
1660 val = -val;
1661 rettv->v_type = VAR_NUMBER;
1662 rettv->vval.v_number = val;
1663 }
1664 }
1665 else if (numeric_only)
1666 {
1667 ++p;
1668 break;
1669 }
1670 else if (*p == '!')
1671 {
1672 int v = tv2bool(rettv);
1673
1674 // '!' is permissive in the type.
1675 clear_tv(rettv);
1676 rettv->v_type = VAR_BOOL;
1677 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1678 }
1679 }
1680 *end = p;
1681 return OK;
1682}
1683
1684/*
1685 * Recognize v: variables that are constants and set "rettv".
1686 */
1687 static void
1688get_vim_constant(char_u **arg, typval_T *rettv)
1689{
1690 if (STRNCMP(*arg, "v:true", 6) == 0)
1691 {
1692 rettv->v_type = VAR_BOOL;
1693 rettv->vval.v_number = VVAL_TRUE;
1694 *arg += 6;
1695 }
1696 else if (STRNCMP(*arg, "v:false", 7) == 0)
1697 {
1698 rettv->v_type = VAR_BOOL;
1699 rettv->vval.v_number = VVAL_FALSE;
1700 *arg += 7;
1701 }
1702 else if (STRNCMP(*arg, "v:null", 6) == 0)
1703 {
1704 rettv->v_type = VAR_SPECIAL;
1705 rettv->vval.v_number = VVAL_NULL;
1706 *arg += 6;
1707 }
1708 else if (STRNCMP(*arg, "v:none", 6) == 0)
1709 {
1710 rettv->v_type = VAR_SPECIAL;
1711 rettv->vval.v_number = VVAL_NONE;
1712 *arg += 6;
1713 }
1714}
1715
1716 exprtype_T
1717get_compare_type(char_u *p, int *len, int *type_is)
1718{
1719 exprtype_T type = EXPR_UNKNOWN;
1720 int i;
1721
1722 switch (p[0])
1723 {
1724 case '=': if (p[1] == '=')
1725 type = EXPR_EQUAL;
1726 else if (p[1] == '~')
1727 type = EXPR_MATCH;
1728 break;
1729 case '!': if (p[1] == '=')
1730 type = EXPR_NEQUAL;
1731 else if (p[1] == '~')
1732 type = EXPR_NOMATCH;
1733 break;
1734 case '>': if (p[1] != '=')
1735 {
1736 type = EXPR_GREATER;
1737 *len = 1;
1738 }
1739 else
1740 type = EXPR_GEQUAL;
1741 break;
1742 case '<': if (p[1] != '=')
1743 {
1744 type = EXPR_SMALLER;
1745 *len = 1;
1746 }
1747 else
1748 type = EXPR_SEQUAL;
1749 break;
1750 case 'i': if (p[1] == 's')
1751 {
1752 // "is" and "isnot"; but not a prefix of a name
1753 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1754 *len = 5;
1755 i = p[*len];
1756 if (!isalnum(i) && i != '_')
1757 {
1758 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1759 *type_is = TRUE;
1760 }
1761 }
1762 break;
1763 }
1764 return type;
1765}
1766
1767/*
1768 * Skip over an expression, ignoring most errors.
1769 */
1770 void
1771skip_expr_cctx(char_u **arg, cctx_T *cctx)
1772{
1773 evalarg_T evalarg;
1774
1775 init_evalarg(&evalarg);
1776 evalarg.eval_cctx = cctx;
1777 skip_expr(arg, &evalarg);
1778 clear_evalarg(&evalarg, NULL);
1779}
1780
1781/*
1782 * Check that the top of the type stack has a type that can be used as a
1783 * condition. Give an error and return FAIL if not.
1784 */
1785 int
1786bool_on_stack(cctx_T *cctx)
1787{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001788 type_T *type;
1789
Bram Moolenaar078a4612022-01-04 15:17:03 +00001790 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791 if (type == &t_bool)
1792 return OK;
1793
Bram Moolenaar4913d422022-10-17 13:13:32 +01001794 if (type->tt_type == VAR_ANY
1795 || type->tt_type == VAR_UNKNOWN
1796 || type->tt_type == VAR_NUMBER
1797 || type == &t_number_bool
1798 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1800 // This requires a runtime type check.
1801 return generate_COND2BOOL(cctx);
1802
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001803 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804}
1805
1806/*
1807 * Give the "white on both sides" error, taking the operator from "p[len]".
1808 */
1809 void
1810error_white_both(char_u *op, int len)
1811{
1812 char_u buf[10];
1813
1814 vim_strncpy(buf, op, len);
1815 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1816}
1817
1818/*
1819 * Compile code to apply '-', '+' and '!'.
1820 * When "numeric_only" is TRUE do not apply '!'.
1821 */
1822 static int
1823compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1824{
1825 char_u *p = *end;
1826
1827 // this works from end to start
1828 while (p > start)
1829 {
1830 --p;
1831 while (VIM_ISWHITE(*p))
1832 --p;
1833 if (*p == '-' || *p == '+')
1834 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001835 type_T *type = get_type_on_stack(cctx, 0);
1836 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001837 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838 return FAIL;
1839
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001840 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001841 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1842 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 }
1844 else if (numeric_only)
1845 {
1846 ++p;
1847 break;
1848 }
1849 else
1850 {
1851 int invert = *p == '!';
1852
1853 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1854 {
1855 if (p[-1] == '!')
1856 invert = !invert;
1857 --p;
1858 }
1859 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1860 return FAIL;
1861 }
1862 }
1863 *end = p;
1864 return OK;
1865}
1866
1867/*
1868 * Compile "(expression)": recursive!
1869 * Return FAIL/OK.
1870 */
1871 static int
1872compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1873{
1874 int ret;
1875 char_u *p = *arg + 1;
1876
1877 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1878 return FAIL;
1879 if (ppconst->pp_used <= PPSIZE - 10)
1880 {
1881 ret = compile_expr1(arg, cctx, ppconst);
1882 }
1883 else
1884 {
1885 // Not enough space in ppconst, flush constants.
1886 if (generate_ppconst(cctx, ppconst) == FAIL)
1887 return FAIL;
1888 ret = compile_expr0(arg, cctx);
1889 }
1890 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1891 return FAIL;
1892 if (**arg == ')')
1893 ++*arg;
1894 else if (ret == OK)
1895 {
1896 emsg(_(e_missing_closing_paren));
1897 ret = FAIL;
1898 }
1899 return ret;
1900}
1901
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001902static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001903
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904/*
1905 * Compile whatever comes after "name" or "name()".
1906 * Advances "*arg" only when something was recognized.
1907 */
1908 static int
1909compile_subscript(
1910 char_u **arg,
1911 cctx_T *cctx,
1912 char_u *start_leader,
1913 char_u **end_leader,
1914 ppconst_T *ppconst)
1915{
1916 char_u *name_start = *end_leader;
1917 int keeping_dict = FALSE;
1918
1919 for (;;)
1920 {
1921 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001922 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923
1924 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1925 {
1926 char_u *next = peek_next_line_from_context(cctx);
1927
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001928 // If a following line starts with "->{", "->(" or "->X" advance to
1929 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001930 // Also if a following line starts with ".x".
1931 if (next != NULL &&
1932 ((next[0] == '-' && next[1] == '>'
1933 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001934 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001935 || ASCII_ISALPHA(*skipwhite(next + 2))))
1936 || (next[0] == '.' && eval_isdictc(next[1]))))
1937 {
1938 next = next_line_from_context(cctx, TRUE);
1939 if (next == NULL)
1940 return FAIL;
1941 *arg = next;
1942 p = skipwhite(*arg);
1943 }
1944 }
1945
1946 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1947 // is not a function call.
1948 if (**arg == '(')
1949 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001950 int argcount = 0;
1951
1952 if (generate_ppconst(cctx, ppconst) == FAIL)
1953 return FAIL;
1954 ppconst->pp_is_const = FALSE;
1955
1956 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001957 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001958
1959 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001960 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961 return FAIL;
1962 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1963 return FAIL;
1964 if (keeping_dict)
1965 {
1966 keeping_dict = FALSE;
1967 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1968 return FAIL;
1969 }
1970 }
1971 else if (*p == '-' && p[1] == '>')
1972 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001973 char_u *pstart = p;
1974 int alt;
1975 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001976
Bram Moolenaarc7349932022-01-16 20:59:39 +00001977 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001978 if (generate_ppconst(cctx, ppconst) == FAIL)
1979 return FAIL;
1980 ppconst->pp_is_const = FALSE;
1981
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982 // Apply the '!', '-' and '+' first:
1983 // -1.0->func() works like (-1.0)->func()
1984 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1985 return FAIL;
1986
1987 p += 2;
1988 *arg = skipwhite(p);
1989 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001990
1991 // Three alternatives handled here:
1992 // 1. "base->name(" only a name, use compile_call()
1993 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1994 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001996 alt = 2;
1997 else
1998 {
1999 // alternative 1 or 3
2000 p = *arg;
2001 if (!eval_isnamec1(*p))
2002 {
2003 semsg(_(e_trailing_characters_str), pstart);
2004 return FAIL;
2005 }
2006 if (ASCII_ISALPHA(*p) && p[1] == ':')
2007 p += 2;
2008 for ( ; eval_isnamec(*p); ++p)
2009 ;
2010 if (*p == '(')
2011 {
2012 // alternative 1
2013 alt = 1;
2014 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2015 return FAIL;
2016 }
2017 else
2018 {
2019 // Must be alternative 3, find the "(". Only works within
2020 // one line.
2021 alt = 3;
2022 paren = vim_strchr(p, '(');
2023 if (paren == NULL)
2024 {
2025 semsg(_(e_missing_parenthesis_str), *arg);
2026 return FAIL;
2027 }
2028 }
2029 }
2030
2031 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032 {
2033 int argcount = 1;
2034 garray_T *stack = &cctx->ctx_type_stack;
2035 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002036 int expr_isn_start = cctx->ctx_instr.ga_len;
2037 int expr_isn_end;
2038 int arg_isn_count;
2039
Bram Moolenaarc7349932022-01-16 20:59:39 +00002040 if (alt == 2)
2041 {
2042 // Funcref call: list->(Refs[2])(arg)
2043 // or lambda: list->((arg) => expr)(arg)
2044 //
2045 // Fist compile the function expression.
2046 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2047 return FAIL;
2048 }
2049 else
2050 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002051 int fail;
2052 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002053 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002054
Bram Moolenaarc7349932022-01-16 20:59:39 +00002055 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002056
2057 // instead of using LOADG for "import.Func" use PUSHFUNC
2058 ++paren_follows_after_expr;
2059
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002060 // do not look in the next line
2061 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002062
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002063 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002064 || *skipwhite(*arg) != NUL;
2065 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002066 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002067 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002068
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002069 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002070 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002071 if (did_emsg == prev_did_emsg)
2072 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002073 return FAIL;
2074 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002075 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002076
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002077 // Compile the arguments.
2078 if (**arg != '(')
2079 {
2080 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002081 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082 else
2083 semsg(_(e_missing_parenthesis_str), *arg);
2084 return FAIL;
2085 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002086
2087 // Remember the next instruction index, where the instructions
2088 // for arguments are being written.
2089 expr_isn_end = cctx->ctx_instr.ga_len;
2090
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002092 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2093 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002094 return FAIL;
2095
2096 // Move the instructions for the arguments to before the
2097 // instructions of the expression and move the type of the
2098 // expression after the argument types. This is what ISN_PCALL
2099 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002100 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2101 if (arg_isn_count > 0)
2102 {
2103 int expr_isn_count = expr_isn_end - expr_isn_start;
2104 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002105 type_T *decl_type;
2106 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002107
2108 if (isn == NULL)
2109 return FAIL;
2110 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2111 + expr_isn_start,
2112 sizeof(isn_T) * expr_isn_count);
2113 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2114 + expr_isn_start,
2115 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2116 sizeof(isn_T) * arg_isn_count);
2117 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2118 + expr_isn_start + arg_isn_count,
2119 isn, sizeof(isn_T) * expr_isn_count);
2120 vim_free(isn);
2121
Bram Moolenaar078a4612022-01-04 15:17:03 +00002122 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2123 type = typep->type_curr;
2124 decl_type = typep->type_decl;
2125 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2126 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2127 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002128 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002129 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2130 typep->type_curr = type;
2131 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132 }
2133
Bram Moolenaar078a4612022-01-04 15:17:03 +00002134 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002135 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2136 return FAIL;
2137 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002138
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002139 if (keeping_dict)
2140 {
2141 keeping_dict = FALSE;
2142 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2143 return FAIL;
2144 }
2145 }
2146 else if (**arg == '[')
2147 {
2148 int is_slice = FALSE;
2149
2150 // list index: list[123]
2151 // dict member: dict[key]
2152 // string index: text[123]
2153 // blob index: blob[123]
2154 if (generate_ppconst(cctx, ppconst) == FAIL)
2155 return FAIL;
2156 ppconst->pp_is_const = FALSE;
2157
2158 ++p;
2159 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2160 return FAIL;
2161 if (**arg == ':')
2162 {
2163 // missing first index is equal to zero
2164 generate_PUSHNR(cctx, 0);
2165 }
2166 else
2167 {
2168 if (compile_expr0(arg, cctx) == FAIL)
2169 return FAIL;
2170 if (**arg == ':')
2171 {
2172 semsg(_(e_white_space_required_before_and_after_str_at_str),
2173 ":", *arg);
2174 return FAIL;
2175 }
2176 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2177 return FAIL;
2178 *arg = skipwhite(*arg);
2179 }
2180 if (**arg == ':')
2181 {
2182 is_slice = TRUE;
2183 ++*arg;
2184 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2185 {
2186 semsg(_(e_white_space_required_before_and_after_str_at_str),
2187 ":", *arg);
2188 return FAIL;
2189 }
2190 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2191 return FAIL;
2192 if (**arg == ']')
2193 // missing second index is equal to end of string
2194 generate_PUSHNR(cctx, -1);
2195 else
2196 {
2197 if (compile_expr0(arg, cctx) == FAIL)
2198 return FAIL;
2199 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2200 return FAIL;
2201 *arg = skipwhite(*arg);
2202 }
2203 }
2204
2205 if (**arg != ']')
2206 {
2207 emsg(_(e_missing_closing_square_brace));
2208 return FAIL;
2209 }
2210 *arg = *arg + 1;
2211
2212 if (keeping_dict)
2213 {
2214 keeping_dict = FALSE;
2215 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2216 return FAIL;
2217 }
2218 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2219 return FAIL;
2220 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002221 else if (*p == '.'
2222 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2223 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2224 {
2225 // class member: SomeClass.varname
2226 // class method: SomeClass.SomeMethod()
2227 // class constructor: SomeClass.new()
2228 // object member: someObject.varname, this.varname
2229 // object method: someObject.SomeMethod(), this.SomeMethod()
2230 if (compile_class_object_index(cctx, arg, type) == FAIL)
2231 return FAIL;
2232 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233 else if (*p == '.' && p[1] != '.')
2234 {
2235 // dictionary member: dict.name
2236 if (generate_ppconst(cctx, ppconst) == FAIL)
2237 return FAIL;
2238 ppconst->pp_is_const = FALSE;
2239
2240 *arg = p + 1;
2241 if (IS_WHITE_OR_NUL(**arg))
2242 {
2243 emsg(_(e_missing_name_after_dot));
2244 return FAIL;
2245 }
2246 p = *arg;
2247 if (eval_isdictc(*p))
2248 while (eval_isnamec(*p))
2249 MB_PTR_ADV(p);
2250 if (p == *arg)
2251 {
2252 semsg(_(e_syntax_error_at_str), *arg);
2253 return FAIL;
2254 }
2255 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2256 return FAIL;
2257 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2258 return FAIL;
2259 keeping_dict = TRUE;
2260 *arg = p;
2261 }
2262 else
2263 break;
2264 }
2265
2266 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2267 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002268 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2269 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270 return FAIL;
2271
2272 return OK;
2273}
2274
2275/*
2276 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2277 * "arg" is advanced until after the expression, skipping white space.
2278 *
2279 * If the value is a constant "ppconst->pp_used" will be non-zero.
2280 * Before instructions are generated, any values in "ppconst" will generated.
2281 *
2282 * This is the compiling equivalent of eval1(), eval2(), etc.
2283 */
2284
2285/*
2286 * number number constant
2287 * 0zFFFFFFFF Blob constant
2288 * "string" string constant
2289 * 'string' literal string constant
2290 * &option-name option value
2291 * @r register contents
2292 * identifier variable value
2293 * function() function call
2294 * $VAR environment variable
2295 * (expression) nested expression
2296 * [expr, expr] List
2297 * {key: val, [key]: val} Dictionary
2298 *
2299 * Also handle:
2300 * ! in front logical NOT
2301 * - in front unary minus
2302 * + in front unary plus (ignored)
2303 * trailing (arg) funcref/partial call
2304 * trailing [] subscript in String or List
2305 * trailing .name entry in Dictionary
2306 * trailing ->name() method call
2307 */
2308 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002309compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002310 char_u **arg,
2311 cctx_T *cctx,
2312 ppconst_T *ppconst)
2313{
2314 char_u *start_leader, *end_leader;
2315 int ret = OK;
2316 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2317 int used_before = ppconst->pp_used;
2318
2319 ppconst->pp_is_const = FALSE;
2320
2321 /*
2322 * Skip '!', '-' and '+' characters. They are handled later.
2323 */
2324 start_leader = *arg;
2325 if (eval_leader(arg, TRUE) == FAIL)
2326 return FAIL;
2327 end_leader = *arg;
2328
2329 rettv->v_type = VAR_UNKNOWN;
2330 switch (**arg)
2331 {
2332 /*
2333 * Number constant.
2334 */
2335 case '0': // also for blob starting with 0z
2336 case '1':
2337 case '2':
2338 case '3':
2339 case '4':
2340 case '5':
2341 case '6':
2342 case '7':
2343 case '8':
2344 case '9':
2345 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2346 return FAIL;
2347 // Apply "-" and "+" just before the number now, right to
2348 // left. Matters especially when "->" follows. Stops at
2349 // '!'.
2350 if (apply_leader(rettv, TRUE,
2351 start_leader, &end_leader) == FAIL)
2352 {
2353 clear_tv(rettv);
2354 return FAIL;
2355 }
2356 break;
2357
2358 /*
2359 * String constant: "string".
2360 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002361 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362 return FAIL;
2363 break;
2364
2365 /*
2366 * Literal string constant: 'str''ing'.
2367 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002368 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 return FAIL;
2370 break;
2371
2372 /*
2373 * Constant Vim variable.
2374 */
2375 case 'v': get_vim_constant(arg, rettv);
2376 ret = NOTDONE;
2377 break;
2378
2379 /*
2380 * "true" constant
2381 */
2382 case 't': if (STRNCMP(*arg, "true", 4) == 0
2383 && !eval_isnamec((*arg)[4]))
2384 {
2385 *arg += 4;
2386 rettv->v_type = VAR_BOOL;
2387 rettv->vval.v_number = VVAL_TRUE;
2388 }
2389 else
2390 ret = NOTDONE;
2391 break;
2392
2393 /*
2394 * "false" constant
2395 */
2396 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2397 && !eval_isnamec((*arg)[5]))
2398 {
2399 *arg += 5;
2400 rettv->v_type = VAR_BOOL;
2401 rettv->vval.v_number = VVAL_FALSE;
2402 }
2403 else
2404 ret = NOTDONE;
2405 break;
2406
2407 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002408 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002410 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002411 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002412 char_u *p = *arg + 4;
2413 int len;
2414
2415 for (len = 0; eval_isnamec(p[len]); ++len)
2416 ;
2417 ret = handle_predefined(*arg, len + 4, rettv);
2418 if (ret == FAIL)
2419 ret = NOTDONE;
2420 else
2421 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422 }
2423 else
2424 ret = NOTDONE;
2425 break;
2426
2427 /*
2428 * List: [expr, expr]
2429 */
2430 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2431 return FAIL;
2432 ret = compile_list(arg, cctx, ppconst);
2433 break;
2434
2435 /*
2436 * Dictionary: {'key': val, 'key': val}
2437 */
2438 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2439 return FAIL;
2440 ret = compile_dict(arg, cctx, ppconst);
2441 break;
2442
2443 /*
2444 * Option value: &name
2445 */
2446 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2447 return FAIL;
2448 ret = compile_get_option(arg, cctx);
2449 break;
2450
2451 /*
2452 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002453 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002454 */
2455 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2456 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002457 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2458 ret = compile_interp_string(arg, cctx);
2459 else
2460 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461 break;
2462
2463 /*
2464 * Register contents: @r.
2465 */
2466 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2467 return FAIL;
2468 ret = compile_get_register(arg, cctx);
2469 break;
2470 /*
2471 * nested expression: (expression).
2472 * lambda: (arg, arg) => expr
2473 * funcref: (arg, arg) => { statement }
2474 */
2475 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2476 ret = compile_lambda(arg, cctx);
2477 if (ret == NOTDONE)
2478 ret = compile_parenthesis(arg, cctx, ppconst);
2479 break;
2480
2481 default: ret = NOTDONE;
2482 break;
2483 }
2484 if (ret == FAIL)
2485 return FAIL;
2486
2487 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2488 {
2489 if (cctx->ctx_skip == SKIP_YES)
2490 clear_tv(rettv);
2491 else
2492 // A constant expression can possibly be handled compile time,
2493 // return the value instead of generating code.
2494 ++ppconst->pp_used;
2495 }
2496 else if (ret == NOTDONE)
2497 {
2498 char_u *p;
2499 int r;
2500
2501 if (!eval_isnamec1(**arg))
2502 {
2503 if (!vim9_bad_comment(*arg))
2504 {
2505 if (ends_excmd(*skipwhite(*arg)))
2506 semsg(_(e_empty_expression_str), *arg);
2507 else
2508 semsg(_(e_name_expected_str), *arg);
2509 }
2510 return FAIL;
2511 }
2512
2513 // "name" or "name()"
2514 p = to_name_end(*arg, TRUE);
2515 if (p - *arg == (size_t)1 && **arg == '_')
2516 {
2517 emsg(_(e_cannot_use_underscore_here));
2518 return FAIL;
2519 }
2520
2521 if (*p == '(')
2522 {
2523 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2524 }
2525 else
2526 {
2527 if (cctx->ctx_skip != SKIP_YES
2528 && generate_ppconst(cctx, ppconst) == FAIL)
2529 return FAIL;
2530 r = compile_load(arg, p, cctx, TRUE, TRUE);
2531 }
2532 if (r == FAIL)
2533 return FAIL;
2534 }
2535
2536 // Handle following "[]", ".member", etc.
2537 // Then deal with prefixed '-', '+' and '!', if not done already.
2538 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2539 ppconst) == FAIL)
2540 return FAIL;
2541 if (ppconst->pp_used > 0)
2542 {
2543 // apply the '!', '-' and '+' before the constant
2544 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2545 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2546 return FAIL;
2547 return OK;
2548 }
2549 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2550 return FAIL;
2551 return OK;
2552}
2553
2554/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002555 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556 */
2557 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002558compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559{
2560 type_T *want_type = NULL;
2561
2562 // Recognize <type>
2563 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2564 {
2565 ++*arg;
2566 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2567 if (want_type == NULL)
2568 return FAIL;
2569
2570 if (**arg != '>')
2571 {
2572 if (*skipwhite(*arg) == '>')
2573 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2574 else
2575 emsg(_(e_missing_gt));
2576 return FAIL;
2577 }
2578 ++*arg;
2579 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2580 return FAIL;
2581 }
2582
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002583 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002584 return FAIL;
2585
2586 if (want_type != NULL)
2587 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002588 type_T *actual;
2589 where_T where = WHERE_INIT;
2590
2591 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002592 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002593 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002595 if (need_type(actual, want_type, FALSE,
2596 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002597 return FAIL;
2598 }
2599 }
2600
2601 return OK;
2602}
2603
2604/*
2605 * * number multiplication
2606 * / number division
2607 * % number modulo
2608 */
2609 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002610compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002611{
2612 char_u *op;
2613 char_u *next;
2614 int ppconst_used = ppconst->pp_used;
2615
2616 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002617 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002618 return FAIL;
2619
2620 /*
2621 * Repeat computing, until no "*", "/" or "%" is following.
2622 */
2623 for (;;)
2624 {
2625 op = may_peek_next_line(cctx, *arg, &next);
2626 if (*op != '*' && *op != '/' && *op != '%')
2627 break;
2628 if (next != NULL)
2629 {
2630 *arg = next_line_from_context(cctx, TRUE);
2631 op = skipwhite(*arg);
2632 }
2633
2634 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2635 {
2636 error_white_both(op, 1);
2637 return FAIL;
2638 }
2639 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2640 return FAIL;
2641
2642 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002643 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002644 return FAIL;
2645
2646 if (ppconst->pp_used == ppconst_used + 2
2647 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2648 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2649 {
2650 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2651 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2652 varnumber_T res = 0;
2653 int failed = FALSE;
2654
2655 // both are numbers: compute the result
2656 switch (*op)
2657 {
2658 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2659 break;
2660 case '/': res = num_divide(tv1->vval.v_number,
2661 tv2->vval.v_number, &failed);
2662 break;
2663 case '%': res = num_modulus(tv1->vval.v_number,
2664 tv2->vval.v_number, &failed);
2665 break;
2666 }
2667 if (failed)
2668 return FAIL;
2669 tv1->vval.v_number = res;
2670 --ppconst->pp_used;
2671 }
2672 else
2673 {
2674 generate_ppconst(cctx, ppconst);
2675 generate_two_op(cctx, op);
2676 }
2677 }
2678
2679 return OK;
2680}
2681
2682/*
2683 * + number addition or list/blobl concatenation
2684 * - number subtraction
2685 * .. string concatenation
2686 */
2687 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002688compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002689{
2690 char_u *op;
2691 char_u *next;
2692 int oplen;
2693 int ppconst_used = ppconst->pp_used;
2694
2695 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002696 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 return FAIL;
2698
2699 /*
2700 * Repeat computing, until no "+", "-" or ".." is following.
2701 */
2702 for (;;)
2703 {
2704 op = may_peek_next_line(cctx, *arg, &next);
2705 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2706 break;
2707 if (op[0] == op[1] && *op != '.' && next)
2708 // Finding "++" or "--" on the next line is a separate command.
2709 // But ".." is concatenation.
2710 break;
2711 oplen = (*op == '.' ? 2 : 1);
2712 if (next != NULL)
2713 {
2714 *arg = next_line_from_context(cctx, TRUE);
2715 op = skipwhite(*arg);
2716 }
2717
2718 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2719 {
2720 error_white_both(op, oplen);
2721 return FAIL;
2722 }
2723
2724 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2725 return FAIL;
2726
2727 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002728 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002729 return FAIL;
2730
2731 if (ppconst->pp_used == ppconst_used + 2
2732 && (*op == '.'
2733 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2734 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2735 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2736 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2737 {
2738 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2739 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2740
2741 // concat/subtract/add constant numbers
2742 if (*op == '+')
2743 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2744 else if (*op == '-')
2745 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2746 else
2747 {
2748 // concatenate constant strings
2749 char_u *s1 = tv1->vval.v_string;
2750 char_u *s2 = tv2->vval.v_string;
2751 size_t len1 = STRLEN(s1);
2752
2753 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2754 if (tv1->vval.v_string == NULL)
2755 {
2756 clear_ppconst(ppconst);
2757 return FAIL;
2758 }
2759 mch_memmove(tv1->vval.v_string, s1, len1);
2760 STRCPY(tv1->vval.v_string + len1, s2);
2761 vim_free(s1);
2762 vim_free(s2);
2763 }
2764 --ppconst->pp_used;
2765 }
2766 else
2767 {
2768 generate_ppconst(cctx, ppconst);
2769 ppconst->pp_is_const = FALSE;
2770 if (*op == '.')
2771 {
2772 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2773 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2774 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002775 if (generate_CONCAT(cctx, 2) == FAIL)
2776 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002777 }
2778 else
2779 generate_two_op(cctx, op);
2780 }
2781 }
2782
2783 return OK;
2784}
2785
2786/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002787 * expr6a >> expr6b
2788 * expr6a << expr6b
2789 *
2790 * Produces instructions:
2791 * OPNR bitwise left or right shift
2792 */
2793 static int
2794compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2795{
2796 exprtype_T type = EXPR_UNKNOWN;
2797 char_u *p;
2798 char_u *next;
2799 int len = 2;
2800 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002801 isn_T *isn;
2802
2803 // get the first variable
2804 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2805 return FAIL;
2806
2807 /*
2808 * Repeat computing, until no "+", "-" or ".." is following.
2809 */
2810 for (;;)
2811 {
2812 type = EXPR_UNKNOWN;
2813
2814 p = may_peek_next_line(cctx, *arg, &next);
2815 if (p[0] == '<' && p[1] == '<')
2816 type = EXPR_LSHIFT;
2817 else if (p[0] == '>' && p[1] == '>')
2818 type = EXPR_RSHIFT;
2819
2820 if (type == EXPR_UNKNOWN)
2821 return OK;
2822
2823 // Handle a bitwise left or right shift operator
2824 if (ppconst->pp_used == ppconst_used + 1)
2825 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002826 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002827 {
2828 // left operand should be a number
2829 emsg(_(e_bitshift_ops_must_be_number));
2830 return FAIL;
2831 }
2832 }
2833 else
2834 {
2835 type_T *t = get_type_on_stack(cctx, 0);
2836
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002837 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002838 {
2839 emsg(_(e_bitshift_ops_must_be_number));
2840 return FAIL;
2841 }
2842 }
2843
2844 if (next != NULL)
2845 {
2846 *arg = next_line_from_context(cctx, TRUE);
2847 p = skipwhite(*arg);
2848 }
2849
2850 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2851 {
2852 error_white_both(p, len);
2853 return FAIL;
2854 }
2855
2856 // get the second variable
2857 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2858 return FAIL;
2859
2860 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2861 return FAIL;
2862
2863 if (ppconst->pp_used == ppconst_used + 2)
2864 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002865 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2866 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2867
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002868 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002869 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2870 {
2871 // right operand should be a positive number
2872 if (tv2->v_type != VAR_NUMBER)
2873 emsg(_(e_bitshift_ops_must_be_number));
2874 else
dundargocc57b5bc2022-11-02 13:30:51 +00002875 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002876 return FAIL;
2877 }
2878
2879 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2880 tv1->vval.v_number = 0;
2881 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002882 tv1->vval.v_number =
2883 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002884 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002885 tv1->vval.v_number =
2886 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002887 clear_tv(tv2);
2888 --ppconst->pp_used;
2889 }
2890 else
2891 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002892 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2893 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002894 {
2895 emsg(_(e_bitshift_ops_must_be_number));
2896 return FAIL;
2897 }
2898
2899 generate_ppconst(cctx, ppconst);
2900
2901 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2902 if (isn == NULL)
2903 return FAIL;
2904
2905 if (isn != NULL)
2906 isn->isn_arg.op.op_type = type;
2907 }
2908 }
2909
2910 return OK;
2911}
2912
2913/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002914 * expr5a == expr5b
2915 * expr5a =~ expr5b
2916 * expr5a != expr5b
2917 * expr5a !~ expr5b
2918 * expr5a > expr5b
2919 * expr5a >= expr5b
2920 * expr5a < expr5b
2921 * expr5a <= expr5b
2922 * expr5a is expr5b
2923 * expr5a isnot expr5b
2924 *
2925 * Produces instructions:
2926 * EVAL expr5a Push result of "expr5a"
2927 * EVAL expr5b Push result of "expr5b"
2928 * COMPARE one of the compare instructions
2929 */
2930 static int
2931compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2932{
2933 exprtype_T type = EXPR_UNKNOWN;
2934 char_u *p;
2935 char_u *next;
2936 int len = 2;
2937 int type_is = FALSE;
2938 int ppconst_used = ppconst->pp_used;
2939
2940 // get the first variable
2941 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2942 return FAIL;
2943
2944 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002945
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002946 type = get_compare_type(p, &len, &type_is);
2947
2948 /*
2949 * If there is a comparative operator, use it.
2950 */
2951 if (type != EXPR_UNKNOWN)
2952 {
2953 int ic = FALSE; // Default: do not ignore case
2954
2955 if (next != NULL)
2956 {
2957 *arg = next_line_from_context(cctx, TRUE);
2958 p = skipwhite(*arg);
2959 }
2960 if (type_is && (p[len] == '?' || p[len] == '#'))
2961 {
2962 semsg(_(e_invalid_expression_str), *arg);
2963 return FAIL;
2964 }
2965 // extra question mark appended: ignore case
2966 if (p[len] == '?')
2967 {
2968 ic = TRUE;
2969 ++len;
2970 }
2971 // extra '#' appended: match case (ignored)
2972 else if (p[len] == '#')
2973 ++len;
2974 // nothing appended: match case
2975
2976 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2977 {
2978 error_white_both(p, len);
2979 return FAIL;
2980 }
2981
2982 // get the second variable
2983 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2984 return FAIL;
2985
2986 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2987 return FAIL;
2988
2989 if (ppconst->pp_used == ppconst_used + 2)
2990 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002991 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002992 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2993 int ret;
2994
2995 // Both sides are a constant, compute the result now.
2996 // First check for a valid combination of types, this is more
2997 // strict than typval_compare().
2998 if (check_compare_types(type, tv1, tv2) == FAIL)
2999 ret = FAIL;
3000 else
3001 {
3002 ret = typval_compare(tv1, tv2, type, ic);
3003 tv1->v_type = VAR_BOOL;
3004 tv1->vval.v_number = tv1->vval.v_number
3005 ? VVAL_TRUE : VVAL_FALSE;
3006 clear_tv(tv2);
3007 --ppconst->pp_used;
3008 }
3009 return ret;
3010 }
3011
3012 generate_ppconst(cctx, ppconst);
3013 return generate_COMPARE(cctx, type, ic);
3014 }
3015
3016 return OK;
3017}
3018
3019static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3020
3021/*
3022 * Compile || or &&.
3023 */
3024 static int
3025compile_and_or(
3026 char_u **arg,
3027 cctx_T *cctx,
3028 char *op,
3029 ppconst_T *ppconst,
3030 int ppconst_used UNUSED)
3031{
3032 char_u *next;
3033 char_u *p = may_peek_next_line(cctx, *arg, &next);
3034 int opchar = *op;
3035
3036 if (p[0] == opchar && p[1] == opchar)
3037 {
3038 garray_T *instr = &cctx->ctx_instr;
3039 garray_T end_ga;
3040 int save_skip = cctx->ctx_skip;
3041
3042 /*
3043 * Repeat until there is no following "||" or "&&"
3044 */
3045 ga_init2(&end_ga, sizeof(int), 10);
3046 while (p[0] == opchar && p[1] == opchar)
3047 {
3048 long start_lnum = SOURCING_LNUM;
3049 long save_sourcing_lnum;
3050 int start_ctx_lnum = cctx->ctx_lnum;
3051 int save_lnum;
3052 int const_used;
3053 int status;
3054 jumpwhen_T jump_when = opchar == '|'
3055 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3056
3057 if (next != NULL)
3058 {
3059 *arg = next_line_from_context(cctx, TRUE);
3060 p = skipwhite(*arg);
3061 }
3062
3063 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3064 {
3065 semsg(_(e_white_space_required_before_and_after_str_at_str),
3066 op, p);
3067 ga_clear(&end_ga);
3068 return FAIL;
3069 }
3070
3071 save_sourcing_lnum = SOURCING_LNUM;
3072 SOURCING_LNUM = start_lnum;
3073 save_lnum = cctx->ctx_lnum;
3074 cctx->ctx_lnum = start_ctx_lnum;
3075
3076 status = check_ppconst_bool(ppconst);
3077 if (status != FAIL)
3078 {
3079 // Use the last ppconst if possible.
3080 if (ppconst->pp_used > 0)
3081 {
3082 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3083 int is_true = tv2bool(tv);
3084
3085 if ((is_true && opchar == '|')
3086 || (!is_true && opchar == '&'))
3087 {
3088 // For "false && expr" and "true || expr" the "expr"
3089 // does not need to be evaluated.
3090 cctx->ctx_skip = SKIP_YES;
3091 clear_tv(tv);
3092 tv->v_type = VAR_BOOL;
3093 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3094 }
3095 else
3096 {
3097 // For "true && expr" and "false || expr" only "expr"
3098 // needs to be evaluated.
3099 --ppconst->pp_used;
3100 jump_when = JUMP_NEVER;
3101 }
3102 }
3103 else
3104 {
3105 // Every part must evaluate to a bool.
3106 status = bool_on_stack(cctx);
3107 }
3108 }
3109 if (status != FAIL)
3110 status = ga_grow(&end_ga, 1);
3111 cctx->ctx_lnum = save_lnum;
3112 if (status == FAIL)
3113 {
3114 ga_clear(&end_ga);
3115 return FAIL;
3116 }
3117
3118 if (jump_when != JUMP_NEVER)
3119 {
3120 if (cctx->ctx_skip != SKIP_YES)
3121 {
3122 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3123 ++end_ga.ga_len;
3124 }
3125 generate_JUMP(cctx, jump_when, 0);
3126 }
3127
3128 // eval the next expression
3129 SOURCING_LNUM = save_sourcing_lnum;
3130 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3131 {
3132 ga_clear(&end_ga);
3133 return FAIL;
3134 }
3135
3136 const_used = ppconst->pp_used;
3137 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3138 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3139 {
3140 ga_clear(&end_ga);
3141 return FAIL;
3142 }
3143
3144 // "0 || 1" results in true, "1 && 0" results in false.
3145 if (ppconst->pp_used == const_used + 1)
3146 {
3147 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3148
3149 if (tv->v_type == VAR_NUMBER
3150 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3151 {
3152 tv->vval.v_number = tv->vval.v_number == 1
3153 ? VVAL_TRUE : VVAL_FALSE;
3154 tv->v_type = VAR_BOOL;
3155 }
3156 }
3157
3158 p = may_peek_next_line(cctx, *arg, &next);
3159 }
3160
3161 if (check_ppconst_bool(ppconst) == FAIL)
3162 {
3163 ga_clear(&end_ga);
3164 return FAIL;
3165 }
3166
3167 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3168 // Every part must evaluate to a bool.
3169 if (bool_on_stack(cctx) == FAIL)
3170 {
3171 ga_clear(&end_ga);
3172 return FAIL;
3173 }
3174
3175 if (end_ga.ga_len > 0)
3176 {
3177 // Fill in the end label in all jumps.
3178 generate_ppconst(cctx, ppconst);
3179 while (end_ga.ga_len > 0)
3180 {
3181 isn_T *isn;
3182
3183 --end_ga.ga_len;
3184 isn = ((isn_T *)instr->ga_data)
3185 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3186 isn->isn_arg.jump.jump_where = instr->ga_len;
3187 }
3188 }
3189 ga_clear(&end_ga);
3190
3191 cctx->ctx_skip = save_skip;
3192 }
3193
3194 return OK;
3195}
3196
3197/*
3198 * expr4a && expr4a && expr4a logical AND
3199 *
3200 * Produces instructions:
3201 * EVAL expr4a Push result of "expr4a"
3202 * COND2BOOL convert to bool if needed
3203 * JUMP_IF_COND_FALSE end
3204 * EVAL expr4b Push result of "expr4b"
3205 * JUMP_IF_COND_FALSE end
3206 * EVAL expr4c Push result of "expr4c"
3207 * end:
3208 */
3209 static int
3210compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3211{
3212 int ppconst_used = ppconst->pp_used;
3213
3214 // get the first variable
3215 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3216 return FAIL;
3217
3218 // || and && work almost the same
3219 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3220}
3221
3222/*
3223 * expr3a || expr3b || expr3c logical OR
3224 *
3225 * Produces instructions:
3226 * EVAL expr3a Push result of "expr3a"
3227 * COND2BOOL convert to bool if needed
3228 * JUMP_IF_COND_TRUE end
3229 * EVAL expr3b Push result of "expr3b"
3230 * JUMP_IF_COND_TRUE end
3231 * EVAL expr3c Push result of "expr3c"
3232 * end:
3233 */
3234 static int
3235compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3236{
3237 int ppconst_used = ppconst->pp_used;
3238
3239 // eval the first expression
3240 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3241 return FAIL;
3242
3243 // || and && work almost the same
3244 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3245}
3246
3247/*
3248 * Toplevel expression: expr2 ? expr1a : expr1b
3249 * Produces instructions:
3250 * EVAL expr2 Push result of "expr2"
3251 * JUMP_IF_FALSE alt jump if false
3252 * EVAL expr1a
3253 * JUMP_ALWAYS end
3254 * alt: EVAL expr1b
3255 * end:
3256 *
3257 * Toplevel expression: expr2 ?? expr1
3258 * Produces instructions:
3259 * EVAL expr2 Push result of "expr2"
3260 * JUMP_AND_KEEP_IF_TRUE end jump if true
3261 * EVAL expr1
3262 * end:
3263 */
3264 int
3265compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3266{
3267 char_u *p;
3268 int ppconst_used = ppconst->pp_used;
3269 char_u *next;
3270
3271 // Ignore all kinds of errors when not producing code.
3272 if (cctx->ctx_skip == SKIP_YES)
3273 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003274 int prev_did_emsg = did_emsg;
3275
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003276 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003277 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003278 }
3279
3280 // Evaluate the first expression.
3281 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3282 return FAIL;
3283
3284 p = may_peek_next_line(cctx, *arg, &next);
3285 if (*p == '?')
3286 {
3287 int op_falsy = p[1] == '?';
3288 garray_T *instr = &cctx->ctx_instr;
3289 garray_T *stack = &cctx->ctx_type_stack;
3290 int alt_idx = instr->ga_len;
3291 int end_idx = 0;
3292 isn_T *isn;
3293 type_T *type1 = NULL;
3294 int has_const_expr = FALSE;
3295 int const_value = FALSE;
3296 int save_skip = cctx->ctx_skip;
3297
3298 if (next != NULL)
3299 {
3300 *arg = next_line_from_context(cctx, TRUE);
3301 p = skipwhite(*arg);
3302 }
3303
3304 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3305 {
3306 semsg(_(e_white_space_required_before_and_after_str_at_str),
3307 op_falsy ? "??" : "?", p);
3308 return FAIL;
3309 }
3310
3311 if (ppconst->pp_used == ppconst_used + 1)
3312 {
3313 // the condition is a constant, we know whether the ? or the :
3314 // expression is to be evaluated.
3315 has_const_expr = TRUE;
3316 if (op_falsy)
3317 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3318 else
3319 {
3320 int error = FALSE;
3321
3322 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3323 &error);
3324 if (error)
3325 return FAIL;
3326 }
3327 cctx->ctx_skip = save_skip == SKIP_YES ||
3328 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3329
3330 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3331 // "left ?? right" and "left" is truthy: produce "left"
3332 generate_ppconst(cctx, ppconst);
3333 else
3334 {
3335 clear_tv(&ppconst->pp_tv[ppconst_used]);
3336 --ppconst->pp_used;
3337 }
3338 }
3339 else
3340 {
3341 generate_ppconst(cctx, ppconst);
3342 if (op_falsy)
3343 end_idx = instr->ga_len;
3344 generate_JUMP(cctx, op_falsy
3345 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3346 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003347 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003348 }
3349
3350 // evaluate the second expression; any type is accepted
3351 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3352 return FAIL;
3353 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3354 return FAIL;
3355
3356 if (!has_const_expr)
3357 {
3358 generate_ppconst(cctx, ppconst);
3359
3360 if (!op_falsy)
3361 {
3362 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003363 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003364 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003365
3366 end_idx = instr->ga_len;
3367 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3368
3369 // jump here from JUMP_IF_FALSE
3370 isn = ((isn_T *)instr->ga_data) + alt_idx;
3371 isn->isn_arg.jump.jump_where = instr->ga_len;
3372 }
3373 }
3374
3375 if (!op_falsy)
3376 {
3377 // Check for the ":".
3378 p = may_peek_next_line(cctx, *arg, &next);
3379 if (*p != ':')
3380 {
3381 emsg(_(e_missing_colon_after_questionmark));
3382 return FAIL;
3383 }
3384 if (next != NULL)
3385 {
3386 *arg = next_line_from_context(cctx, TRUE);
3387 p = skipwhite(*arg);
3388 }
3389
3390 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3391 {
3392 semsg(_(e_white_space_required_before_and_after_str_at_str),
3393 ":", p);
3394 return FAIL;
3395 }
3396
3397 // evaluate the third expression
3398 if (has_const_expr)
3399 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3400 ? SKIP_YES : SKIP_NOT;
3401 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3402 return FAIL;
3403 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3404 return FAIL;
3405 }
3406
3407 if (!has_const_expr)
3408 {
3409 type_T **typep;
3410
3411 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003412 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003413
3414 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003415 typep = &((((type2_T *)stack->ga_data)
3416 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003417 common_type(type1, *typep, typep, cctx->ctx_type_list);
3418
3419 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3420 isn = ((isn_T *)instr->ga_data) + end_idx;
3421 isn->isn_arg.jump.jump_where = instr->ga_len;
3422 }
3423
3424 cctx->ctx_skip = save_skip;
3425 }
3426 return OK;
3427}
3428
3429/*
3430 * Toplevel expression.
3431 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3432 * Returns OK or FAIL.
3433 */
3434 int
3435compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3436{
3437 ppconst_T ppconst;
3438
3439 CLEAR_FIELD(ppconst);
3440 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3441 {
3442 clear_ppconst(&ppconst);
3443 return FAIL;
3444 }
3445 if (is_const != NULL)
3446 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3447 if (generate_ppconst(cctx, &ppconst) == FAIL)
3448 return FAIL;
3449 return OK;
3450}
3451
3452/*
3453 * Toplevel expression.
3454 */
3455 int
3456compile_expr0(char_u **arg, cctx_T *cctx)
3457{
3458 return compile_expr0_ext(arg, cctx, NULL);
3459}
3460
3461
3462#endif // defined(FEAT_EVAL)