blob: 68de7360129320ace5500061b107c20bcda3bb45 [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/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +010076 * Compile getting a member from a tuple. Stack has the indexable value and
77 * the index or the two indexes of a slice.
78 */
79 static int
80compile_tuple_member(
81 type2_T *typep,
82 int is_slice,
83 cctx_T *cctx)
84{
85 if (is_slice)
86 {
87 if (generate_instr_drop(cctx, ISN_TUPLESLICE, 2) == FAIL)
88 return FAIL;
89 // a copy is made so the member type is no longer declared
90 if (typep->type_decl->tt_type == VAR_TUPLE)
91 typep->type_decl = &t_tuple_any;
92
93 // a copy is made, the composite is no longer "const"
94 if (typep->type_curr->tt_flags & TTFLAG_CONST)
95 {
96 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
97
98 if (type != typep->type_curr) // did get a copy
99 {
100 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
101 typep->type_curr = type;
102 }
103 }
104 }
105 else
106 {
107 if (typep->type_curr->tt_type == VAR_TUPLE)
108 {
109 if (typep->type_curr->tt_argcount == 1)
110 {
111 if (typep->type_curr->tt_flags & TTFLAG_VARARGS)
112 typep->type_curr
113 = typep->type_curr->tt_args[0]->tt_member;
114 else
115 typep->type_curr = typep->type_curr->tt_args[0];
116 }
117 else
118 typep->type_curr = &t_any;
119 if (typep->type_decl->tt_type == VAR_TUPLE)
120 {
121 if (typep->type_decl->tt_argcount == 1)
122 {
123 if (typep->type_decl->tt_flags & TTFLAG_VARARGS)
124 typep->type_decl
125 = typep->type_decl->tt_args[0]->tt_member;
126 else
127 typep->type_decl = typep->type_decl->tt_args[0];
128 }
129 else
130 typep->type_curr = &t_any;
131 }
132 else
133 typep->type_decl = typep->type_curr;
134 }
135 if (generate_instr_drop(cctx, ISN_TUPLEINDEX, 1) == FAIL)
136 return FAIL;
137 }
138
139 return OK;
140}
141
142/*
143 * Compile getting a member from a list/tuple/dict/string/blob. Stack has the
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 * indexable value and the index or the two indexes of a slice.
145 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
146 */
147 int
148compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
149{
Bram Moolenaar078a4612022-01-04 15:17:03 +0000150 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000151 garray_T *stack = &cctx->ctx_type_stack;
152 vartype_T vartype;
153 type_T *idxtype;
154
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100155 // We can index a list, tuple, dict and blob. If we don't know the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000156 // we can use the index value type. If we still don't know use an "ANY"
157 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000158 // TODO: what about the decl type?
159 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
160 vartype = typep->type_curr->tt_type;
161 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000162 // If the index is a string, the variable must be a Dict.
Bram Moolenaar4913d422022-10-17 13:13:32 +0100163 if ((typep->type_curr->tt_type == VAR_ANY
164 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000165 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 vartype = VAR_DICT;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100167 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB
168 || vartype == VAR_TUPLE)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000169 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000170 if (need_type(idxtype, &t_number, FALSE,
171 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000172 return FAIL;
173 if (is_slice)
174 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000175 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000176 if (need_type(idxtype, &t_number, FALSE,
177 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000178 return FAIL;
179 }
180 }
181
182 if (vartype == VAR_DICT)
183 {
184 if (is_slice)
185 {
186 emsg(_(e_cannot_slice_dictionary));
187 return FAIL;
188 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000189 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000190 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000191 typep->type_curr = typep->type_curr->tt_member;
192 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000193 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000194 typep->type_curr = &t_any;
195 if (typep->type_decl->tt_type == VAR_DICT)
196 {
197 typep->type_decl = typep->type_decl->tt_member;
198 if (typep->type_decl == &t_unknown)
199 // empty dict was used
200 typep->type_decl = &t_any;
201 }
202 else
203 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000204 }
205 else
206 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000207 if (need_type(typep->type_curr, &t_dict_any, FALSE,
208 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000209 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000210 typep->type_curr = &t_any;
211 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000212 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200213 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL
Bram Moolenaard0132f42022-05-12 11:05:40 +0100214 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000215 return FAIL;
216 if (keeping_dict != NULL)
217 *keeping_dict = TRUE;
218 }
219 else if (vartype == VAR_STRING)
220 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000221 typep->type_curr = &t_string;
222 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000223 if ((is_slice
224 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
225 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
226 return FAIL;
227 }
228 else if (vartype == VAR_BLOB)
229 {
230 if (is_slice)
231 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000232 typep->type_curr = &t_blob;
233 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000234 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
235 return FAIL;
236 }
237 else
238 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000239 typep->type_curr = &t_number;
240 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000241 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
242 return FAIL;
243 }
244 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100245 else if (vartype == VAR_TUPLE)
246 {
247 if (compile_tuple_member(typep, is_slice, cctx) == FAIL)
248 return FAIL;
249 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100250 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
251 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000252 {
253 if (is_slice)
254 {
255 if (generate_instr_drop(cctx,
256 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
257 2) == FAIL)
258 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000259 // a copy is made so the member type is no longer declared
260 if (typep->type_decl->tt_type == VAR_LIST)
261 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000262
263 // a copy is made, the composite is no longer "const"
264 if (typep->type_curr->tt_flags & TTFLAG_CONST)
265 {
266 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
267
268 if (type != typep->type_curr) // did get a copy
269 {
270 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
271 typep->type_curr = type;
272 }
273 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000274 }
275 else
276 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000277 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000278 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000279 typep->type_curr = typep->type_curr->tt_member;
280 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000281 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000282 typep->type_curr = &t_any;
283 if (typep->type_decl->tt_type == VAR_LIST)
284 {
285 typep->type_decl = typep->type_decl->tt_member;
286 if (typep->type_decl == &t_unknown)
287 // empty list was used
288 typep->type_decl = &t_any;
289 }
290 else
291 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000292 }
293 if (generate_instr_drop(cctx,
294 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
295 == FAIL)
296 return FAIL;
297 }
298 }
299 else
300 {
301 switch (vartype)
302 {
303 case VAR_FUNC:
304 case VAR_PARTIAL:
305 emsg(_(e_cannot_index_a_funcref));
306 break;
307 case VAR_BOOL:
308 case VAR_SPECIAL:
309 case VAR_JOB:
310 case VAR_CHANNEL:
311 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000312 case VAR_CLASS:
313 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200314 case VAR_TYPEALIAS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000315 case VAR_UNKNOWN:
316 case VAR_ANY:
317 case VAR_VOID:
318 emsg(_(e_cannot_index_special_variable));
319 break;
320 default:
321 emsg(_(e_string_list_dict_or_blob_required));
322 }
323 return FAIL;
324 }
325 return OK;
326}
327
328/*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200329 * Returns TRUE if the current function is inside the class "cl" or one of the
330 * parent classes.
331 */
332 static int
333inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
334{
335 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
336 {
337 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
338 {
339 class_T *clp = cctx->ctx_ufunc->uf_class;
340 while (clp != NULL)
341 {
342 if (clp == cl)
343 return TRUE;
344 clp = clp->class_extends;
345 }
346 }
347 }
348
349 return FALSE;
350}
351
352/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000353 * Compile ".member" coming after an object or class.
354 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000355 static int
356compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
357{
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200358 int m_idx;
359
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000360 if (VIM_ISWHITE((*arg)[1]))
361 {
362 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
363 return FAIL;
364 }
365
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000366 class_T *cl = type->tt_class;
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100367 int is_super = ((type->tt_flags & TTFLAG_SUPER) == TTFLAG_SUPER);
Bram Moolenaar58b40092023-01-11 15:59:05 +0000368 if (type == &t_super)
369 {
370 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000371 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200372 emsg(_(e_using_super_not_in_class_method));
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000373 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000374 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000375 is_super = TRUE;
376 cl = cctx->ctx_ufunc->uf_class;
377 // Remove &t_super from the stack.
378 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000379 }
380 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000381 {
382 garray_T *instr = &cctx->ctx_instr;
383 if (instr->ga_len > 0)
384 {
385 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
386 if (isn->isn_type == ISN_LOADSCRIPT)
387 {
388 // The class was recognized as a script item. We only need
389 // to know what class it is, drop the instruction.
390 --instr->ga_len;
391 vim_free(isn->isn_arg.script.scriptref);
392 }
393 }
394 }
395
Ernie Raelf77a7f72023-03-03 15:05:30 +0000396 if (cl == NULL)
397 {
398 // TODO: this should not give an error but be handled at runtime
399 emsg(_(e_incomplete_type));
400 return FAIL;
401 }
402
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000403 ++*arg;
404 char_u *name = *arg;
405 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
406 if (name_end == name)
407 return FAIL;
408 size_t len = name_end - name;
409
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000410 if (*name_end == '(')
411 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000412 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000413 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000414 ufunc_T **functions;
415
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000416 if (type->tt_type == VAR_CLASS)
417 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000418 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000419 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000420 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000421 }
422 else
423 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000424 // type->tt_type == VAR_OBJECT: method call
Ernie Rael58c95792024-08-13 23:27:22 +0200425 // When compiling Func and doing "super.SomeFunc()", must be in the
426 // class context that defines Func.
427 if (is_super)
428 cl = cctx->ctx_ufunc->uf_defclass;
429
Bram Moolenaar574950d2023-01-03 19:08:50 +0000430 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000431 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000432 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000433 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000434
435 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000436 int fi;
437 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000438 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000439 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000440 // Use a separate pointer to avoid that ASAN complains about
441 // uf_name[] only being 4 characters.
442 char_u *ufname = (char_u *)fp->uf_name;
443 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
444 {
445 ufunc = fp;
446 break;
447 }
448 }
Ernie Raelbce60c42025-01-19 10:03:00 +0100449
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200450 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000451 if (ufunc == NULL)
452 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200453 // could be a funcref in a member variable
454 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
455 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
456 {
457 method_not_found_msg(cl, type->tt_type, name, len);
458 return FAIL;
459 }
460 if (type->tt_type == VAR_CLASS)
461 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200462 // Remove the class type from the stack
463 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200464 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
465 return FAIL;
466 }
467 else
468 {
Yegappan Lakshmananc10342d2025-01-11 09:39:01 +0100469 int status;
470
471 if (IS_INTERFACE(cl))
472 status = generate_GET_ITF_MEMBER(cctx, cl, m_idx,
473 ocm->ocm_type);
474 else
475 status = generate_GET_OBJ_MEMBER(cctx, m_idx,
476 ocm->ocm_type);
477 if (status == FAIL)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200478 return FAIL;
479 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000480 }
481
Yegappan Lakshmanancb848b62025-01-20 21:38:09 +0100482 if (is_super && ufunc != NULL && IS_ABSTRACT_METHOD(ufunc))
Ernie Raelbce60c42025-01-19 10:03:00 +0100483 {
484 // Trying to invoke an abstract method in a super class is not
485 // allowed.
486 semsg(_(e_abstract_method_str_direct), ufunc->uf_name,
487 ufunc->uf_defclass->class_name);
488 return FAIL;
489 }
490
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200491 // A private object method can be used only inside the class where it
492 // is defined or in one of the child classes.
493 // A private class method can be used only in the class where it is
494 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200495 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200496 ((type->tt_type == VAR_OBJECT
497 && !inside_class_hierarchy(cctx, cl))
498 || (type->tt_type == VAR_CLASS
499 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200500 {
Ernie Rael03042a22023-11-11 08:53:32 +0100501 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200502 return FAIL;
503 }
504
Bram Moolenaar574950d2023-01-03 19:08:50 +0000505 // Compile the arguments and call the class function or object method.
506 // The object method will know that the object is on the stack, just
507 // before the arguments.
508 *arg = skipwhite(name_end + 1);
509 int argcount = 0;
510 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
511 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000512
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200513 if (ocm != NULL)
514 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000515 if (type->tt_type == VAR_OBJECT
516 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
Ernie Rael58c95792024-08-13 23:27:22 +0200517 return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super);
518 return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000519 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000520
521 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000522 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200523 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200524 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000525 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200526 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000527 {
Ernie Rael03042a22023-11-11 08:53:32 +0100528 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200529 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200530 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200531 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200532
533 *arg = name_end;
534 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200535 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
536 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200537 }
538
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200539 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200540 m_idx = object_method_idx(cl, name, len);
541 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000542 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200543 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100544 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200545 if (*name == '_' && !inside_class(cctx, cl))
546 {
Ernie Rael03042a22023-11-11 08:53:32 +0100547 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200548 return FAIL;
549 }
550 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200551 // Remove the object type from the stack
552 --cctx->ctx_type_stack.ga_len;
553 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000554 }
555
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200556 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000557 }
558 else
559 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000560 // load class member
561 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200562 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
563 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000564 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200565 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200566 // A private class variable can be accessed only in the class where
567 // it is defined.
568 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200569 {
Ernie Rael03042a22023-11-11 08:53:32 +0100570 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200571 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200572 return FAIL;
573 }
574
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000575 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200576 // Remove the class type from the stack
577 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000578 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
579 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200580
581 // Could be a class method reference: "class.Func".
582 m_idx = class_method_idx(cl, name, len);
583 if (m_idx >= 0)
584 {
585 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100586 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200587 if (*name == '_' && !inside_class(cctx, cl))
588 {
Ernie Rael03042a22023-11-11 08:53:32 +0100589 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200590 return FAIL;
591 }
592 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200593 // Remove the class type from the stack
594 --cctx->ctx_type_stack.ga_len;
595 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200596 }
597
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200598 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000599 }
600
601 return FAIL;
602}
603
604/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605 * Generate an instruction to load script-local variable "name", without the
606 * leading "s:".
607 * Also finds imported variables.
608 */
609 int
610compile_load_scriptvar(
611 cctx_T *cctx,
612 char_u *name, // variable NUL terminated
613 char_u *start, // start of variable
Hirohito Higashi6c012832025-02-25 20:29:50 +0100614 char_u **end, // end of variable, may be NULL
615 imported_T *import) // found imported item, can be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000616{
617 scriptitem_T *si;
618 int idx;
Hirohito Higashi6c012832025-02-25 20:29:50 +0100619 imported_T *imp;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620
621 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
622 return FAIL;
623 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000624 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000625 if (idx >= 0)
626 {
627 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
628
629 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
630 current_sctx.sc_sid, idx, sv->sv_type);
631 return OK;
632 }
633
Hirohito Higashi6c012832025-02-25 20:29:50 +0100634 if (end == NULL)
635 imp = NULL;
636 else if (import == NULL)
637 imp = find_imported(name, 0, FALSE);
638 else
639 imp = import;
640
641 if (imp != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000643 char_u *p = skipwhite(*end);
644 char_u *exp_name;
645 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100646 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000647 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000648 int done = FALSE;
649 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000650
Hirohito Higashi6c012832025-02-25 20:29:50 +0100651 check_script_symlink(imp->imp_sid);
652 import_check_sourced_sid(&imp->imp_sid);
Ernie Rael9a901792024-04-16 22:11:56 +0200653
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000654 // Need to lookup the member.
655 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000656 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000657 semsg(_(e_expected_dot_after_name_str), start);
658 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000660 ++p;
661 if (VIM_ISWHITE(*p))
662 {
663 emsg(_(e_no_white_space_allowed_after_dot));
664 return FAIL;
665 }
666
667 // isolate one name
668 exp_name = p;
669 while (eval_isnamec(*p))
670 ++p;
671 cc = *p;
672 *p = NUL;
673
Hirohito Higashi6c012832025-02-25 20:29:50 +0100674 si = SCRIPT_ITEM(imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100675 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
676 // "import autoload './dir/script.vim'" or
677 // "import autoload './autoload/script.vim'" - load script first
Hirohito Higashi6c012832025-02-25 20:29:50 +0100678 res = generate_SOURCE(cctx, imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100679
680 if (res == OK)
681 {
682 if (si->sn_autoload_prefix != NULL
683 && si->sn_state == SN_STATE_NOT_LOADED)
684 {
685 char_u *auto_name =
686 concat_str(si->sn_autoload_prefix, exp_name);
687
688 // autoload script must be loaded later, access by the autoload
689 // name. If a '(' follows it must be a function. Otherwise we
690 // don't know, it can be "script.Func".
691 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100692 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100693 else
694 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
695 vim_free(auto_name);
696 done = TRUE;
697 }
698 else if (si->sn_import_autoload
699 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100700 {
701 // If a '(' follows it must be a function. Otherwise we don't
702 // know, it can be "script.Func".
703 if (cc == '(' || paren_follows_after_expr)
704 {
705 char_u sid_name[MAX_FUNC_NAME_LEN];
706
Hirohito Higashi6c012832025-02-25 20:29:50 +0100707 func_name_with_sid(exp_name, imp->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100708 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100709 }
710 else
711 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100712 imp->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100713 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100714 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100715 else
716 {
Hirohito Higashi6c012832025-02-25 20:29:50 +0100717 idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100718 cctx, NULL, TRUE);
719 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100720 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100721
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000722 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000723 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000724 if (done)
725 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000726
727 if (idx < 0)
728 {
729 if (ufunc != NULL)
730 {
731 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100732 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000733 return OK;
734 }
735 return FAIL;
736 }
737
738 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100739 imp->imp_sid,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000740 idx,
741 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 return OK;
743 }
744
Bram Moolenaard0132f42022-05-12 11:05:40 +0100745 // Can only get here if we know "name" is a script variable and not in a
746 // Vim9 script (variable is not in sn_var_vals): old style script.
747 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000748 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749}
750
751 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000752generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000754 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000755 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000757 // Reject a global non-autoload function found without the "g:" prefix.
758 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 return FAIL;
760
761 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000762 compile_type = get_compile_type(ufunc);
763 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000764 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100766 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767}
768
769/*
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100770 * Returns TRUE if compiling a class method.
771 */
772 static int
773compiling_a_class_method(cctx_T *cctx)
774{
775 // For an object method, the FC_OBJECT flag will be set.
776 // For a constructor method, the FC_NEW flag will be set.
777 // Excluding these methods, the others are class methods.
778 // When compiling a closure function inside an object method,
779 // cctx->ctx_outer->ctx_func will point to the object method.
780 return cctx->ctx_ufunc != NULL
781 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0
782 && (cctx->ctx_outer == NULL
783 || cctx->ctx_outer->ctx_ufunc == NULL
784 || cctx->ctx_outer->ctx_ufunc->uf_class == NULL
785 || (cctx->ctx_outer->ctx_ufunc->uf_flags
786 & (FC_OBJECT|FC_NEW)) == 0);
787}
788
789/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 * Compile a variable name into a load instruction.
791 * "end" points to just after the name.
792 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
793 * When "error" is FALSE do not give an error when not found.
794 */
795 int
796compile_load(
797 char_u **arg,
798 char_u *end_arg,
799 cctx_T *cctx,
800 int is_expr,
801 int error)
802{
803 type_T *type;
804 char_u *name = NULL;
805 char_u *end = end_arg;
806 int res = FAIL;
807 int prev_called_emsg = called_emsg;
808
809 if (*(*arg + 1) == ':')
810 {
811 if (end <= *arg + 2)
812 {
813 isntype_T isn_type;
814
815 // load dictionary of namespace
816 switch (**arg)
817 {
818 case 'g': isn_type = ISN_LOADGDICT; break;
819 case 'w': isn_type = ISN_LOADWDICT; break;
820 case 't': isn_type = ISN_LOADTDICT; break;
821 case 'b': isn_type = ISN_LOADBDICT; break;
822 default:
823 semsg(_(e_namespace_not_supported_str), *arg);
824 goto theend;
825 }
826 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
827 goto theend;
828 res = OK;
829 }
830 else
831 {
832 isntype_T isn_type = ISN_DROP;
833
834 // load namespaced variable
835 name = vim_strnsave(*arg + 2, end - (*arg + 2));
836 if (name == NULL)
837 return FAIL;
838
839 switch (**arg)
840 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100841 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000843 case 's': if (current_script_is_vim9())
844 {
845 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
846 *arg);
847 vim_free(name);
848 return FAIL;
849 }
Kota Kato948a3892022-08-16 16:09:59 +0100850 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000851 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852 else
853 res = compile_load_scriptvar(cctx, name,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100854 NULL, &end, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 break;
856 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
857 {
858 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000859 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000860 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861 else
862 isn_type = ISN_LOADG;
863 }
864 else
865 {
866 isn_type = ISN_LOADAUTO;
867 vim_free(name);
868 name = vim_strnsave(*arg, end - *arg);
869 if (name == NULL)
870 return FAIL;
871 }
872 break;
873 case 'w': isn_type = ISN_LOADW; break;
874 case 't': isn_type = ISN_LOADT; break;
875 case 'b': isn_type = ISN_LOADB; break;
876 default: // cannot happen, just in case
877 semsg(_(e_namespace_not_supported_str), *arg);
878 goto theend;
879 }
880 if (isn_type != ISN_DROP)
881 {
882 // Global, Buffer-local, Window-local and Tabpage-local
883 // variables can be defined later, thus we don't check if it
884 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000885 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886 }
887 }
888 }
889 else
890 {
891 size_t len = end - *arg;
892 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200893 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894 int gen_load = FALSE;
895 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100896 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100897 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898
Hirohito Higashif7cb9f92025-02-04 16:37:19 +0100899 name = vim_strnsave(*arg, len);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900 if (name == NULL)
901 return FAIL;
902
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100903 if (STRCMP(name, "super") == 0 && compiling_a_class_method(cctx))
Bram Moolenaar58b40092023-01-11 15:59:05 +0000904 {
905 // super.SomeFunc() in a class function: push &t_super type, this
906 // is recognized in compile_subscript().
907 res = push_type_stack(cctx, &t_super);
908 if (*end != '.')
909 emsg(_(e_super_must_be_followed_by_dot));
910 }
911 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000912 {
913 script_autoload(name, FALSE);
914 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
915 }
916 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
917 == OK)
918 {
919 if (gen_load_outer == 0)
920 gen_load = TRUE;
921 }
922 else
923 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000924 lvar_T lvar;
925 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926
927 if (lookup_local(*arg, len, &lvar, cctx) == OK)
928 {
929 type = lvar.lv_type;
930 idx = lvar.lv_idx;
931 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100932 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000933 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100934 outer_loop_depth = lvar.lv_loop_depth;
935 outer_loop_idx = lvar.lv_loop_idx;
936 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000937 else
938 gen_load = TRUE;
939 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200940 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
941 (((idx =
942 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
943 || ((method_idx =
944 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000945 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200946 // Referencing a class variable or method without the class
947 // name. A class variable or method can be referenced without
948 // the class name only in the class where the function is
949 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200950 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200951 {
952 if (idx >= 0)
953 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
954 else
955 {
956 ufunc_T *fp = cl->class_class_functions[method_idx];
957 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
958 NULL);
959 }
960 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200961 else
962 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200963 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200964 name, cl->class_name);
965 res = FAIL;
966 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000967 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000968 else
969 {
Hirohito Higashi6c012832025-02-25 20:29:50 +0100970 imported_T *imp = NULL;
971
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972 // "var" can be script-local even without using "s:" if it
973 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000974 if (script_var_exists(*arg, len, cctx, NULL) == OK
Hirohito Higashi6c012832025-02-25 20:29:50 +0100975 || (imp = find_imported(name, 0, FALSE)) != NULL)
976 res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000977
978 // When evaluating an expression and the name starts with an
979 // uppercase letter it can be a user defined function.
980 // generate_funcref() will fail if the function can't be found.
981 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000982 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 }
984 }
985 if (gen_load)
986 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
987 if (gen_load_outer > 0)
988 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100989 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
990 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000991 cctx->ctx_outer_used = TRUE;
992 }
993 }
994
995 *arg = end;
996
997theend:
998 if (res == FAIL && error && called_emsg == prev_called_emsg)
999 semsg(_(e_variable_not_found_str), name);
1000 vim_free(name);
1001 return res;
1002}
1003
1004/*
1005 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +01001006 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 * Returns FAIL if compilation fails.
1008 */
1009 static int
LemonBoyf3b48952022-05-05 13:53:03 +01001010compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011{
LemonBoyf3b48952022-05-05 13:53:03 +01001012 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001013 garray_T save_ga = cctx->ctx_instr;
1014 int expr_res;
1015 int trailing_error;
1016 int instr_count;
1017 isn_T *instr = NULL;
1018
1019 // Remove the string type from the stack.
1020 --cctx->ctx_type_stack.ga_len;
1021
1022 // Temporarily reset the list of instructions so that the jump labels are
1023 // correct.
1024 cctx->ctx_instr.ga_len = 0;
1025 cctx->ctx_instr.ga_maxlen = 0;
1026 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +00001027
1028 // avoid peeking a next line
1029 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
1030 cctx->ctx_ufunc->uf_lines.ga_len = 0;
1031
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001032 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +00001033
1034 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
1035
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 s = skipwhite(s);
1037 trailing_error = *s != NUL;
1038
1039 if (expr_res == FAIL || trailing_error
1040 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
1041 {
1042 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001043 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 clear_instr_ga(&cctx->ctx_instr);
1045 cctx->ctx_instr = save_ga;
1046 ++cctx->ctx_type_stack.ga_len;
1047 return FAIL;
1048 }
1049
1050 // Move the generated instructions into the ISN_INSTR instruction, then
1051 // restore the list of instructions.
1052 instr_count = cctx->ctx_instr.ga_len;
1053 instr = cctx->ctx_instr.ga_data;
1054 instr[instr_count].isn_type = ISN_FINISH;
1055
1056 cctx->ctx_instr = save_ga;
1057 vim_free(isn->isn_arg.string);
1058 isn->isn_type = ISN_INSTR;
1059 isn->isn_arg.instr = instr;
1060 return OK;
1061}
1062
1063/*
1064 * Compile the argument expressions.
1065 * "arg" points to just after the "(" and is advanced to after the ")"
1066 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001067 int
LemonBoyf3b48952022-05-05 13:53:03 +01001068compile_arguments(
1069 char_u **arg,
1070 cctx_T *cctx,
1071 int *argcount,
1072 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001073{
1074 char_u *p = *arg;
1075 char_u *whitep = *arg;
1076 int must_end = FALSE;
1077 int instr_count;
1078
1079 for (;;)
1080 {
1081 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1082 goto failret;
1083 if (*p == ')')
1084 {
1085 *arg = p + 1;
1086 return OK;
1087 }
1088 if (must_end)
1089 {
1090 semsg(_(e_missing_comma_before_argument_str), p);
1091 return FAIL;
1092 }
1093
1094 instr_count = cctx->ctx_instr.ga_len;
1095 if (compile_expr0(&p, cctx) == FAIL)
1096 return FAIL;
1097 ++*argcount;
1098
LemonBoyf3b48952022-05-05 13:53:03 +01001099 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100 && cctx->ctx_instr.ga_len == instr_count + 1)
1101 {
1102 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1103
1104 // {skip} argument of searchpair() can be compiled if not empty
1105 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +01001106 compile_string(isn, cctx, 0);
1107 }
1108 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
1109 && cctx->ctx_instr.ga_len == instr_count + 1)
1110 {
1111 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1112
1113 // {sub} argument of substitute() can be compiled if it starts
1114 // with \=
1115 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001116 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001117 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001118 }
1119
1120 if (*p != ',' && *skipwhite(p) == ',')
1121 {
1122 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1123 p = skipwhite(p);
1124 }
1125 if (*p == ',')
1126 {
1127 ++p;
1128 if (*p != NUL && !VIM_ISWHITE(*p))
1129 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1130 }
1131 else
1132 must_end = TRUE;
1133 whitep = p;
1134 p = skipwhite(p);
1135 }
1136failret:
1137 emsg(_(e_missing_closing_paren));
1138 return FAIL;
1139}
1140
1141/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001142 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1143 * etc.) if the class implements it.
1144 */
1145 static int
1146compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1147{
1148 type_T *type = get_decl_type_on_stack(cctx, 0);
1149 int res = FAIL;
1150
1151 // If the built in function is invoked on an object and the class
1152 // implements the corresponding built in method, then invoke the object
1153 // method.
1154 if (type->tt_type == VAR_OBJECT)
1155 {
1156 int method_idx;
1157 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1158 &method_idx);
1159 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001160 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001161 }
1162
1163 return res;
1164}
1165
1166
1167/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001168 * Compile a function call: name(arg1, arg2)
1169 * "arg" points to "name", "arg + varlen" to the "(".
1170 * "argcount_init" is 1 for "value->method()"
1171 * Instructions:
1172 * EVAL arg1
1173 * EVAL arg2
1174 * BCALL / DCALL / UCALL
1175 */
1176 static int
1177compile_call(
1178 char_u **arg,
1179 size_t varlen,
1180 cctx_T *cctx,
1181 ppconst_T *ppconst,
1182 int argcount_init)
1183{
1184 char_u *name = *arg;
1185 char_u *p;
1186 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001187 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 char_u fname_buf[FLEN_FIXED + 1];
1189 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190 ufunc_T *ufunc = NULL;
1191 int res = FAIL;
1192 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001193 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001194 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001195 imported_T *import;
1196
1197 if (varlen >= sizeof(namebuf))
1198 {
1199 semsg(_(e_name_too_long_str), name);
1200 return FAIL;
1201 }
1202 vim_strncpy(namebuf, *arg, varlen);
1203
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001204 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001205 if (import != NULL)
1206 {
1207 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1208 return FAIL;
1209 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001210
1211 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001212 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001213 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001214 if ((varlen == 3
1215 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1217 {
1218 char_u *s = skipwhite(*arg + varlen + 1);
1219 typval_T argvars[2];
1220 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001221 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001222
1223 argvars[0].v_type = VAR_UNKNOWN;
1224 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001225 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001226 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001227 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228 s = skipwhite(s);
1229 if (*s == ')' && argvars[0].v_type == VAR_STRING
1230 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1231 || !is_has))
1232 {
1233 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1234
1235 *arg = s + 1;
1236 argvars[1].v_type = VAR_UNKNOWN;
1237 tv->v_type = VAR_NUMBER;
1238 tv->vval.v_number = 0;
1239 if (is_has)
1240 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001241 else if (is_len)
1242 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 else
1244 f_exists(argvars, tv);
1245 clear_tv(&argvars[0]);
1246 ++ppconst->pp_used;
1247 return OK;
1248 }
1249 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001250 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 {
1252 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1253 return FAIL;
1254 }
1255 }
1256
1257 if (generate_ppconst(cctx, ppconst) == FAIL)
1258 return FAIL;
1259
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001260 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001261 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1262
1263 // We handle the "skip" argument of searchpair() and searchpairpos()
1264 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001265 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1266 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1267 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1268 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1269 special_fn = CA_SEARCHPAIR;
1270 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1271 special_fn = CA_SUBSTITUTE;
1272 else
1273 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001274
1275 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001276 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277 goto theend;
1278
1279 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1280 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1281 {
1282 int idx;
1283
1284 // builtin function
1285 idx = find_internal_func(name);
1286 if (idx >= 0)
1287 {
1288 if (STRCMP(name, "flatten") == 0)
1289 {
1290 emsg(_(e_cannot_use_flatten_in_vim9_script));
1291 goto theend;
1292 }
1293
1294 if (STRCMP(name, "add") == 0 && argcount == 2)
1295 {
h-eastb895b0f2023-09-24 15:46:31 +02001296 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001297 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1298 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001299
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 // add() can be compiled to instructions if we know the type
1301 if (type->tt_type == VAR_LIST)
1302 {
1303 // inline "add(list, item)" so that the type can be checked
1304 res = generate_LISTAPPEND(cctx);
1305 idx = -1;
1306 }
1307 else if (type->tt_type == VAR_BLOB)
1308 {
1309 // inline "add(blob, nr)" so that the type can be checked
1310 res = generate_BLOBAPPEND(cctx);
1311 idx = -1;
1312 }
1313 }
1314
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001315 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1316 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001317 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001318 // May have the "D" or "R" flag, reserve a variable for a
1319 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001320 if (get_defer_var_idx(cctx) == 0)
1321 idx = -1;
1322 }
1323
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001324 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1325 if (STRCMP(name, "string") == 0)
1326 builtin_method = CLASS_BUILTIN_STRING;
1327 else if (STRCMP(name, "empty") == 0)
1328 builtin_method = CLASS_BUILTIN_EMPTY;
1329 else if (STRCMP(name, "len") == 0)
1330 builtin_method = CLASS_BUILTIN_LEN;
1331 if (builtin_method != CLASS_BUILTIN_INVALID)
1332 {
1333 res = compile_builtin_method_call(cctx, builtin_method);
1334 if (res == OK)
1335 idx = -1;
1336 }
1337
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338 if (idx >= 0)
1339 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1340 }
1341 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001342 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001343 goto theend;
1344 }
1345
Bram Moolenaar62aec932022-01-29 21:45:34 +00001346 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1347
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348 // An argument or local variable can be a function reference, this
1349 // overrules a function name.
1350 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1351 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1352 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001353 class_T *cl = NULL;
1354 int mi = 0;
1355
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 // If we can find the function by name generate the right call.
1357 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001358 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001359 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001361 if (!func_is_global(ufunc))
1362 {
Ernie Rael58c95792024-08-13 23:27:22 +02001363 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001364 goto theend;
1365 }
1366 if (!has_g_namespace
1367 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1368 {
1369 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001370 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001371 goto theend;
1372 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001373 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001374 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001375 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001376 // Class method invocation without the class name.
1377 // A class method can be referenced without the class name only in
1378 // the class where the function is defined.
1379 if (cctx->ctx_ufunc->uf_defclass == cl)
1380 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001381 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001382 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001383 }
1384 else
1385 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001386 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001387 name, cl->class_name);
1388 res = FAIL;
1389 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001390 goto theend;
1391 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392 }
1393
1394 // If the name is a variable, load it and use PCALL.
1395 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001396 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001397 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001398 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1400 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001401 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001402
Christian Brabandtd5475e82023-08-17 23:34:09 +02001403 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001404 goto theend;
1405 }
1406
1407 // If we can find a global function by name generate the right call.
1408 if (ufunc != NULL)
1409 {
Ernie Rael58c95792024-08-13 23:27:22 +02001410 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001411 goto theend;
1412 }
1413
1414 // A global function may be defined only later. Need to figure out at
1415 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001416 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001417 res = generate_UCALL(cctx, name, argcount);
1418 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001419 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420
1421theend:
1422 vim_free(tofree);
1423 return res;
1424}
1425
1426// like NAMESPACE_CHAR but with 'a' and 'l'.
1427#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1428
1429/*
1430 * Find the end of a variable or function name. Unlike find_name_end() this
1431 * does not recognize magic braces.
1432 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1433 * Return a pointer to just after the name. Equal to "arg" if there is no
1434 * valid name.
1435 */
1436 char_u *
1437to_name_end(char_u *arg, int use_namespace)
1438{
1439 char_u *p;
1440
1441 // Quick check for valid starting character.
1442 if (!eval_isnamec1(*arg))
1443 return arg;
1444
1445 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1446 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1447 // and can be used in slice "[n:]".
1448 if (*p == ':' && (p != arg + 1
1449 || !use_namespace
1450 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1451 break;
1452 return p;
1453}
1454
1455/*
1456 * Like to_name_end() but also skip over a list or dict constant.
1457 * Also accept "<SNR>123_Func".
1458 * This intentionally does not handle line continuation.
1459 */
1460 char_u *
1461to_name_const_end(char_u *arg)
1462{
1463 char_u *p = arg;
1464 typval_T rettv;
1465
1466 if (STRNCMP(p, "<SNR>", 5) == 0)
1467 p = skipdigits(p + 5);
1468 p = to_name_end(p, TRUE);
1469 if (p == arg && *arg == '[')
1470 {
1471
1472 // Can be "[1, 2, 3]->Func()".
1473 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1474 p = arg;
1475 }
1476 return p;
1477}
1478
1479/*
1480 * parse a list: [expr, expr]
1481 * "*arg" points to the '['.
1482 * ppconst->pp_is_const is set if all items are a constant.
1483 */
1484 static int
1485compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1486{
1487 char_u *p = skipwhite(*arg + 1);
1488 char_u *whitep = *arg + 1;
1489 int count = 0;
1490 int is_const;
1491 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001492 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001493
1494 for (;;)
1495 {
1496 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001498 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001499 return FAIL;
1500 }
1501 if (*p == ',')
1502 {
1503 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1504 return FAIL;
1505 }
1506 if (*p == ']')
1507 {
1508 ++p;
1509 break;
1510 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001511 if (must_end)
1512 {
1513 semsg(_(e_missing_comma_in_list_str), p);
1514 return FAIL;
1515 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001516 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1517 return FAIL;
1518 if (!is_const)
1519 is_all_const = FALSE;
1520 ++count;
1521 if (*p == ',')
1522 {
1523 ++p;
1524 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1525 {
1526 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1527 return FAIL;
1528 }
1529 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001530 else
1531 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001532 whitep = p;
1533 p = skipwhite(p);
1534 }
1535 *arg = p;
1536
1537 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001538 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001539}
1540
1541/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001542 * parse a tuple: (expr, expr)
1543 * "*arg" points to the ','.
1544 * ppconst->pp_is_const is set if all the items are constants.
1545 */
1546 static int
1547compile_tuple(
1548 char_u **arg,
1549 cctx_T *cctx,
1550 ppconst_T *ppconst,
1551 int first_item_const)
1552{
1553 char_u *p = *arg + 1;
1554 char_u *whitep = *arg + 1;
1555 int count = 0;
1556 int is_const;
1557 int is_all_const = TRUE; // reset when non-const encountered
1558 int must_end = FALSE;
1559
1560 if (**arg != ')')
1561 {
1562 if (*p != ')' && !IS_WHITE_OR_NUL(*p))
1563 {
1564 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1565 return FAIL;
1566 }
1567 count = 1; // the first tuple item is already processed
1568 is_all_const = first_item_const;
1569 for (;;)
1570 {
1571 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1572 {
1573 semsg(_(e_missing_end_of_tuple_rsp_str), *arg);
1574 return FAIL;
1575 }
1576 if (*p == ',')
1577 {
1578 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1579 return FAIL;
1580 }
1581 if (*p == ')')
1582 {
1583 ++p;
1584 break;
1585 }
1586 if (must_end)
1587 {
1588 semsg(_(e_missing_comma_in_tuple_str), p);
1589 return FAIL;
1590 }
1591 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1592 return FAIL;
1593 if (!is_const)
1594 is_all_const = FALSE;
1595 ++count;
1596 if (*p == ',')
1597 {
1598 ++p;
1599 if (*p != ')' && !IS_WHITE_OR_NUL(*p))
1600 {
1601 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1602 return FAIL;
1603 }
1604 }
1605 else
1606 must_end = TRUE;
1607 whitep = p;
1608 p = skipwhite(p);
1609 }
1610 }
1611 *arg = p;
1612
1613 ppconst->pp_is_const = is_all_const;
1614 return generate_NEWTUPLE(cctx, count, FALSE);
1615}
1616
1617/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001618 * Parse a lambda: "(arg, arg) => expr"
1619 * "*arg" points to the '('.
1620 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1621 */
1622 static int
1623compile_lambda(char_u **arg, cctx_T *cctx)
1624{
1625 int r;
1626 typval_T rettv;
1627 ufunc_T *ufunc;
1628 evalarg_T evalarg;
1629
1630 init_evalarg(&evalarg);
1631 evalarg.eval_flags = EVAL_EVALUATE;
1632 evalarg.eval_cctx = cctx;
1633
1634 // Get the funcref in "rettv".
1635 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1636 if (r != OK)
1637 {
1638 clear_evalarg(&evalarg, NULL);
1639 return r;
1640 }
1641
1642 // "rettv" will now be a partial referencing the function.
1643 ufunc = rettv.vval.v_partial->pt_func;
1644 ++ufunc->uf_refcount;
1645 clear_tv(&rettv);
1646
1647 // Compile it here to get the return type. The return type is optional,
1648 // when it's missing use t_unknown. This is recognized in
1649 // compile_return().
1650 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1651 ufunc->uf_ret_type = &t_unknown;
1652 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1653
1654 // When the outer function is compiled for profiling or debugging, the
1655 // lambda may be called without profiling or debugging. Compile it here in
1656 // the right context.
1657 if (cctx->ctx_compile_type == CT_DEBUG
1658#ifdef FEAT_PROFILE
1659 || cctx->ctx_compile_type == CT_PROFILE
1660#endif
1661 )
1662 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1663
Bram Moolenaar139575d2022-03-15 19:29:30 +00001664 // if the outer function is not compiled for debugging or profiling, this
1665 // one might be
1666 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001667 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001668 compiletype_T compile_type = get_compile_type(ufunc);
1669
1670 if (compile_type != CT_NONE)
1671 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001672 }
1673
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001674 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1675 // "*arg" may point into it. Point into the original line to avoid a
1676 // dangling pointer.
1677 if (evalarg.eval_using_cmdline)
1678 {
1679 garray_T *gap = &evalarg.eval_tofree_ga;
1680 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1681
1682 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1683 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001684 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001685 }
1686
1687 clear_evalarg(&evalarg, NULL);
1688
1689 if (ufunc->uf_def_status == UF_COMPILED)
1690 {
1691 // The return type will now be known.
1692 set_function_type(ufunc);
1693
1694 // The function reference count will be 1. When the ISN_FUNCREF
1695 // instruction is deleted the reference count is decremented and the
1696 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001697 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698 }
1699
1700 func_ptr_unref(ufunc);
1701 return FAIL;
1702}
1703
1704/*
1705 * Get a lambda and compile it. Uses Vim9 syntax.
1706 */
1707 int
1708get_lambda_tv_and_compile(
1709 char_u **arg,
1710 typval_T *rettv,
1711 int types_optional,
1712 evalarg_T *evalarg)
1713{
1714 int r;
1715 ufunc_T *ufunc;
1716 int save_sc_version = current_sctx.sc_version;
1717
1718 // Get the funcref in "rettv".
1719 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1720 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1721 current_sctx.sc_version = save_sc_version;
1722 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001723 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001724
1725 // "rettv" will now be a partial referencing the function.
1726 ufunc = rettv->vval.v_partial->pt_func;
1727
1728 // Compile it here to get the return type. The return type is optional,
1729 // when it's missing use t_unknown. This is recognized in
1730 // compile_return().
1731 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1732 ufunc->uf_ret_type = &t_unknown;
1733 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1734
1735 if (ufunc->uf_def_status == UF_COMPILED)
1736 {
1737 // The return type will now be known.
1738 set_function_type(ufunc);
1739 return OK;
1740 }
1741 clear_tv(rettv);
1742 return FAIL;
1743}
1744
1745/*
1746 * parse a dict: {key: val, [key]: val}
1747 * "*arg" points to the '{'.
1748 * ppconst->pp_is_const is set if all item values are a constant.
1749 */
1750 static int
1751compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1752{
1753 garray_T *instr = &cctx->ctx_instr;
1754 int count = 0;
1755 dict_T *d = dict_alloc();
1756 dictitem_T *item;
1757 char_u *whitep = *arg + 1;
1758 char_u *p;
1759 int is_const;
1760 int is_all_const = TRUE; // reset when non-const encountered
1761
1762 if (d == NULL)
1763 return FAIL;
1764 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001765 {
1766 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001767 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001768 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 for (;;)
1770 {
1771 char_u *key = NULL;
1772
1773 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1774 {
1775 *arg = NULL;
1776 goto failret;
1777 }
1778
1779 if (**arg == '}')
1780 break;
1781
1782 if (**arg == '[')
1783 {
1784 isn_T *isn;
1785
1786 // {[expr]: value} uses an evaluated key.
1787 *arg = skipwhite(*arg + 1);
1788 if (compile_expr0(arg, cctx) == FAIL)
1789 return FAIL;
1790 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1791 if (isn->isn_type == ISN_PUSHNR)
1792 {
1793 char buf[NUMBUFLEN];
1794
1795 // Convert to string at compile time.
1796 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1797 isn->isn_type = ISN_PUSHS;
1798 isn->isn_arg.string = vim_strsave((char_u *)buf);
1799 }
1800 if (isn->isn_type == ISN_PUSHS)
1801 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001802 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001803 return FAIL;
1804 *arg = skipwhite(*arg);
1805 if (**arg != ']')
1806 {
1807 emsg(_(e_missing_matching_bracket_after_dict_key));
1808 return FAIL;
1809 }
1810 ++*arg;
1811 }
1812 else
1813 {
1814 // {"name": value},
1815 // {'name': value},
1816 // {name: value} use "name" as a literal key
1817 key = get_literal_key(arg);
1818 if (key == NULL)
1819 return FAIL;
1820 if (generate_PUSHS(cctx, &key) == FAIL)
1821 return FAIL;
1822 }
1823
1824 // Check for duplicate keys, if using string keys.
1825 if (key != NULL)
1826 {
1827 item = dict_find(d, key, -1);
1828 if (item != NULL)
1829 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001830 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831 goto failret;
1832 }
1833 item = dictitem_alloc(key);
1834 if (item != NULL)
1835 {
1836 item->di_tv.v_type = VAR_UNKNOWN;
1837 item->di_tv.v_lock = 0;
1838 if (dict_add(d, item) == FAIL)
1839 dictitem_free(item);
1840 }
1841 }
1842
1843 if (**arg != ':')
1844 {
1845 if (*skipwhite(*arg) == ':')
1846 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1847 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001848 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 return FAIL;
1850 }
1851 whitep = *arg + 1;
1852 if (!IS_WHITE_OR_NUL(*whitep))
1853 {
1854 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1855 return FAIL;
1856 }
1857
1858 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1859 {
1860 *arg = NULL;
1861 goto failret;
1862 }
1863
1864 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1865 return FAIL;
1866 if (!is_const)
1867 is_all_const = FALSE;
1868 ++count;
1869
1870 whitep = *arg;
1871 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1872 {
1873 *arg = NULL;
1874 goto failret;
1875 }
1876 if (**arg == '}')
1877 break;
1878 if (**arg != ',')
1879 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001880 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001881 goto failret;
1882 }
1883 if (IS_WHITE_OR_NUL(*whitep))
1884 {
1885 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1886 return FAIL;
1887 }
1888 whitep = *arg + 1;
1889 if (!IS_WHITE_OR_NUL(*whitep))
1890 {
1891 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1892 return FAIL;
1893 }
1894 *arg = skipwhite(whitep);
1895 }
1896
1897 *arg = *arg + 1;
1898
1899 // Allow for following comment, after at least one space.
1900 p = skipwhite(*arg);
1901 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1902 *arg += STRLEN(*arg);
1903
1904 dict_unref(d);
1905 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001906 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907
1908failret:
1909 if (*arg == NULL)
1910 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001911 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001912 *arg = (char_u *)"";
1913 }
1914 dict_unref(d);
1915 return FAIL;
1916}
1917
1918/*
1919 * Compile "&option".
1920 */
1921 static int
1922compile_get_option(char_u **arg, cctx_T *cctx)
1923{
1924 typval_T rettv;
1925 char_u *start = *arg;
1926 int ret;
1927
1928 // parse the option and get the current value to get the type.
1929 rettv.v_type = VAR_UNKNOWN;
1930 ret = eval_option(arg, &rettv, TRUE);
1931 if (ret == OK)
1932 {
1933 // include the '&' in the name, eval_option() expects it.
1934 char_u *name = vim_strnsave(start, *arg - start);
1935 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1936 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1937
1938 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1939 vim_free(name);
1940 }
1941 clear_tv(&rettv);
1942
1943 return ret;
1944}
1945
1946/*
1947 * Compile "$VAR".
1948 */
1949 static int
1950compile_get_env(char_u **arg, cctx_T *cctx)
1951{
1952 char_u *start = *arg;
1953 int len;
1954 int ret;
1955 char_u *name;
1956
1957 ++*arg;
1958 len = get_env_len(arg);
1959 if (len == 0)
1960 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001961 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001962 return FAIL;
1963 }
1964
1965 // include the '$' in the name, eval_env_var() expects it.
1966 name = vim_strnsave(start, len + 1);
1967 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1968 vim_free(name);
1969 return ret;
1970}
1971
1972/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001973 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001974 */
1975 static int
1976compile_interp_string(char_u **arg, cctx_T *cctx)
1977{
1978 typval_T tv;
1979 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001980 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001981 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001982 int count = 0;
1983 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001984
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001985 // *arg is on the '$' character, move it to the first string character.
1986 ++*arg;
1987 quote = **arg;
1988 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001989
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001990 for (;;)
1991 {
1992 // Get the string up to the matching quote or to a single '{'.
1993 // "arg" is advanced to either the quote or the '{'.
1994 if (quote == '"')
1995 ret = eval_string(arg, &tv, evaluate, TRUE);
1996 else
1997 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1998 if (ret == FAIL)
1999 break;
2000 if (evaluate)
2001 {
2002 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
2003 || (**arg != '{' && count == 0))
2004 {
2005 // generate non-empty string or empty string if it's the only
2006 // one
2007 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
2008 return FAIL;
2009 tv.vval.v_string = NULL; // don't free it now
2010 ++count;
2011 }
2012 clear_tv(&tv);
2013 }
2014
2015 if (**arg != '{')
2016 {
2017 // found terminating quote
2018 ++*arg;
2019 break;
2020 }
2021
2022 p = compile_one_expr_in_str(*arg, cctx);
2023 if (p == NULL)
2024 {
2025 ret = FAIL;
2026 break;
2027 }
2028 ++count;
2029 *arg = p;
2030 }
LemonBoy2eaef102022-05-06 13:14:50 +01002031
2032 if (ret == FAIL || !evaluate)
2033 return ret;
2034
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002035 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
2036 if (count > 1)
2037 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01002038
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002039 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002040}
2041
2042/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002043 * Compile "@r".
2044 */
2045 static int
2046compile_get_register(char_u **arg, cctx_T *cctx)
2047{
2048 int ret;
2049
2050 ++*arg;
2051 if (**arg == NUL)
2052 {
2053 semsg(_(e_syntax_error_at_str), *arg - 1);
2054 return FAIL;
2055 }
2056 if (!valid_yank_reg(**arg, FALSE))
2057 {
2058 emsg_invreg(**arg);
2059 return FAIL;
2060 }
2061 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2062 ++*arg;
2063 return ret;
2064}
2065
2066/*
2067 * Apply leading '!', '-' and '+' to constant "rettv".
2068 * When "numeric_only" is TRUE do not apply '!'.
2069 */
2070 static int
2071apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
2072{
2073 char_u *p = *end;
2074
2075 // this works from end to start
2076 while (p > start)
2077 {
2078 --p;
2079 if (*p == '-' || *p == '+')
2080 {
2081 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082 if (rettv->v_type == VAR_FLOAT)
2083 {
2084 if (*p == '-')
2085 rettv->vval.v_float = -rettv->vval.v_float;
2086 }
2087 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002088 {
2089 varnumber_T val;
2090 int error = FALSE;
2091
2092 // tv_get_number_chk() accepts a string, but we don't want that
2093 // here
2094 if (check_not_string(rettv) == FAIL)
2095 return FAIL;
2096 val = tv_get_number_chk(rettv, &error);
2097 clear_tv(rettv);
2098 if (error)
2099 return FAIL;
2100 if (*p == '-')
2101 val = -val;
2102 rettv->v_type = VAR_NUMBER;
2103 rettv->vval.v_number = val;
2104 }
2105 }
2106 else if (numeric_only)
2107 {
2108 ++p;
2109 break;
2110 }
2111 else if (*p == '!')
2112 {
2113 int v = tv2bool(rettv);
2114
2115 // '!' is permissive in the type.
2116 clear_tv(rettv);
2117 rettv->v_type = VAR_BOOL;
2118 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2119 }
2120 }
2121 *end = p;
2122 return OK;
2123}
2124
2125/*
2126 * Recognize v: variables that are constants and set "rettv".
2127 */
2128 static void
2129get_vim_constant(char_u **arg, typval_T *rettv)
2130{
2131 if (STRNCMP(*arg, "v:true", 6) == 0)
2132 {
2133 rettv->v_type = VAR_BOOL;
2134 rettv->vval.v_number = VVAL_TRUE;
2135 *arg += 6;
2136 }
2137 else if (STRNCMP(*arg, "v:false", 7) == 0)
2138 {
2139 rettv->v_type = VAR_BOOL;
2140 rettv->vval.v_number = VVAL_FALSE;
2141 *arg += 7;
2142 }
2143 else if (STRNCMP(*arg, "v:null", 6) == 0)
2144 {
2145 rettv->v_type = VAR_SPECIAL;
2146 rettv->vval.v_number = VVAL_NULL;
2147 *arg += 6;
2148 }
2149 else if (STRNCMP(*arg, "v:none", 6) == 0)
2150 {
2151 rettv->v_type = VAR_SPECIAL;
2152 rettv->vval.v_number = VVAL_NONE;
2153 *arg += 6;
2154 }
2155}
2156
2157 exprtype_T
2158get_compare_type(char_u *p, int *len, int *type_is)
2159{
2160 exprtype_T type = EXPR_UNKNOWN;
2161 int i;
2162
2163 switch (p[0])
2164 {
2165 case '=': if (p[1] == '=')
2166 type = EXPR_EQUAL;
2167 else if (p[1] == '~')
2168 type = EXPR_MATCH;
2169 break;
2170 case '!': if (p[1] == '=')
2171 type = EXPR_NEQUAL;
2172 else if (p[1] == '~')
2173 type = EXPR_NOMATCH;
2174 break;
2175 case '>': if (p[1] != '=')
2176 {
2177 type = EXPR_GREATER;
2178 *len = 1;
2179 }
2180 else
2181 type = EXPR_GEQUAL;
2182 break;
2183 case '<': if (p[1] != '=')
2184 {
2185 type = EXPR_SMALLER;
2186 *len = 1;
2187 }
2188 else
2189 type = EXPR_SEQUAL;
2190 break;
2191 case 'i': if (p[1] == 's')
2192 {
2193 // "is" and "isnot"; but not a prefix of a name
2194 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2195 *len = 5;
2196 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002197 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002198 {
2199 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2200 *type_is = TRUE;
2201 }
2202 }
2203 break;
2204 }
2205 return type;
2206}
2207
2208/*
2209 * Skip over an expression, ignoring most errors.
2210 */
2211 void
2212skip_expr_cctx(char_u **arg, cctx_T *cctx)
2213{
2214 evalarg_T evalarg;
2215
2216 init_evalarg(&evalarg);
2217 evalarg.eval_cctx = cctx;
2218 skip_expr(arg, &evalarg);
2219 clear_evalarg(&evalarg, NULL);
2220}
2221
2222/*
2223 * Check that the top of the type stack has a type that can be used as a
2224 * condition. Give an error and return FAIL if not.
2225 */
2226 int
2227bool_on_stack(cctx_T *cctx)
2228{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 type_T *type;
2230
Bram Moolenaar078a4612022-01-04 15:17:03 +00002231 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002232 if (type == &t_bool)
2233 return OK;
2234
Bram Moolenaar4913d422022-10-17 13:13:32 +01002235 if (type->tt_type == VAR_ANY
2236 || type->tt_type == VAR_UNKNOWN
2237 || type->tt_type == VAR_NUMBER
2238 || type == &t_number_bool
2239 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002240 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2241 // This requires a runtime type check.
2242 return generate_COND2BOOL(cctx);
2243
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002244 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002245}
2246
2247/*
2248 * Give the "white on both sides" error, taking the operator from "p[len]".
2249 */
2250 void
2251error_white_both(char_u *op, int len)
2252{
2253 char_u buf[10];
2254
2255 vim_strncpy(buf, op, len);
2256 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2257}
2258
2259/*
2260 * Compile code to apply '-', '+' and '!'.
2261 * When "numeric_only" is TRUE do not apply '!'.
2262 */
2263 static int
2264compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2265{
2266 char_u *p = *end;
2267
2268 // this works from end to start
2269 while (p > start)
2270 {
2271 --p;
2272 while (VIM_ISWHITE(*p))
2273 --p;
2274 if (*p == '-' || *p == '+')
2275 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002276 type_T *type = get_type_on_stack(cctx, 0);
2277 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002278 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002279 return FAIL;
2280
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002281 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002282 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2283 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002284 }
2285 else if (numeric_only)
2286 {
2287 ++p;
2288 break;
2289 }
2290 else
2291 {
2292 int invert = *p == '!';
2293
2294 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2295 {
2296 if (p[-1] == '!')
2297 invert = !invert;
2298 --p;
2299 }
2300 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2301 return FAIL;
2302 }
2303 }
2304 *end = p;
2305 return OK;
2306}
2307
2308/*
2309 * Compile "(expression)": recursive!
2310 * Return FAIL/OK.
2311 */
2312 static int
2313compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2314{
2315 int ret;
2316 char_u *p = *arg + 1;
2317
2318 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2319 return FAIL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002320
2321 if (**arg == ')')
2322 // empty tuple
2323 return compile_tuple(arg, cctx, ppconst, FALSE);
2324
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 if (ppconst->pp_used <= PPSIZE - 10)
2326 {
2327 ret = compile_expr1(arg, cctx, ppconst);
2328 }
2329 else
2330 {
2331 // Not enough space in ppconst, flush constants.
2332 if (generate_ppconst(cctx, ppconst) == FAIL)
2333 return FAIL;
2334 ret = compile_expr0(arg, cctx);
2335 }
2336 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2337 return FAIL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002338 if (ret == OK && **arg == ',')
2339 {
2340 // tuple
2341 int is_const = ppconst->pp_used > 0 || ppconst->pp_is_const;
2342 if (generate_ppconst(cctx, ppconst) == FAIL)
2343 return FAIL;
2344 return compile_tuple(arg, cctx, ppconst, is_const);
2345 }
2346
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002347 if (**arg == ')')
2348 ++*arg;
2349 else if (ret == OK)
2350 {
2351 emsg(_(e_missing_closing_paren));
2352 ret = FAIL;
2353 }
2354 return ret;
2355}
2356
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002357static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002358
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359/*
2360 * Compile whatever comes after "name" or "name()".
2361 * Advances "*arg" only when something was recognized.
2362 */
2363 static int
2364compile_subscript(
2365 char_u **arg,
2366 cctx_T *cctx,
2367 char_u *start_leader,
2368 char_u **end_leader,
2369 ppconst_T *ppconst)
2370{
2371 char_u *name_start = *end_leader;
2372 int keeping_dict = FALSE;
2373
2374 for (;;)
2375 {
2376 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002377 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002378
2379 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2380 {
2381 char_u *next = peek_next_line_from_context(cctx);
2382
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002383 // If a following line starts with "->{", "->(" or "->X" advance to
2384 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002385 // Also if a following line starts with ".x".
2386 if (next != NULL &&
2387 ((next[0] == '-' && next[1] == '>'
2388 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002389 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002390 || ASCII_ISALPHA(*skipwhite(next + 2))))
2391 || (next[0] == '.' && eval_isdictc(next[1]))))
2392 {
2393 next = next_line_from_context(cctx, TRUE);
2394 if (next == NULL)
2395 return FAIL;
2396 *arg = next;
2397 p = skipwhite(*arg);
2398 }
2399 }
2400
2401 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2402 // is not a function call.
2403 if (**arg == '(')
2404 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002405 int argcount = 0;
2406
2407 if (generate_ppconst(cctx, ppconst) == FAIL)
2408 return FAIL;
2409 ppconst->pp_is_const = FALSE;
2410
2411 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002412 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002413
2414 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002415 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002416 return FAIL;
2417 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2418 return FAIL;
2419 if (keeping_dict)
2420 {
2421 keeping_dict = FALSE;
2422 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2423 return FAIL;
2424 }
2425 }
2426 else if (*p == '-' && p[1] == '>')
2427 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002428 char_u *pstart = p;
2429 int alt;
2430 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431
Bram Moolenaarc7349932022-01-16 20:59:39 +00002432 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433 if (generate_ppconst(cctx, ppconst) == FAIL)
2434 return FAIL;
2435 ppconst->pp_is_const = FALSE;
2436
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437 // Apply the '!', '-' and '+' first:
2438 // -1.0->func() works like (-1.0)->func()
2439 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2440 return FAIL;
2441
2442 p += 2;
2443 *arg = skipwhite(p);
2444 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002445
2446 // Three alternatives handled here:
2447 // 1. "base->name(" only a name, use compile_call()
2448 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2449 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002450 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002451 alt = 2;
2452 else
2453 {
2454 // alternative 1 or 3
2455 p = *arg;
2456 if (!eval_isnamec1(*p))
2457 {
2458 semsg(_(e_trailing_characters_str), pstart);
2459 return FAIL;
2460 }
2461 if (ASCII_ISALPHA(*p) && p[1] == ':')
2462 p += 2;
2463 for ( ; eval_isnamec(*p); ++p)
2464 ;
2465 if (*p == '(')
2466 {
2467 // alternative 1
2468 alt = 1;
2469 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2470 return FAIL;
2471 }
2472 else
2473 {
2474 // Must be alternative 3, find the "(". Only works within
2475 // one line.
2476 alt = 3;
2477 paren = vim_strchr(p, '(');
2478 if (paren == NULL)
2479 {
2480 semsg(_(e_missing_parenthesis_str), *arg);
2481 return FAIL;
2482 }
2483 }
2484 }
2485
2486 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 {
2488 int argcount = 1;
2489 garray_T *stack = &cctx->ctx_type_stack;
2490 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491 int expr_isn_start = cctx->ctx_instr.ga_len;
2492 int expr_isn_end;
2493 int arg_isn_count;
2494
Bram Moolenaarc7349932022-01-16 20:59:39 +00002495 if (alt == 2)
2496 {
2497 // Funcref call: list->(Refs[2])(arg)
2498 // or lambda: list->((arg) => expr)(arg)
2499 //
2500 // Fist compile the function expression.
2501 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2502 return FAIL;
2503 }
2504 else
2505 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002506 int fail;
2507 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002508 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002509
Bram Moolenaarc7349932022-01-16 20:59:39 +00002510 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002511
2512 // instead of using LOADG for "import.Func" use PUSHFUNC
2513 ++paren_follows_after_expr;
2514
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002515 // do not look in the next line
2516 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002517
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002518 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002519 || *skipwhite(*arg) != NUL;
2520 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002521 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002522 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002523
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002524 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002525 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002526 if (did_emsg == prev_did_emsg)
2527 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002528 return FAIL;
2529 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002530 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002531
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532 // Compile the arguments.
2533 if (**arg != '(')
2534 {
2535 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002536 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002537 else
2538 semsg(_(e_missing_parenthesis_str), *arg);
2539 return FAIL;
2540 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002541
2542 // Remember the next instruction index, where the instructions
2543 // for arguments are being written.
2544 expr_isn_end = cctx->ctx_instr.ga_len;
2545
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002546 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002547 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2548 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 return FAIL;
2550
2551 // Move the instructions for the arguments to before the
2552 // instructions of the expression and move the type of the
2553 // expression after the argument types. This is what ISN_PCALL
2554 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002555 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2556 if (arg_isn_count > 0)
2557 {
2558 int expr_isn_count = expr_isn_end - expr_isn_start;
2559 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002560 type_T *decl_type;
2561 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002562
2563 if (isn == NULL)
2564 return FAIL;
2565 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2566 + expr_isn_start,
2567 sizeof(isn_T) * expr_isn_count);
2568 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2569 + expr_isn_start,
2570 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2571 sizeof(isn_T) * arg_isn_count);
2572 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2573 + expr_isn_start + arg_isn_count,
2574 isn, sizeof(isn_T) * expr_isn_count);
2575 vim_free(isn);
2576
Bram Moolenaar078a4612022-01-04 15:17:03 +00002577 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2578 type = typep->type_curr;
2579 decl_type = typep->type_decl;
2580 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2581 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2582 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002583 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002584 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2585 typep->type_curr = type;
2586 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002587 }
2588
Bram Moolenaar078a4612022-01-04 15:17:03 +00002589 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002590 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2591 return FAIL;
2592 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002593
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 if (keeping_dict)
2595 {
2596 keeping_dict = FALSE;
2597 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2598 return FAIL;
2599 }
2600 }
2601 else if (**arg == '[')
2602 {
2603 int is_slice = FALSE;
2604
2605 // list index: list[123]
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002606 // tuple index: tuple[123]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002607 // dict member: dict[key]
2608 // string index: text[123]
2609 // blob index: blob[123]
2610 if (generate_ppconst(cctx, ppconst) == FAIL)
2611 return FAIL;
2612 ppconst->pp_is_const = FALSE;
2613
2614 ++p;
2615 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2616 return FAIL;
2617 if (**arg == ':')
2618 {
2619 // missing first index is equal to zero
2620 generate_PUSHNR(cctx, 0);
2621 }
2622 else
2623 {
2624 if (compile_expr0(arg, cctx) == FAIL)
2625 return FAIL;
2626 if (**arg == ':')
2627 {
2628 semsg(_(e_white_space_required_before_and_after_str_at_str),
2629 ":", *arg);
2630 return FAIL;
2631 }
2632 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2633 return FAIL;
2634 *arg = skipwhite(*arg);
2635 }
2636 if (**arg == ':')
2637 {
2638 is_slice = TRUE;
2639 ++*arg;
2640 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2641 {
2642 semsg(_(e_white_space_required_before_and_after_str_at_str),
2643 ":", *arg);
2644 return FAIL;
2645 }
2646 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2647 return FAIL;
2648 if (**arg == ']')
2649 // missing second index is equal to end of string
2650 generate_PUSHNR(cctx, -1);
2651 else
2652 {
2653 if (compile_expr0(arg, cctx) == FAIL)
2654 return FAIL;
2655 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2656 return FAIL;
2657 *arg = skipwhite(*arg);
2658 }
2659 }
2660
2661 if (**arg != ']')
2662 {
2663 emsg(_(e_missing_closing_square_brace));
2664 return FAIL;
2665 }
2666 *arg = *arg + 1;
2667
2668 if (keeping_dict)
2669 {
2670 keeping_dict = FALSE;
2671 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2672 return FAIL;
2673 }
2674 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2675 return FAIL;
2676 }
2677 else if (*p == '.' && p[1] != '.')
2678 {
2679 // dictionary member: dict.name
2680 if (generate_ppconst(cctx, ppconst) == FAIL)
2681 return FAIL;
2682 ppconst->pp_is_const = FALSE;
2683
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002684 type = get_type_on_stack(cctx, 0);
2685 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002686 && (type->tt_type == VAR_CLASS
2687 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002688 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002689 // class member: SomeClass.varname
2690 // class method: SomeClass.SomeMethod()
2691 // class constructor: SomeClass.new()
2692 // object member: someObject.varname, this.varname
2693 // object method: someObject.SomeMethod(), this.SomeMethod()
2694 *arg = p;
2695 if (compile_class_object_index(cctx, arg, type) == FAIL)
2696 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002698 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002699 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002700 *arg = p + 1;
2701 if (IS_WHITE_OR_NUL(**arg))
2702 {
2703 emsg(_(e_missing_name_after_dot));
2704 return FAIL;
2705 }
2706 p = *arg;
2707 if (eval_isdictc(*p))
2708 while (eval_isnamec(*p))
2709 MB_PTR_ADV(p);
2710 if (p == *arg)
2711 {
2712 semsg(_(e_syntax_error_at_str), *arg);
2713 return FAIL;
2714 }
2715 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2716 return FAIL;
2717 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2718 return FAIL;
2719 keeping_dict = TRUE;
2720 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002721 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 }
2723 else
2724 break;
2725 }
2726
2727 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2728 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002729 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2730 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 return FAIL;
2732
2733 return OK;
2734}
2735
2736/*
2737 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2738 * "arg" is advanced until after the expression, skipping white space.
2739 *
2740 * If the value is a constant "ppconst->pp_used" will be non-zero.
2741 * Before instructions are generated, any values in "ppconst" will generated.
2742 *
2743 * This is the compiling equivalent of eval1(), eval2(), etc.
2744 */
2745
2746/*
2747 * number number constant
2748 * 0zFFFFFFFF Blob constant
2749 * "string" string constant
2750 * 'string' literal string constant
2751 * &option-name option value
2752 * @r register contents
2753 * identifier variable value
2754 * function() function call
2755 * $VAR environment variable
2756 * (expression) nested expression
2757 * [expr, expr] List
2758 * {key: val, [key]: val} Dictionary
2759 *
2760 * Also handle:
2761 * ! in front logical NOT
2762 * - in front unary minus
2763 * + in front unary plus (ignored)
2764 * trailing (arg) funcref/partial call
2765 * trailing [] subscript in String or List
2766 * trailing .name entry in Dictionary
2767 * trailing ->name() method call
2768 */
2769 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002770compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002771 char_u **arg,
2772 cctx_T *cctx,
2773 ppconst_T *ppconst)
2774{
2775 char_u *start_leader, *end_leader;
2776 int ret = OK;
2777 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2778 int used_before = ppconst->pp_used;
2779
2780 ppconst->pp_is_const = FALSE;
2781
2782 /*
2783 * Skip '!', '-' and '+' characters. They are handled later.
2784 */
2785 start_leader = *arg;
2786 if (eval_leader(arg, TRUE) == FAIL)
2787 return FAIL;
2788 end_leader = *arg;
2789
2790 rettv->v_type = VAR_UNKNOWN;
2791 switch (**arg)
2792 {
2793 /*
2794 * Number constant.
2795 */
2796 case '0': // also for blob starting with 0z
2797 case '1':
2798 case '2':
2799 case '3':
2800 case '4':
2801 case '5':
2802 case '6':
2803 case '7':
2804 case '8':
2805 case '9':
2806 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2807 return FAIL;
2808 // Apply "-" and "+" just before the number now, right to
2809 // left. Matters especially when "->" follows. Stops at
2810 // '!'.
2811 if (apply_leader(rettv, TRUE,
2812 start_leader, &end_leader) == FAIL)
2813 {
2814 clear_tv(rettv);
2815 return FAIL;
2816 }
2817 break;
2818
2819 /*
2820 * String constant: "string".
2821 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002822 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002823 return FAIL;
2824 break;
2825
2826 /*
2827 * Literal string constant: 'str''ing'.
2828 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002829 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002830 return FAIL;
2831 break;
2832
2833 /*
2834 * Constant Vim variable.
2835 */
2836 case 'v': get_vim_constant(arg, rettv);
2837 ret = NOTDONE;
2838 break;
2839
2840 /*
2841 * "true" constant
2842 */
2843 case 't': if (STRNCMP(*arg, "true", 4) == 0
2844 && !eval_isnamec((*arg)[4]))
2845 {
2846 *arg += 4;
2847 rettv->v_type = VAR_BOOL;
2848 rettv->vval.v_number = VVAL_TRUE;
2849 }
2850 else
2851 ret = NOTDONE;
2852 break;
2853
2854 /*
2855 * "false" constant
2856 */
2857 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2858 && !eval_isnamec((*arg)[5]))
2859 {
2860 *arg += 5;
2861 rettv->v_type = VAR_BOOL;
2862 rettv->vval.v_number = VVAL_FALSE;
2863 }
2864 else
2865 ret = NOTDONE;
2866 break;
2867
2868 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002869 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002870 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002871 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002872 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002873 char_u *p = *arg + 4;
2874 int len;
2875
2876 for (len = 0; eval_isnamec(p[len]); ++len)
2877 ;
2878 ret = handle_predefined(*arg, len + 4, rettv);
2879 if (ret == FAIL)
2880 ret = NOTDONE;
2881 else
2882 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002883 }
2884 else
2885 ret = NOTDONE;
2886 break;
2887
2888 /*
2889 * List: [expr, expr]
2890 */
2891 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2892 return FAIL;
2893 ret = compile_list(arg, cctx, ppconst);
2894 break;
2895
2896 /*
2897 * Dictionary: {'key': val, 'key': val}
2898 */
2899 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2900 return FAIL;
2901 ret = compile_dict(arg, cctx, ppconst);
2902 break;
2903
2904 /*
2905 * Option value: &name
2906 */
2907 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2908 return FAIL;
2909 ret = compile_get_option(arg, cctx);
2910 break;
2911
2912 /*
2913 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002914 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002915 */
2916 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2917 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002918 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2919 ret = compile_interp_string(arg, cctx);
2920 else
2921 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002922 break;
2923
2924 /*
2925 * Register contents: @r.
2926 */
2927 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2928 return FAIL;
2929 ret = compile_get_register(arg, cctx);
2930 break;
2931 /*
2932 * nested expression: (expression).
2933 * lambda: (arg, arg) => expr
2934 * funcref: (arg, arg) => { statement }
2935 */
2936 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2937 ret = compile_lambda(arg, cctx);
2938 if (ret == NOTDONE)
2939 ret = compile_parenthesis(arg, cctx, ppconst);
2940 break;
2941
2942 default: ret = NOTDONE;
2943 break;
2944 }
2945 if (ret == FAIL)
2946 return FAIL;
2947
2948 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2949 {
2950 if (cctx->ctx_skip == SKIP_YES)
2951 clear_tv(rettv);
2952 else
2953 // A constant expression can possibly be handled compile time,
2954 // return the value instead of generating code.
2955 ++ppconst->pp_used;
2956 }
2957 else if (ret == NOTDONE)
2958 {
2959 char_u *p;
2960 int r;
2961
2962 if (!eval_isnamec1(**arg))
2963 {
2964 if (!vim9_bad_comment(*arg))
2965 {
2966 if (ends_excmd(*skipwhite(*arg)))
2967 semsg(_(e_empty_expression_str), *arg);
2968 else
2969 semsg(_(e_name_expected_str), *arg);
2970 }
2971 return FAIL;
2972 }
2973
2974 // "name" or "name()"
2975 p = to_name_end(*arg, TRUE);
2976 if (p - *arg == (size_t)1 && **arg == '_')
2977 {
2978 emsg(_(e_cannot_use_underscore_here));
2979 return FAIL;
2980 }
2981
2982 if (*p == '(')
2983 {
2984 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2985 }
2986 else
2987 {
2988 if (cctx->ctx_skip != SKIP_YES
2989 && generate_ppconst(cctx, ppconst) == FAIL)
2990 return FAIL;
2991 r = compile_load(arg, p, cctx, TRUE, TRUE);
2992 }
2993 if (r == FAIL)
2994 return FAIL;
2995 }
2996
2997 // Handle following "[]", ".member", etc.
2998 // Then deal with prefixed '-', '+' and '!', if not done already.
2999 if (compile_subscript(arg, cctx, start_leader, &end_leader,
3000 ppconst) == FAIL)
3001 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01003002 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003003 {
3004 // apply the '!', '-' and '+' before the constant
3005 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3006 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
3007 return FAIL;
3008 return OK;
3009 }
3010 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
3011 return FAIL;
3012 return OK;
3013}
3014
3015/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003016 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003017 */
3018 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003019compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003020{
3021 type_T *want_type = NULL;
3022
3023 // Recognize <type>
3024 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3025 {
3026 ++*arg;
3027 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
3028 if (want_type == NULL)
3029 return FAIL;
3030
3031 if (**arg != '>')
3032 {
3033 if (*skipwhite(*arg) == '>')
3034 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3035 else
3036 emsg(_(e_missing_gt));
3037 return FAIL;
3038 }
3039 ++*arg;
3040 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3041 return FAIL;
3042 }
3043
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003044 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003045 return FAIL;
3046
3047 if (want_type != NULL)
3048 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003049 type_T *actual;
3050 where_T where = WHERE_INIT;
3051
LemonBoy50d48542024-07-04 17:03:17 +02003052 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003053 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00003054 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00003055 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003056 {
LemonBoy50d48542024-07-04 17:03:17 +02003057 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
3058 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003059 return FAIL;
3060 }
3061 }
3062
3063 return OK;
3064}
3065
3066/*
3067 * * number multiplication
3068 * / number division
3069 * % number modulo
3070 */
3071 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003072compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003073{
3074 char_u *op;
3075 char_u *next;
3076 int ppconst_used = ppconst->pp_used;
3077
3078 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003079 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003080 return FAIL;
3081
3082 /*
3083 * Repeat computing, until no "*", "/" or "%" is following.
3084 */
3085 for (;;)
3086 {
3087 op = may_peek_next_line(cctx, *arg, &next);
3088 if (*op != '*' && *op != '/' && *op != '%')
3089 break;
3090 if (next != NULL)
3091 {
3092 *arg = next_line_from_context(cctx, TRUE);
3093 op = skipwhite(*arg);
3094 }
3095
3096 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
3097 {
3098 error_white_both(op, 1);
3099 return FAIL;
3100 }
3101 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
3102 return FAIL;
3103
3104 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003105 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003106 return FAIL;
3107
3108 if (ppconst->pp_used == ppconst_used + 2
3109 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3110 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
3111 {
3112 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3113 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3114 varnumber_T res = 0;
3115 int failed = FALSE;
3116
3117 // both are numbers: compute the result
3118 switch (*op)
3119 {
3120 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
3121 break;
3122 case '/': res = num_divide(tv1->vval.v_number,
3123 tv2->vval.v_number, &failed);
3124 break;
3125 case '%': res = num_modulus(tv1->vval.v_number,
3126 tv2->vval.v_number, &failed);
3127 break;
3128 }
3129 if (failed)
3130 return FAIL;
3131 tv1->vval.v_number = res;
3132 --ppconst->pp_used;
3133 }
3134 else
3135 {
3136 generate_ppconst(cctx, ppconst);
3137 generate_two_op(cctx, op);
3138 }
3139 }
3140
3141 return OK;
3142}
3143
3144/*
3145 * + number addition or list/blobl concatenation
3146 * - number subtraction
3147 * .. string concatenation
3148 */
3149 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003150compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003151{
3152 char_u *op;
3153 char_u *next;
3154 int oplen;
3155 int ppconst_used = ppconst->pp_used;
3156
3157 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003158 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003159 return FAIL;
3160
3161 /*
3162 * Repeat computing, until no "+", "-" or ".." is following.
3163 */
3164 for (;;)
3165 {
3166 op = may_peek_next_line(cctx, *arg, &next);
3167 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
3168 break;
3169 if (op[0] == op[1] && *op != '.' && next)
3170 // Finding "++" or "--" on the next line is a separate command.
3171 // But ".." is concatenation.
3172 break;
3173 oplen = (*op == '.' ? 2 : 1);
3174 if (next != NULL)
3175 {
3176 *arg = next_line_from_context(cctx, TRUE);
3177 op = skipwhite(*arg);
3178 }
3179
3180 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3181 {
3182 error_white_both(op, oplen);
3183 return FAIL;
3184 }
3185
3186 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3187 return FAIL;
3188
3189 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003190 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003191 return FAIL;
3192
3193 if (ppconst->pp_used == ppconst_used + 2
3194 && (*op == '.'
3195 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3196 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3197 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3198 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3199 {
3200 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3201 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3202
3203 // concat/subtract/add constant numbers
3204 if (*op == '+')
3205 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3206 else if (*op == '-')
3207 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3208 else
3209 {
3210 // concatenate constant strings
3211 char_u *s1 = tv1->vval.v_string;
3212 char_u *s2 = tv2->vval.v_string;
3213 size_t len1 = STRLEN(s1);
3214
3215 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3216 if (tv1->vval.v_string == NULL)
3217 {
3218 clear_ppconst(ppconst);
3219 return FAIL;
3220 }
3221 mch_memmove(tv1->vval.v_string, s1, len1);
3222 STRCPY(tv1->vval.v_string + len1, s2);
3223 vim_free(s1);
3224 vim_free(s2);
3225 }
3226 --ppconst->pp_used;
3227 }
3228 else
3229 {
3230 generate_ppconst(cctx, ppconst);
3231 ppconst->pp_is_const = FALSE;
3232 if (*op == '.')
3233 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003234 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3235 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003236 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003237 if (generate_CONCAT(cctx, 2) == FAIL)
3238 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003239 }
3240 else
3241 generate_two_op(cctx, op);
3242 }
3243 }
3244
3245 return OK;
3246}
3247
3248/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003249 * expr6a >> expr6b
3250 * expr6a << expr6b
3251 *
3252 * Produces instructions:
3253 * OPNR bitwise left or right shift
3254 */
3255 static int
3256compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3257{
3258 exprtype_T type = EXPR_UNKNOWN;
3259 char_u *p;
3260 char_u *next;
3261 int len = 2;
3262 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003263 isn_T *isn;
3264
3265 // get the first variable
3266 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3267 return FAIL;
3268
3269 /*
3270 * Repeat computing, until no "+", "-" or ".." is following.
3271 */
3272 for (;;)
3273 {
3274 type = EXPR_UNKNOWN;
3275
3276 p = may_peek_next_line(cctx, *arg, &next);
3277 if (p[0] == '<' && p[1] == '<')
3278 type = EXPR_LSHIFT;
3279 else if (p[0] == '>' && p[1] == '>')
3280 type = EXPR_RSHIFT;
3281
3282 if (type == EXPR_UNKNOWN)
3283 return OK;
3284
3285 // Handle a bitwise left or right shift operator
3286 if (ppconst->pp_used == ppconst_used + 1)
3287 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003288 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003289 {
3290 // left operand should be a number
3291 emsg(_(e_bitshift_ops_must_be_number));
3292 return FAIL;
3293 }
3294 }
3295 else
3296 {
3297 type_T *t = get_type_on_stack(cctx, 0);
3298
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003299 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003300 {
3301 emsg(_(e_bitshift_ops_must_be_number));
3302 return FAIL;
3303 }
3304 }
3305
3306 if (next != NULL)
3307 {
3308 *arg = next_line_from_context(cctx, TRUE);
3309 p = skipwhite(*arg);
3310 }
3311
3312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3313 {
3314 error_white_both(p, len);
3315 return FAIL;
3316 }
3317
3318 // get the second variable
3319 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3320 return FAIL;
3321
3322 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3323 return FAIL;
3324
3325 if (ppconst->pp_used == ppconst_used + 2)
3326 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003327 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3328 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3329
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003330 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003331 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3332 {
3333 // right operand should be a positive number
3334 if (tv2->v_type != VAR_NUMBER)
3335 emsg(_(e_bitshift_ops_must_be_number));
3336 else
dundargocc57b5bc2022-11-02 13:30:51 +00003337 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003338 return FAIL;
3339 }
3340
3341 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3342 tv1->vval.v_number = 0;
3343 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003344 tv1->vval.v_number =
3345 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003346 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003347 tv1->vval.v_number =
3348 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003349 clear_tv(tv2);
3350 --ppconst->pp_used;
3351 }
3352 else
3353 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003354 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3355 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003356 {
3357 emsg(_(e_bitshift_ops_must_be_number));
3358 return FAIL;
3359 }
3360
3361 generate_ppconst(cctx, ppconst);
3362
3363 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3364 if (isn == NULL)
3365 return FAIL;
3366
3367 if (isn != NULL)
3368 isn->isn_arg.op.op_type = type;
3369 }
3370 }
3371
3372 return OK;
3373}
3374
3375/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003376 * expr5a == expr5b
3377 * expr5a =~ expr5b
3378 * expr5a != expr5b
3379 * expr5a !~ expr5b
3380 * expr5a > expr5b
3381 * expr5a >= expr5b
3382 * expr5a < expr5b
3383 * expr5a <= expr5b
3384 * expr5a is expr5b
3385 * expr5a isnot expr5b
3386 *
3387 * Produces instructions:
3388 * EVAL expr5a Push result of "expr5a"
3389 * EVAL expr5b Push result of "expr5b"
3390 * COMPARE one of the compare instructions
3391 */
3392 static int
3393compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3394{
3395 exprtype_T type = EXPR_UNKNOWN;
3396 char_u *p;
3397 char_u *next;
3398 int len = 2;
3399 int type_is = FALSE;
3400 int ppconst_used = ppconst->pp_used;
3401
3402 // get the first variable
3403 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3404 return FAIL;
3405
3406 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003407
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003408 type = get_compare_type(p, &len, &type_is);
3409
3410 /*
3411 * If there is a comparative operator, use it.
3412 */
3413 if (type != EXPR_UNKNOWN)
3414 {
3415 int ic = FALSE; // Default: do not ignore case
3416
3417 if (next != NULL)
3418 {
3419 *arg = next_line_from_context(cctx, TRUE);
3420 p = skipwhite(*arg);
3421 }
3422 if (type_is && (p[len] == '?' || p[len] == '#'))
3423 {
3424 semsg(_(e_invalid_expression_str), *arg);
3425 return FAIL;
3426 }
3427 // extra question mark appended: ignore case
3428 if (p[len] == '?')
3429 {
3430 ic = TRUE;
3431 ++len;
3432 }
3433 // extra '#' appended: match case (ignored)
3434 else if (p[len] == '#')
3435 ++len;
3436 // nothing appended: match case
3437
3438 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3439 {
3440 error_white_both(p, len);
3441 return FAIL;
3442 }
3443
3444 // get the second variable
3445 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3446 return FAIL;
3447
3448 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3449 return FAIL;
3450
3451 if (ppconst->pp_used == ppconst_used + 2)
3452 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003453 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003454 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3455 int ret;
3456
3457 // Both sides are a constant, compute the result now.
3458 // First check for a valid combination of types, this is more
3459 // strict than typval_compare().
3460 if (check_compare_types(type, tv1, tv2) == FAIL)
3461 ret = FAIL;
3462 else
3463 {
3464 ret = typval_compare(tv1, tv2, type, ic);
3465 tv1->v_type = VAR_BOOL;
3466 tv1->vval.v_number = tv1->vval.v_number
3467 ? VVAL_TRUE : VVAL_FALSE;
3468 clear_tv(tv2);
3469 --ppconst->pp_used;
3470 }
3471 return ret;
3472 }
3473
3474 generate_ppconst(cctx, ppconst);
3475 return generate_COMPARE(cctx, type, ic);
3476 }
3477
3478 return OK;
3479}
3480
3481static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3482
3483/*
3484 * Compile || or &&.
3485 */
3486 static int
3487compile_and_or(
3488 char_u **arg,
3489 cctx_T *cctx,
3490 char *op,
3491 ppconst_T *ppconst,
3492 int ppconst_used UNUSED)
3493{
3494 char_u *next;
3495 char_u *p = may_peek_next_line(cctx, *arg, &next);
3496 int opchar = *op;
3497
3498 if (p[0] == opchar && p[1] == opchar)
3499 {
3500 garray_T *instr = &cctx->ctx_instr;
3501 garray_T end_ga;
3502 int save_skip = cctx->ctx_skip;
3503
3504 /*
3505 * Repeat until there is no following "||" or "&&"
3506 */
3507 ga_init2(&end_ga, sizeof(int), 10);
3508 while (p[0] == opchar && p[1] == opchar)
3509 {
3510 long start_lnum = SOURCING_LNUM;
3511 long save_sourcing_lnum;
3512 int start_ctx_lnum = cctx->ctx_lnum;
3513 int save_lnum;
3514 int const_used;
3515 int status;
3516 jumpwhen_T jump_when = opchar == '|'
3517 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3518
3519 if (next != NULL)
3520 {
3521 *arg = next_line_from_context(cctx, TRUE);
3522 p = skipwhite(*arg);
3523 }
3524
3525 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3526 {
3527 semsg(_(e_white_space_required_before_and_after_str_at_str),
3528 op, p);
3529 ga_clear(&end_ga);
3530 return FAIL;
3531 }
3532
3533 save_sourcing_lnum = SOURCING_LNUM;
3534 SOURCING_LNUM = start_lnum;
3535 save_lnum = cctx->ctx_lnum;
3536 cctx->ctx_lnum = start_ctx_lnum;
3537
3538 status = check_ppconst_bool(ppconst);
3539 if (status != FAIL)
3540 {
3541 // Use the last ppconst if possible.
3542 if (ppconst->pp_used > 0)
3543 {
3544 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3545 int is_true = tv2bool(tv);
3546
3547 if ((is_true && opchar == '|')
3548 || (!is_true && opchar == '&'))
3549 {
3550 // For "false && expr" and "true || expr" the "expr"
3551 // does not need to be evaluated.
3552 cctx->ctx_skip = SKIP_YES;
3553 clear_tv(tv);
3554 tv->v_type = VAR_BOOL;
3555 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3556 }
3557 else
3558 {
3559 // For "true && expr" and "false || expr" only "expr"
3560 // needs to be evaluated.
3561 --ppconst->pp_used;
3562 jump_when = JUMP_NEVER;
3563 }
3564 }
3565 else
3566 {
3567 // Every part must evaluate to a bool.
3568 status = bool_on_stack(cctx);
3569 }
3570 }
3571 if (status != FAIL)
3572 status = ga_grow(&end_ga, 1);
3573 cctx->ctx_lnum = save_lnum;
3574 if (status == FAIL)
3575 {
3576 ga_clear(&end_ga);
3577 return FAIL;
3578 }
3579
3580 if (jump_when != JUMP_NEVER)
3581 {
3582 if (cctx->ctx_skip != SKIP_YES)
3583 {
3584 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3585 ++end_ga.ga_len;
3586 }
3587 generate_JUMP(cctx, jump_when, 0);
3588 }
3589
3590 // eval the next expression
3591 SOURCING_LNUM = save_sourcing_lnum;
3592 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3593 {
3594 ga_clear(&end_ga);
3595 return FAIL;
3596 }
3597
3598 const_used = ppconst->pp_used;
3599 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3600 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3601 {
3602 ga_clear(&end_ga);
3603 return FAIL;
3604 }
3605
3606 // "0 || 1" results in true, "1 && 0" results in false.
3607 if (ppconst->pp_used == const_used + 1)
3608 {
3609 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3610
3611 if (tv->v_type == VAR_NUMBER
3612 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3613 {
3614 tv->vval.v_number = tv->vval.v_number == 1
3615 ? VVAL_TRUE : VVAL_FALSE;
3616 tv->v_type = VAR_BOOL;
3617 }
3618 }
3619
3620 p = may_peek_next_line(cctx, *arg, &next);
3621 }
3622
3623 if (check_ppconst_bool(ppconst) == FAIL)
3624 {
3625 ga_clear(&end_ga);
3626 return FAIL;
3627 }
3628
3629 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3630 // Every part must evaluate to a bool.
3631 if (bool_on_stack(cctx) == FAIL)
3632 {
3633 ga_clear(&end_ga);
3634 return FAIL;
3635 }
3636
3637 if (end_ga.ga_len > 0)
3638 {
3639 // Fill in the end label in all jumps.
3640 generate_ppconst(cctx, ppconst);
3641 while (end_ga.ga_len > 0)
3642 {
3643 isn_T *isn;
3644
3645 --end_ga.ga_len;
3646 isn = ((isn_T *)instr->ga_data)
3647 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3648 isn->isn_arg.jump.jump_where = instr->ga_len;
3649 }
3650 }
3651 ga_clear(&end_ga);
3652
3653 cctx->ctx_skip = save_skip;
3654 }
3655
3656 return OK;
3657}
3658
3659/*
3660 * expr4a && expr4a && expr4a logical AND
3661 *
3662 * Produces instructions:
3663 * EVAL expr4a Push result of "expr4a"
3664 * COND2BOOL convert to bool if needed
3665 * JUMP_IF_COND_FALSE end
3666 * EVAL expr4b Push result of "expr4b"
3667 * JUMP_IF_COND_FALSE end
3668 * EVAL expr4c Push result of "expr4c"
3669 * end:
3670 */
3671 static int
3672compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3673{
3674 int ppconst_used = ppconst->pp_used;
3675
3676 // get the first variable
3677 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3678 return FAIL;
3679
3680 // || and && work almost the same
3681 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3682}
3683
3684/*
3685 * expr3a || expr3b || expr3c logical OR
3686 *
3687 * Produces instructions:
3688 * EVAL expr3a Push result of "expr3a"
3689 * COND2BOOL convert to bool if needed
3690 * JUMP_IF_COND_TRUE end
3691 * EVAL expr3b Push result of "expr3b"
3692 * JUMP_IF_COND_TRUE end
3693 * EVAL expr3c Push result of "expr3c"
3694 * end:
3695 */
3696 static int
3697compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3698{
3699 int ppconst_used = ppconst->pp_used;
3700
3701 // eval the first expression
3702 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3703 return FAIL;
3704
3705 // || and && work almost the same
3706 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3707}
3708
3709/*
3710 * Toplevel expression: expr2 ? expr1a : expr1b
3711 * Produces instructions:
3712 * EVAL expr2 Push result of "expr2"
3713 * JUMP_IF_FALSE alt jump if false
3714 * EVAL expr1a
3715 * JUMP_ALWAYS end
3716 * alt: EVAL expr1b
3717 * end:
3718 *
3719 * Toplevel expression: expr2 ?? expr1
3720 * Produces instructions:
3721 * EVAL expr2 Push result of "expr2"
3722 * JUMP_AND_KEEP_IF_TRUE end jump if true
3723 * EVAL expr1
3724 * end:
3725 */
3726 int
3727compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3728{
3729 char_u *p;
3730 int ppconst_used = ppconst->pp_used;
3731 char_u *next;
3732
3733 // Ignore all kinds of errors when not producing code.
3734 if (cctx->ctx_skip == SKIP_YES)
3735 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003736 int prev_did_emsg = did_emsg;
3737
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003738 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003739 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003740 }
3741
3742 // Evaluate the first expression.
3743 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3744 return FAIL;
3745
3746 p = may_peek_next_line(cctx, *arg, &next);
3747 if (*p == '?')
3748 {
3749 int op_falsy = p[1] == '?';
3750 garray_T *instr = &cctx->ctx_instr;
3751 garray_T *stack = &cctx->ctx_type_stack;
3752 int alt_idx = instr->ga_len;
3753 int end_idx = 0;
3754 isn_T *isn;
3755 type_T *type1 = NULL;
3756 int has_const_expr = FALSE;
3757 int const_value = FALSE;
3758 int save_skip = cctx->ctx_skip;
3759
3760 if (next != NULL)
3761 {
3762 *arg = next_line_from_context(cctx, TRUE);
3763 p = skipwhite(*arg);
3764 }
3765
3766 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3767 {
3768 semsg(_(e_white_space_required_before_and_after_str_at_str),
3769 op_falsy ? "??" : "?", p);
3770 return FAIL;
3771 }
3772
3773 if (ppconst->pp_used == ppconst_used + 1)
3774 {
3775 // the condition is a constant, we know whether the ? or the :
3776 // expression is to be evaluated.
3777 has_const_expr = TRUE;
3778 if (op_falsy)
3779 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3780 else
3781 {
3782 int error = FALSE;
3783
3784 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3785 &error);
3786 if (error)
3787 return FAIL;
3788 }
3789 cctx->ctx_skip = save_skip == SKIP_YES ||
3790 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3791
3792 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3793 // "left ?? right" and "left" is truthy: produce "left"
3794 generate_ppconst(cctx, ppconst);
3795 else
3796 {
3797 clear_tv(&ppconst->pp_tv[ppconst_used]);
3798 --ppconst->pp_used;
3799 }
3800 }
3801 else
3802 {
3803 generate_ppconst(cctx, ppconst);
3804 if (op_falsy)
3805 end_idx = instr->ga_len;
3806 generate_JUMP(cctx, op_falsy
3807 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3808 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003809 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003810 }
3811
3812 // evaluate the second expression; any type is accepted
3813 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3814 return FAIL;
3815 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3816 return FAIL;
3817
3818 if (!has_const_expr)
3819 {
3820 generate_ppconst(cctx, ppconst);
3821
3822 if (!op_falsy)
3823 {
3824 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003825 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003826 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003827
3828 end_idx = instr->ga_len;
3829 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3830
3831 // jump here from JUMP_IF_FALSE
3832 isn = ((isn_T *)instr->ga_data) + alt_idx;
3833 isn->isn_arg.jump.jump_where = instr->ga_len;
3834 }
3835 }
3836
3837 if (!op_falsy)
3838 {
3839 // Check for the ":".
3840 p = may_peek_next_line(cctx, *arg, &next);
3841 if (*p != ':')
3842 {
3843 emsg(_(e_missing_colon_after_questionmark));
3844 return FAIL;
3845 }
3846 if (next != NULL)
3847 {
3848 *arg = next_line_from_context(cctx, TRUE);
3849 p = skipwhite(*arg);
3850 }
3851
3852 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3853 {
3854 semsg(_(e_white_space_required_before_and_after_str_at_str),
3855 ":", p);
3856 return FAIL;
3857 }
3858
3859 // evaluate the third expression
3860 if (has_const_expr)
3861 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3862 ? SKIP_YES : SKIP_NOT;
3863 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3864 return FAIL;
3865 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3866 return FAIL;
3867 }
3868
3869 if (!has_const_expr)
3870 {
3871 type_T **typep;
3872
3873 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003874 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003875
3876 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003877 typep = &((((type2_T *)stack->ga_data)
3878 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003879 common_type(type1, *typep, typep, cctx->ctx_type_list);
3880
3881 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3882 isn = ((isn_T *)instr->ga_data) + end_idx;
3883 isn->isn_arg.jump.jump_where = instr->ga_len;
3884 }
3885
3886 cctx->ctx_skip = save_skip;
3887 }
3888 return OK;
3889}
3890
3891/*
3892 * Toplevel expression.
3893 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3894 * Returns OK or FAIL.
3895 */
3896 int
3897compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3898{
3899 ppconst_T ppconst;
3900
3901 CLEAR_FIELD(ppconst);
3902 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3903 {
3904 clear_ppconst(&ppconst);
3905 return FAIL;
3906 }
3907 if (is_const != NULL)
3908 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3909 if (generate_ppconst(cctx, &ppconst) == FAIL)
3910 return FAIL;
3911 return OK;
3912}
3913
3914/*
3915 * Toplevel expression.
3916 */
3917 int
3918compile_expr0(char_u **arg, cctx_T *cctx)
3919{
3920 return compile_expr0_ext(arg, cctx, NULL);
3921}
3922
3923
3924#endif // defined(FEAT_EVAL)