blob: bbf3059e2b00a1f56fac347bac035d0530f66882 [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 Moolenaar58b40092023-01-11 15:59:05 +0000266 class_T *cl = (class_T *)type->tt_member;
267 int is_super = type->tt_flags & TTFLAG_SUPER;
268 if (type == &t_super)
269 {
270 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000271 {
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000272 emsg(_(e_using_super_not_in_class_function));
273 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000274 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000275 is_super = TRUE;
276 cl = cctx->ctx_ufunc->uf_class;
277 // Remove &t_super from the stack.
278 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000279 }
280 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000281 {
282 garray_T *instr = &cctx->ctx_instr;
283 if (instr->ga_len > 0)
284 {
285 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
286 if (isn->isn_type == ISN_LOADSCRIPT)
287 {
288 // The class was recognized as a script item. We only need
289 // to know what class it is, drop the instruction.
290 --instr->ga_len;
291 vim_free(isn->isn_arg.script.scriptref);
292 }
293 }
294 }
295
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000296 ++*arg;
297 char_u *name = *arg;
298 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
299 if (name_end == name)
300 return FAIL;
301 size_t len = name_end - name;
302
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000303 if (*name_end == '(')
304 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000305 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000306 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000307 ufunc_T **functions;
308
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000309 if (type->tt_type == VAR_CLASS)
310 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000311 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000312 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000313 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000314 }
315 else
316 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000317 // type->tt_type == VAR_OBJECT: method call
318 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000319 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000320 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000321 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000322
323 ufunc_T *ufunc = NULL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000324 for (int i = is_super ? child_count : 0; i < function_count; ++i)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000325 {
326 ufunc_T *fp = functions[i];
327 // Use a separate pointer to avoid that ASAN complains about
328 // uf_name[] only being 4 characters.
329 char_u *ufname = (char_u *)fp->uf_name;
330 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
331 {
332 ufunc = fp;
333 break;
334 }
335 }
336 if (ufunc == NULL)
337 {
338 // TODO: different error for object method?
339 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
340 return FAIL;
341 }
342
343 // Compile the arguments and call the class function or object method.
344 // The object method will know that the object is on the stack, just
345 // before the arguments.
346 *arg = skipwhite(name_end + 1);
347 int argcount = 0;
348 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
349 return FAIL;
350 return generate_CALL(cctx, ufunc, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000351 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000352
353 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000354 {
355 for (int i = 0; i < cl->class_obj_member_count; ++i)
356 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000357 ocmember_T *m = &cl->class_obj_members[i];
358 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000359 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000360 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
361 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000362 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000363 return FAIL;
364 }
365
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000366 *arg = name_end;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000367 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000368 }
369 }
370
371 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
372 }
373 else
374 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000375 // load class member
376 int idx;
377 for (idx = 0; idx < cl->class_class_member_count; ++idx)
378 {
379 ocmember_T *m = &cl->class_class_members[idx];
380 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
381 break;
382 }
383 if (idx < cl->class_class_member_count)
384 {
385 *arg = name_end;
386 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
387 }
388 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000389 }
390
391 return FAIL;
392}
393
394/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000395 * Generate an instruction to load script-local variable "name", without the
396 * leading "s:".
397 * Also finds imported variables.
398 */
399 int
400compile_load_scriptvar(
401 cctx_T *cctx,
402 char_u *name, // variable NUL terminated
403 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100404 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000405{
406 scriptitem_T *si;
407 int idx;
408 imported_T *import;
409
410 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
411 return FAIL;
412 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000413 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 if (idx >= 0)
415 {
416 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
417
418 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
419 current_sctx.sc_sid, idx, sv->sv_type);
420 return OK;
421 }
422
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000423 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000424 if (import != NULL)
425 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000426 char_u *p = skipwhite(*end);
427 char_u *exp_name;
428 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100429 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000430 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000431 int done = FALSE;
432 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000433
434 // Need to lookup the member.
435 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000436 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000437 semsg(_(e_expected_dot_after_name_str), start);
438 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000439 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000440 ++p;
441 if (VIM_ISWHITE(*p))
442 {
443 emsg(_(e_no_white_space_allowed_after_dot));
444 return FAIL;
445 }
446
447 // isolate one name
448 exp_name = p;
449 while (eval_isnamec(*p))
450 ++p;
451 cc = *p;
452 *p = NUL;
453
Bram Moolenaard041f422022-01-12 19:54:00 +0000454 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100455 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
456 // "import autoload './dir/script.vim'" or
457 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100458 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100459
460 if (res == OK)
461 {
462 if (si->sn_autoload_prefix != NULL
463 && si->sn_state == SN_STATE_NOT_LOADED)
464 {
465 char_u *auto_name =
466 concat_str(si->sn_autoload_prefix, exp_name);
467
468 // autoload script must be loaded later, access by the autoload
469 // name. If a '(' follows it must be a function. Otherwise we
470 // don't know, it can be "script.Func".
471 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100472 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100473 else
474 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
475 vim_free(auto_name);
476 done = TRUE;
477 }
478 else if (si->sn_import_autoload
479 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100480 {
481 // If a '(' follows it must be a function. Otherwise we don't
482 // know, it can be "script.Func".
483 if (cc == '(' || paren_follows_after_expr)
484 {
485 char_u sid_name[MAX_FUNC_NAME_LEN];
486
487 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100488 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100489 }
490 else
491 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
492 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100493 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100494 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100495 else
496 {
497 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
498 cctx, NULL, TRUE);
499 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100500 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100501
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000502 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000503 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000504 if (done)
505 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000506
507 if (idx < 0)
508 {
509 if (ufunc != NULL)
510 {
511 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100512 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000513 return OK;
514 }
515 return FAIL;
516 }
517
518 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
519 import->imp_sid,
520 idx,
521 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000522 return OK;
523 }
524
Bram Moolenaard0132f42022-05-12 11:05:40 +0100525 // Can only get here if we know "name" is a script variable and not in a
526 // Vim9 script (variable is not in sn_var_vals): old style script.
527 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000528 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529}
530
531 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000532generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000534 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000535 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000537 // Reject a global non-autoload function found without the "g:" prefix.
538 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539 return FAIL;
540
541 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000542 compile_type = get_compile_type(ufunc);
543 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000544 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100546 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547}
548
549/*
550 * Compile a variable name into a load instruction.
551 * "end" points to just after the name.
552 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
553 * When "error" is FALSE do not give an error when not found.
554 */
555 int
556compile_load(
557 char_u **arg,
558 char_u *end_arg,
559 cctx_T *cctx,
560 int is_expr,
561 int error)
562{
563 type_T *type;
564 char_u *name = NULL;
565 char_u *end = end_arg;
566 int res = FAIL;
567 int prev_called_emsg = called_emsg;
568
569 if (*(*arg + 1) == ':')
570 {
571 if (end <= *arg + 2)
572 {
573 isntype_T isn_type;
574
575 // load dictionary of namespace
576 switch (**arg)
577 {
578 case 'g': isn_type = ISN_LOADGDICT; break;
579 case 'w': isn_type = ISN_LOADWDICT; break;
580 case 't': isn_type = ISN_LOADTDICT; break;
581 case 'b': isn_type = ISN_LOADBDICT; break;
582 default:
583 semsg(_(e_namespace_not_supported_str), *arg);
584 goto theend;
585 }
586 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
587 goto theend;
588 res = OK;
589 }
590 else
591 {
592 isntype_T isn_type = ISN_DROP;
593
594 // load namespaced variable
595 name = vim_strnsave(*arg + 2, end - (*arg + 2));
596 if (name == NULL)
597 return FAIL;
598
599 switch (**arg)
600 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100601 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000603 case 's': if (current_script_is_vim9())
604 {
605 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
606 *arg);
607 vim_free(name);
608 return FAIL;
609 }
Kota Kato948a3892022-08-16 16:09:59 +0100610 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000611 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612 else
613 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100614 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 break;
616 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
617 {
618 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000619 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000620 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000621 else
622 isn_type = ISN_LOADG;
623 }
624 else
625 {
626 isn_type = ISN_LOADAUTO;
627 vim_free(name);
628 name = vim_strnsave(*arg, end - *arg);
629 if (name == NULL)
630 return FAIL;
631 }
632 break;
633 case 'w': isn_type = ISN_LOADW; break;
634 case 't': isn_type = ISN_LOADT; break;
635 case 'b': isn_type = ISN_LOADB; break;
636 default: // cannot happen, just in case
637 semsg(_(e_namespace_not_supported_str), *arg);
638 goto theend;
639 }
640 if (isn_type != ISN_DROP)
641 {
642 // Global, Buffer-local, Window-local and Tabpage-local
643 // variables can be defined later, thus we don't check if it
644 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000645 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646 }
647 }
648 }
649 else
650 {
651 size_t len = end - *arg;
652 int idx;
653 int gen_load = FALSE;
654 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100655 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100656 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657
658 name = vim_strnsave(*arg, end - *arg);
659 if (name == NULL)
660 return FAIL;
661
Bram Moolenaar58b40092023-01-11 15:59:05 +0000662 if (STRCMP(name, "super") == 0
663 && cctx->ctx_ufunc != NULL
664 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
665 {
666 // super.SomeFunc() in a class function: push &t_super type, this
667 // is recognized in compile_subscript().
668 res = push_type_stack(cctx, &t_super);
669 if (*end != '.')
670 emsg(_(e_super_must_be_followed_by_dot));
671 }
672 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000673 {
674 script_autoload(name, FALSE);
675 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
676 }
677 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
678 == OK)
679 {
680 if (gen_load_outer == 0)
681 gen_load = TRUE;
682 }
683 else
684 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000685 lvar_T lvar;
686 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687
688 if (lookup_local(*arg, len, &lvar, cctx) == OK)
689 {
690 type = lvar.lv_type;
691 idx = lvar.lv_idx;
692 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100693 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100695 outer_loop_depth = lvar.lv_loop_depth;
696 outer_loop_idx = lvar.lv_loop_idx;
697 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000698 else
699 gen_load = TRUE;
700 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000701 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000702 {
703 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
704 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000705 else
706 {
707 // "var" can be script-local even without using "s:" if it
708 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000709 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000710 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100711 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000712
713 // When evaluating an expression and the name starts with an
714 // uppercase letter it can be a user defined function.
715 // generate_funcref() will fail if the function can't be found.
716 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000717 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718 }
719 }
720 if (gen_load)
721 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
722 if (gen_load_outer > 0)
723 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100724 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
725 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000726 cctx->ctx_outer_used = TRUE;
727 }
728 }
729
730 *arg = end;
731
732theend:
733 if (res == FAIL && error && called_emsg == prev_called_emsg)
734 semsg(_(e_variable_not_found_str), name);
735 vim_free(name);
736 return res;
737}
738
739/*
740 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100741 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 * Returns FAIL if compilation fails.
743 */
744 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100745compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746{
LemonBoyf3b48952022-05-05 13:53:03 +0100747 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 garray_T save_ga = cctx->ctx_instr;
749 int expr_res;
750 int trailing_error;
751 int instr_count;
752 isn_T *instr = NULL;
753
754 // Remove the string type from the stack.
755 --cctx->ctx_type_stack.ga_len;
756
757 // Temporarily reset the list of instructions so that the jump labels are
758 // correct.
759 cctx->ctx_instr.ga_len = 0;
760 cctx->ctx_instr.ga_maxlen = 0;
761 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000762
763 // avoid peeking a next line
764 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
765 cctx->ctx_ufunc->uf_lines.ga_len = 0;
766
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000768
769 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
770
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 s = skipwhite(s);
772 trailing_error = *s != NUL;
773
774 if (expr_res == FAIL || trailing_error
775 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
776 {
777 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000778 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 clear_instr_ga(&cctx->ctx_instr);
780 cctx->ctx_instr = save_ga;
781 ++cctx->ctx_type_stack.ga_len;
782 return FAIL;
783 }
784
785 // Move the generated instructions into the ISN_INSTR instruction, then
786 // restore the list of instructions.
787 instr_count = cctx->ctx_instr.ga_len;
788 instr = cctx->ctx_instr.ga_data;
789 instr[instr_count].isn_type = ISN_FINISH;
790
791 cctx->ctx_instr = save_ga;
792 vim_free(isn->isn_arg.string);
793 isn->isn_type = ISN_INSTR;
794 isn->isn_arg.instr = instr;
795 return OK;
796}
797
798/*
799 * Compile the argument expressions.
800 * "arg" points to just after the "(" and is advanced to after the ")"
801 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100802 int
LemonBoyf3b48952022-05-05 13:53:03 +0100803compile_arguments(
804 char_u **arg,
805 cctx_T *cctx,
806 int *argcount,
807 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000808{
809 char_u *p = *arg;
810 char_u *whitep = *arg;
811 int must_end = FALSE;
812 int instr_count;
813
814 for (;;)
815 {
816 if (may_get_next_line(whitep, &p, cctx) == FAIL)
817 goto failret;
818 if (*p == ')')
819 {
820 *arg = p + 1;
821 return OK;
822 }
823 if (must_end)
824 {
825 semsg(_(e_missing_comma_before_argument_str), p);
826 return FAIL;
827 }
828
829 instr_count = cctx->ctx_instr.ga_len;
830 if (compile_expr0(&p, cctx) == FAIL)
831 return FAIL;
832 ++*argcount;
833
LemonBoyf3b48952022-05-05 13:53:03 +0100834 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 && cctx->ctx_instr.ga_len == instr_count + 1)
836 {
837 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
838
839 // {skip} argument of searchpair() can be compiled if not empty
840 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100841 compile_string(isn, cctx, 0);
842 }
843 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
844 && cctx->ctx_instr.ga_len == instr_count + 1)
845 {
846 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
847
848 // {sub} argument of substitute() can be compiled if it starts
849 // with \=
850 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100851 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100852 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 }
854
855 if (*p != ',' && *skipwhite(p) == ',')
856 {
857 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
858 p = skipwhite(p);
859 }
860 if (*p == ',')
861 {
862 ++p;
863 if (*p != NUL && !VIM_ISWHITE(*p))
864 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
865 }
866 else
867 must_end = TRUE;
868 whitep = p;
869 p = skipwhite(p);
870 }
871failret:
872 emsg(_(e_missing_closing_paren));
873 return FAIL;
874}
875
876/*
877 * Compile a function call: name(arg1, arg2)
878 * "arg" points to "name", "arg + varlen" to the "(".
879 * "argcount_init" is 1 for "value->method()"
880 * Instructions:
881 * EVAL arg1
882 * EVAL arg2
883 * BCALL / DCALL / UCALL
884 */
885 static int
886compile_call(
887 char_u **arg,
888 size_t varlen,
889 cctx_T *cctx,
890 ppconst_T *ppconst,
891 int argcount_init)
892{
893 char_u *name = *arg;
894 char_u *p;
895 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100896 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 char_u fname_buf[FLEN_FIXED + 1];
898 char_u *tofree = NULL;
899 int error = FCERR_NONE;
900 ufunc_T *ufunc = NULL;
901 int res = FAIL;
902 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000903 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100904 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000905 imported_T *import;
906
907 if (varlen >= sizeof(namebuf))
908 {
909 semsg(_(e_name_too_long_str), name);
910 return FAIL;
911 }
912 vim_strncpy(namebuf, *arg, varlen);
913
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000914 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000915 if (import != NULL)
916 {
917 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
918 return FAIL;
919 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920
921 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100922 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100924 if ((varlen == 3
925 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
927 {
928 char_u *s = skipwhite(*arg + varlen + 1);
929 typval_T argvars[2];
930 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100931 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000932
933 argvars[0].v_type = VAR_UNKNOWN;
934 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100935 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100937 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938 s = skipwhite(s);
939 if (*s == ')' && argvars[0].v_type == VAR_STRING
940 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
941 || !is_has))
942 {
943 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
944
945 *arg = s + 1;
946 argvars[1].v_type = VAR_UNKNOWN;
947 tv->v_type = VAR_NUMBER;
948 tv->vval.v_number = 0;
949 if (is_has)
950 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100951 else if (is_len)
952 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000953 else
954 f_exists(argvars, tv);
955 clear_tv(&argvars[0]);
956 ++ppconst->pp_used;
957 return OK;
958 }
959 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100960 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 {
962 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
963 return FAIL;
964 }
965 }
966
967 if (generate_ppconst(cctx, ppconst) == FAIL)
968 return FAIL;
969
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
971
972 // We handle the "skip" argument of searchpair() and searchpairpos()
973 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100974 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
975 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
976 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
977 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
978 special_fn = CA_SEARCHPAIR;
979 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
980 special_fn = CA_SUBSTITUTE;
981 else
982 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983
984 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100985 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 goto theend;
987
988 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
989 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
990 {
991 int idx;
992
993 // builtin function
994 idx = find_internal_func(name);
995 if (idx >= 0)
996 {
997 if (STRCMP(name, "flatten") == 0)
998 {
999 emsg(_(e_cannot_use_flatten_in_vim9_script));
1000 goto theend;
1001 }
1002
1003 if (STRCMP(name, "add") == 0 && argcount == 2)
1004 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +00001005 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001006
1007 // add() can be compiled to instructions if we know the type
1008 if (type->tt_type == VAR_LIST)
1009 {
1010 // inline "add(list, item)" so that the type can be checked
1011 res = generate_LISTAPPEND(cctx);
1012 idx = -1;
1013 }
1014 else if (type->tt_type == VAR_BLOB)
1015 {
1016 // inline "add(blob, nr)" so that the type can be checked
1017 res = generate_BLOBAPPEND(cctx);
1018 idx = -1;
1019 }
1020 }
1021
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001022 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1023 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001024 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001025 // May have the "D" or "R" flag, reserve a variable for a
1026 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001027 if (get_defer_var_idx(cctx) == 0)
1028 idx = -1;
1029 }
1030
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001031 if (idx >= 0)
1032 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1033 }
1034 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001035 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 goto theend;
1037 }
1038
Bram Moolenaar62aec932022-01-29 21:45:34 +00001039 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1040
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041 // An argument or local variable can be a function reference, this
1042 // overrules a function name.
1043 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1044 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1045 {
1046 // If we can find the function by name generate the right call.
1047 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001048 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001049 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001050 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001051 if (!func_is_global(ufunc))
1052 {
1053 res = generate_CALL(cctx, ufunc, argcount);
1054 goto theend;
1055 }
1056 if (!has_g_namespace
1057 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1058 {
1059 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001060 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001061 goto theend;
1062 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001063 }
1064 }
1065
1066 // If the name is a variable, load it and use PCALL.
1067 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001068 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001069 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001070 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1072 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001073 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001074
1075 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1076 goto theend;
1077 }
1078
1079 // If we can find a global function by name generate the right call.
1080 if (ufunc != NULL)
1081 {
1082 res = generate_CALL(cctx, ufunc, argcount);
1083 goto theend;
1084 }
1085
1086 // A global function may be defined only later. Need to figure out at
1087 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001088 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089 res = generate_UCALL(cctx, name, argcount);
1090 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001091 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092
1093theend:
1094 vim_free(tofree);
1095 return res;
1096}
1097
1098// like NAMESPACE_CHAR but with 'a' and 'l'.
1099#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1100
1101/*
1102 * Find the end of a variable or function name. Unlike find_name_end() this
1103 * does not recognize magic braces.
1104 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1105 * Return a pointer to just after the name. Equal to "arg" if there is no
1106 * valid name.
1107 */
1108 char_u *
1109to_name_end(char_u *arg, int use_namespace)
1110{
1111 char_u *p;
1112
1113 // Quick check for valid starting character.
1114 if (!eval_isnamec1(*arg))
1115 return arg;
1116
1117 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1118 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1119 // and can be used in slice "[n:]".
1120 if (*p == ':' && (p != arg + 1
1121 || !use_namespace
1122 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1123 break;
1124 return p;
1125}
1126
1127/*
1128 * Like to_name_end() but also skip over a list or dict constant.
1129 * Also accept "<SNR>123_Func".
1130 * This intentionally does not handle line continuation.
1131 */
1132 char_u *
1133to_name_const_end(char_u *arg)
1134{
1135 char_u *p = arg;
1136 typval_T rettv;
1137
1138 if (STRNCMP(p, "<SNR>", 5) == 0)
1139 p = skipdigits(p + 5);
1140 p = to_name_end(p, TRUE);
1141 if (p == arg && *arg == '[')
1142 {
1143
1144 // Can be "[1, 2, 3]->Func()".
1145 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1146 p = arg;
1147 }
1148 return p;
1149}
1150
1151/*
1152 * parse a list: [expr, expr]
1153 * "*arg" points to the '['.
1154 * ppconst->pp_is_const is set if all items are a constant.
1155 */
1156 static int
1157compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1158{
1159 char_u *p = skipwhite(*arg + 1);
1160 char_u *whitep = *arg + 1;
1161 int count = 0;
1162 int is_const;
1163 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001164 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165
1166 for (;;)
1167 {
1168 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1169 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001170 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171 return FAIL;
1172 }
1173 if (*p == ',')
1174 {
1175 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1176 return FAIL;
1177 }
1178 if (*p == ']')
1179 {
1180 ++p;
1181 break;
1182 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001183 if (must_end)
1184 {
1185 semsg(_(e_missing_comma_in_list_str), p);
1186 return FAIL;
1187 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1189 return FAIL;
1190 if (!is_const)
1191 is_all_const = FALSE;
1192 ++count;
1193 if (*p == ',')
1194 {
1195 ++p;
1196 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1197 {
1198 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1199 return FAIL;
1200 }
1201 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001202 else
1203 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204 whitep = p;
1205 p = skipwhite(p);
1206 }
1207 *arg = p;
1208
1209 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001210 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211}
1212
1213/*
1214 * Parse a lambda: "(arg, arg) => expr"
1215 * "*arg" points to the '('.
1216 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1217 */
1218 static int
1219compile_lambda(char_u **arg, cctx_T *cctx)
1220{
1221 int r;
1222 typval_T rettv;
1223 ufunc_T *ufunc;
1224 evalarg_T evalarg;
1225
1226 init_evalarg(&evalarg);
1227 evalarg.eval_flags = EVAL_EVALUATE;
1228 evalarg.eval_cctx = cctx;
1229
1230 // Get the funcref in "rettv".
1231 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1232 if (r != OK)
1233 {
1234 clear_evalarg(&evalarg, NULL);
1235 return r;
1236 }
1237
1238 // "rettv" will now be a partial referencing the function.
1239 ufunc = rettv.vval.v_partial->pt_func;
1240 ++ufunc->uf_refcount;
1241 clear_tv(&rettv);
1242
1243 // Compile it here to get the return type. The return type is optional,
1244 // when it's missing use t_unknown. This is recognized in
1245 // compile_return().
1246 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1247 ufunc->uf_ret_type = &t_unknown;
1248 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1249
1250 // When the outer function is compiled for profiling or debugging, the
1251 // lambda may be called without profiling or debugging. Compile it here in
1252 // the right context.
1253 if (cctx->ctx_compile_type == CT_DEBUG
1254#ifdef FEAT_PROFILE
1255 || cctx->ctx_compile_type == CT_PROFILE
1256#endif
1257 )
1258 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1259
Bram Moolenaar139575d2022-03-15 19:29:30 +00001260 // if the outer function is not compiled for debugging or profiling, this
1261 // one might be
1262 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001263 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001264 compiletype_T compile_type = get_compile_type(ufunc);
1265
1266 if (compile_type != CT_NONE)
1267 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001268 }
1269
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1271 // "*arg" may point into it. Point into the original line to avoid a
1272 // dangling pointer.
1273 if (evalarg.eval_using_cmdline)
1274 {
1275 garray_T *gap = &evalarg.eval_tofree_ga;
1276 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1277
1278 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1279 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001280 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001281 }
1282
1283 clear_evalarg(&evalarg, NULL);
1284
1285 if (ufunc->uf_def_status == UF_COMPILED)
1286 {
1287 // The return type will now be known.
1288 set_function_type(ufunc);
1289
1290 // The function reference count will be 1. When the ISN_FUNCREF
1291 // instruction is deleted the reference count is decremented and the
1292 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001293 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 }
1295
1296 func_ptr_unref(ufunc);
1297 return FAIL;
1298}
1299
1300/*
1301 * Get a lambda and compile it. Uses Vim9 syntax.
1302 */
1303 int
1304get_lambda_tv_and_compile(
1305 char_u **arg,
1306 typval_T *rettv,
1307 int types_optional,
1308 evalarg_T *evalarg)
1309{
1310 int r;
1311 ufunc_T *ufunc;
1312 int save_sc_version = current_sctx.sc_version;
1313
1314 // Get the funcref in "rettv".
1315 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1316 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1317 current_sctx.sc_version = save_sc_version;
1318 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001319 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320
1321 // "rettv" will now be a partial referencing the function.
1322 ufunc = rettv->vval.v_partial->pt_func;
1323
1324 // Compile it here to get the return type. The return type is optional,
1325 // when it's missing use t_unknown. This is recognized in
1326 // compile_return().
1327 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1328 ufunc->uf_ret_type = &t_unknown;
1329 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1330
1331 if (ufunc->uf_def_status == UF_COMPILED)
1332 {
1333 // The return type will now be known.
1334 set_function_type(ufunc);
1335 return OK;
1336 }
1337 clear_tv(rettv);
1338 return FAIL;
1339}
1340
1341/*
1342 * parse a dict: {key: val, [key]: val}
1343 * "*arg" points to the '{'.
1344 * ppconst->pp_is_const is set if all item values are a constant.
1345 */
1346 static int
1347compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1348{
1349 garray_T *instr = &cctx->ctx_instr;
1350 int count = 0;
1351 dict_T *d = dict_alloc();
1352 dictitem_T *item;
1353 char_u *whitep = *arg + 1;
1354 char_u *p;
1355 int is_const;
1356 int is_all_const = TRUE; // reset when non-const encountered
1357
1358 if (d == NULL)
1359 return FAIL;
1360 if (generate_ppconst(cctx, ppconst) == FAIL)
1361 return FAIL;
1362 for (;;)
1363 {
1364 char_u *key = NULL;
1365
1366 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1367 {
1368 *arg = NULL;
1369 goto failret;
1370 }
1371
1372 if (**arg == '}')
1373 break;
1374
1375 if (**arg == '[')
1376 {
1377 isn_T *isn;
1378
1379 // {[expr]: value} uses an evaluated key.
1380 *arg = skipwhite(*arg + 1);
1381 if (compile_expr0(arg, cctx) == FAIL)
1382 return FAIL;
1383 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1384 if (isn->isn_type == ISN_PUSHNR)
1385 {
1386 char buf[NUMBUFLEN];
1387
1388 // Convert to string at compile time.
1389 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1390 isn->isn_type = ISN_PUSHS;
1391 isn->isn_arg.string = vim_strsave((char_u *)buf);
1392 }
1393 if (isn->isn_type == ISN_PUSHS)
1394 key = isn->isn_arg.string;
1395 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1396 return FAIL;
1397 *arg = skipwhite(*arg);
1398 if (**arg != ']')
1399 {
1400 emsg(_(e_missing_matching_bracket_after_dict_key));
1401 return FAIL;
1402 }
1403 ++*arg;
1404 }
1405 else
1406 {
1407 // {"name": value},
1408 // {'name': value},
1409 // {name: value} use "name" as a literal key
1410 key = get_literal_key(arg);
1411 if (key == NULL)
1412 return FAIL;
1413 if (generate_PUSHS(cctx, &key) == FAIL)
1414 return FAIL;
1415 }
1416
1417 // Check for duplicate keys, if using string keys.
1418 if (key != NULL)
1419 {
1420 item = dict_find(d, key, -1);
1421 if (item != NULL)
1422 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001423 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001424 goto failret;
1425 }
1426 item = dictitem_alloc(key);
1427 if (item != NULL)
1428 {
1429 item->di_tv.v_type = VAR_UNKNOWN;
1430 item->di_tv.v_lock = 0;
1431 if (dict_add(d, item) == FAIL)
1432 dictitem_free(item);
1433 }
1434 }
1435
1436 if (**arg != ':')
1437 {
1438 if (*skipwhite(*arg) == ':')
1439 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1440 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001441 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 return FAIL;
1443 }
1444 whitep = *arg + 1;
1445 if (!IS_WHITE_OR_NUL(*whitep))
1446 {
1447 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1448 return FAIL;
1449 }
1450
1451 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1452 {
1453 *arg = NULL;
1454 goto failret;
1455 }
1456
1457 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1458 return FAIL;
1459 if (!is_const)
1460 is_all_const = FALSE;
1461 ++count;
1462
1463 whitep = *arg;
1464 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1465 {
1466 *arg = NULL;
1467 goto failret;
1468 }
1469 if (**arg == '}')
1470 break;
1471 if (**arg != ',')
1472 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001473 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001474 goto failret;
1475 }
1476 if (IS_WHITE_OR_NUL(*whitep))
1477 {
1478 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1479 return FAIL;
1480 }
1481 whitep = *arg + 1;
1482 if (!IS_WHITE_OR_NUL(*whitep))
1483 {
1484 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1485 return FAIL;
1486 }
1487 *arg = skipwhite(whitep);
1488 }
1489
1490 *arg = *arg + 1;
1491
1492 // Allow for following comment, after at least one space.
1493 p = skipwhite(*arg);
1494 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1495 *arg += STRLEN(*arg);
1496
1497 dict_unref(d);
1498 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001499 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001500
1501failret:
1502 if (*arg == NULL)
1503 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001504 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001505 *arg = (char_u *)"";
1506 }
1507 dict_unref(d);
1508 return FAIL;
1509}
1510
1511/*
1512 * Compile "&option".
1513 */
1514 static int
1515compile_get_option(char_u **arg, cctx_T *cctx)
1516{
1517 typval_T rettv;
1518 char_u *start = *arg;
1519 int ret;
1520
1521 // parse the option and get the current value to get the type.
1522 rettv.v_type = VAR_UNKNOWN;
1523 ret = eval_option(arg, &rettv, TRUE);
1524 if (ret == OK)
1525 {
1526 // include the '&' in the name, eval_option() expects it.
1527 char_u *name = vim_strnsave(start, *arg - start);
1528 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1529 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1530
1531 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1532 vim_free(name);
1533 }
1534 clear_tv(&rettv);
1535
1536 return ret;
1537}
1538
1539/*
1540 * Compile "$VAR".
1541 */
1542 static int
1543compile_get_env(char_u **arg, cctx_T *cctx)
1544{
1545 char_u *start = *arg;
1546 int len;
1547 int ret;
1548 char_u *name;
1549
1550 ++*arg;
1551 len = get_env_len(arg);
1552 if (len == 0)
1553 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001554 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555 return FAIL;
1556 }
1557
1558 // include the '$' in the name, eval_env_var() expects it.
1559 name = vim_strnsave(start, len + 1);
1560 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1561 vim_free(name);
1562 return ret;
1563}
1564
1565/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001566 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001567 */
1568 static int
1569compile_interp_string(char_u **arg, cctx_T *cctx)
1570{
1571 typval_T tv;
1572 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001573 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001574 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001575 int count = 0;
1576 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001577
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001578 // *arg is on the '$' character, move it to the first string character.
1579 ++*arg;
1580 quote = **arg;
1581 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001582
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001583 for (;;)
1584 {
1585 // Get the string up to the matching quote or to a single '{'.
1586 // "arg" is advanced to either the quote or the '{'.
1587 if (quote == '"')
1588 ret = eval_string(arg, &tv, evaluate, TRUE);
1589 else
1590 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1591 if (ret == FAIL)
1592 break;
1593 if (evaluate)
1594 {
1595 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1596 || (**arg != '{' && count == 0))
1597 {
1598 // generate non-empty string or empty string if it's the only
1599 // one
1600 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1601 return FAIL;
1602 tv.vval.v_string = NULL; // don't free it now
1603 ++count;
1604 }
1605 clear_tv(&tv);
1606 }
1607
1608 if (**arg != '{')
1609 {
1610 // found terminating quote
1611 ++*arg;
1612 break;
1613 }
1614
1615 p = compile_one_expr_in_str(*arg, cctx);
1616 if (p == NULL)
1617 {
1618 ret = FAIL;
1619 break;
1620 }
1621 ++count;
1622 *arg = p;
1623 }
LemonBoy2eaef102022-05-06 13:14:50 +01001624
1625 if (ret == FAIL || !evaluate)
1626 return ret;
1627
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001628 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1629 if (count > 1)
1630 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001631
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001632 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001633}
1634
1635/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001636 * Compile "@r".
1637 */
1638 static int
1639compile_get_register(char_u **arg, cctx_T *cctx)
1640{
1641 int ret;
1642
1643 ++*arg;
1644 if (**arg == NUL)
1645 {
1646 semsg(_(e_syntax_error_at_str), *arg - 1);
1647 return FAIL;
1648 }
1649 if (!valid_yank_reg(**arg, FALSE))
1650 {
1651 emsg_invreg(**arg);
1652 return FAIL;
1653 }
1654 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1655 ++*arg;
1656 return ret;
1657}
1658
1659/*
1660 * Apply leading '!', '-' and '+' to constant "rettv".
1661 * When "numeric_only" is TRUE do not apply '!'.
1662 */
1663 static int
1664apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1665{
1666 char_u *p = *end;
1667
1668 // this works from end to start
1669 while (p > start)
1670 {
1671 --p;
1672 if (*p == '-' || *p == '+')
1673 {
1674 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675 if (rettv->v_type == VAR_FLOAT)
1676 {
1677 if (*p == '-')
1678 rettv->vval.v_float = -rettv->vval.v_float;
1679 }
1680 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001681 {
1682 varnumber_T val;
1683 int error = FALSE;
1684
1685 // tv_get_number_chk() accepts a string, but we don't want that
1686 // here
1687 if (check_not_string(rettv) == FAIL)
1688 return FAIL;
1689 val = tv_get_number_chk(rettv, &error);
1690 clear_tv(rettv);
1691 if (error)
1692 return FAIL;
1693 if (*p == '-')
1694 val = -val;
1695 rettv->v_type = VAR_NUMBER;
1696 rettv->vval.v_number = val;
1697 }
1698 }
1699 else if (numeric_only)
1700 {
1701 ++p;
1702 break;
1703 }
1704 else if (*p == '!')
1705 {
1706 int v = tv2bool(rettv);
1707
1708 // '!' is permissive in the type.
1709 clear_tv(rettv);
1710 rettv->v_type = VAR_BOOL;
1711 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1712 }
1713 }
1714 *end = p;
1715 return OK;
1716}
1717
1718/*
1719 * Recognize v: variables that are constants and set "rettv".
1720 */
1721 static void
1722get_vim_constant(char_u **arg, typval_T *rettv)
1723{
1724 if (STRNCMP(*arg, "v:true", 6) == 0)
1725 {
1726 rettv->v_type = VAR_BOOL;
1727 rettv->vval.v_number = VVAL_TRUE;
1728 *arg += 6;
1729 }
1730 else if (STRNCMP(*arg, "v:false", 7) == 0)
1731 {
1732 rettv->v_type = VAR_BOOL;
1733 rettv->vval.v_number = VVAL_FALSE;
1734 *arg += 7;
1735 }
1736 else if (STRNCMP(*arg, "v:null", 6) == 0)
1737 {
1738 rettv->v_type = VAR_SPECIAL;
1739 rettv->vval.v_number = VVAL_NULL;
1740 *arg += 6;
1741 }
1742 else if (STRNCMP(*arg, "v:none", 6) == 0)
1743 {
1744 rettv->v_type = VAR_SPECIAL;
1745 rettv->vval.v_number = VVAL_NONE;
1746 *arg += 6;
1747 }
1748}
1749
1750 exprtype_T
1751get_compare_type(char_u *p, int *len, int *type_is)
1752{
1753 exprtype_T type = EXPR_UNKNOWN;
1754 int i;
1755
1756 switch (p[0])
1757 {
1758 case '=': if (p[1] == '=')
1759 type = EXPR_EQUAL;
1760 else if (p[1] == '~')
1761 type = EXPR_MATCH;
1762 break;
1763 case '!': if (p[1] == '=')
1764 type = EXPR_NEQUAL;
1765 else if (p[1] == '~')
1766 type = EXPR_NOMATCH;
1767 break;
1768 case '>': if (p[1] != '=')
1769 {
1770 type = EXPR_GREATER;
1771 *len = 1;
1772 }
1773 else
1774 type = EXPR_GEQUAL;
1775 break;
1776 case '<': if (p[1] != '=')
1777 {
1778 type = EXPR_SMALLER;
1779 *len = 1;
1780 }
1781 else
1782 type = EXPR_SEQUAL;
1783 break;
1784 case 'i': if (p[1] == 's')
1785 {
1786 // "is" and "isnot"; but not a prefix of a name
1787 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1788 *len = 5;
1789 i = p[*len];
1790 if (!isalnum(i) && i != '_')
1791 {
1792 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1793 *type_is = TRUE;
1794 }
1795 }
1796 break;
1797 }
1798 return type;
1799}
1800
1801/*
1802 * Skip over an expression, ignoring most errors.
1803 */
1804 void
1805skip_expr_cctx(char_u **arg, cctx_T *cctx)
1806{
1807 evalarg_T evalarg;
1808
1809 init_evalarg(&evalarg);
1810 evalarg.eval_cctx = cctx;
1811 skip_expr(arg, &evalarg);
1812 clear_evalarg(&evalarg, NULL);
1813}
1814
1815/*
1816 * Check that the top of the type stack has a type that can be used as a
1817 * condition. Give an error and return FAIL if not.
1818 */
1819 int
1820bool_on_stack(cctx_T *cctx)
1821{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 type_T *type;
1823
Bram Moolenaar078a4612022-01-04 15:17:03 +00001824 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 if (type == &t_bool)
1826 return OK;
1827
Bram Moolenaar4913d422022-10-17 13:13:32 +01001828 if (type->tt_type == VAR_ANY
1829 || type->tt_type == VAR_UNKNOWN
1830 || type->tt_type == VAR_NUMBER
1831 || type == &t_number_bool
1832 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001833 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1834 // This requires a runtime type check.
1835 return generate_COND2BOOL(cctx);
1836
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001837 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838}
1839
1840/*
1841 * Give the "white on both sides" error, taking the operator from "p[len]".
1842 */
1843 void
1844error_white_both(char_u *op, int len)
1845{
1846 char_u buf[10];
1847
1848 vim_strncpy(buf, op, len);
1849 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1850}
1851
1852/*
1853 * Compile code to apply '-', '+' and '!'.
1854 * When "numeric_only" is TRUE do not apply '!'.
1855 */
1856 static int
1857compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1858{
1859 char_u *p = *end;
1860
1861 // this works from end to start
1862 while (p > start)
1863 {
1864 --p;
1865 while (VIM_ISWHITE(*p))
1866 --p;
1867 if (*p == '-' || *p == '+')
1868 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001869 type_T *type = get_type_on_stack(cctx, 0);
1870 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001871 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001872 return FAIL;
1873
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001874 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001875 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1876 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 }
1878 else if (numeric_only)
1879 {
1880 ++p;
1881 break;
1882 }
1883 else
1884 {
1885 int invert = *p == '!';
1886
1887 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1888 {
1889 if (p[-1] == '!')
1890 invert = !invert;
1891 --p;
1892 }
1893 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1894 return FAIL;
1895 }
1896 }
1897 *end = p;
1898 return OK;
1899}
1900
1901/*
1902 * Compile "(expression)": recursive!
1903 * Return FAIL/OK.
1904 */
1905 static int
1906compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1907{
1908 int ret;
1909 char_u *p = *arg + 1;
1910
1911 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1912 return FAIL;
1913 if (ppconst->pp_used <= PPSIZE - 10)
1914 {
1915 ret = compile_expr1(arg, cctx, ppconst);
1916 }
1917 else
1918 {
1919 // Not enough space in ppconst, flush constants.
1920 if (generate_ppconst(cctx, ppconst) == FAIL)
1921 return FAIL;
1922 ret = compile_expr0(arg, cctx);
1923 }
1924 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1925 return FAIL;
1926 if (**arg == ')')
1927 ++*arg;
1928 else if (ret == OK)
1929 {
1930 emsg(_(e_missing_closing_paren));
1931 ret = FAIL;
1932 }
1933 return ret;
1934}
1935
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001936static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001937
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938/*
1939 * Compile whatever comes after "name" or "name()".
1940 * Advances "*arg" only when something was recognized.
1941 */
1942 static int
1943compile_subscript(
1944 char_u **arg,
1945 cctx_T *cctx,
1946 char_u *start_leader,
1947 char_u **end_leader,
1948 ppconst_T *ppconst)
1949{
1950 char_u *name_start = *end_leader;
1951 int keeping_dict = FALSE;
1952
1953 for (;;)
1954 {
1955 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001956 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001957
1958 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1959 {
1960 char_u *next = peek_next_line_from_context(cctx);
1961
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001962 // If a following line starts with "->{", "->(" or "->X" advance to
1963 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 // Also if a following line starts with ".x".
1965 if (next != NULL &&
1966 ((next[0] == '-' && next[1] == '>'
1967 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001968 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 || ASCII_ISALPHA(*skipwhite(next + 2))))
1970 || (next[0] == '.' && eval_isdictc(next[1]))))
1971 {
1972 next = next_line_from_context(cctx, TRUE);
1973 if (next == NULL)
1974 return FAIL;
1975 *arg = next;
1976 p = skipwhite(*arg);
1977 }
1978 }
1979
1980 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1981 // is not a function call.
1982 if (**arg == '(')
1983 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 int argcount = 0;
1985
1986 if (generate_ppconst(cctx, ppconst) == FAIL)
1987 return FAIL;
1988 ppconst->pp_is_const = FALSE;
1989
1990 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001991 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001992
1993 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001994 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 return FAIL;
1996 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1997 return FAIL;
1998 if (keeping_dict)
1999 {
2000 keeping_dict = FALSE;
2001 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2002 return FAIL;
2003 }
2004 }
2005 else if (*p == '-' && p[1] == '>')
2006 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002007 char_u *pstart = p;
2008 int alt;
2009 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010
Bram Moolenaarc7349932022-01-16 20:59:39 +00002011 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012 if (generate_ppconst(cctx, ppconst) == FAIL)
2013 return FAIL;
2014 ppconst->pp_is_const = FALSE;
2015
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002016 // Apply the '!', '-' and '+' first:
2017 // -1.0->func() works like (-1.0)->func()
2018 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2019 return FAIL;
2020
2021 p += 2;
2022 *arg = skipwhite(p);
2023 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002024
2025 // Three alternatives handled here:
2026 // 1. "base->name(" only a name, use compile_call()
2027 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2028 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002030 alt = 2;
2031 else
2032 {
2033 // alternative 1 or 3
2034 p = *arg;
2035 if (!eval_isnamec1(*p))
2036 {
2037 semsg(_(e_trailing_characters_str), pstart);
2038 return FAIL;
2039 }
2040 if (ASCII_ISALPHA(*p) && p[1] == ':')
2041 p += 2;
2042 for ( ; eval_isnamec(*p); ++p)
2043 ;
2044 if (*p == '(')
2045 {
2046 // alternative 1
2047 alt = 1;
2048 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2049 return FAIL;
2050 }
2051 else
2052 {
2053 // Must be alternative 3, find the "(". Only works within
2054 // one line.
2055 alt = 3;
2056 paren = vim_strchr(p, '(');
2057 if (paren == NULL)
2058 {
2059 semsg(_(e_missing_parenthesis_str), *arg);
2060 return FAIL;
2061 }
2062 }
2063 }
2064
2065 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066 {
2067 int argcount = 1;
2068 garray_T *stack = &cctx->ctx_type_stack;
2069 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002070 int expr_isn_start = cctx->ctx_instr.ga_len;
2071 int expr_isn_end;
2072 int arg_isn_count;
2073
Bram Moolenaarc7349932022-01-16 20:59:39 +00002074 if (alt == 2)
2075 {
2076 // Funcref call: list->(Refs[2])(arg)
2077 // or lambda: list->((arg) => expr)(arg)
2078 //
2079 // Fist compile the function expression.
2080 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2081 return FAIL;
2082 }
2083 else
2084 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002085 int fail;
2086 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002087 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002088
Bram Moolenaarc7349932022-01-16 20:59:39 +00002089 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002090
2091 // instead of using LOADG for "import.Func" use PUSHFUNC
2092 ++paren_follows_after_expr;
2093
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002094 // do not look in the next line
2095 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002096
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002097 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002098 || *skipwhite(*arg) != NUL;
2099 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002100 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002101 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002102
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002103 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002104 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002105 if (did_emsg == prev_did_emsg)
2106 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002107 return FAIL;
2108 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002109 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002110
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002111 // Compile the arguments.
2112 if (**arg != '(')
2113 {
2114 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002115 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002116 else
2117 semsg(_(e_missing_parenthesis_str), *arg);
2118 return FAIL;
2119 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002120
2121 // Remember the next instruction index, where the instructions
2122 // for arguments are being written.
2123 expr_isn_end = cctx->ctx_instr.ga_len;
2124
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002126 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2127 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002128 return FAIL;
2129
2130 // Move the instructions for the arguments to before the
2131 // instructions of the expression and move the type of the
2132 // expression after the argument types. This is what ISN_PCALL
2133 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002134 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2135 if (arg_isn_count > 0)
2136 {
2137 int expr_isn_count = expr_isn_end - expr_isn_start;
2138 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002139 type_T *decl_type;
2140 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141
2142 if (isn == NULL)
2143 return FAIL;
2144 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2145 + expr_isn_start,
2146 sizeof(isn_T) * expr_isn_count);
2147 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2148 + expr_isn_start,
2149 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2150 sizeof(isn_T) * arg_isn_count);
2151 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2152 + expr_isn_start + arg_isn_count,
2153 isn, sizeof(isn_T) * expr_isn_count);
2154 vim_free(isn);
2155
Bram Moolenaar078a4612022-01-04 15:17:03 +00002156 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2157 type = typep->type_curr;
2158 decl_type = typep->type_decl;
2159 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2160 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2161 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002162 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002163 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2164 typep->type_curr = type;
2165 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 }
2167
Bram Moolenaar078a4612022-01-04 15:17:03 +00002168 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002169 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2170 return FAIL;
2171 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002172
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002173 if (keeping_dict)
2174 {
2175 keeping_dict = FALSE;
2176 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2177 return FAIL;
2178 }
2179 }
2180 else if (**arg == '[')
2181 {
2182 int is_slice = FALSE;
2183
2184 // list index: list[123]
2185 // dict member: dict[key]
2186 // string index: text[123]
2187 // blob index: blob[123]
2188 if (generate_ppconst(cctx, ppconst) == FAIL)
2189 return FAIL;
2190 ppconst->pp_is_const = FALSE;
2191
2192 ++p;
2193 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2194 return FAIL;
2195 if (**arg == ':')
2196 {
2197 // missing first index is equal to zero
2198 generate_PUSHNR(cctx, 0);
2199 }
2200 else
2201 {
2202 if (compile_expr0(arg, cctx) == FAIL)
2203 return FAIL;
2204 if (**arg == ':')
2205 {
2206 semsg(_(e_white_space_required_before_and_after_str_at_str),
2207 ":", *arg);
2208 return FAIL;
2209 }
2210 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2211 return FAIL;
2212 *arg = skipwhite(*arg);
2213 }
2214 if (**arg == ':')
2215 {
2216 is_slice = TRUE;
2217 ++*arg;
2218 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2219 {
2220 semsg(_(e_white_space_required_before_and_after_str_at_str),
2221 ":", *arg);
2222 return FAIL;
2223 }
2224 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2225 return FAIL;
2226 if (**arg == ']')
2227 // missing second index is equal to end of string
2228 generate_PUSHNR(cctx, -1);
2229 else
2230 {
2231 if (compile_expr0(arg, cctx) == FAIL)
2232 return FAIL;
2233 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2234 return FAIL;
2235 *arg = skipwhite(*arg);
2236 }
2237 }
2238
2239 if (**arg != ']')
2240 {
2241 emsg(_(e_missing_closing_square_brace));
2242 return FAIL;
2243 }
2244 *arg = *arg + 1;
2245
2246 if (keeping_dict)
2247 {
2248 keeping_dict = FALSE;
2249 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2250 return FAIL;
2251 }
2252 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2253 return FAIL;
2254 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002255 else if (*p == '.'
2256 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2257 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2258 {
2259 // class member: SomeClass.varname
2260 // class method: SomeClass.SomeMethod()
2261 // class constructor: SomeClass.new()
2262 // object member: someObject.varname, this.varname
2263 // object method: someObject.SomeMethod(), this.SomeMethod()
Bram Moolenaar6aa09372023-01-11 17:59:38 +00002264 *arg = p;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002265 if (compile_class_object_index(cctx, arg, type) == FAIL)
2266 return FAIL;
2267 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002268 else if (*p == '.' && p[1] != '.')
2269 {
2270 // dictionary member: dict.name
2271 if (generate_ppconst(cctx, ppconst) == FAIL)
2272 return FAIL;
2273 ppconst->pp_is_const = FALSE;
2274
2275 *arg = p + 1;
2276 if (IS_WHITE_OR_NUL(**arg))
2277 {
2278 emsg(_(e_missing_name_after_dot));
2279 return FAIL;
2280 }
2281 p = *arg;
2282 if (eval_isdictc(*p))
2283 while (eval_isnamec(*p))
2284 MB_PTR_ADV(p);
2285 if (p == *arg)
2286 {
2287 semsg(_(e_syntax_error_at_str), *arg);
2288 return FAIL;
2289 }
2290 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2291 return FAIL;
2292 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2293 return FAIL;
2294 keeping_dict = TRUE;
2295 *arg = p;
2296 }
2297 else
2298 break;
2299 }
2300
2301 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2302 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002303 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2304 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002305 return FAIL;
2306
2307 return OK;
2308}
2309
2310/*
2311 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2312 * "arg" is advanced until after the expression, skipping white space.
2313 *
2314 * If the value is a constant "ppconst->pp_used" will be non-zero.
2315 * Before instructions are generated, any values in "ppconst" will generated.
2316 *
2317 * This is the compiling equivalent of eval1(), eval2(), etc.
2318 */
2319
2320/*
2321 * number number constant
2322 * 0zFFFFFFFF Blob constant
2323 * "string" string constant
2324 * 'string' literal string constant
2325 * &option-name option value
2326 * @r register contents
2327 * identifier variable value
2328 * function() function call
2329 * $VAR environment variable
2330 * (expression) nested expression
2331 * [expr, expr] List
2332 * {key: val, [key]: val} Dictionary
2333 *
2334 * Also handle:
2335 * ! in front logical NOT
2336 * - in front unary minus
2337 * + in front unary plus (ignored)
2338 * trailing (arg) funcref/partial call
2339 * trailing [] subscript in String or List
2340 * trailing .name entry in Dictionary
2341 * trailing ->name() method call
2342 */
2343 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002344compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002345 char_u **arg,
2346 cctx_T *cctx,
2347 ppconst_T *ppconst)
2348{
2349 char_u *start_leader, *end_leader;
2350 int ret = OK;
2351 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2352 int used_before = ppconst->pp_used;
2353
2354 ppconst->pp_is_const = FALSE;
2355
2356 /*
2357 * Skip '!', '-' and '+' characters. They are handled later.
2358 */
2359 start_leader = *arg;
2360 if (eval_leader(arg, TRUE) == FAIL)
2361 return FAIL;
2362 end_leader = *arg;
2363
2364 rettv->v_type = VAR_UNKNOWN;
2365 switch (**arg)
2366 {
2367 /*
2368 * Number constant.
2369 */
2370 case '0': // also for blob starting with 0z
2371 case '1':
2372 case '2':
2373 case '3':
2374 case '4':
2375 case '5':
2376 case '6':
2377 case '7':
2378 case '8':
2379 case '9':
2380 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2381 return FAIL;
2382 // Apply "-" and "+" just before the number now, right to
2383 // left. Matters especially when "->" follows. Stops at
2384 // '!'.
2385 if (apply_leader(rettv, TRUE,
2386 start_leader, &end_leader) == FAIL)
2387 {
2388 clear_tv(rettv);
2389 return FAIL;
2390 }
2391 break;
2392
2393 /*
2394 * String constant: "string".
2395 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002396 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002397 return FAIL;
2398 break;
2399
2400 /*
2401 * Literal string constant: 'str''ing'.
2402 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002403 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002404 return FAIL;
2405 break;
2406
2407 /*
2408 * Constant Vim variable.
2409 */
2410 case 'v': get_vim_constant(arg, rettv);
2411 ret = NOTDONE;
2412 break;
2413
2414 /*
2415 * "true" constant
2416 */
2417 case 't': if (STRNCMP(*arg, "true", 4) == 0
2418 && !eval_isnamec((*arg)[4]))
2419 {
2420 *arg += 4;
2421 rettv->v_type = VAR_BOOL;
2422 rettv->vval.v_number = VVAL_TRUE;
2423 }
2424 else
2425 ret = NOTDONE;
2426 break;
2427
2428 /*
2429 * "false" constant
2430 */
2431 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2432 && !eval_isnamec((*arg)[5]))
2433 {
2434 *arg += 5;
2435 rettv->v_type = VAR_BOOL;
2436 rettv->vval.v_number = VVAL_FALSE;
2437 }
2438 else
2439 ret = NOTDONE;
2440 break;
2441
2442 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002443 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002445 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002447 char_u *p = *arg + 4;
2448 int len;
2449
2450 for (len = 0; eval_isnamec(p[len]); ++len)
2451 ;
2452 ret = handle_predefined(*arg, len + 4, rettv);
2453 if (ret == FAIL)
2454 ret = NOTDONE;
2455 else
2456 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002457 }
2458 else
2459 ret = NOTDONE;
2460 break;
2461
2462 /*
2463 * List: [expr, expr]
2464 */
2465 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2466 return FAIL;
2467 ret = compile_list(arg, cctx, ppconst);
2468 break;
2469
2470 /*
2471 * Dictionary: {'key': val, 'key': val}
2472 */
2473 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2474 return FAIL;
2475 ret = compile_dict(arg, cctx, ppconst);
2476 break;
2477
2478 /*
2479 * Option value: &name
2480 */
2481 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2482 return FAIL;
2483 ret = compile_get_option(arg, cctx);
2484 break;
2485
2486 /*
2487 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002488 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 */
2490 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2491 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002492 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2493 ret = compile_interp_string(arg, cctx);
2494 else
2495 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 break;
2497
2498 /*
2499 * Register contents: @r.
2500 */
2501 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2502 return FAIL;
2503 ret = compile_get_register(arg, cctx);
2504 break;
2505 /*
2506 * nested expression: (expression).
2507 * lambda: (arg, arg) => expr
2508 * funcref: (arg, arg) => { statement }
2509 */
2510 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2511 ret = compile_lambda(arg, cctx);
2512 if (ret == NOTDONE)
2513 ret = compile_parenthesis(arg, cctx, ppconst);
2514 break;
2515
2516 default: ret = NOTDONE;
2517 break;
2518 }
2519 if (ret == FAIL)
2520 return FAIL;
2521
2522 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2523 {
2524 if (cctx->ctx_skip == SKIP_YES)
2525 clear_tv(rettv);
2526 else
2527 // A constant expression can possibly be handled compile time,
2528 // return the value instead of generating code.
2529 ++ppconst->pp_used;
2530 }
2531 else if (ret == NOTDONE)
2532 {
2533 char_u *p;
2534 int r;
2535
2536 if (!eval_isnamec1(**arg))
2537 {
2538 if (!vim9_bad_comment(*arg))
2539 {
2540 if (ends_excmd(*skipwhite(*arg)))
2541 semsg(_(e_empty_expression_str), *arg);
2542 else
2543 semsg(_(e_name_expected_str), *arg);
2544 }
2545 return FAIL;
2546 }
2547
2548 // "name" or "name()"
2549 p = to_name_end(*arg, TRUE);
2550 if (p - *arg == (size_t)1 && **arg == '_')
2551 {
2552 emsg(_(e_cannot_use_underscore_here));
2553 return FAIL;
2554 }
2555
2556 if (*p == '(')
2557 {
2558 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2559 }
2560 else
2561 {
2562 if (cctx->ctx_skip != SKIP_YES
2563 && generate_ppconst(cctx, ppconst) == FAIL)
2564 return FAIL;
2565 r = compile_load(arg, p, cctx, TRUE, TRUE);
2566 }
2567 if (r == FAIL)
2568 return FAIL;
2569 }
2570
2571 // Handle following "[]", ".member", etc.
2572 // Then deal with prefixed '-', '+' and '!', if not done already.
2573 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2574 ppconst) == FAIL)
2575 return FAIL;
2576 if (ppconst->pp_used > 0)
2577 {
2578 // apply the '!', '-' and '+' before the constant
2579 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2580 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2581 return FAIL;
2582 return OK;
2583 }
2584 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2585 return FAIL;
2586 return OK;
2587}
2588
2589/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002590 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591 */
2592 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002593compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594{
2595 type_T *want_type = NULL;
2596
2597 // Recognize <type>
2598 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2599 {
2600 ++*arg;
2601 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2602 if (want_type == NULL)
2603 return FAIL;
2604
2605 if (**arg != '>')
2606 {
2607 if (*skipwhite(*arg) == '>')
2608 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2609 else
2610 emsg(_(e_missing_gt));
2611 return FAIL;
2612 }
2613 ++*arg;
2614 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2615 return FAIL;
2616 }
2617
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002618 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002619 return FAIL;
2620
2621 if (want_type != NULL)
2622 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002623 type_T *actual;
2624 where_T where = WHERE_INIT;
2625
2626 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002627 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002628 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002629 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002630 if (need_type(actual, want_type, FALSE,
2631 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002632 return FAIL;
2633 }
2634 }
2635
2636 return OK;
2637}
2638
2639/*
2640 * * number multiplication
2641 * / number division
2642 * % number modulo
2643 */
2644 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002645compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002646{
2647 char_u *op;
2648 char_u *next;
2649 int ppconst_used = ppconst->pp_used;
2650
2651 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002652 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002653 return FAIL;
2654
2655 /*
2656 * Repeat computing, until no "*", "/" or "%" is following.
2657 */
2658 for (;;)
2659 {
2660 op = may_peek_next_line(cctx, *arg, &next);
2661 if (*op != '*' && *op != '/' && *op != '%')
2662 break;
2663 if (next != NULL)
2664 {
2665 *arg = next_line_from_context(cctx, TRUE);
2666 op = skipwhite(*arg);
2667 }
2668
2669 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2670 {
2671 error_white_both(op, 1);
2672 return FAIL;
2673 }
2674 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2675 return FAIL;
2676
2677 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002678 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679 return FAIL;
2680
2681 if (ppconst->pp_used == ppconst_used + 2
2682 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2683 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2684 {
2685 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2686 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2687 varnumber_T res = 0;
2688 int failed = FALSE;
2689
2690 // both are numbers: compute the result
2691 switch (*op)
2692 {
2693 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2694 break;
2695 case '/': res = num_divide(tv1->vval.v_number,
2696 tv2->vval.v_number, &failed);
2697 break;
2698 case '%': res = num_modulus(tv1->vval.v_number,
2699 tv2->vval.v_number, &failed);
2700 break;
2701 }
2702 if (failed)
2703 return FAIL;
2704 tv1->vval.v_number = res;
2705 --ppconst->pp_used;
2706 }
2707 else
2708 {
2709 generate_ppconst(cctx, ppconst);
2710 generate_two_op(cctx, op);
2711 }
2712 }
2713
2714 return OK;
2715}
2716
2717/*
2718 * + number addition or list/blobl concatenation
2719 * - number subtraction
2720 * .. string concatenation
2721 */
2722 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002723compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002724{
2725 char_u *op;
2726 char_u *next;
2727 int oplen;
2728 int ppconst_used = ppconst->pp_used;
2729
2730 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002731 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002732 return FAIL;
2733
2734 /*
2735 * Repeat computing, until no "+", "-" or ".." is following.
2736 */
2737 for (;;)
2738 {
2739 op = may_peek_next_line(cctx, *arg, &next);
2740 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2741 break;
2742 if (op[0] == op[1] && *op != '.' && next)
2743 // Finding "++" or "--" on the next line is a separate command.
2744 // But ".." is concatenation.
2745 break;
2746 oplen = (*op == '.' ? 2 : 1);
2747 if (next != NULL)
2748 {
2749 *arg = next_line_from_context(cctx, TRUE);
2750 op = skipwhite(*arg);
2751 }
2752
2753 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2754 {
2755 error_white_both(op, oplen);
2756 return FAIL;
2757 }
2758
2759 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2760 return FAIL;
2761
2762 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002763 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002764 return FAIL;
2765
2766 if (ppconst->pp_used == ppconst_used + 2
2767 && (*op == '.'
2768 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2769 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2770 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2771 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2772 {
2773 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2774 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2775
2776 // concat/subtract/add constant numbers
2777 if (*op == '+')
2778 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2779 else if (*op == '-')
2780 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2781 else
2782 {
2783 // concatenate constant strings
2784 char_u *s1 = tv1->vval.v_string;
2785 char_u *s2 = tv2->vval.v_string;
2786 size_t len1 = STRLEN(s1);
2787
2788 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2789 if (tv1->vval.v_string == NULL)
2790 {
2791 clear_ppconst(ppconst);
2792 return FAIL;
2793 }
2794 mch_memmove(tv1->vval.v_string, s1, len1);
2795 STRCPY(tv1->vval.v_string + len1, s2);
2796 vim_free(s1);
2797 vim_free(s2);
2798 }
2799 --ppconst->pp_used;
2800 }
2801 else
2802 {
2803 generate_ppconst(cctx, ppconst);
2804 ppconst->pp_is_const = FALSE;
2805 if (*op == '.')
2806 {
2807 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2808 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2809 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002810 if (generate_CONCAT(cctx, 2) == FAIL)
2811 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002812 }
2813 else
2814 generate_two_op(cctx, op);
2815 }
2816 }
2817
2818 return OK;
2819}
2820
2821/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002822 * expr6a >> expr6b
2823 * expr6a << expr6b
2824 *
2825 * Produces instructions:
2826 * OPNR bitwise left or right shift
2827 */
2828 static int
2829compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2830{
2831 exprtype_T type = EXPR_UNKNOWN;
2832 char_u *p;
2833 char_u *next;
2834 int len = 2;
2835 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002836 isn_T *isn;
2837
2838 // get the first variable
2839 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2840 return FAIL;
2841
2842 /*
2843 * Repeat computing, until no "+", "-" or ".." is following.
2844 */
2845 for (;;)
2846 {
2847 type = EXPR_UNKNOWN;
2848
2849 p = may_peek_next_line(cctx, *arg, &next);
2850 if (p[0] == '<' && p[1] == '<')
2851 type = EXPR_LSHIFT;
2852 else if (p[0] == '>' && p[1] == '>')
2853 type = EXPR_RSHIFT;
2854
2855 if (type == EXPR_UNKNOWN)
2856 return OK;
2857
2858 // Handle a bitwise left or right shift operator
2859 if (ppconst->pp_used == ppconst_used + 1)
2860 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002861 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002862 {
2863 // left operand should be a number
2864 emsg(_(e_bitshift_ops_must_be_number));
2865 return FAIL;
2866 }
2867 }
2868 else
2869 {
2870 type_T *t = get_type_on_stack(cctx, 0);
2871
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002872 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002873 {
2874 emsg(_(e_bitshift_ops_must_be_number));
2875 return FAIL;
2876 }
2877 }
2878
2879 if (next != NULL)
2880 {
2881 *arg = next_line_from_context(cctx, TRUE);
2882 p = skipwhite(*arg);
2883 }
2884
2885 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2886 {
2887 error_white_both(p, len);
2888 return FAIL;
2889 }
2890
2891 // get the second variable
2892 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2893 return FAIL;
2894
2895 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2896 return FAIL;
2897
2898 if (ppconst->pp_used == ppconst_used + 2)
2899 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002900 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2901 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2902
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002903 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002904 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2905 {
2906 // right operand should be a positive number
2907 if (tv2->v_type != VAR_NUMBER)
2908 emsg(_(e_bitshift_ops_must_be_number));
2909 else
dundargocc57b5bc2022-11-02 13:30:51 +00002910 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002911 return FAIL;
2912 }
2913
2914 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2915 tv1->vval.v_number = 0;
2916 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002917 tv1->vval.v_number =
2918 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002919 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002920 tv1->vval.v_number =
2921 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002922 clear_tv(tv2);
2923 --ppconst->pp_used;
2924 }
2925 else
2926 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002927 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2928 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002929 {
2930 emsg(_(e_bitshift_ops_must_be_number));
2931 return FAIL;
2932 }
2933
2934 generate_ppconst(cctx, ppconst);
2935
2936 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2937 if (isn == NULL)
2938 return FAIL;
2939
2940 if (isn != NULL)
2941 isn->isn_arg.op.op_type = type;
2942 }
2943 }
2944
2945 return OK;
2946}
2947
2948/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002949 * expr5a == expr5b
2950 * expr5a =~ expr5b
2951 * expr5a != expr5b
2952 * expr5a !~ expr5b
2953 * expr5a > expr5b
2954 * expr5a >= expr5b
2955 * expr5a < expr5b
2956 * expr5a <= expr5b
2957 * expr5a is expr5b
2958 * expr5a isnot expr5b
2959 *
2960 * Produces instructions:
2961 * EVAL expr5a Push result of "expr5a"
2962 * EVAL expr5b Push result of "expr5b"
2963 * COMPARE one of the compare instructions
2964 */
2965 static int
2966compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2967{
2968 exprtype_T type = EXPR_UNKNOWN;
2969 char_u *p;
2970 char_u *next;
2971 int len = 2;
2972 int type_is = FALSE;
2973 int ppconst_used = ppconst->pp_used;
2974
2975 // get the first variable
2976 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2977 return FAIL;
2978
2979 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002980
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002981 type = get_compare_type(p, &len, &type_is);
2982
2983 /*
2984 * If there is a comparative operator, use it.
2985 */
2986 if (type != EXPR_UNKNOWN)
2987 {
2988 int ic = FALSE; // Default: do not ignore case
2989
2990 if (next != NULL)
2991 {
2992 *arg = next_line_from_context(cctx, TRUE);
2993 p = skipwhite(*arg);
2994 }
2995 if (type_is && (p[len] == '?' || p[len] == '#'))
2996 {
2997 semsg(_(e_invalid_expression_str), *arg);
2998 return FAIL;
2999 }
3000 // extra question mark appended: ignore case
3001 if (p[len] == '?')
3002 {
3003 ic = TRUE;
3004 ++len;
3005 }
3006 // extra '#' appended: match case (ignored)
3007 else if (p[len] == '#')
3008 ++len;
3009 // nothing appended: match case
3010
3011 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3012 {
3013 error_white_both(p, len);
3014 return FAIL;
3015 }
3016
3017 // get the second variable
3018 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3019 return FAIL;
3020
3021 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3022 return FAIL;
3023
3024 if (ppconst->pp_used == ppconst_used + 2)
3025 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003026 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003027 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3028 int ret;
3029
3030 // Both sides are a constant, compute the result now.
3031 // First check for a valid combination of types, this is more
3032 // strict than typval_compare().
3033 if (check_compare_types(type, tv1, tv2) == FAIL)
3034 ret = FAIL;
3035 else
3036 {
3037 ret = typval_compare(tv1, tv2, type, ic);
3038 tv1->v_type = VAR_BOOL;
3039 tv1->vval.v_number = tv1->vval.v_number
3040 ? VVAL_TRUE : VVAL_FALSE;
3041 clear_tv(tv2);
3042 --ppconst->pp_used;
3043 }
3044 return ret;
3045 }
3046
3047 generate_ppconst(cctx, ppconst);
3048 return generate_COMPARE(cctx, type, ic);
3049 }
3050
3051 return OK;
3052}
3053
3054static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3055
3056/*
3057 * Compile || or &&.
3058 */
3059 static int
3060compile_and_or(
3061 char_u **arg,
3062 cctx_T *cctx,
3063 char *op,
3064 ppconst_T *ppconst,
3065 int ppconst_used UNUSED)
3066{
3067 char_u *next;
3068 char_u *p = may_peek_next_line(cctx, *arg, &next);
3069 int opchar = *op;
3070
3071 if (p[0] == opchar && p[1] == opchar)
3072 {
3073 garray_T *instr = &cctx->ctx_instr;
3074 garray_T end_ga;
3075 int save_skip = cctx->ctx_skip;
3076
3077 /*
3078 * Repeat until there is no following "||" or "&&"
3079 */
3080 ga_init2(&end_ga, sizeof(int), 10);
3081 while (p[0] == opchar && p[1] == opchar)
3082 {
3083 long start_lnum = SOURCING_LNUM;
3084 long save_sourcing_lnum;
3085 int start_ctx_lnum = cctx->ctx_lnum;
3086 int save_lnum;
3087 int const_used;
3088 int status;
3089 jumpwhen_T jump_when = opchar == '|'
3090 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3091
3092 if (next != NULL)
3093 {
3094 *arg = next_line_from_context(cctx, TRUE);
3095 p = skipwhite(*arg);
3096 }
3097
3098 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3099 {
3100 semsg(_(e_white_space_required_before_and_after_str_at_str),
3101 op, p);
3102 ga_clear(&end_ga);
3103 return FAIL;
3104 }
3105
3106 save_sourcing_lnum = SOURCING_LNUM;
3107 SOURCING_LNUM = start_lnum;
3108 save_lnum = cctx->ctx_lnum;
3109 cctx->ctx_lnum = start_ctx_lnum;
3110
3111 status = check_ppconst_bool(ppconst);
3112 if (status != FAIL)
3113 {
3114 // Use the last ppconst if possible.
3115 if (ppconst->pp_used > 0)
3116 {
3117 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3118 int is_true = tv2bool(tv);
3119
3120 if ((is_true && opchar == '|')
3121 || (!is_true && opchar == '&'))
3122 {
3123 // For "false && expr" and "true || expr" the "expr"
3124 // does not need to be evaluated.
3125 cctx->ctx_skip = SKIP_YES;
3126 clear_tv(tv);
3127 tv->v_type = VAR_BOOL;
3128 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3129 }
3130 else
3131 {
3132 // For "true && expr" and "false || expr" only "expr"
3133 // needs to be evaluated.
3134 --ppconst->pp_used;
3135 jump_when = JUMP_NEVER;
3136 }
3137 }
3138 else
3139 {
3140 // Every part must evaluate to a bool.
3141 status = bool_on_stack(cctx);
3142 }
3143 }
3144 if (status != FAIL)
3145 status = ga_grow(&end_ga, 1);
3146 cctx->ctx_lnum = save_lnum;
3147 if (status == FAIL)
3148 {
3149 ga_clear(&end_ga);
3150 return FAIL;
3151 }
3152
3153 if (jump_when != JUMP_NEVER)
3154 {
3155 if (cctx->ctx_skip != SKIP_YES)
3156 {
3157 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3158 ++end_ga.ga_len;
3159 }
3160 generate_JUMP(cctx, jump_when, 0);
3161 }
3162
3163 // eval the next expression
3164 SOURCING_LNUM = save_sourcing_lnum;
3165 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3166 {
3167 ga_clear(&end_ga);
3168 return FAIL;
3169 }
3170
3171 const_used = ppconst->pp_used;
3172 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3173 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3174 {
3175 ga_clear(&end_ga);
3176 return FAIL;
3177 }
3178
3179 // "0 || 1" results in true, "1 && 0" results in false.
3180 if (ppconst->pp_used == const_used + 1)
3181 {
3182 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3183
3184 if (tv->v_type == VAR_NUMBER
3185 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3186 {
3187 tv->vval.v_number = tv->vval.v_number == 1
3188 ? VVAL_TRUE : VVAL_FALSE;
3189 tv->v_type = VAR_BOOL;
3190 }
3191 }
3192
3193 p = may_peek_next_line(cctx, *arg, &next);
3194 }
3195
3196 if (check_ppconst_bool(ppconst) == FAIL)
3197 {
3198 ga_clear(&end_ga);
3199 return FAIL;
3200 }
3201
3202 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3203 // Every part must evaluate to a bool.
3204 if (bool_on_stack(cctx) == FAIL)
3205 {
3206 ga_clear(&end_ga);
3207 return FAIL;
3208 }
3209
3210 if (end_ga.ga_len > 0)
3211 {
3212 // Fill in the end label in all jumps.
3213 generate_ppconst(cctx, ppconst);
3214 while (end_ga.ga_len > 0)
3215 {
3216 isn_T *isn;
3217
3218 --end_ga.ga_len;
3219 isn = ((isn_T *)instr->ga_data)
3220 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3221 isn->isn_arg.jump.jump_where = instr->ga_len;
3222 }
3223 }
3224 ga_clear(&end_ga);
3225
3226 cctx->ctx_skip = save_skip;
3227 }
3228
3229 return OK;
3230}
3231
3232/*
3233 * expr4a && expr4a && expr4a logical AND
3234 *
3235 * Produces instructions:
3236 * EVAL expr4a Push result of "expr4a"
3237 * COND2BOOL convert to bool if needed
3238 * JUMP_IF_COND_FALSE end
3239 * EVAL expr4b Push result of "expr4b"
3240 * JUMP_IF_COND_FALSE end
3241 * EVAL expr4c Push result of "expr4c"
3242 * end:
3243 */
3244 static int
3245compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3246{
3247 int ppconst_used = ppconst->pp_used;
3248
3249 // get the first variable
3250 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3251 return FAIL;
3252
3253 // || and && work almost the same
3254 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3255}
3256
3257/*
3258 * expr3a || expr3b || expr3c logical OR
3259 *
3260 * Produces instructions:
3261 * EVAL expr3a Push result of "expr3a"
3262 * COND2BOOL convert to bool if needed
3263 * JUMP_IF_COND_TRUE end
3264 * EVAL expr3b Push result of "expr3b"
3265 * JUMP_IF_COND_TRUE end
3266 * EVAL expr3c Push result of "expr3c"
3267 * end:
3268 */
3269 static int
3270compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3271{
3272 int ppconst_used = ppconst->pp_used;
3273
3274 // eval the first expression
3275 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3276 return FAIL;
3277
3278 // || and && work almost the same
3279 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3280}
3281
3282/*
3283 * Toplevel expression: expr2 ? expr1a : expr1b
3284 * Produces instructions:
3285 * EVAL expr2 Push result of "expr2"
3286 * JUMP_IF_FALSE alt jump if false
3287 * EVAL expr1a
3288 * JUMP_ALWAYS end
3289 * alt: EVAL expr1b
3290 * end:
3291 *
3292 * Toplevel expression: expr2 ?? expr1
3293 * Produces instructions:
3294 * EVAL expr2 Push result of "expr2"
3295 * JUMP_AND_KEEP_IF_TRUE end jump if true
3296 * EVAL expr1
3297 * end:
3298 */
3299 int
3300compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3301{
3302 char_u *p;
3303 int ppconst_used = ppconst->pp_used;
3304 char_u *next;
3305
3306 // Ignore all kinds of errors when not producing code.
3307 if (cctx->ctx_skip == SKIP_YES)
3308 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003309 int prev_did_emsg = did_emsg;
3310
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003311 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003312 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003313 }
3314
3315 // Evaluate the first expression.
3316 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3317 return FAIL;
3318
3319 p = may_peek_next_line(cctx, *arg, &next);
3320 if (*p == '?')
3321 {
3322 int op_falsy = p[1] == '?';
3323 garray_T *instr = &cctx->ctx_instr;
3324 garray_T *stack = &cctx->ctx_type_stack;
3325 int alt_idx = instr->ga_len;
3326 int end_idx = 0;
3327 isn_T *isn;
3328 type_T *type1 = NULL;
3329 int has_const_expr = FALSE;
3330 int const_value = FALSE;
3331 int save_skip = cctx->ctx_skip;
3332
3333 if (next != NULL)
3334 {
3335 *arg = next_line_from_context(cctx, TRUE);
3336 p = skipwhite(*arg);
3337 }
3338
3339 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3340 {
3341 semsg(_(e_white_space_required_before_and_after_str_at_str),
3342 op_falsy ? "??" : "?", p);
3343 return FAIL;
3344 }
3345
3346 if (ppconst->pp_used == ppconst_used + 1)
3347 {
3348 // the condition is a constant, we know whether the ? or the :
3349 // expression is to be evaluated.
3350 has_const_expr = TRUE;
3351 if (op_falsy)
3352 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3353 else
3354 {
3355 int error = FALSE;
3356
3357 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3358 &error);
3359 if (error)
3360 return FAIL;
3361 }
3362 cctx->ctx_skip = save_skip == SKIP_YES ||
3363 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3364
3365 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3366 // "left ?? right" and "left" is truthy: produce "left"
3367 generate_ppconst(cctx, ppconst);
3368 else
3369 {
3370 clear_tv(&ppconst->pp_tv[ppconst_used]);
3371 --ppconst->pp_used;
3372 }
3373 }
3374 else
3375 {
3376 generate_ppconst(cctx, ppconst);
3377 if (op_falsy)
3378 end_idx = instr->ga_len;
3379 generate_JUMP(cctx, op_falsy
3380 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3381 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003382 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003383 }
3384
3385 // evaluate the second expression; any type is accepted
3386 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3387 return FAIL;
3388 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3389 return FAIL;
3390
3391 if (!has_const_expr)
3392 {
3393 generate_ppconst(cctx, ppconst);
3394
3395 if (!op_falsy)
3396 {
3397 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003398 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003399 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003400
3401 end_idx = instr->ga_len;
3402 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3403
3404 // jump here from JUMP_IF_FALSE
3405 isn = ((isn_T *)instr->ga_data) + alt_idx;
3406 isn->isn_arg.jump.jump_where = instr->ga_len;
3407 }
3408 }
3409
3410 if (!op_falsy)
3411 {
3412 // Check for the ":".
3413 p = may_peek_next_line(cctx, *arg, &next);
3414 if (*p != ':')
3415 {
3416 emsg(_(e_missing_colon_after_questionmark));
3417 return FAIL;
3418 }
3419 if (next != NULL)
3420 {
3421 *arg = next_line_from_context(cctx, TRUE);
3422 p = skipwhite(*arg);
3423 }
3424
3425 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3426 {
3427 semsg(_(e_white_space_required_before_and_after_str_at_str),
3428 ":", p);
3429 return FAIL;
3430 }
3431
3432 // evaluate the third expression
3433 if (has_const_expr)
3434 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3435 ? SKIP_YES : SKIP_NOT;
3436 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3437 return FAIL;
3438 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3439 return FAIL;
3440 }
3441
3442 if (!has_const_expr)
3443 {
3444 type_T **typep;
3445
3446 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003447 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003448
3449 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003450 typep = &((((type2_T *)stack->ga_data)
3451 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003452 common_type(type1, *typep, typep, cctx->ctx_type_list);
3453
3454 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3455 isn = ((isn_T *)instr->ga_data) + end_idx;
3456 isn->isn_arg.jump.jump_where = instr->ga_len;
3457 }
3458
3459 cctx->ctx_skip = save_skip;
3460 }
3461 return OK;
3462}
3463
3464/*
3465 * Toplevel expression.
3466 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3467 * Returns OK or FAIL.
3468 */
3469 int
3470compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3471{
3472 ppconst_T ppconst;
3473
3474 CLEAR_FIELD(ppconst);
3475 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3476 {
3477 clear_ppconst(&ppconst);
3478 return FAIL;
3479 }
3480 if (is_const != NULL)
3481 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3482 if (generate_ppconst(cctx, &ppconst) == FAIL)
3483 return FAIL;
3484 return OK;
3485}
3486
3487/*
3488 * Toplevel expression.
3489 */
3490 int
3491compile_expr0(char_u **arg, cctx_T *cctx)
3492{
3493 return compile_expr0_ext(arg, cctx, NULL);
3494}
3495
3496
3497#endif // defined(FEAT_EVAL)