blob: 40c5fcc721d4a8e13aee786b6392992c0bcdbc10 [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)
Yegappan Lakshmanan5ce1e4a2025-04-07 21:09:18 +0200407 {
408 emsg(_(e_missing_name_after_dot));
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000409 return FAIL;
Yegappan Lakshmanan5ce1e4a2025-04-07 21:09:18 +0200410 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000411 size_t len = name_end - name;
412
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000413 if (*name_end == '(')
414 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000415 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000416 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000417 ufunc_T **functions;
418
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000419 if (type->tt_type == VAR_CLASS)
420 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000421 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000422 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000423 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000424 }
425 else
426 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000427 // type->tt_type == VAR_OBJECT: method call
Ernie Rael58c95792024-08-13 23:27:22 +0200428 // When compiling Func and doing "super.SomeFunc()", must be in the
429 // class context that defines Func.
430 if (is_super)
431 cl = cctx->ctx_ufunc->uf_defclass;
432
Bram Moolenaar574950d2023-01-03 19:08:50 +0000433 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000434 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000435 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000436 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000437
438 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000439 int fi;
440 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000441 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000442 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000443 // Use a separate pointer to avoid that ASAN complains about
444 // uf_name[] only being 4 characters.
445 char_u *ufname = (char_u *)fp->uf_name;
446 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
447 {
448 ufunc = fp;
449 break;
450 }
451 }
Ernie Raelbce60c42025-01-19 10:03:00 +0100452
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200453 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000454 if (ufunc == NULL)
455 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200456 // could be a funcref in a member variable
457 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
458 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
459 {
460 method_not_found_msg(cl, type->tt_type, name, len);
461 return FAIL;
462 }
463 if (type->tt_type == VAR_CLASS)
464 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200465 // Remove the class type from the stack
466 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200467 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
468 return FAIL;
469 }
470 else
471 {
Yegappan Lakshmananc10342d2025-01-11 09:39:01 +0100472 int status;
473
474 if (IS_INTERFACE(cl))
475 status = generate_GET_ITF_MEMBER(cctx, cl, m_idx,
476 ocm->ocm_type);
477 else
478 status = generate_GET_OBJ_MEMBER(cctx, m_idx,
479 ocm->ocm_type);
480 if (status == FAIL)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200481 return FAIL;
482 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000483 }
484
Yegappan Lakshmanancb848b62025-01-20 21:38:09 +0100485 if (is_super && ufunc != NULL && IS_ABSTRACT_METHOD(ufunc))
Ernie Raelbce60c42025-01-19 10:03:00 +0100486 {
487 // Trying to invoke an abstract method in a super class is not
488 // allowed.
489 semsg(_(e_abstract_method_str_direct), ufunc->uf_name,
490 ufunc->uf_defclass->class_name);
491 return FAIL;
492 }
493
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200494 // A private object method can be used only inside the class where it
495 // is defined or in one of the child classes.
496 // A private class method can be used only in the class where it is
497 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200498 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200499 ((type->tt_type == VAR_OBJECT
500 && !inside_class_hierarchy(cctx, cl))
501 || (type->tt_type == VAR_CLASS
502 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200503 {
Ernie Rael03042a22023-11-11 08:53:32 +0100504 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200505 return FAIL;
506 }
507
Bram Moolenaar574950d2023-01-03 19:08:50 +0000508 // Compile the arguments and call the class function or object method.
509 // The object method will know that the object is on the stack, just
510 // before the arguments.
511 *arg = skipwhite(name_end + 1);
512 int argcount = 0;
513 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
514 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000515
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200516 if (ocm != NULL)
517 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000518 if (type->tt_type == VAR_OBJECT
519 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
Ernie Rael58c95792024-08-13 23:27:22 +0200520 return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super);
521 return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000522 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000523
524 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000525 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200526 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200527 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000528 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200529 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000530 {
Ernie Rael03042a22023-11-11 08:53:32 +0100531 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200532 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200533 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200534 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200535
536 *arg = name_end;
537 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200538 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
539 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200540 }
541
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200542 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200543 m_idx = object_method_idx(cl, name, len);
544 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000545 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200546 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100547 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200548 if (*name == '_' && !inside_class(cctx, cl))
549 {
Ernie Rael03042a22023-11-11 08:53:32 +0100550 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200551 return FAIL;
552 }
553 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200554 // Remove the object type from the stack
555 --cctx->ctx_type_stack.ga_len;
556 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000557 }
558
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200559 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000560 }
561 else
562 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000563 // load class member
564 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200565 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
566 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000567 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200568 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200569 // A private class variable can be accessed only in the class where
570 // it is defined.
571 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200572 {
Ernie Rael03042a22023-11-11 08:53:32 +0100573 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200574 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200575 return FAIL;
576 }
577
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000578 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200579 // Remove the class type from the stack
580 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000581 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
582 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200583
584 // Could be a class method reference: "class.Func".
585 m_idx = class_method_idx(cl, name, len);
586 if (m_idx >= 0)
587 {
588 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100589 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200590 if (*name == '_' && !inside_class(cctx, cl))
591 {
Ernie Rael03042a22023-11-11 08:53:32 +0100592 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200593 return FAIL;
594 }
595 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200596 // Remove the class type from the stack
597 --cctx->ctx_type_stack.ga_len;
598 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200599 }
600
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200601 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000602 }
603
604 return FAIL;
605}
606
607/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000608 * Generate an instruction to load script-local variable "name", without the
609 * leading "s:".
610 * Also finds imported variables.
611 */
612 int
613compile_load_scriptvar(
614 cctx_T *cctx,
615 char_u *name, // variable NUL terminated
616 char_u *start, // start of variable
Hirohito Higashi6c012832025-02-25 20:29:50 +0100617 char_u **end, // end of variable, may be NULL
618 imported_T *import) // found imported item, can be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000619{
620 scriptitem_T *si;
621 int idx;
Hirohito Higashi6c012832025-02-25 20:29:50 +0100622 imported_T *imp;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000623
624 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
625 return FAIL;
626 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000627 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000628 if (idx >= 0)
629 {
630 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
631
632 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
633 current_sctx.sc_sid, idx, sv->sv_type);
634 return OK;
635 }
636
Hirohito Higashi6c012832025-02-25 20:29:50 +0100637 if (end == NULL)
638 imp = NULL;
639 else if (import == NULL)
640 imp = find_imported(name, 0, FALSE);
641 else
642 imp = import;
643
644 if (imp != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000646 char_u *p = skipwhite(*end);
647 char_u *exp_name;
648 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100649 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000650 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000651 int done = FALSE;
652 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000653
Hirohito Higashi6c012832025-02-25 20:29:50 +0100654 check_script_symlink(imp->imp_sid);
655 import_check_sourced_sid(&imp->imp_sid);
Ernie Rael9a901792024-04-16 22:11:56 +0200656
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000657 // Need to lookup the member.
658 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000660 semsg(_(e_expected_dot_after_name_str), start);
661 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000663 ++p;
664 if (VIM_ISWHITE(*p))
665 {
666 emsg(_(e_no_white_space_allowed_after_dot));
667 return FAIL;
668 }
669
670 // isolate one name
671 exp_name = p;
672 while (eval_isnamec(*p))
673 ++p;
674 cc = *p;
675 *p = NUL;
676
Hirohito Higashi6c012832025-02-25 20:29:50 +0100677 si = SCRIPT_ITEM(imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100678 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
679 // "import autoload './dir/script.vim'" or
680 // "import autoload './autoload/script.vim'" - load script first
Hirohito Higashi6c012832025-02-25 20:29:50 +0100681 res = generate_SOURCE(cctx, imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100682
683 if (res == OK)
684 {
685 if (si->sn_autoload_prefix != NULL
686 && si->sn_state == SN_STATE_NOT_LOADED)
687 {
688 char_u *auto_name =
689 concat_str(si->sn_autoload_prefix, exp_name);
690
691 // autoload script must be loaded later, access by the autoload
692 // name. If a '(' follows it must be a function. Otherwise we
693 // don't know, it can be "script.Func".
694 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100695 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100696 else
697 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
698 vim_free(auto_name);
699 done = TRUE;
700 }
701 else if (si->sn_import_autoload
702 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100703 {
704 // If a '(' follows it must be a function. Otherwise we don't
705 // know, it can be "script.Func".
706 if (cc == '(' || paren_follows_after_expr)
707 {
708 char_u sid_name[MAX_FUNC_NAME_LEN];
709
Hirohito Higashi6c012832025-02-25 20:29:50 +0100710 func_name_with_sid(exp_name, imp->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100711 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100712 }
713 else
714 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100715 imp->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100716 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100717 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100718 else
719 {
Hirohito Higashi6c012832025-02-25 20:29:50 +0100720 idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100721 cctx, NULL, TRUE);
722 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100723 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100724
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000725 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000726 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000727 if (done)
728 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000729
730 if (idx < 0)
731 {
732 if (ufunc != NULL)
733 {
734 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100735 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000736 return OK;
737 }
738 return FAIL;
739 }
740
741 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100742 imp->imp_sid,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000743 idx,
744 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 return OK;
746 }
747
Bram Moolenaard0132f42022-05-12 11:05:40 +0100748 // Can only get here if we know "name" is a script variable and not in a
749 // Vim9 script (variable is not in sn_var_vals): old style script.
750 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000751 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752}
753
754 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000755generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000757 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000758 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000760 // Reject a global non-autoload function found without the "g:" prefix.
761 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 return FAIL;
763
764 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000765 compile_type = get_compile_type(ufunc);
766 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000767 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100769 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770}
771
772/*
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100773 * Returns TRUE if compiling a class method.
774 */
775 static int
776compiling_a_class_method(cctx_T *cctx)
777{
778 // For an object method, the FC_OBJECT flag will be set.
779 // For a constructor method, the FC_NEW flag will be set.
780 // Excluding these methods, the others are class methods.
781 // When compiling a closure function inside an object method,
782 // cctx->ctx_outer->ctx_func will point to the object method.
783 return cctx->ctx_ufunc != NULL
784 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0
785 && (cctx->ctx_outer == NULL
786 || cctx->ctx_outer->ctx_ufunc == NULL
787 || cctx->ctx_outer->ctx_ufunc->uf_class == NULL
788 || (cctx->ctx_outer->ctx_ufunc->uf_flags
789 & (FC_OBJECT|FC_NEW)) == 0);
790}
791
792/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 * Compile a variable name into a load instruction.
794 * "end" points to just after the name.
795 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
796 * When "error" is FALSE do not give an error when not found.
797 */
798 int
799compile_load(
800 char_u **arg,
801 char_u *end_arg,
802 cctx_T *cctx,
803 int is_expr,
804 int error)
805{
806 type_T *type;
807 char_u *name = NULL;
808 char_u *end = end_arg;
809 int res = FAIL;
810 int prev_called_emsg = called_emsg;
811
812 if (*(*arg + 1) == ':')
813 {
814 if (end <= *arg + 2)
815 {
816 isntype_T isn_type;
817
818 // load dictionary of namespace
819 switch (**arg)
820 {
821 case 'g': isn_type = ISN_LOADGDICT; break;
822 case 'w': isn_type = ISN_LOADWDICT; break;
823 case 't': isn_type = ISN_LOADTDICT; break;
824 case 'b': isn_type = ISN_LOADBDICT; break;
825 default:
826 semsg(_(e_namespace_not_supported_str), *arg);
827 goto theend;
828 }
829 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
830 goto theend;
831 res = OK;
832 }
833 else
834 {
835 isntype_T isn_type = ISN_DROP;
836
837 // load namespaced variable
838 name = vim_strnsave(*arg + 2, end - (*arg + 2));
839 if (name == NULL)
840 return FAIL;
841
842 switch (**arg)
843 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100844 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000846 case 's': if (current_script_is_vim9())
847 {
848 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
849 *arg);
850 vim_free(name);
851 return FAIL;
852 }
Kota Kato948a3892022-08-16 16:09:59 +0100853 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000854 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 else
856 res = compile_load_scriptvar(cctx, name,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100857 NULL, &end, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000858 break;
859 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
860 {
861 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000862 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000863 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 else
865 isn_type = ISN_LOADG;
866 }
867 else
868 {
869 isn_type = ISN_LOADAUTO;
870 vim_free(name);
871 name = vim_strnsave(*arg, end - *arg);
872 if (name == NULL)
873 return FAIL;
874 }
875 break;
876 case 'w': isn_type = ISN_LOADW; break;
877 case 't': isn_type = ISN_LOADT; break;
878 case 'b': isn_type = ISN_LOADB; break;
879 default: // cannot happen, just in case
880 semsg(_(e_namespace_not_supported_str), *arg);
881 goto theend;
882 }
883 if (isn_type != ISN_DROP)
884 {
885 // Global, Buffer-local, Window-local and Tabpage-local
886 // variables can be defined later, thus we don't check if it
887 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000888 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 }
890 }
891 }
892 else
893 {
894 size_t len = end - *arg;
895 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200896 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 int gen_load = FALSE;
898 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100899 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100900 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901
Hirohito Higashif7cb9f92025-02-04 16:37:19 +0100902 name = vim_strnsave(*arg, len);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 if (name == NULL)
904 return FAIL;
905
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100906 if (STRCMP(name, "super") == 0 && compiling_a_class_method(cctx))
Bram Moolenaar58b40092023-01-11 15:59:05 +0000907 {
908 // super.SomeFunc() in a class function: push &t_super type, this
909 // is recognized in compile_subscript().
910 res = push_type_stack(cctx, &t_super);
911 if (*end != '.')
912 emsg(_(e_super_must_be_followed_by_dot));
913 }
914 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000915 {
916 script_autoload(name, FALSE);
917 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
918 }
919 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
920 == OK)
921 {
922 if (gen_load_outer == 0)
923 gen_load = TRUE;
924 }
925 else
926 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000927 lvar_T lvar;
928 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929
930 if (lookup_local(*arg, len, &lvar, cctx) == OK)
931 {
932 type = lvar.lv_type;
933 idx = lvar.lv_idx;
934 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100935 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100937 outer_loop_depth = lvar.lv_loop_depth;
938 outer_loop_idx = lvar.lv_loop_idx;
939 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000940 else
941 gen_load = TRUE;
942 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200943 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
944 (((idx =
945 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
946 || ((method_idx =
947 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000948 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200949 // Referencing a class variable or method without the class
950 // name. A class variable or method can be referenced without
951 // the class name only in the class where the function is
952 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200953 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200954 {
955 if (idx >= 0)
956 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
957 else
958 {
959 ufunc_T *fp = cl->class_class_functions[method_idx];
960 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
961 NULL);
962 }
963 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200964 else
965 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200966 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200967 name, cl->class_name);
968 res = FAIL;
969 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000970 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971 else
972 {
Hirohito Higashi6c012832025-02-25 20:29:50 +0100973 imported_T *imp = NULL;
974
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000975 // "var" can be script-local even without using "s:" if it
976 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000977 if (script_var_exists(*arg, len, cctx, NULL) == OK
Hirohito Higashi6c012832025-02-25 20:29:50 +0100978 || (imp = find_imported(name, 0, FALSE)) != NULL)
979 res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000980
981 // When evaluating an expression and the name starts with an
982 // uppercase letter it can be a user defined function.
983 // generate_funcref() will fail if the function can't be found.
984 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000985 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 }
987 }
988 if (gen_load)
989 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
990 if (gen_load_outer > 0)
991 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100992 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
993 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000994 cctx->ctx_outer_used = TRUE;
995 }
996 }
997
998 *arg = end;
999
1000theend:
1001 if (res == FAIL && error && called_emsg == prev_called_emsg)
1002 semsg(_(e_variable_not_found_str), name);
1003 vim_free(name);
1004 return res;
1005}
1006
1007/*
1008 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +01001009 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001010 * Returns FAIL if compilation fails.
1011 */
1012 static int
LemonBoyf3b48952022-05-05 13:53:03 +01001013compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001014{
LemonBoyf3b48952022-05-05 13:53:03 +01001015 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001016 garray_T save_ga = cctx->ctx_instr;
1017 int expr_res;
1018 int trailing_error;
1019 int instr_count;
1020 isn_T *instr = NULL;
1021
1022 // Remove the string type from the stack.
1023 --cctx->ctx_type_stack.ga_len;
1024
1025 // Temporarily reset the list of instructions so that the jump labels are
1026 // correct.
1027 cctx->ctx_instr.ga_len = 0;
1028 cctx->ctx_instr.ga_maxlen = 0;
1029 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +00001030
1031 // avoid peeking a next line
1032 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
1033 cctx->ctx_ufunc->uf_lines.ga_len = 0;
1034
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001035 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +00001036
1037 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
1038
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039 s = skipwhite(s);
1040 trailing_error = *s != NUL;
1041
1042 if (expr_res == FAIL || trailing_error
1043 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
1044 {
1045 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001046 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001047 clear_instr_ga(&cctx->ctx_instr);
1048 cctx->ctx_instr = save_ga;
1049 ++cctx->ctx_type_stack.ga_len;
1050 return FAIL;
1051 }
1052
1053 // Move the generated instructions into the ISN_INSTR instruction, then
1054 // restore the list of instructions.
1055 instr_count = cctx->ctx_instr.ga_len;
1056 instr = cctx->ctx_instr.ga_data;
1057 instr[instr_count].isn_type = ISN_FINISH;
1058
1059 cctx->ctx_instr = save_ga;
1060 vim_free(isn->isn_arg.string);
1061 isn->isn_type = ISN_INSTR;
1062 isn->isn_arg.instr = instr;
1063 return OK;
1064}
1065
1066/*
1067 * Compile the argument expressions.
1068 * "arg" points to just after the "(" and is advanced to after the ")"
1069 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001070 int
LemonBoyf3b48952022-05-05 13:53:03 +01001071compile_arguments(
1072 char_u **arg,
1073 cctx_T *cctx,
1074 int *argcount,
1075 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001076{
1077 char_u *p = *arg;
1078 char_u *whitep = *arg;
1079 int must_end = FALSE;
1080 int instr_count;
1081
1082 for (;;)
1083 {
1084 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1085 goto failret;
1086 if (*p == ')')
1087 {
1088 *arg = p + 1;
1089 return OK;
1090 }
1091 if (must_end)
1092 {
1093 semsg(_(e_missing_comma_before_argument_str), p);
1094 return FAIL;
1095 }
1096
1097 instr_count = cctx->ctx_instr.ga_len;
1098 if (compile_expr0(&p, cctx) == FAIL)
1099 return FAIL;
1100 ++*argcount;
1101
LemonBoyf3b48952022-05-05 13:53:03 +01001102 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103 && cctx->ctx_instr.ga_len == instr_count + 1)
1104 {
1105 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1106
1107 // {skip} argument of searchpair() can be compiled if not empty
1108 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +01001109 compile_string(isn, cctx, 0);
1110 }
1111 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
1112 && cctx->ctx_instr.ga_len == instr_count + 1)
1113 {
1114 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1115
1116 // {sub} argument of substitute() can be compiled if it starts
1117 // with \=
1118 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001119 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001120 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001121 }
1122
1123 if (*p != ',' && *skipwhite(p) == ',')
1124 {
1125 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1126 p = skipwhite(p);
1127 }
1128 if (*p == ',')
1129 {
1130 ++p;
1131 if (*p != NUL && !VIM_ISWHITE(*p))
1132 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1133 }
1134 else
1135 must_end = TRUE;
1136 whitep = p;
1137 p = skipwhite(p);
1138 }
1139failret:
1140 emsg(_(e_missing_closing_paren));
1141 return FAIL;
1142}
1143
1144/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001145 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1146 * etc.) if the class implements it.
1147 */
1148 static int
1149compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1150{
1151 type_T *type = get_decl_type_on_stack(cctx, 0);
1152 int res = FAIL;
1153
1154 // If the built in function is invoked on an object and the class
1155 // implements the corresponding built in method, then invoke the object
1156 // method.
1157 if (type->tt_type == VAR_OBJECT)
1158 {
1159 int method_idx;
1160 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1161 &method_idx);
1162 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001163 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001164 }
1165
1166 return res;
1167}
1168
1169
1170/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171 * Compile a function call: name(arg1, arg2)
1172 * "arg" points to "name", "arg + varlen" to the "(".
1173 * "argcount_init" is 1 for "value->method()"
1174 * Instructions:
1175 * EVAL arg1
1176 * EVAL arg2
1177 * BCALL / DCALL / UCALL
1178 */
1179 static int
1180compile_call(
1181 char_u **arg,
1182 size_t varlen,
1183 cctx_T *cctx,
1184 ppconst_T *ppconst,
1185 int argcount_init)
1186{
1187 char_u *name = *arg;
1188 char_u *p;
1189 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001190 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001191 char_u fname_buf[FLEN_FIXED + 1];
1192 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001193 ufunc_T *ufunc = NULL;
1194 int res = FAIL;
1195 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001196 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001197 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001198 imported_T *import;
1199
1200 if (varlen >= sizeof(namebuf))
1201 {
1202 semsg(_(e_name_too_long_str), name);
1203 return FAIL;
1204 }
1205 vim_strncpy(namebuf, *arg, varlen);
1206
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001207 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001208 if (import != NULL)
1209 {
1210 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1211 return FAIL;
1212 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001213
1214 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001215 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001217 if ((varlen == 3
1218 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001219 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1220 {
1221 char_u *s = skipwhite(*arg + varlen + 1);
1222 typval_T argvars[2];
1223 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001224 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001225
1226 argvars[0].v_type = VAR_UNKNOWN;
1227 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001228 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001230 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231 s = skipwhite(s);
1232 if (*s == ')' && argvars[0].v_type == VAR_STRING
1233 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1234 || !is_has))
1235 {
1236 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1237
1238 *arg = s + 1;
1239 argvars[1].v_type = VAR_UNKNOWN;
1240 tv->v_type = VAR_NUMBER;
1241 tv->vval.v_number = 0;
1242 if (is_has)
1243 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001244 else if (is_len)
1245 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 else
1247 f_exists(argvars, tv);
1248 clear_tv(&argvars[0]);
1249 ++ppconst->pp_used;
1250 return OK;
1251 }
1252 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001253 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001254 {
1255 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1256 return FAIL;
1257 }
1258 }
1259
1260 if (generate_ppconst(cctx, ppconst) == FAIL)
1261 return FAIL;
1262
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001263 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1265
1266 // We handle the "skip" argument of searchpair() and searchpairpos()
1267 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001268 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1269 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1270 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1271 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1272 special_fn = CA_SEARCHPAIR;
1273 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1274 special_fn = CA_SUBSTITUTE;
1275 else
1276 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277
1278 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001279 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 goto theend;
1281
1282 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1283 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1284 {
1285 int idx;
1286
1287 // builtin function
1288 idx = find_internal_func(name);
1289 if (idx >= 0)
1290 {
1291 if (STRCMP(name, "flatten") == 0)
1292 {
1293 emsg(_(e_cannot_use_flatten_in_vim9_script));
1294 goto theend;
1295 }
1296
1297 if (STRCMP(name, "add") == 0 && argcount == 2)
1298 {
h-eastb895b0f2023-09-24 15:46:31 +02001299 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001300 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1301 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001302
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001303 // add() can be compiled to instructions if we know the type
1304 if (type->tt_type == VAR_LIST)
1305 {
1306 // inline "add(list, item)" so that the type can be checked
1307 res = generate_LISTAPPEND(cctx);
1308 idx = -1;
1309 }
1310 else if (type->tt_type == VAR_BLOB)
1311 {
1312 // inline "add(blob, nr)" so that the type can be checked
1313 res = generate_BLOBAPPEND(cctx);
1314 idx = -1;
1315 }
1316 }
1317
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001318 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1319 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001320 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001321 // May have the "D" or "R" flag, reserve a variable for a
1322 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001323 if (get_defer_var_idx(cctx) == 0)
1324 idx = -1;
1325 }
1326
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001327 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1328 if (STRCMP(name, "string") == 0)
1329 builtin_method = CLASS_BUILTIN_STRING;
1330 else if (STRCMP(name, "empty") == 0)
1331 builtin_method = CLASS_BUILTIN_EMPTY;
1332 else if (STRCMP(name, "len") == 0)
1333 builtin_method = CLASS_BUILTIN_LEN;
1334 if (builtin_method != CLASS_BUILTIN_INVALID)
1335 {
1336 res = compile_builtin_method_call(cctx, builtin_method);
1337 if (res == OK)
1338 idx = -1;
1339 }
1340
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341 if (idx >= 0)
1342 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1343 }
1344 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001345 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001346 goto theend;
1347 }
1348
Bram Moolenaar62aec932022-01-29 21:45:34 +00001349 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1350
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351 // An argument or local variable can be a function reference, this
1352 // overrules a function name.
1353 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1354 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1355 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001356 class_T *cl = NULL;
1357 int mi = 0;
1358
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001359 // If we can find the function by name generate the right call.
1360 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001361 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001362 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001363 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001364 if (!func_is_global(ufunc))
1365 {
Ernie Rael58c95792024-08-13 23:27:22 +02001366 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001367 goto theend;
1368 }
1369 if (!has_g_namespace
1370 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1371 {
1372 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001373 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001374 goto theend;
1375 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001376 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001377 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001378 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001379 // Class method invocation without the class name.
1380 // A class method can be referenced without the class name only in
1381 // the class where the function is defined.
1382 if (cctx->ctx_ufunc->uf_defclass == cl)
1383 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001384 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001385 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001386 }
1387 else
1388 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001389 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001390 name, cl->class_name);
1391 res = FAIL;
1392 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001393 goto theend;
1394 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395 }
1396
1397 // If the name is a variable, load it and use PCALL.
1398 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001399 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001400 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001401 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001402 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1403 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001404 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001405
Christian Brabandtd5475e82023-08-17 23:34:09 +02001406 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001407 goto theend;
1408 }
1409
1410 // If we can find a global function by name generate the right call.
1411 if (ufunc != NULL)
1412 {
Ernie Rael58c95792024-08-13 23:27:22 +02001413 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001414 goto theend;
1415 }
1416
1417 // A global function may be defined only later. Need to figure out at
1418 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001419 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420 res = generate_UCALL(cctx, name, argcount);
1421 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001422 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001423
1424theend:
1425 vim_free(tofree);
1426 return res;
1427}
1428
1429// like NAMESPACE_CHAR but with 'a' and 'l'.
1430#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1431
1432/*
1433 * Find the end of a variable or function name. Unlike find_name_end() this
1434 * does not recognize magic braces.
1435 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1436 * Return a pointer to just after the name. Equal to "arg" if there is no
1437 * valid name.
1438 */
1439 char_u *
1440to_name_end(char_u *arg, int use_namespace)
1441{
1442 char_u *p;
1443
1444 // Quick check for valid starting character.
1445 if (!eval_isnamec1(*arg))
1446 return arg;
1447
1448 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1449 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1450 // and can be used in slice "[n:]".
1451 if (*p == ':' && (p != arg + 1
1452 || !use_namespace
1453 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1454 break;
1455 return p;
1456}
1457
1458/*
1459 * Like to_name_end() but also skip over a list or dict constant.
1460 * Also accept "<SNR>123_Func".
1461 * This intentionally does not handle line continuation.
1462 */
1463 char_u *
1464to_name_const_end(char_u *arg)
1465{
1466 char_u *p = arg;
1467 typval_T rettv;
1468
1469 if (STRNCMP(p, "<SNR>", 5) == 0)
1470 p = skipdigits(p + 5);
1471 p = to_name_end(p, TRUE);
1472 if (p == arg && *arg == '[')
1473 {
1474
1475 // Can be "[1, 2, 3]->Func()".
1476 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1477 p = arg;
1478 }
1479 return p;
1480}
1481
1482/*
1483 * parse a list: [expr, expr]
1484 * "*arg" points to the '['.
1485 * ppconst->pp_is_const is set if all items are a constant.
1486 */
1487 static int
1488compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1489{
1490 char_u *p = skipwhite(*arg + 1);
1491 char_u *whitep = *arg + 1;
1492 int count = 0;
1493 int is_const;
1494 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001495 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001496
1497 for (;;)
1498 {
1499 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1500 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001501 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502 return FAIL;
1503 }
1504 if (*p == ',')
1505 {
1506 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1507 return FAIL;
1508 }
1509 if (*p == ']')
1510 {
1511 ++p;
1512 break;
1513 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001514 if (must_end)
1515 {
1516 semsg(_(e_missing_comma_in_list_str), p);
1517 return FAIL;
1518 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001519 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1520 return FAIL;
1521 if (!is_const)
1522 is_all_const = FALSE;
1523 ++count;
1524 if (*p == ',')
1525 {
1526 ++p;
1527 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1528 {
1529 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1530 return FAIL;
1531 }
1532 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001533 else
1534 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001535 whitep = p;
1536 p = skipwhite(p);
1537 }
1538 *arg = p;
1539
1540 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001541 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001542}
1543
1544/*
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001545 * parse a tuple: (expr, expr)
1546 * "*arg" points to the ','.
1547 * ppconst->pp_is_const is set if all the items are constants.
1548 */
1549 static int
1550compile_tuple(
1551 char_u **arg,
1552 cctx_T *cctx,
1553 ppconst_T *ppconst,
1554 int first_item_const)
1555{
1556 char_u *p = *arg + 1;
1557 char_u *whitep = *arg + 1;
1558 int count = 0;
1559 int is_const;
1560 int is_all_const = TRUE; // reset when non-const encountered
1561 int must_end = FALSE;
1562
1563 if (**arg != ')')
1564 {
1565 if (*p != ')' && !IS_WHITE_OR_NUL(*p))
1566 {
1567 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1568 return FAIL;
1569 }
1570 count = 1; // the first tuple item is already processed
1571 is_all_const = first_item_const;
1572 for (;;)
1573 {
1574 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1575 {
1576 semsg(_(e_missing_end_of_tuple_rsp_str), *arg);
1577 return FAIL;
1578 }
1579 if (*p == ',')
1580 {
1581 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1582 return FAIL;
1583 }
1584 if (*p == ')')
1585 {
1586 ++p;
1587 break;
1588 }
1589 if (must_end)
1590 {
1591 semsg(_(e_missing_comma_in_tuple_str), p);
1592 return FAIL;
1593 }
1594 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1595 return FAIL;
1596 if (!is_const)
1597 is_all_const = FALSE;
1598 ++count;
1599 if (*p == ',')
1600 {
1601 ++p;
1602 if (*p != ')' && !IS_WHITE_OR_NUL(*p))
1603 {
1604 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1605 return FAIL;
1606 }
1607 }
1608 else
1609 must_end = TRUE;
1610 whitep = p;
1611 p = skipwhite(p);
1612 }
1613 }
1614 *arg = p;
1615
1616 ppconst->pp_is_const = is_all_const;
1617 return generate_NEWTUPLE(cctx, count, FALSE);
1618}
1619
1620/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001621 * Parse a lambda: "(arg, arg) => expr"
1622 * "*arg" points to the '('.
1623 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1624 */
1625 static int
1626compile_lambda(char_u **arg, cctx_T *cctx)
1627{
1628 int r;
1629 typval_T rettv;
1630 ufunc_T *ufunc;
1631 evalarg_T evalarg;
1632
1633 init_evalarg(&evalarg);
1634 evalarg.eval_flags = EVAL_EVALUATE;
1635 evalarg.eval_cctx = cctx;
1636
1637 // Get the funcref in "rettv".
1638 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1639 if (r != OK)
1640 {
1641 clear_evalarg(&evalarg, NULL);
1642 return r;
1643 }
1644
1645 // "rettv" will now be a partial referencing the function.
1646 ufunc = rettv.vval.v_partial->pt_func;
1647 ++ufunc->uf_refcount;
1648 clear_tv(&rettv);
1649
1650 // Compile it here to get the return type. The return type is optional,
1651 // when it's missing use t_unknown. This is recognized in
1652 // compile_return().
1653 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1654 ufunc->uf_ret_type = &t_unknown;
1655 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1656
1657 // When the outer function is compiled for profiling or debugging, the
1658 // lambda may be called without profiling or debugging. Compile it here in
1659 // the right context.
1660 if (cctx->ctx_compile_type == CT_DEBUG
1661#ifdef FEAT_PROFILE
1662 || cctx->ctx_compile_type == CT_PROFILE
1663#endif
1664 )
1665 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1666
Bram Moolenaar139575d2022-03-15 19:29:30 +00001667 // if the outer function is not compiled for debugging or profiling, this
1668 // one might be
1669 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001670 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001671 compiletype_T compile_type = get_compile_type(ufunc);
1672
1673 if (compile_type != CT_NONE)
1674 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001675 }
1676
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001677 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1678 // "*arg" may point into it. Point into the original line to avoid a
1679 // dangling pointer.
1680 if (evalarg.eval_using_cmdline)
1681 {
1682 garray_T *gap = &evalarg.eval_tofree_ga;
1683 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1684
1685 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1686 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001687 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 }
1689
1690 clear_evalarg(&evalarg, NULL);
1691
1692 if (ufunc->uf_def_status == UF_COMPILED)
1693 {
1694 // The return type will now be known.
1695 set_function_type(ufunc);
1696
1697 // The function reference count will be 1. When the ISN_FUNCREF
1698 // instruction is deleted the reference count is decremented and the
1699 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001700 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001701 }
1702
1703 func_ptr_unref(ufunc);
1704 return FAIL;
1705}
1706
1707/*
1708 * Get a lambda and compile it. Uses Vim9 syntax.
1709 */
1710 int
1711get_lambda_tv_and_compile(
1712 char_u **arg,
1713 typval_T *rettv,
1714 int types_optional,
1715 evalarg_T *evalarg)
1716{
1717 int r;
1718 ufunc_T *ufunc;
1719 int save_sc_version = current_sctx.sc_version;
1720
1721 // Get the funcref in "rettv".
1722 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1723 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1724 current_sctx.sc_version = save_sc_version;
1725 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001726 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001727
1728 // "rettv" will now be a partial referencing the function.
1729 ufunc = rettv->vval.v_partial->pt_func;
1730
1731 // Compile it here to get the return type. The return type is optional,
1732 // when it's missing use t_unknown. This is recognized in
1733 // compile_return().
1734 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1735 ufunc->uf_ret_type = &t_unknown;
1736 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1737
1738 if (ufunc->uf_def_status == UF_COMPILED)
1739 {
1740 // The return type will now be known.
1741 set_function_type(ufunc);
1742 return OK;
1743 }
1744 clear_tv(rettv);
1745 return FAIL;
1746}
1747
1748/*
1749 * parse a dict: {key: val, [key]: val}
1750 * "*arg" points to the '{'.
1751 * ppconst->pp_is_const is set if all item values are a constant.
1752 */
1753 static int
1754compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1755{
1756 garray_T *instr = &cctx->ctx_instr;
1757 int count = 0;
1758 dict_T *d = dict_alloc();
1759 dictitem_T *item;
1760 char_u *whitep = *arg + 1;
1761 char_u *p;
1762 int is_const;
1763 int is_all_const = TRUE; // reset when non-const encountered
1764
1765 if (d == NULL)
1766 return FAIL;
1767 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001768 {
1769 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001770 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001771 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 for (;;)
1773 {
1774 char_u *key = NULL;
1775
1776 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1777 {
1778 *arg = NULL;
1779 goto failret;
1780 }
1781
1782 if (**arg == '}')
1783 break;
1784
1785 if (**arg == '[')
1786 {
1787 isn_T *isn;
1788
1789 // {[expr]: value} uses an evaluated key.
1790 *arg = skipwhite(*arg + 1);
1791 if (compile_expr0(arg, cctx) == FAIL)
1792 return FAIL;
1793 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1794 if (isn->isn_type == ISN_PUSHNR)
1795 {
1796 char buf[NUMBUFLEN];
1797
1798 // Convert to string at compile time.
1799 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1800 isn->isn_type = ISN_PUSHS;
1801 isn->isn_arg.string = vim_strsave((char_u *)buf);
1802 }
1803 if (isn->isn_type == ISN_PUSHS)
1804 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001805 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001806 return FAIL;
1807 *arg = skipwhite(*arg);
1808 if (**arg != ']')
1809 {
1810 emsg(_(e_missing_matching_bracket_after_dict_key));
1811 return FAIL;
1812 }
1813 ++*arg;
1814 }
1815 else
1816 {
1817 // {"name": value},
1818 // {'name': value},
1819 // {name: value} use "name" as a literal key
1820 key = get_literal_key(arg);
1821 if (key == NULL)
1822 return FAIL;
1823 if (generate_PUSHS(cctx, &key) == FAIL)
1824 return FAIL;
1825 }
1826
1827 // Check for duplicate keys, if using string keys.
1828 if (key != NULL)
1829 {
1830 item = dict_find(d, key, -1);
1831 if (item != NULL)
1832 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001833 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834 goto failret;
1835 }
1836 item = dictitem_alloc(key);
1837 if (item != NULL)
1838 {
1839 item->di_tv.v_type = VAR_UNKNOWN;
1840 item->di_tv.v_lock = 0;
1841 if (dict_add(d, item) == FAIL)
1842 dictitem_free(item);
1843 }
1844 }
1845
1846 if (**arg != ':')
1847 {
1848 if (*skipwhite(*arg) == ':')
1849 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1850 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001851 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001852 return FAIL;
1853 }
1854 whitep = *arg + 1;
1855 if (!IS_WHITE_OR_NUL(*whitep))
1856 {
1857 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1858 return FAIL;
1859 }
1860
1861 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1862 {
1863 *arg = NULL;
1864 goto failret;
1865 }
1866
1867 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1868 return FAIL;
1869 if (!is_const)
1870 is_all_const = FALSE;
1871 ++count;
1872
1873 whitep = *arg;
1874 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1875 {
1876 *arg = NULL;
1877 goto failret;
1878 }
1879 if (**arg == '}')
1880 break;
1881 if (**arg != ',')
1882 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001883 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884 goto failret;
1885 }
1886 if (IS_WHITE_OR_NUL(*whitep))
1887 {
1888 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1889 return FAIL;
1890 }
1891 whitep = *arg + 1;
1892 if (!IS_WHITE_OR_NUL(*whitep))
1893 {
1894 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1895 return FAIL;
1896 }
1897 *arg = skipwhite(whitep);
1898 }
1899
1900 *arg = *arg + 1;
1901
1902 // Allow for following comment, after at least one space.
1903 p = skipwhite(*arg);
1904 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1905 *arg += STRLEN(*arg);
1906
1907 dict_unref(d);
1908 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001909 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910
1911failret:
1912 if (*arg == NULL)
1913 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001914 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001915 *arg = (char_u *)"";
1916 }
1917 dict_unref(d);
1918 return FAIL;
1919}
1920
1921/*
1922 * Compile "&option".
1923 */
1924 static int
1925compile_get_option(char_u **arg, cctx_T *cctx)
1926{
1927 typval_T rettv;
1928 char_u *start = *arg;
1929 int ret;
1930
1931 // parse the option and get the current value to get the type.
1932 rettv.v_type = VAR_UNKNOWN;
1933 ret = eval_option(arg, &rettv, TRUE);
1934 if (ret == OK)
1935 {
1936 // include the '&' in the name, eval_option() expects it.
1937 char_u *name = vim_strnsave(start, *arg - start);
1938 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1939 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1940
1941 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1942 vim_free(name);
1943 }
1944 clear_tv(&rettv);
1945
1946 return ret;
1947}
1948
1949/*
1950 * Compile "$VAR".
1951 */
1952 static int
1953compile_get_env(char_u **arg, cctx_T *cctx)
1954{
1955 char_u *start = *arg;
1956 int len;
1957 int ret;
1958 char_u *name;
1959
1960 ++*arg;
1961 len = get_env_len(arg);
1962 if (len == 0)
1963 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001964 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001965 return FAIL;
1966 }
1967
1968 // include the '$' in the name, eval_env_var() expects it.
1969 name = vim_strnsave(start, len + 1);
1970 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1971 vim_free(name);
1972 return ret;
1973}
1974
1975/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001976 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001977 */
1978 static int
1979compile_interp_string(char_u **arg, cctx_T *cctx)
1980{
1981 typval_T tv;
1982 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001983 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001984 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001985 int count = 0;
1986 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001987
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001988 // *arg is on the '$' character, move it to the first string character.
1989 ++*arg;
1990 quote = **arg;
1991 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001992
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001993 for (;;)
1994 {
1995 // Get the string up to the matching quote or to a single '{'.
1996 // "arg" is advanced to either the quote or the '{'.
1997 if (quote == '"')
1998 ret = eval_string(arg, &tv, evaluate, TRUE);
1999 else
2000 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2001 if (ret == FAIL)
2002 break;
2003 if (evaluate)
2004 {
2005 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
2006 || (**arg != '{' && count == 0))
2007 {
2008 // generate non-empty string or empty string if it's the only
2009 // one
2010 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
2011 return FAIL;
2012 tv.vval.v_string = NULL; // don't free it now
2013 ++count;
2014 }
2015 clear_tv(&tv);
2016 }
2017
2018 if (**arg != '{')
2019 {
2020 // found terminating quote
2021 ++*arg;
2022 break;
2023 }
2024
2025 p = compile_one_expr_in_str(*arg, cctx);
2026 if (p == NULL)
2027 {
2028 ret = FAIL;
2029 break;
2030 }
2031 ++count;
2032 *arg = p;
2033 }
LemonBoy2eaef102022-05-06 13:14:50 +01002034
2035 if (ret == FAIL || !evaluate)
2036 return ret;
2037
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002038 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
2039 if (count > 1)
2040 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01002041
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002042 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002043}
2044
2045/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002046 * Compile "@r".
2047 */
2048 static int
2049compile_get_register(char_u **arg, cctx_T *cctx)
2050{
2051 int ret;
2052
2053 ++*arg;
2054 if (**arg == NUL)
2055 {
2056 semsg(_(e_syntax_error_at_str), *arg - 1);
2057 return FAIL;
2058 }
2059 if (!valid_yank_reg(**arg, FALSE))
2060 {
2061 emsg_invreg(**arg);
2062 return FAIL;
2063 }
2064 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2065 ++*arg;
2066 return ret;
2067}
2068
2069/*
2070 * Apply leading '!', '-' and '+' to constant "rettv".
2071 * When "numeric_only" is TRUE do not apply '!'.
2072 */
2073 static int
2074apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
2075{
2076 char_u *p = *end;
2077
2078 // this works from end to start
2079 while (p > start)
2080 {
2081 --p;
2082 if (*p == '-' || *p == '+')
2083 {
2084 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085 if (rettv->v_type == VAR_FLOAT)
2086 {
2087 if (*p == '-')
2088 rettv->vval.v_float = -rettv->vval.v_float;
2089 }
2090 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 {
2092 varnumber_T val;
2093 int error = FALSE;
2094
2095 // tv_get_number_chk() accepts a string, but we don't want that
2096 // here
2097 if (check_not_string(rettv) == FAIL)
2098 return FAIL;
2099 val = tv_get_number_chk(rettv, &error);
2100 clear_tv(rettv);
2101 if (error)
2102 return FAIL;
2103 if (*p == '-')
2104 val = -val;
2105 rettv->v_type = VAR_NUMBER;
2106 rettv->vval.v_number = val;
2107 }
2108 }
2109 else if (numeric_only)
2110 {
2111 ++p;
2112 break;
2113 }
2114 else if (*p == '!')
2115 {
2116 int v = tv2bool(rettv);
2117
2118 // '!' is permissive in the type.
2119 clear_tv(rettv);
2120 rettv->v_type = VAR_BOOL;
2121 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2122 }
2123 }
2124 *end = p;
2125 return OK;
2126}
2127
2128/*
2129 * Recognize v: variables that are constants and set "rettv".
2130 */
2131 static void
2132get_vim_constant(char_u **arg, typval_T *rettv)
2133{
2134 if (STRNCMP(*arg, "v:true", 6) == 0)
2135 {
2136 rettv->v_type = VAR_BOOL;
2137 rettv->vval.v_number = VVAL_TRUE;
2138 *arg += 6;
2139 }
2140 else if (STRNCMP(*arg, "v:false", 7) == 0)
2141 {
2142 rettv->v_type = VAR_BOOL;
2143 rettv->vval.v_number = VVAL_FALSE;
2144 *arg += 7;
2145 }
2146 else if (STRNCMP(*arg, "v:null", 6) == 0)
2147 {
2148 rettv->v_type = VAR_SPECIAL;
2149 rettv->vval.v_number = VVAL_NULL;
2150 *arg += 6;
2151 }
2152 else if (STRNCMP(*arg, "v:none", 6) == 0)
2153 {
2154 rettv->v_type = VAR_SPECIAL;
2155 rettv->vval.v_number = VVAL_NONE;
2156 *arg += 6;
2157 }
2158}
2159
2160 exprtype_T
2161get_compare_type(char_u *p, int *len, int *type_is)
2162{
2163 exprtype_T type = EXPR_UNKNOWN;
2164 int i;
2165
2166 switch (p[0])
2167 {
2168 case '=': if (p[1] == '=')
2169 type = EXPR_EQUAL;
2170 else if (p[1] == '~')
2171 type = EXPR_MATCH;
2172 break;
2173 case '!': if (p[1] == '=')
2174 type = EXPR_NEQUAL;
2175 else if (p[1] == '~')
2176 type = EXPR_NOMATCH;
2177 break;
2178 case '>': if (p[1] != '=')
2179 {
2180 type = EXPR_GREATER;
2181 *len = 1;
2182 }
2183 else
2184 type = EXPR_GEQUAL;
2185 break;
2186 case '<': if (p[1] != '=')
2187 {
2188 type = EXPR_SMALLER;
2189 *len = 1;
2190 }
2191 else
2192 type = EXPR_SEQUAL;
2193 break;
2194 case 'i': if (p[1] == 's')
2195 {
2196 // "is" and "isnot"; but not a prefix of a name
2197 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2198 *len = 5;
2199 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002200 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201 {
2202 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2203 *type_is = TRUE;
2204 }
2205 }
2206 break;
2207 }
2208 return type;
2209}
2210
2211/*
2212 * Skip over an expression, ignoring most errors.
2213 */
2214 void
2215skip_expr_cctx(char_u **arg, cctx_T *cctx)
2216{
2217 evalarg_T evalarg;
2218
2219 init_evalarg(&evalarg);
2220 evalarg.eval_cctx = cctx;
2221 skip_expr(arg, &evalarg);
2222 clear_evalarg(&evalarg, NULL);
2223}
2224
2225/*
2226 * Check that the top of the type stack has a type that can be used as a
2227 * condition. Give an error and return FAIL if not.
2228 */
2229 int
2230bool_on_stack(cctx_T *cctx)
2231{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002232 type_T *type;
2233
Bram Moolenaar078a4612022-01-04 15:17:03 +00002234 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002235 if (type == &t_bool)
2236 return OK;
2237
Bram Moolenaar4913d422022-10-17 13:13:32 +01002238 if (type->tt_type == VAR_ANY
2239 || type->tt_type == VAR_UNKNOWN
2240 || type->tt_type == VAR_NUMBER
2241 || type == &t_number_bool
2242 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002243 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2244 // This requires a runtime type check.
2245 return generate_COND2BOOL(cctx);
2246
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002247 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248}
2249
2250/*
2251 * Give the "white on both sides" error, taking the operator from "p[len]".
2252 */
2253 void
2254error_white_both(char_u *op, int len)
2255{
2256 char_u buf[10];
2257
2258 vim_strncpy(buf, op, len);
2259 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2260}
2261
2262/*
2263 * Compile code to apply '-', '+' and '!'.
2264 * When "numeric_only" is TRUE do not apply '!'.
2265 */
2266 static int
2267compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2268{
2269 char_u *p = *end;
2270
2271 // this works from end to start
2272 while (p > start)
2273 {
2274 --p;
2275 while (VIM_ISWHITE(*p))
2276 --p;
2277 if (*p == '-' || *p == '+')
2278 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002279 type_T *type = get_type_on_stack(cctx, 0);
2280 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002281 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002282 return FAIL;
2283
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002284 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002285 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2286 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 }
2288 else if (numeric_only)
2289 {
2290 ++p;
2291 break;
2292 }
2293 else
2294 {
2295 int invert = *p == '!';
2296
2297 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2298 {
2299 if (p[-1] == '!')
2300 invert = !invert;
2301 --p;
2302 }
2303 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2304 return FAIL;
2305 }
2306 }
2307 *end = p;
2308 return OK;
2309}
2310
2311/*
2312 * Compile "(expression)": recursive!
2313 * Return FAIL/OK.
2314 */
2315 static int
2316compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2317{
2318 int ret;
2319 char_u *p = *arg + 1;
2320
2321 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2322 return FAIL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002323
2324 if (**arg == ')')
2325 // empty tuple
2326 return compile_tuple(arg, cctx, ppconst, FALSE);
2327
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 if (ppconst->pp_used <= PPSIZE - 10)
2329 {
2330 ret = compile_expr1(arg, cctx, ppconst);
2331 }
2332 else
2333 {
2334 // Not enough space in ppconst, flush constants.
2335 if (generate_ppconst(cctx, ppconst) == FAIL)
2336 return FAIL;
2337 ret = compile_expr0(arg, cctx);
2338 }
2339 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2340 return FAIL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002341 if (ret == OK && **arg == ',')
2342 {
2343 // tuple
2344 int is_const = ppconst->pp_used > 0 || ppconst->pp_is_const;
2345 if (generate_ppconst(cctx, ppconst) == FAIL)
2346 return FAIL;
2347 return compile_tuple(arg, cctx, ppconst, is_const);
2348 }
2349
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002350 if (**arg == ')')
2351 ++*arg;
2352 else if (ret == OK)
2353 {
2354 emsg(_(e_missing_closing_paren));
2355 ret = FAIL;
2356 }
2357 return ret;
2358}
2359
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002360static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002361
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362/*
2363 * Compile whatever comes after "name" or "name()".
2364 * Advances "*arg" only when something was recognized.
2365 */
2366 static int
2367compile_subscript(
2368 char_u **arg,
2369 cctx_T *cctx,
2370 char_u *start_leader,
2371 char_u **end_leader,
2372 ppconst_T *ppconst)
2373{
2374 char_u *name_start = *end_leader;
2375 int keeping_dict = FALSE;
2376
2377 for (;;)
2378 {
2379 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002380 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002381
2382 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2383 {
2384 char_u *next = peek_next_line_from_context(cctx);
2385
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002386 // If a following line starts with "->{", "->(" or "->X" advance to
2387 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002388 // Also if a following line starts with ".x".
2389 if (next != NULL &&
2390 ((next[0] == '-' && next[1] == '>'
2391 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002392 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 || ASCII_ISALPHA(*skipwhite(next + 2))))
2394 || (next[0] == '.' && eval_isdictc(next[1]))))
2395 {
2396 next = next_line_from_context(cctx, TRUE);
2397 if (next == NULL)
2398 return FAIL;
2399 *arg = next;
2400 p = skipwhite(*arg);
2401 }
2402 }
2403
2404 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2405 // is not a function call.
2406 if (**arg == '(')
2407 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002408 int argcount = 0;
2409
2410 if (generate_ppconst(cctx, ppconst) == FAIL)
2411 return FAIL;
2412 ppconst->pp_is_const = FALSE;
2413
2414 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002415 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002416
2417 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002418 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002419 return FAIL;
2420 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2421 return FAIL;
2422 if (keeping_dict)
2423 {
2424 keeping_dict = FALSE;
2425 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2426 return FAIL;
2427 }
2428 }
2429 else if (*p == '-' && p[1] == '>')
2430 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002431 char_u *pstart = p;
2432 int alt;
2433 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002434
Bram Moolenaarc7349932022-01-16 20:59:39 +00002435 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436 if (generate_ppconst(cctx, ppconst) == FAIL)
2437 return FAIL;
2438 ppconst->pp_is_const = FALSE;
2439
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002440 // Apply the '!', '-' and '+' first:
2441 // -1.0->func() works like (-1.0)->func()
2442 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2443 return FAIL;
2444
2445 p += 2;
2446 *arg = skipwhite(p);
2447 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002448
2449 // Three alternatives handled here:
2450 // 1. "base->name(" only a name, use compile_call()
2451 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2452 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002454 alt = 2;
2455 else
2456 {
2457 // alternative 1 or 3
2458 p = *arg;
2459 if (!eval_isnamec1(*p))
2460 {
2461 semsg(_(e_trailing_characters_str), pstart);
2462 return FAIL;
2463 }
2464 if (ASCII_ISALPHA(*p) && p[1] == ':')
2465 p += 2;
2466 for ( ; eval_isnamec(*p); ++p)
2467 ;
2468 if (*p == '(')
2469 {
2470 // alternative 1
2471 alt = 1;
2472 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2473 return FAIL;
2474 }
2475 else
2476 {
2477 // Must be alternative 3, find the "(". Only works within
2478 // one line.
2479 alt = 3;
2480 paren = vim_strchr(p, '(');
2481 if (paren == NULL)
2482 {
2483 semsg(_(e_missing_parenthesis_str), *arg);
2484 return FAIL;
2485 }
2486 }
2487 }
2488
2489 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490 {
2491 int argcount = 1;
2492 garray_T *stack = &cctx->ctx_type_stack;
2493 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002494 int expr_isn_start = cctx->ctx_instr.ga_len;
2495 int expr_isn_end;
2496 int arg_isn_count;
2497
Bram Moolenaarc7349932022-01-16 20:59:39 +00002498 if (alt == 2)
2499 {
2500 // Funcref call: list->(Refs[2])(arg)
2501 // or lambda: list->((arg) => expr)(arg)
2502 //
2503 // Fist compile the function expression.
2504 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2505 return FAIL;
2506 }
2507 else
2508 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002509 int fail;
2510 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002511 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002512
Bram Moolenaarc7349932022-01-16 20:59:39 +00002513 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002514
2515 // instead of using LOADG for "import.Func" use PUSHFUNC
2516 ++paren_follows_after_expr;
2517
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002518 // do not look in the next line
2519 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002520
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002521 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002522 || *skipwhite(*arg) != NUL;
2523 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002524 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002525 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002526
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002527 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002528 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002529 if (did_emsg == prev_did_emsg)
2530 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002531 return FAIL;
2532 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002533 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002534
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 // Compile the arguments.
2536 if (**arg != '(')
2537 {
2538 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002539 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002540 else
2541 semsg(_(e_missing_parenthesis_str), *arg);
2542 return FAIL;
2543 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002544
2545 // Remember the next instruction index, where the instructions
2546 // for arguments are being written.
2547 expr_isn_end = cctx->ctx_instr.ga_len;
2548
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002550 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2551 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002552 return FAIL;
2553
2554 // Move the instructions for the arguments to before the
2555 // instructions of the expression and move the type of the
2556 // expression after the argument types. This is what ISN_PCALL
2557 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002558 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2559 if (arg_isn_count > 0)
2560 {
2561 int expr_isn_count = expr_isn_end - expr_isn_start;
2562 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002563 type_T *decl_type;
2564 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002565
2566 if (isn == NULL)
2567 return FAIL;
2568 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2569 + expr_isn_start,
2570 sizeof(isn_T) * expr_isn_count);
2571 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2572 + expr_isn_start,
2573 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2574 sizeof(isn_T) * arg_isn_count);
2575 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2576 + expr_isn_start + arg_isn_count,
2577 isn, sizeof(isn_T) * expr_isn_count);
2578 vim_free(isn);
2579
Bram Moolenaar078a4612022-01-04 15:17:03 +00002580 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2581 type = typep->type_curr;
2582 decl_type = typep->type_decl;
2583 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2584 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2585 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002586 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002587 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2588 typep->type_curr = type;
2589 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002590 }
2591
Bram Moolenaar078a4612022-01-04 15:17:03 +00002592 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002593 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2594 return FAIL;
2595 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002596
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002597 if (keeping_dict)
2598 {
2599 keeping_dict = FALSE;
2600 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2601 return FAIL;
2602 }
2603 }
2604 else if (**arg == '[')
2605 {
2606 int is_slice = FALSE;
2607
2608 // list index: list[123]
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002609 // tuple index: tuple[123]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002610 // dict member: dict[key]
2611 // string index: text[123]
2612 // blob index: blob[123]
2613 if (generate_ppconst(cctx, ppconst) == FAIL)
2614 return FAIL;
2615 ppconst->pp_is_const = FALSE;
2616
2617 ++p;
2618 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2619 return FAIL;
2620 if (**arg == ':')
2621 {
2622 // missing first index is equal to zero
2623 generate_PUSHNR(cctx, 0);
2624 }
2625 else
2626 {
2627 if (compile_expr0(arg, cctx) == FAIL)
2628 return FAIL;
2629 if (**arg == ':')
2630 {
2631 semsg(_(e_white_space_required_before_and_after_str_at_str),
2632 ":", *arg);
2633 return FAIL;
2634 }
2635 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2636 return FAIL;
2637 *arg = skipwhite(*arg);
2638 }
2639 if (**arg == ':')
2640 {
2641 is_slice = TRUE;
2642 ++*arg;
2643 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2644 {
2645 semsg(_(e_white_space_required_before_and_after_str_at_str),
2646 ":", *arg);
2647 return FAIL;
2648 }
2649 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2650 return FAIL;
2651 if (**arg == ']')
2652 // missing second index is equal to end of string
2653 generate_PUSHNR(cctx, -1);
2654 else
2655 {
2656 if (compile_expr0(arg, cctx) == FAIL)
2657 return FAIL;
2658 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2659 return FAIL;
2660 *arg = skipwhite(*arg);
2661 }
2662 }
2663
2664 if (**arg != ']')
2665 {
2666 emsg(_(e_missing_closing_square_brace));
2667 return FAIL;
2668 }
2669 *arg = *arg + 1;
2670
2671 if (keeping_dict)
2672 {
2673 keeping_dict = FALSE;
2674 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2675 return FAIL;
2676 }
2677 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2678 return FAIL;
2679 }
2680 else if (*p == '.' && p[1] != '.')
2681 {
2682 // dictionary member: dict.name
2683 if (generate_ppconst(cctx, ppconst) == FAIL)
2684 return FAIL;
2685 ppconst->pp_is_const = FALSE;
2686
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002687 type = get_type_on_stack(cctx, 0);
2688 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002689 && (type->tt_type == VAR_CLASS
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002690 || (type->tt_type == VAR_OBJECT
2691 && type != &t_object_any)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002692 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002693 // class member: SomeClass.varname
2694 // class method: SomeClass.SomeMethod()
2695 // class constructor: SomeClass.new()
2696 // object member: someObject.varname, this.varname
2697 // object method: someObject.SomeMethod(), this.SomeMethod()
2698 *arg = p;
2699 if (compile_class_object_index(cctx, arg, type) == FAIL)
2700 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002701 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002702 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002703 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002704 *arg = p + 1;
2705 if (IS_WHITE_OR_NUL(**arg))
2706 {
2707 emsg(_(e_missing_name_after_dot));
2708 return FAIL;
2709 }
2710 p = *arg;
2711 if (eval_isdictc(*p))
2712 while (eval_isnamec(*p))
2713 MB_PTR_ADV(p);
2714 if (p == *arg)
2715 {
2716 semsg(_(e_syntax_error_at_str), *arg);
2717 return FAIL;
2718 }
2719 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2720 return FAIL;
2721 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2722 return FAIL;
2723 keeping_dict = TRUE;
2724 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002725 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002726 }
2727 else
2728 break;
2729 }
2730
2731 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2732 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002733 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2734 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002735 return FAIL;
2736
2737 return OK;
2738}
2739
2740/*
2741 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2742 * "arg" is advanced until after the expression, skipping white space.
2743 *
2744 * If the value is a constant "ppconst->pp_used" will be non-zero.
2745 * Before instructions are generated, any values in "ppconst" will generated.
2746 *
2747 * This is the compiling equivalent of eval1(), eval2(), etc.
2748 */
2749
2750/*
2751 * number number constant
2752 * 0zFFFFFFFF Blob constant
2753 * "string" string constant
2754 * 'string' literal string constant
2755 * &option-name option value
2756 * @r register contents
2757 * identifier variable value
2758 * function() function call
2759 * $VAR environment variable
2760 * (expression) nested expression
2761 * [expr, expr] List
2762 * {key: val, [key]: val} Dictionary
2763 *
2764 * Also handle:
2765 * ! in front logical NOT
2766 * - in front unary minus
2767 * + in front unary plus (ignored)
2768 * trailing (arg) funcref/partial call
2769 * trailing [] subscript in String or List
2770 * trailing .name entry in Dictionary
2771 * trailing ->name() method call
2772 */
2773 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002774compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002775 char_u **arg,
2776 cctx_T *cctx,
2777 ppconst_T *ppconst)
2778{
2779 char_u *start_leader, *end_leader;
2780 int ret = OK;
2781 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2782 int used_before = ppconst->pp_used;
2783
2784 ppconst->pp_is_const = FALSE;
2785
2786 /*
2787 * Skip '!', '-' and '+' characters. They are handled later.
2788 */
2789 start_leader = *arg;
2790 if (eval_leader(arg, TRUE) == FAIL)
2791 return FAIL;
2792 end_leader = *arg;
2793
2794 rettv->v_type = VAR_UNKNOWN;
2795 switch (**arg)
2796 {
2797 /*
2798 * Number constant.
2799 */
2800 case '0': // also for blob starting with 0z
2801 case '1':
2802 case '2':
2803 case '3':
2804 case '4':
2805 case '5':
2806 case '6':
2807 case '7':
2808 case '8':
2809 case '9':
2810 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2811 return FAIL;
2812 // Apply "-" and "+" just before the number now, right to
2813 // left. Matters especially when "->" follows. Stops at
2814 // '!'.
2815 if (apply_leader(rettv, TRUE,
2816 start_leader, &end_leader) == FAIL)
2817 {
2818 clear_tv(rettv);
2819 return FAIL;
2820 }
2821 break;
2822
2823 /*
2824 * String constant: "string".
2825 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002826 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002827 return FAIL;
2828 break;
2829
2830 /*
2831 * Literal string constant: 'str''ing'.
2832 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002833 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002834 return FAIL;
2835 break;
2836
2837 /*
2838 * Constant Vim variable.
2839 */
2840 case 'v': get_vim_constant(arg, rettv);
2841 ret = NOTDONE;
2842 break;
2843
2844 /*
2845 * "true" constant
2846 */
2847 case 't': if (STRNCMP(*arg, "true", 4) == 0
2848 && !eval_isnamec((*arg)[4]))
2849 {
2850 *arg += 4;
2851 rettv->v_type = VAR_BOOL;
2852 rettv->vval.v_number = VVAL_TRUE;
2853 }
2854 else
2855 ret = NOTDONE;
2856 break;
2857
2858 /*
2859 * "false" constant
2860 */
2861 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2862 && !eval_isnamec((*arg)[5]))
2863 {
2864 *arg += 5;
2865 rettv->v_type = VAR_BOOL;
2866 rettv->vval.v_number = VVAL_FALSE;
2867 }
2868 else
2869 ret = NOTDONE;
2870 break;
2871
2872 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002873 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002874 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002875 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002876 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002877 char_u *p = *arg + 4;
2878 int len;
2879
2880 for (len = 0; eval_isnamec(p[len]); ++len)
2881 ;
2882 ret = handle_predefined(*arg, len + 4, rettv);
2883 if (ret == FAIL)
2884 ret = NOTDONE;
2885 else
2886 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002887 }
2888 else
2889 ret = NOTDONE;
2890 break;
2891
2892 /*
2893 * List: [expr, expr]
2894 */
2895 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2896 return FAIL;
2897 ret = compile_list(arg, cctx, ppconst);
2898 break;
2899
2900 /*
2901 * Dictionary: {'key': val, 'key': val}
2902 */
2903 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2904 return FAIL;
2905 ret = compile_dict(arg, cctx, ppconst);
2906 break;
2907
2908 /*
2909 * Option value: &name
2910 */
2911 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2912 return FAIL;
2913 ret = compile_get_option(arg, cctx);
2914 break;
2915
2916 /*
2917 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002918 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002919 */
2920 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2921 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002922 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2923 ret = compile_interp_string(arg, cctx);
2924 else
2925 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002926 break;
2927
2928 /*
2929 * Register contents: @r.
2930 */
2931 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2932 return FAIL;
2933 ret = compile_get_register(arg, cctx);
2934 break;
2935 /*
2936 * nested expression: (expression).
2937 * lambda: (arg, arg) => expr
2938 * funcref: (arg, arg) => { statement }
2939 */
2940 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2941 ret = compile_lambda(arg, cctx);
2942 if (ret == NOTDONE)
2943 ret = compile_parenthesis(arg, cctx, ppconst);
2944 break;
2945
2946 default: ret = NOTDONE;
2947 break;
2948 }
2949 if (ret == FAIL)
2950 return FAIL;
2951
2952 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2953 {
2954 if (cctx->ctx_skip == SKIP_YES)
2955 clear_tv(rettv);
2956 else
2957 // A constant expression can possibly be handled compile time,
2958 // return the value instead of generating code.
2959 ++ppconst->pp_used;
2960 }
2961 else if (ret == NOTDONE)
2962 {
2963 char_u *p;
2964 int r;
2965
2966 if (!eval_isnamec1(**arg))
2967 {
2968 if (!vim9_bad_comment(*arg))
2969 {
2970 if (ends_excmd(*skipwhite(*arg)))
2971 semsg(_(e_empty_expression_str), *arg);
2972 else
2973 semsg(_(e_name_expected_str), *arg);
2974 }
2975 return FAIL;
2976 }
2977
2978 // "name" or "name()"
2979 p = to_name_end(*arg, TRUE);
2980 if (p - *arg == (size_t)1 && **arg == '_')
2981 {
2982 emsg(_(e_cannot_use_underscore_here));
2983 return FAIL;
2984 }
2985
2986 if (*p == '(')
2987 {
2988 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2989 }
2990 else
2991 {
2992 if (cctx->ctx_skip != SKIP_YES
2993 && generate_ppconst(cctx, ppconst) == FAIL)
2994 return FAIL;
2995 r = compile_load(arg, p, cctx, TRUE, TRUE);
2996 }
2997 if (r == FAIL)
2998 return FAIL;
2999 }
3000
3001 // Handle following "[]", ".member", etc.
3002 // Then deal with prefixed '-', '+' and '!', if not done already.
3003 if (compile_subscript(arg, cctx, start_leader, &end_leader,
3004 ppconst) == FAIL)
3005 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01003006 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003007 {
3008 // apply the '!', '-' and '+' before the constant
3009 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3010 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
3011 return FAIL;
3012 return OK;
3013 }
3014 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
3015 return FAIL;
3016 return OK;
3017}
3018
3019/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003020 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003021 */
3022 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003023compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003024{
3025 type_T *want_type = NULL;
3026
3027 // Recognize <type>
3028 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3029 {
3030 ++*arg;
3031 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
3032 if (want_type == NULL)
3033 return FAIL;
3034
3035 if (**arg != '>')
3036 {
3037 if (*skipwhite(*arg) == '>')
3038 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3039 else
3040 emsg(_(e_missing_gt));
3041 return FAIL;
3042 }
3043 ++*arg;
3044 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3045 return FAIL;
3046 }
3047
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003048 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003049 return FAIL;
3050
3051 if (want_type != NULL)
3052 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003053 type_T *actual;
3054 where_T where = WHERE_INIT;
3055
LemonBoy50d48542024-07-04 17:03:17 +02003056 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003057 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00003058 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00003059 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003060 {
LemonBoy50d48542024-07-04 17:03:17 +02003061 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
3062 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003063 return FAIL;
3064 }
3065 }
3066
3067 return OK;
3068}
3069
3070/*
3071 * * number multiplication
3072 * / number division
3073 * % number modulo
3074 */
3075 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003076compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003077{
3078 char_u *op;
3079 char_u *next;
3080 int ppconst_used = ppconst->pp_used;
3081
3082 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003083 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003084 return FAIL;
3085
3086 /*
3087 * Repeat computing, until no "*", "/" or "%" is following.
3088 */
3089 for (;;)
3090 {
3091 op = may_peek_next_line(cctx, *arg, &next);
3092 if (*op != '*' && *op != '/' && *op != '%')
3093 break;
3094 if (next != NULL)
3095 {
3096 *arg = next_line_from_context(cctx, TRUE);
3097 op = skipwhite(*arg);
3098 }
3099
3100 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
3101 {
3102 error_white_both(op, 1);
3103 return FAIL;
3104 }
3105 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
3106 return FAIL;
3107
3108 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003109 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003110 return FAIL;
3111
3112 if (ppconst->pp_used == ppconst_used + 2
3113 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3114 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
3115 {
3116 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3117 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3118 varnumber_T res = 0;
3119 int failed = FALSE;
3120
3121 // both are numbers: compute the result
3122 switch (*op)
3123 {
3124 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
3125 break;
3126 case '/': res = num_divide(tv1->vval.v_number,
3127 tv2->vval.v_number, &failed);
3128 break;
3129 case '%': res = num_modulus(tv1->vval.v_number,
3130 tv2->vval.v_number, &failed);
3131 break;
3132 }
3133 if (failed)
3134 return FAIL;
3135 tv1->vval.v_number = res;
3136 --ppconst->pp_used;
3137 }
3138 else
3139 {
3140 generate_ppconst(cctx, ppconst);
3141 generate_two_op(cctx, op);
3142 }
3143 }
3144
3145 return OK;
3146}
3147
3148/*
3149 * + number addition or list/blobl concatenation
3150 * - number subtraction
3151 * .. string concatenation
3152 */
3153 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003154compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003155{
3156 char_u *op;
3157 char_u *next;
3158 int oplen;
3159 int ppconst_used = ppconst->pp_used;
3160
3161 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003162 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003163 return FAIL;
3164
3165 /*
3166 * Repeat computing, until no "+", "-" or ".." is following.
3167 */
3168 for (;;)
3169 {
3170 op = may_peek_next_line(cctx, *arg, &next);
3171 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
3172 break;
3173 if (op[0] == op[1] && *op != '.' && next)
3174 // Finding "++" or "--" on the next line is a separate command.
3175 // But ".." is concatenation.
3176 break;
3177 oplen = (*op == '.' ? 2 : 1);
3178 if (next != NULL)
3179 {
3180 *arg = next_line_from_context(cctx, TRUE);
3181 op = skipwhite(*arg);
3182 }
3183
3184 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3185 {
3186 error_white_both(op, oplen);
3187 return FAIL;
3188 }
3189
3190 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3191 return FAIL;
3192
3193 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003194 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003195 return FAIL;
3196
3197 if (ppconst->pp_used == ppconst_used + 2
3198 && (*op == '.'
3199 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3200 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3201 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3202 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3203 {
3204 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3205 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3206
3207 // concat/subtract/add constant numbers
3208 if (*op == '+')
3209 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3210 else if (*op == '-')
3211 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3212 else
3213 {
3214 // concatenate constant strings
3215 char_u *s1 = tv1->vval.v_string;
3216 char_u *s2 = tv2->vval.v_string;
3217 size_t len1 = STRLEN(s1);
3218
3219 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3220 if (tv1->vval.v_string == NULL)
3221 {
3222 clear_ppconst(ppconst);
3223 return FAIL;
3224 }
3225 mch_memmove(tv1->vval.v_string, s1, len1);
3226 STRCPY(tv1->vval.v_string + len1, s2);
3227 vim_free(s1);
3228 vim_free(s2);
3229 }
3230 --ppconst->pp_used;
3231 }
3232 else
3233 {
3234 generate_ppconst(cctx, ppconst);
3235 ppconst->pp_is_const = FALSE;
3236 if (*op == '.')
3237 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003238 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3239 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003240 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003241 if (generate_CONCAT(cctx, 2) == FAIL)
3242 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003243 }
3244 else
3245 generate_two_op(cctx, op);
3246 }
3247 }
3248
3249 return OK;
3250}
3251
3252/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003253 * expr6a >> expr6b
3254 * expr6a << expr6b
3255 *
3256 * Produces instructions:
3257 * OPNR bitwise left or right shift
3258 */
3259 static int
3260compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3261{
3262 exprtype_T type = EXPR_UNKNOWN;
3263 char_u *p;
3264 char_u *next;
3265 int len = 2;
3266 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003267 isn_T *isn;
3268
3269 // get the first variable
3270 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3271 return FAIL;
3272
3273 /*
3274 * Repeat computing, until no "+", "-" or ".." is following.
3275 */
3276 for (;;)
3277 {
3278 type = EXPR_UNKNOWN;
3279
3280 p = may_peek_next_line(cctx, *arg, &next);
3281 if (p[0] == '<' && p[1] == '<')
3282 type = EXPR_LSHIFT;
3283 else if (p[0] == '>' && p[1] == '>')
3284 type = EXPR_RSHIFT;
3285
3286 if (type == EXPR_UNKNOWN)
3287 return OK;
3288
3289 // Handle a bitwise left or right shift operator
3290 if (ppconst->pp_used == ppconst_used + 1)
3291 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003292 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003293 {
3294 // left operand should be a number
3295 emsg(_(e_bitshift_ops_must_be_number));
3296 return FAIL;
3297 }
3298 }
3299 else
3300 {
3301 type_T *t = get_type_on_stack(cctx, 0);
3302
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003303 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003304 {
3305 emsg(_(e_bitshift_ops_must_be_number));
3306 return FAIL;
3307 }
3308 }
3309
3310 if (next != NULL)
3311 {
3312 *arg = next_line_from_context(cctx, TRUE);
3313 p = skipwhite(*arg);
3314 }
3315
3316 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3317 {
3318 error_white_both(p, len);
3319 return FAIL;
3320 }
3321
3322 // get the second variable
3323 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3324 return FAIL;
3325
3326 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3327 return FAIL;
3328
3329 if (ppconst->pp_used == ppconst_used + 2)
3330 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003331 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3332 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3333
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003334 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003335 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3336 {
3337 // right operand should be a positive number
3338 if (tv2->v_type != VAR_NUMBER)
3339 emsg(_(e_bitshift_ops_must_be_number));
3340 else
dundargocc57b5bc2022-11-02 13:30:51 +00003341 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003342 return FAIL;
3343 }
3344
3345 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3346 tv1->vval.v_number = 0;
3347 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003348 tv1->vval.v_number =
3349 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003350 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003351 tv1->vval.v_number =
3352 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003353 clear_tv(tv2);
3354 --ppconst->pp_used;
3355 }
3356 else
3357 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003358 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3359 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003360 {
3361 emsg(_(e_bitshift_ops_must_be_number));
3362 return FAIL;
3363 }
3364
3365 generate_ppconst(cctx, ppconst);
3366
3367 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3368 if (isn == NULL)
3369 return FAIL;
3370
3371 if (isn != NULL)
3372 isn->isn_arg.op.op_type = type;
3373 }
3374 }
3375
3376 return OK;
3377}
3378
3379/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003380 * expr5a == expr5b
3381 * expr5a =~ expr5b
3382 * expr5a != expr5b
3383 * expr5a !~ expr5b
3384 * expr5a > expr5b
3385 * expr5a >= expr5b
3386 * expr5a < expr5b
3387 * expr5a <= expr5b
3388 * expr5a is expr5b
3389 * expr5a isnot expr5b
3390 *
3391 * Produces instructions:
3392 * EVAL expr5a Push result of "expr5a"
3393 * EVAL expr5b Push result of "expr5b"
3394 * COMPARE one of the compare instructions
3395 */
3396 static int
3397compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3398{
3399 exprtype_T type = EXPR_UNKNOWN;
3400 char_u *p;
3401 char_u *next;
3402 int len = 2;
3403 int type_is = FALSE;
3404 int ppconst_used = ppconst->pp_used;
3405
3406 // get the first variable
3407 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3408 return FAIL;
3409
3410 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003411
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003412 type = get_compare_type(p, &len, &type_is);
3413
3414 /*
3415 * If there is a comparative operator, use it.
3416 */
3417 if (type != EXPR_UNKNOWN)
3418 {
3419 int ic = FALSE; // Default: do not ignore case
3420
3421 if (next != NULL)
3422 {
3423 *arg = next_line_from_context(cctx, TRUE);
3424 p = skipwhite(*arg);
3425 }
3426 if (type_is && (p[len] == '?' || p[len] == '#'))
3427 {
3428 semsg(_(e_invalid_expression_str), *arg);
3429 return FAIL;
3430 }
3431 // extra question mark appended: ignore case
3432 if (p[len] == '?')
3433 {
3434 ic = TRUE;
3435 ++len;
3436 }
3437 // extra '#' appended: match case (ignored)
3438 else if (p[len] == '#')
3439 ++len;
3440 // nothing appended: match case
3441
3442 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3443 {
3444 error_white_both(p, len);
3445 return FAIL;
3446 }
3447
3448 // get the second variable
3449 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3450 return FAIL;
3451
3452 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3453 return FAIL;
3454
3455 if (ppconst->pp_used == ppconst_used + 2)
3456 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003457 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003458 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3459 int ret;
3460
3461 // Both sides are a constant, compute the result now.
3462 // First check for a valid combination of types, this is more
3463 // strict than typval_compare().
3464 if (check_compare_types(type, tv1, tv2) == FAIL)
3465 ret = FAIL;
3466 else
3467 {
3468 ret = typval_compare(tv1, tv2, type, ic);
3469 tv1->v_type = VAR_BOOL;
3470 tv1->vval.v_number = tv1->vval.v_number
3471 ? VVAL_TRUE : VVAL_FALSE;
3472 clear_tv(tv2);
3473 --ppconst->pp_used;
3474 }
3475 return ret;
3476 }
3477
3478 generate_ppconst(cctx, ppconst);
3479 return generate_COMPARE(cctx, type, ic);
3480 }
3481
3482 return OK;
3483}
3484
3485static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3486
3487/*
3488 * Compile || or &&.
3489 */
3490 static int
3491compile_and_or(
3492 char_u **arg,
3493 cctx_T *cctx,
3494 char *op,
3495 ppconst_T *ppconst,
3496 int ppconst_used UNUSED)
3497{
3498 char_u *next;
3499 char_u *p = may_peek_next_line(cctx, *arg, &next);
3500 int opchar = *op;
3501
3502 if (p[0] == opchar && p[1] == opchar)
3503 {
3504 garray_T *instr = &cctx->ctx_instr;
3505 garray_T end_ga;
3506 int save_skip = cctx->ctx_skip;
3507
3508 /*
3509 * Repeat until there is no following "||" or "&&"
3510 */
3511 ga_init2(&end_ga, sizeof(int), 10);
3512 while (p[0] == opchar && p[1] == opchar)
3513 {
3514 long start_lnum = SOURCING_LNUM;
3515 long save_sourcing_lnum;
3516 int start_ctx_lnum = cctx->ctx_lnum;
3517 int save_lnum;
3518 int const_used;
3519 int status;
3520 jumpwhen_T jump_when = opchar == '|'
3521 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3522
3523 if (next != NULL)
3524 {
3525 *arg = next_line_from_context(cctx, TRUE);
3526 p = skipwhite(*arg);
3527 }
3528
3529 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3530 {
3531 semsg(_(e_white_space_required_before_and_after_str_at_str),
3532 op, p);
3533 ga_clear(&end_ga);
3534 return FAIL;
3535 }
3536
3537 save_sourcing_lnum = SOURCING_LNUM;
3538 SOURCING_LNUM = start_lnum;
3539 save_lnum = cctx->ctx_lnum;
3540 cctx->ctx_lnum = start_ctx_lnum;
3541
3542 status = check_ppconst_bool(ppconst);
3543 if (status != FAIL)
3544 {
3545 // Use the last ppconst if possible.
3546 if (ppconst->pp_used > 0)
3547 {
3548 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3549 int is_true = tv2bool(tv);
3550
3551 if ((is_true && opchar == '|')
3552 || (!is_true && opchar == '&'))
3553 {
3554 // For "false && expr" and "true || expr" the "expr"
3555 // does not need to be evaluated.
3556 cctx->ctx_skip = SKIP_YES;
3557 clear_tv(tv);
3558 tv->v_type = VAR_BOOL;
3559 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3560 }
3561 else
3562 {
3563 // For "true && expr" and "false || expr" only "expr"
3564 // needs to be evaluated.
3565 --ppconst->pp_used;
3566 jump_when = JUMP_NEVER;
3567 }
3568 }
3569 else
3570 {
3571 // Every part must evaluate to a bool.
3572 status = bool_on_stack(cctx);
3573 }
3574 }
3575 if (status != FAIL)
3576 status = ga_grow(&end_ga, 1);
3577 cctx->ctx_lnum = save_lnum;
3578 if (status == FAIL)
3579 {
3580 ga_clear(&end_ga);
3581 return FAIL;
3582 }
3583
3584 if (jump_when != JUMP_NEVER)
3585 {
3586 if (cctx->ctx_skip != SKIP_YES)
3587 {
3588 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3589 ++end_ga.ga_len;
3590 }
3591 generate_JUMP(cctx, jump_when, 0);
3592 }
3593
3594 // eval the next expression
3595 SOURCING_LNUM = save_sourcing_lnum;
3596 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3597 {
3598 ga_clear(&end_ga);
3599 return FAIL;
3600 }
3601
3602 const_used = ppconst->pp_used;
3603 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3604 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3605 {
3606 ga_clear(&end_ga);
3607 return FAIL;
3608 }
3609
3610 // "0 || 1" results in true, "1 && 0" results in false.
3611 if (ppconst->pp_used == const_used + 1)
3612 {
3613 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3614
3615 if (tv->v_type == VAR_NUMBER
3616 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3617 {
3618 tv->vval.v_number = tv->vval.v_number == 1
3619 ? VVAL_TRUE : VVAL_FALSE;
3620 tv->v_type = VAR_BOOL;
3621 }
3622 }
3623
3624 p = may_peek_next_line(cctx, *arg, &next);
3625 }
3626
3627 if (check_ppconst_bool(ppconst) == FAIL)
3628 {
3629 ga_clear(&end_ga);
3630 return FAIL;
3631 }
3632
3633 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3634 // Every part must evaluate to a bool.
3635 if (bool_on_stack(cctx) == FAIL)
3636 {
3637 ga_clear(&end_ga);
3638 return FAIL;
3639 }
3640
3641 if (end_ga.ga_len > 0)
3642 {
3643 // Fill in the end label in all jumps.
3644 generate_ppconst(cctx, ppconst);
3645 while (end_ga.ga_len > 0)
3646 {
3647 isn_T *isn;
3648
3649 --end_ga.ga_len;
3650 isn = ((isn_T *)instr->ga_data)
3651 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3652 isn->isn_arg.jump.jump_where = instr->ga_len;
3653 }
3654 }
3655 ga_clear(&end_ga);
3656
3657 cctx->ctx_skip = save_skip;
3658 }
3659
3660 return OK;
3661}
3662
3663/*
3664 * expr4a && expr4a && expr4a logical AND
3665 *
3666 * Produces instructions:
3667 * EVAL expr4a Push result of "expr4a"
3668 * COND2BOOL convert to bool if needed
3669 * JUMP_IF_COND_FALSE end
3670 * EVAL expr4b Push result of "expr4b"
3671 * JUMP_IF_COND_FALSE end
3672 * EVAL expr4c Push result of "expr4c"
3673 * end:
3674 */
3675 static int
3676compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3677{
3678 int ppconst_used = ppconst->pp_used;
3679
3680 // get the first variable
3681 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3682 return FAIL;
3683
3684 // || and && work almost the same
3685 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3686}
3687
3688/*
3689 * expr3a || expr3b || expr3c logical OR
3690 *
3691 * Produces instructions:
3692 * EVAL expr3a Push result of "expr3a"
3693 * COND2BOOL convert to bool if needed
3694 * JUMP_IF_COND_TRUE end
3695 * EVAL expr3b Push result of "expr3b"
3696 * JUMP_IF_COND_TRUE end
3697 * EVAL expr3c Push result of "expr3c"
3698 * end:
3699 */
3700 static int
3701compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3702{
3703 int ppconst_used = ppconst->pp_used;
3704
3705 // eval the first expression
3706 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3707 return FAIL;
3708
3709 // || and && work almost the same
3710 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3711}
3712
3713/*
3714 * Toplevel expression: expr2 ? expr1a : expr1b
3715 * Produces instructions:
3716 * EVAL expr2 Push result of "expr2"
3717 * JUMP_IF_FALSE alt jump if false
3718 * EVAL expr1a
3719 * JUMP_ALWAYS end
3720 * alt: EVAL expr1b
3721 * end:
3722 *
3723 * Toplevel expression: expr2 ?? expr1
3724 * Produces instructions:
3725 * EVAL expr2 Push result of "expr2"
3726 * JUMP_AND_KEEP_IF_TRUE end jump if true
3727 * EVAL expr1
3728 * end:
3729 */
3730 int
3731compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3732{
3733 char_u *p;
3734 int ppconst_used = ppconst->pp_used;
3735 char_u *next;
3736
3737 // Ignore all kinds of errors when not producing code.
3738 if (cctx->ctx_skip == SKIP_YES)
3739 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003740 int prev_did_emsg = did_emsg;
3741
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003742 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003743 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003744 }
3745
3746 // Evaluate the first expression.
3747 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3748 return FAIL;
3749
3750 p = may_peek_next_line(cctx, *arg, &next);
3751 if (*p == '?')
3752 {
3753 int op_falsy = p[1] == '?';
3754 garray_T *instr = &cctx->ctx_instr;
3755 garray_T *stack = &cctx->ctx_type_stack;
3756 int alt_idx = instr->ga_len;
3757 int end_idx = 0;
3758 isn_T *isn;
3759 type_T *type1 = NULL;
3760 int has_const_expr = FALSE;
3761 int const_value = FALSE;
3762 int save_skip = cctx->ctx_skip;
3763
3764 if (next != NULL)
3765 {
3766 *arg = next_line_from_context(cctx, TRUE);
3767 p = skipwhite(*arg);
3768 }
3769
3770 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3771 {
3772 semsg(_(e_white_space_required_before_and_after_str_at_str),
3773 op_falsy ? "??" : "?", p);
3774 return FAIL;
3775 }
3776
3777 if (ppconst->pp_used == ppconst_used + 1)
3778 {
3779 // the condition is a constant, we know whether the ? or the :
3780 // expression is to be evaluated.
3781 has_const_expr = TRUE;
3782 if (op_falsy)
3783 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3784 else
3785 {
3786 int error = FALSE;
3787
3788 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3789 &error);
3790 if (error)
3791 return FAIL;
3792 }
3793 cctx->ctx_skip = save_skip == SKIP_YES ||
3794 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3795
3796 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3797 // "left ?? right" and "left" is truthy: produce "left"
3798 generate_ppconst(cctx, ppconst);
3799 else
3800 {
3801 clear_tv(&ppconst->pp_tv[ppconst_used]);
3802 --ppconst->pp_used;
3803 }
3804 }
3805 else
3806 {
3807 generate_ppconst(cctx, ppconst);
3808 if (op_falsy)
3809 end_idx = instr->ga_len;
3810 generate_JUMP(cctx, op_falsy
3811 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3812 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003813 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003814 }
3815
3816 // evaluate the second expression; any type is accepted
3817 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3818 return FAIL;
3819 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3820 return FAIL;
3821
3822 if (!has_const_expr)
3823 {
3824 generate_ppconst(cctx, ppconst);
3825
3826 if (!op_falsy)
3827 {
3828 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003829 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003830 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003831
3832 end_idx = instr->ga_len;
3833 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3834
3835 // jump here from JUMP_IF_FALSE
3836 isn = ((isn_T *)instr->ga_data) + alt_idx;
3837 isn->isn_arg.jump.jump_where = instr->ga_len;
3838 }
3839 }
3840
3841 if (!op_falsy)
3842 {
3843 // Check for the ":".
3844 p = may_peek_next_line(cctx, *arg, &next);
3845 if (*p != ':')
3846 {
3847 emsg(_(e_missing_colon_after_questionmark));
3848 return FAIL;
3849 }
3850 if (next != NULL)
3851 {
3852 *arg = next_line_from_context(cctx, TRUE);
3853 p = skipwhite(*arg);
3854 }
3855
3856 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3857 {
3858 semsg(_(e_white_space_required_before_and_after_str_at_str),
3859 ":", p);
3860 return FAIL;
3861 }
3862
3863 // evaluate the third expression
3864 if (has_const_expr)
3865 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3866 ? SKIP_YES : SKIP_NOT;
3867 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3868 return FAIL;
3869 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3870 return FAIL;
3871 }
3872
3873 if (!has_const_expr)
3874 {
3875 type_T **typep;
3876
3877 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003878 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003879
3880 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003881 typep = &((((type2_T *)stack->ga_data)
3882 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003883 common_type(type1, *typep, typep, cctx->ctx_type_list);
3884
3885 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3886 isn = ((isn_T *)instr->ga_data) + end_idx;
3887 isn->isn_arg.jump.jump_where = instr->ga_len;
3888 }
3889
3890 cctx->ctx_skip = save_skip;
3891 }
3892 return OK;
3893}
3894
3895/*
3896 * Toplevel expression.
3897 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3898 * Returns OK or FAIL.
3899 */
3900 int
3901compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3902{
3903 ppconst_T ppconst;
3904
3905 CLEAR_FIELD(ppconst);
3906 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3907 {
3908 clear_ppconst(&ppconst);
3909 return FAIL;
3910 }
3911 if (is_const != NULL)
3912 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3913 if (generate_ppconst(cctx, &ppconst) == FAIL)
3914 return FAIL;
3915 return OK;
3916}
3917
3918/*
3919 * Toplevel expression.
3920 */
3921 int
3922compile_expr0(char_u **arg, cctx_T *cctx)
3923{
3924 return compile_expr0_ext(arg, cctx, NULL);
3925}
3926
3927
3928#endif // defined(FEAT_EVAL)