blob: 0ceff0b851a2c275445644f495172564c799ea17 [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
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002687 || (type->tt_type == VAR_OBJECT
2688 && type != &t_object_any)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002689 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002690 // class member: SomeClass.varname
2691 // class method: SomeClass.SomeMethod()
2692 // class constructor: SomeClass.new()
2693 // object member: someObject.varname, this.varname
2694 // object method: someObject.SomeMethod(), this.SomeMethod()
2695 *arg = p;
2696 if (compile_class_object_index(cctx, arg, type) == FAIL)
2697 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002698 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002699 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002700 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002701 *arg = p + 1;
2702 if (IS_WHITE_OR_NUL(**arg))
2703 {
2704 emsg(_(e_missing_name_after_dot));
2705 return FAIL;
2706 }
2707 p = *arg;
2708 if (eval_isdictc(*p))
2709 while (eval_isnamec(*p))
2710 MB_PTR_ADV(p);
2711 if (p == *arg)
2712 {
2713 semsg(_(e_syntax_error_at_str), *arg);
2714 return FAIL;
2715 }
2716 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2717 return FAIL;
2718 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2719 return FAIL;
2720 keeping_dict = TRUE;
2721 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002723 }
2724 else
2725 break;
2726 }
2727
2728 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2729 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002730 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2731 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002732 return FAIL;
2733
2734 return OK;
2735}
2736
2737/*
2738 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2739 * "arg" is advanced until after the expression, skipping white space.
2740 *
2741 * If the value is a constant "ppconst->pp_used" will be non-zero.
2742 * Before instructions are generated, any values in "ppconst" will generated.
2743 *
2744 * This is the compiling equivalent of eval1(), eval2(), etc.
2745 */
2746
2747/*
2748 * number number constant
2749 * 0zFFFFFFFF Blob constant
2750 * "string" string constant
2751 * 'string' literal string constant
2752 * &option-name option value
2753 * @r register contents
2754 * identifier variable value
2755 * function() function call
2756 * $VAR environment variable
2757 * (expression) nested expression
2758 * [expr, expr] List
2759 * {key: val, [key]: val} Dictionary
2760 *
2761 * Also handle:
2762 * ! in front logical NOT
2763 * - in front unary minus
2764 * + in front unary plus (ignored)
2765 * trailing (arg) funcref/partial call
2766 * trailing [] subscript in String or List
2767 * trailing .name entry in Dictionary
2768 * trailing ->name() method call
2769 */
2770 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002771compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002772 char_u **arg,
2773 cctx_T *cctx,
2774 ppconst_T *ppconst)
2775{
2776 char_u *start_leader, *end_leader;
2777 int ret = OK;
2778 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2779 int used_before = ppconst->pp_used;
2780
2781 ppconst->pp_is_const = FALSE;
2782
2783 /*
2784 * Skip '!', '-' and '+' characters. They are handled later.
2785 */
2786 start_leader = *arg;
2787 if (eval_leader(arg, TRUE) == FAIL)
2788 return FAIL;
2789 end_leader = *arg;
2790
2791 rettv->v_type = VAR_UNKNOWN;
2792 switch (**arg)
2793 {
2794 /*
2795 * Number constant.
2796 */
2797 case '0': // also for blob starting with 0z
2798 case '1':
2799 case '2':
2800 case '3':
2801 case '4':
2802 case '5':
2803 case '6':
2804 case '7':
2805 case '8':
2806 case '9':
2807 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2808 return FAIL;
2809 // Apply "-" and "+" just before the number now, right to
2810 // left. Matters especially when "->" follows. Stops at
2811 // '!'.
2812 if (apply_leader(rettv, TRUE,
2813 start_leader, &end_leader) == FAIL)
2814 {
2815 clear_tv(rettv);
2816 return FAIL;
2817 }
2818 break;
2819
2820 /*
2821 * String constant: "string".
2822 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002823 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002824 return FAIL;
2825 break;
2826
2827 /*
2828 * Literal string constant: 'str''ing'.
2829 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002830 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002831 return FAIL;
2832 break;
2833
2834 /*
2835 * Constant Vim variable.
2836 */
2837 case 'v': get_vim_constant(arg, rettv);
2838 ret = NOTDONE;
2839 break;
2840
2841 /*
2842 * "true" constant
2843 */
2844 case 't': if (STRNCMP(*arg, "true", 4) == 0
2845 && !eval_isnamec((*arg)[4]))
2846 {
2847 *arg += 4;
2848 rettv->v_type = VAR_BOOL;
2849 rettv->vval.v_number = VVAL_TRUE;
2850 }
2851 else
2852 ret = NOTDONE;
2853 break;
2854
2855 /*
2856 * "false" constant
2857 */
2858 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2859 && !eval_isnamec((*arg)[5]))
2860 {
2861 *arg += 5;
2862 rettv->v_type = VAR_BOOL;
2863 rettv->vval.v_number = VVAL_FALSE;
2864 }
2865 else
2866 ret = NOTDONE;
2867 break;
2868
2869 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002870 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002871 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002872 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002873 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002874 char_u *p = *arg + 4;
2875 int len;
2876
2877 for (len = 0; eval_isnamec(p[len]); ++len)
2878 ;
2879 ret = handle_predefined(*arg, len + 4, rettv);
2880 if (ret == FAIL)
2881 ret = NOTDONE;
2882 else
2883 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002884 }
2885 else
2886 ret = NOTDONE;
2887 break;
2888
2889 /*
2890 * List: [expr, expr]
2891 */
2892 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2893 return FAIL;
2894 ret = compile_list(arg, cctx, ppconst);
2895 break;
2896
2897 /*
2898 * Dictionary: {'key': val, 'key': val}
2899 */
2900 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2901 return FAIL;
2902 ret = compile_dict(arg, cctx, ppconst);
2903 break;
2904
2905 /*
2906 * Option value: &name
2907 */
2908 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2909 return FAIL;
2910 ret = compile_get_option(arg, cctx);
2911 break;
2912
2913 /*
2914 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002915 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002916 */
2917 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2918 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002919 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2920 ret = compile_interp_string(arg, cctx);
2921 else
2922 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002923 break;
2924
2925 /*
2926 * Register contents: @r.
2927 */
2928 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2929 return FAIL;
2930 ret = compile_get_register(arg, cctx);
2931 break;
2932 /*
2933 * nested expression: (expression).
2934 * lambda: (arg, arg) => expr
2935 * funcref: (arg, arg) => { statement }
2936 */
2937 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2938 ret = compile_lambda(arg, cctx);
2939 if (ret == NOTDONE)
2940 ret = compile_parenthesis(arg, cctx, ppconst);
2941 break;
2942
2943 default: ret = NOTDONE;
2944 break;
2945 }
2946 if (ret == FAIL)
2947 return FAIL;
2948
2949 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2950 {
2951 if (cctx->ctx_skip == SKIP_YES)
2952 clear_tv(rettv);
2953 else
2954 // A constant expression can possibly be handled compile time,
2955 // return the value instead of generating code.
2956 ++ppconst->pp_used;
2957 }
2958 else if (ret == NOTDONE)
2959 {
2960 char_u *p;
2961 int r;
2962
2963 if (!eval_isnamec1(**arg))
2964 {
2965 if (!vim9_bad_comment(*arg))
2966 {
2967 if (ends_excmd(*skipwhite(*arg)))
2968 semsg(_(e_empty_expression_str), *arg);
2969 else
2970 semsg(_(e_name_expected_str), *arg);
2971 }
2972 return FAIL;
2973 }
2974
2975 // "name" or "name()"
2976 p = to_name_end(*arg, TRUE);
2977 if (p - *arg == (size_t)1 && **arg == '_')
2978 {
2979 emsg(_(e_cannot_use_underscore_here));
2980 return FAIL;
2981 }
2982
2983 if (*p == '(')
2984 {
2985 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2986 }
2987 else
2988 {
2989 if (cctx->ctx_skip != SKIP_YES
2990 && generate_ppconst(cctx, ppconst) == FAIL)
2991 return FAIL;
2992 r = compile_load(arg, p, cctx, TRUE, TRUE);
2993 }
2994 if (r == FAIL)
2995 return FAIL;
2996 }
2997
2998 // Handle following "[]", ".member", etc.
2999 // Then deal with prefixed '-', '+' and '!', if not done already.
3000 if (compile_subscript(arg, cctx, start_leader, &end_leader,
3001 ppconst) == FAIL)
3002 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01003003 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003004 {
3005 // apply the '!', '-' and '+' before the constant
3006 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3007 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
3008 return FAIL;
3009 return OK;
3010 }
3011 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
3012 return FAIL;
3013 return OK;
3014}
3015
3016/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003017 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003018 */
3019 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003020compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003021{
3022 type_T *want_type = NULL;
3023
3024 // Recognize <type>
3025 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3026 {
3027 ++*arg;
3028 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
3029 if (want_type == NULL)
3030 return FAIL;
3031
3032 if (**arg != '>')
3033 {
3034 if (*skipwhite(*arg) == '>')
3035 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3036 else
3037 emsg(_(e_missing_gt));
3038 return FAIL;
3039 }
3040 ++*arg;
3041 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3042 return FAIL;
3043 }
3044
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003045 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003046 return FAIL;
3047
3048 if (want_type != NULL)
3049 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003050 type_T *actual;
3051 where_T where = WHERE_INIT;
3052
LemonBoy50d48542024-07-04 17:03:17 +02003053 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003054 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00003055 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00003056 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003057 {
LemonBoy50d48542024-07-04 17:03:17 +02003058 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
3059 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003060 return FAIL;
3061 }
3062 }
3063
3064 return OK;
3065}
3066
3067/*
3068 * * number multiplication
3069 * / number division
3070 * % number modulo
3071 */
3072 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003073compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003074{
3075 char_u *op;
3076 char_u *next;
3077 int ppconst_used = ppconst->pp_used;
3078
3079 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003080 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003081 return FAIL;
3082
3083 /*
3084 * Repeat computing, until no "*", "/" or "%" is following.
3085 */
3086 for (;;)
3087 {
3088 op = may_peek_next_line(cctx, *arg, &next);
3089 if (*op != '*' && *op != '/' && *op != '%')
3090 break;
3091 if (next != NULL)
3092 {
3093 *arg = next_line_from_context(cctx, TRUE);
3094 op = skipwhite(*arg);
3095 }
3096
3097 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
3098 {
3099 error_white_both(op, 1);
3100 return FAIL;
3101 }
3102 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
3103 return FAIL;
3104
3105 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003106 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003107 return FAIL;
3108
3109 if (ppconst->pp_used == ppconst_used + 2
3110 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3111 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
3112 {
3113 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3114 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3115 varnumber_T res = 0;
3116 int failed = FALSE;
3117
3118 // both are numbers: compute the result
3119 switch (*op)
3120 {
3121 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
3122 break;
3123 case '/': res = num_divide(tv1->vval.v_number,
3124 tv2->vval.v_number, &failed);
3125 break;
3126 case '%': res = num_modulus(tv1->vval.v_number,
3127 tv2->vval.v_number, &failed);
3128 break;
3129 }
3130 if (failed)
3131 return FAIL;
3132 tv1->vval.v_number = res;
3133 --ppconst->pp_used;
3134 }
3135 else
3136 {
3137 generate_ppconst(cctx, ppconst);
3138 generate_two_op(cctx, op);
3139 }
3140 }
3141
3142 return OK;
3143}
3144
3145/*
3146 * + number addition or list/blobl concatenation
3147 * - number subtraction
3148 * .. string concatenation
3149 */
3150 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003151compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003152{
3153 char_u *op;
3154 char_u *next;
3155 int oplen;
3156 int ppconst_used = ppconst->pp_used;
3157
3158 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003159 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003160 return FAIL;
3161
3162 /*
3163 * Repeat computing, until no "+", "-" or ".." is following.
3164 */
3165 for (;;)
3166 {
3167 op = may_peek_next_line(cctx, *arg, &next);
3168 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
3169 break;
3170 if (op[0] == op[1] && *op != '.' && next)
3171 // Finding "++" or "--" on the next line is a separate command.
3172 // But ".." is concatenation.
3173 break;
3174 oplen = (*op == '.' ? 2 : 1);
3175 if (next != NULL)
3176 {
3177 *arg = next_line_from_context(cctx, TRUE);
3178 op = skipwhite(*arg);
3179 }
3180
3181 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3182 {
3183 error_white_both(op, oplen);
3184 return FAIL;
3185 }
3186
3187 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3188 return FAIL;
3189
3190 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003191 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003192 return FAIL;
3193
3194 if (ppconst->pp_used == ppconst_used + 2
3195 && (*op == '.'
3196 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3197 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3198 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3199 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3200 {
3201 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3202 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3203
3204 // concat/subtract/add constant numbers
3205 if (*op == '+')
3206 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3207 else if (*op == '-')
3208 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3209 else
3210 {
3211 // concatenate constant strings
3212 char_u *s1 = tv1->vval.v_string;
3213 char_u *s2 = tv2->vval.v_string;
3214 size_t len1 = STRLEN(s1);
3215
3216 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3217 if (tv1->vval.v_string == NULL)
3218 {
3219 clear_ppconst(ppconst);
3220 return FAIL;
3221 }
3222 mch_memmove(tv1->vval.v_string, s1, len1);
3223 STRCPY(tv1->vval.v_string + len1, s2);
3224 vim_free(s1);
3225 vim_free(s2);
3226 }
3227 --ppconst->pp_used;
3228 }
3229 else
3230 {
3231 generate_ppconst(cctx, ppconst);
3232 ppconst->pp_is_const = FALSE;
3233 if (*op == '.')
3234 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003235 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3236 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003237 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003238 if (generate_CONCAT(cctx, 2) == FAIL)
3239 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003240 }
3241 else
3242 generate_two_op(cctx, op);
3243 }
3244 }
3245
3246 return OK;
3247}
3248
3249/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003250 * expr6a >> expr6b
3251 * expr6a << expr6b
3252 *
3253 * Produces instructions:
3254 * OPNR bitwise left or right shift
3255 */
3256 static int
3257compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3258{
3259 exprtype_T type = EXPR_UNKNOWN;
3260 char_u *p;
3261 char_u *next;
3262 int len = 2;
3263 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003264 isn_T *isn;
3265
3266 // get the first variable
3267 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3268 return FAIL;
3269
3270 /*
3271 * Repeat computing, until no "+", "-" or ".." is following.
3272 */
3273 for (;;)
3274 {
3275 type = EXPR_UNKNOWN;
3276
3277 p = may_peek_next_line(cctx, *arg, &next);
3278 if (p[0] == '<' && p[1] == '<')
3279 type = EXPR_LSHIFT;
3280 else if (p[0] == '>' && p[1] == '>')
3281 type = EXPR_RSHIFT;
3282
3283 if (type == EXPR_UNKNOWN)
3284 return OK;
3285
3286 // Handle a bitwise left or right shift operator
3287 if (ppconst->pp_used == ppconst_used + 1)
3288 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003289 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003290 {
3291 // left operand should be a number
3292 emsg(_(e_bitshift_ops_must_be_number));
3293 return FAIL;
3294 }
3295 }
3296 else
3297 {
3298 type_T *t = get_type_on_stack(cctx, 0);
3299
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003300 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003301 {
3302 emsg(_(e_bitshift_ops_must_be_number));
3303 return FAIL;
3304 }
3305 }
3306
3307 if (next != NULL)
3308 {
3309 *arg = next_line_from_context(cctx, TRUE);
3310 p = skipwhite(*arg);
3311 }
3312
3313 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3314 {
3315 error_white_both(p, len);
3316 return FAIL;
3317 }
3318
3319 // get the second variable
3320 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3321 return FAIL;
3322
3323 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3324 return FAIL;
3325
3326 if (ppconst->pp_used == ppconst_used + 2)
3327 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003328 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3329 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3330
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003331 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003332 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3333 {
3334 // right operand should be a positive number
3335 if (tv2->v_type != VAR_NUMBER)
3336 emsg(_(e_bitshift_ops_must_be_number));
3337 else
dundargocc57b5bc2022-11-02 13:30:51 +00003338 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003339 return FAIL;
3340 }
3341
3342 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3343 tv1->vval.v_number = 0;
3344 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003345 tv1->vval.v_number =
3346 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003347 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003348 tv1->vval.v_number =
3349 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003350 clear_tv(tv2);
3351 --ppconst->pp_used;
3352 }
3353 else
3354 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003355 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3356 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003357 {
3358 emsg(_(e_bitshift_ops_must_be_number));
3359 return FAIL;
3360 }
3361
3362 generate_ppconst(cctx, ppconst);
3363
3364 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3365 if (isn == NULL)
3366 return FAIL;
3367
3368 if (isn != NULL)
3369 isn->isn_arg.op.op_type = type;
3370 }
3371 }
3372
3373 return OK;
3374}
3375
3376/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003377 * expr5a == expr5b
3378 * expr5a =~ expr5b
3379 * expr5a != expr5b
3380 * expr5a !~ expr5b
3381 * expr5a > expr5b
3382 * expr5a >= expr5b
3383 * expr5a < expr5b
3384 * expr5a <= expr5b
3385 * expr5a is expr5b
3386 * expr5a isnot expr5b
3387 *
3388 * Produces instructions:
3389 * EVAL expr5a Push result of "expr5a"
3390 * EVAL expr5b Push result of "expr5b"
3391 * COMPARE one of the compare instructions
3392 */
3393 static int
3394compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3395{
3396 exprtype_T type = EXPR_UNKNOWN;
3397 char_u *p;
3398 char_u *next;
3399 int len = 2;
3400 int type_is = FALSE;
3401 int ppconst_used = ppconst->pp_used;
3402
3403 // get the first variable
3404 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3405 return FAIL;
3406
3407 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003408
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003409 type = get_compare_type(p, &len, &type_is);
3410
3411 /*
3412 * If there is a comparative operator, use it.
3413 */
3414 if (type != EXPR_UNKNOWN)
3415 {
3416 int ic = FALSE; // Default: do not ignore case
3417
3418 if (next != NULL)
3419 {
3420 *arg = next_line_from_context(cctx, TRUE);
3421 p = skipwhite(*arg);
3422 }
3423 if (type_is && (p[len] == '?' || p[len] == '#'))
3424 {
3425 semsg(_(e_invalid_expression_str), *arg);
3426 return FAIL;
3427 }
3428 // extra question mark appended: ignore case
3429 if (p[len] == '?')
3430 {
3431 ic = TRUE;
3432 ++len;
3433 }
3434 // extra '#' appended: match case (ignored)
3435 else if (p[len] == '#')
3436 ++len;
3437 // nothing appended: match case
3438
3439 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3440 {
3441 error_white_both(p, len);
3442 return FAIL;
3443 }
3444
3445 // get the second variable
3446 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3447 return FAIL;
3448
3449 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3450 return FAIL;
3451
3452 if (ppconst->pp_used == ppconst_used + 2)
3453 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003454 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003455 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3456 int ret;
3457
3458 // Both sides are a constant, compute the result now.
3459 // First check for a valid combination of types, this is more
3460 // strict than typval_compare().
3461 if (check_compare_types(type, tv1, tv2) == FAIL)
3462 ret = FAIL;
3463 else
3464 {
3465 ret = typval_compare(tv1, tv2, type, ic);
3466 tv1->v_type = VAR_BOOL;
3467 tv1->vval.v_number = tv1->vval.v_number
3468 ? VVAL_TRUE : VVAL_FALSE;
3469 clear_tv(tv2);
3470 --ppconst->pp_used;
3471 }
3472 return ret;
3473 }
3474
3475 generate_ppconst(cctx, ppconst);
3476 return generate_COMPARE(cctx, type, ic);
3477 }
3478
3479 return OK;
3480}
3481
3482static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3483
3484/*
3485 * Compile || or &&.
3486 */
3487 static int
3488compile_and_or(
3489 char_u **arg,
3490 cctx_T *cctx,
3491 char *op,
3492 ppconst_T *ppconst,
3493 int ppconst_used UNUSED)
3494{
3495 char_u *next;
3496 char_u *p = may_peek_next_line(cctx, *arg, &next);
3497 int opchar = *op;
3498
3499 if (p[0] == opchar && p[1] == opchar)
3500 {
3501 garray_T *instr = &cctx->ctx_instr;
3502 garray_T end_ga;
3503 int save_skip = cctx->ctx_skip;
3504
3505 /*
3506 * Repeat until there is no following "||" or "&&"
3507 */
3508 ga_init2(&end_ga, sizeof(int), 10);
3509 while (p[0] == opchar && p[1] == opchar)
3510 {
3511 long start_lnum = SOURCING_LNUM;
3512 long save_sourcing_lnum;
3513 int start_ctx_lnum = cctx->ctx_lnum;
3514 int save_lnum;
3515 int const_used;
3516 int status;
3517 jumpwhen_T jump_when = opchar == '|'
3518 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3519
3520 if (next != NULL)
3521 {
3522 *arg = next_line_from_context(cctx, TRUE);
3523 p = skipwhite(*arg);
3524 }
3525
3526 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3527 {
3528 semsg(_(e_white_space_required_before_and_after_str_at_str),
3529 op, p);
3530 ga_clear(&end_ga);
3531 return FAIL;
3532 }
3533
3534 save_sourcing_lnum = SOURCING_LNUM;
3535 SOURCING_LNUM = start_lnum;
3536 save_lnum = cctx->ctx_lnum;
3537 cctx->ctx_lnum = start_ctx_lnum;
3538
3539 status = check_ppconst_bool(ppconst);
3540 if (status != FAIL)
3541 {
3542 // Use the last ppconst if possible.
3543 if (ppconst->pp_used > 0)
3544 {
3545 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3546 int is_true = tv2bool(tv);
3547
3548 if ((is_true && opchar == '|')
3549 || (!is_true && opchar == '&'))
3550 {
3551 // For "false && expr" and "true || expr" the "expr"
3552 // does not need to be evaluated.
3553 cctx->ctx_skip = SKIP_YES;
3554 clear_tv(tv);
3555 tv->v_type = VAR_BOOL;
3556 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3557 }
3558 else
3559 {
3560 // For "true && expr" and "false || expr" only "expr"
3561 // needs to be evaluated.
3562 --ppconst->pp_used;
3563 jump_when = JUMP_NEVER;
3564 }
3565 }
3566 else
3567 {
3568 // Every part must evaluate to a bool.
3569 status = bool_on_stack(cctx);
3570 }
3571 }
3572 if (status != FAIL)
3573 status = ga_grow(&end_ga, 1);
3574 cctx->ctx_lnum = save_lnum;
3575 if (status == FAIL)
3576 {
3577 ga_clear(&end_ga);
3578 return FAIL;
3579 }
3580
3581 if (jump_when != JUMP_NEVER)
3582 {
3583 if (cctx->ctx_skip != SKIP_YES)
3584 {
3585 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3586 ++end_ga.ga_len;
3587 }
3588 generate_JUMP(cctx, jump_when, 0);
3589 }
3590
3591 // eval the next expression
3592 SOURCING_LNUM = save_sourcing_lnum;
3593 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3594 {
3595 ga_clear(&end_ga);
3596 return FAIL;
3597 }
3598
3599 const_used = ppconst->pp_used;
3600 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3601 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3602 {
3603 ga_clear(&end_ga);
3604 return FAIL;
3605 }
3606
3607 // "0 || 1" results in true, "1 && 0" results in false.
3608 if (ppconst->pp_used == const_used + 1)
3609 {
3610 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3611
3612 if (tv->v_type == VAR_NUMBER
3613 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3614 {
3615 tv->vval.v_number = tv->vval.v_number == 1
3616 ? VVAL_TRUE : VVAL_FALSE;
3617 tv->v_type = VAR_BOOL;
3618 }
3619 }
3620
3621 p = may_peek_next_line(cctx, *arg, &next);
3622 }
3623
3624 if (check_ppconst_bool(ppconst) == FAIL)
3625 {
3626 ga_clear(&end_ga);
3627 return FAIL;
3628 }
3629
3630 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3631 // Every part must evaluate to a bool.
3632 if (bool_on_stack(cctx) == FAIL)
3633 {
3634 ga_clear(&end_ga);
3635 return FAIL;
3636 }
3637
3638 if (end_ga.ga_len > 0)
3639 {
3640 // Fill in the end label in all jumps.
3641 generate_ppconst(cctx, ppconst);
3642 while (end_ga.ga_len > 0)
3643 {
3644 isn_T *isn;
3645
3646 --end_ga.ga_len;
3647 isn = ((isn_T *)instr->ga_data)
3648 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3649 isn->isn_arg.jump.jump_where = instr->ga_len;
3650 }
3651 }
3652 ga_clear(&end_ga);
3653
3654 cctx->ctx_skip = save_skip;
3655 }
3656
3657 return OK;
3658}
3659
3660/*
3661 * expr4a && expr4a && expr4a logical AND
3662 *
3663 * Produces instructions:
3664 * EVAL expr4a Push result of "expr4a"
3665 * COND2BOOL convert to bool if needed
3666 * JUMP_IF_COND_FALSE end
3667 * EVAL expr4b Push result of "expr4b"
3668 * JUMP_IF_COND_FALSE end
3669 * EVAL expr4c Push result of "expr4c"
3670 * end:
3671 */
3672 static int
3673compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3674{
3675 int ppconst_used = ppconst->pp_used;
3676
3677 // get the first variable
3678 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3679 return FAIL;
3680
3681 // || and && work almost the same
3682 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3683}
3684
3685/*
3686 * expr3a || expr3b || expr3c logical OR
3687 *
3688 * Produces instructions:
3689 * EVAL expr3a Push result of "expr3a"
3690 * COND2BOOL convert to bool if needed
3691 * JUMP_IF_COND_TRUE end
3692 * EVAL expr3b Push result of "expr3b"
3693 * JUMP_IF_COND_TRUE end
3694 * EVAL expr3c Push result of "expr3c"
3695 * end:
3696 */
3697 static int
3698compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3699{
3700 int ppconst_used = ppconst->pp_used;
3701
3702 // eval the first expression
3703 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3704 return FAIL;
3705
3706 // || and && work almost the same
3707 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3708}
3709
3710/*
3711 * Toplevel expression: expr2 ? expr1a : expr1b
3712 * Produces instructions:
3713 * EVAL expr2 Push result of "expr2"
3714 * JUMP_IF_FALSE alt jump if false
3715 * EVAL expr1a
3716 * JUMP_ALWAYS end
3717 * alt: EVAL expr1b
3718 * end:
3719 *
3720 * Toplevel expression: expr2 ?? expr1
3721 * Produces instructions:
3722 * EVAL expr2 Push result of "expr2"
3723 * JUMP_AND_KEEP_IF_TRUE end jump if true
3724 * EVAL expr1
3725 * end:
3726 */
3727 int
3728compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3729{
3730 char_u *p;
3731 int ppconst_used = ppconst->pp_used;
3732 char_u *next;
3733
3734 // Ignore all kinds of errors when not producing code.
3735 if (cctx->ctx_skip == SKIP_YES)
3736 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003737 int prev_did_emsg = did_emsg;
3738
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003739 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003740 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003741 }
3742
3743 // Evaluate the first expression.
3744 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3745 return FAIL;
3746
3747 p = may_peek_next_line(cctx, *arg, &next);
3748 if (*p == '?')
3749 {
3750 int op_falsy = p[1] == '?';
3751 garray_T *instr = &cctx->ctx_instr;
3752 garray_T *stack = &cctx->ctx_type_stack;
3753 int alt_idx = instr->ga_len;
3754 int end_idx = 0;
3755 isn_T *isn;
3756 type_T *type1 = NULL;
3757 int has_const_expr = FALSE;
3758 int const_value = FALSE;
3759 int save_skip = cctx->ctx_skip;
3760
3761 if (next != NULL)
3762 {
3763 *arg = next_line_from_context(cctx, TRUE);
3764 p = skipwhite(*arg);
3765 }
3766
3767 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3768 {
3769 semsg(_(e_white_space_required_before_and_after_str_at_str),
3770 op_falsy ? "??" : "?", p);
3771 return FAIL;
3772 }
3773
3774 if (ppconst->pp_used == ppconst_used + 1)
3775 {
3776 // the condition is a constant, we know whether the ? or the :
3777 // expression is to be evaluated.
3778 has_const_expr = TRUE;
3779 if (op_falsy)
3780 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3781 else
3782 {
3783 int error = FALSE;
3784
3785 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3786 &error);
3787 if (error)
3788 return FAIL;
3789 }
3790 cctx->ctx_skip = save_skip == SKIP_YES ||
3791 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3792
3793 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3794 // "left ?? right" and "left" is truthy: produce "left"
3795 generate_ppconst(cctx, ppconst);
3796 else
3797 {
3798 clear_tv(&ppconst->pp_tv[ppconst_used]);
3799 --ppconst->pp_used;
3800 }
3801 }
3802 else
3803 {
3804 generate_ppconst(cctx, ppconst);
3805 if (op_falsy)
3806 end_idx = instr->ga_len;
3807 generate_JUMP(cctx, op_falsy
3808 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3809 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003810 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003811 }
3812
3813 // evaluate the second expression; any type is accepted
3814 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3815 return FAIL;
3816 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3817 return FAIL;
3818
3819 if (!has_const_expr)
3820 {
3821 generate_ppconst(cctx, ppconst);
3822
3823 if (!op_falsy)
3824 {
3825 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003826 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003827 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003828
3829 end_idx = instr->ga_len;
3830 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3831
3832 // jump here from JUMP_IF_FALSE
3833 isn = ((isn_T *)instr->ga_data) + alt_idx;
3834 isn->isn_arg.jump.jump_where = instr->ga_len;
3835 }
3836 }
3837
3838 if (!op_falsy)
3839 {
3840 // Check for the ":".
3841 p = may_peek_next_line(cctx, *arg, &next);
3842 if (*p != ':')
3843 {
3844 emsg(_(e_missing_colon_after_questionmark));
3845 return FAIL;
3846 }
3847 if (next != NULL)
3848 {
3849 *arg = next_line_from_context(cctx, TRUE);
3850 p = skipwhite(*arg);
3851 }
3852
3853 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3854 {
3855 semsg(_(e_white_space_required_before_and_after_str_at_str),
3856 ":", p);
3857 return FAIL;
3858 }
3859
3860 // evaluate the third expression
3861 if (has_const_expr)
3862 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3863 ? SKIP_YES : SKIP_NOT;
3864 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3865 return FAIL;
3866 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3867 return FAIL;
3868 }
3869
3870 if (!has_const_expr)
3871 {
3872 type_T **typep;
3873
3874 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003875 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003876
3877 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003878 typep = &((((type2_T *)stack->ga_data)
3879 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003880 common_type(type1, *typep, typep, cctx->ctx_type_list);
3881
3882 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3883 isn = ((isn_T *)instr->ga_data) + end_idx;
3884 isn->isn_arg.jump.jump_where = instr->ga_len;
3885 }
3886
3887 cctx->ctx_skip = save_skip;
3888 }
3889 return OK;
3890}
3891
3892/*
3893 * Toplevel expression.
3894 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3895 * Returns OK or FAIL.
3896 */
3897 int
3898compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3899{
3900 ppconst_T ppconst;
3901
3902 CLEAR_FIELD(ppconst);
3903 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3904 {
3905 clear_ppconst(&ppconst);
3906 return FAIL;
3907 }
3908 if (is_const != NULL)
3909 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3910 if (generate_ppconst(cctx, &ppconst) == FAIL)
3911 return FAIL;
3912 return OK;
3913}
3914
3915/*
3916 * Toplevel expression.
3917 */
3918 int
3919compile_expr0(char_u **arg, cctx_T *cctx)
3920{
3921 return compile_expr0_ext(arg, cctx, NULL);
3922}
3923
3924
3925#endif // defined(FEAT_EVAL)