blob: d169ed75ab6ea665b31662bf27fea7d36b4f2501 [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
Yegappan Lakshmanan69e6ab62025-04-22 19:55:38 +02001650 if (cctx->ctx_ufunc != NULL)
1651 // This lambda might be defined in a class method. Inherit the class
1652 // from the current function.
1653 ufunc->uf_defclass = cctx->ctx_ufunc->uf_defclass;
1654
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001655 // Compile it here to get the return type. The return type is optional,
1656 // when it's missing use t_unknown. This is recognized in
1657 // compile_return().
1658 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1659 ufunc->uf_ret_type = &t_unknown;
1660 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1661
1662 // When the outer function is compiled for profiling or debugging, the
1663 // lambda may be called without profiling or debugging. Compile it here in
1664 // the right context.
1665 if (cctx->ctx_compile_type == CT_DEBUG
1666#ifdef FEAT_PROFILE
1667 || cctx->ctx_compile_type == CT_PROFILE
1668#endif
1669 )
1670 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1671
Bram Moolenaar139575d2022-03-15 19:29:30 +00001672 // if the outer function is not compiled for debugging or profiling, this
1673 // one might be
1674 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001675 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001676 compiletype_T compile_type = get_compile_type(ufunc);
1677
1678 if (compile_type != CT_NONE)
1679 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001680 }
1681
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001682 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1683 // "*arg" may point into it. Point into the original line to avoid a
1684 // dangling pointer.
1685 if (evalarg.eval_using_cmdline)
1686 {
1687 garray_T *gap = &evalarg.eval_tofree_ga;
1688 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1689
1690 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1691 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001692 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001693 }
1694
1695 clear_evalarg(&evalarg, NULL);
1696
1697 if (ufunc->uf_def_status == UF_COMPILED)
1698 {
1699 // The return type will now be known.
1700 set_function_type(ufunc);
1701
1702 // The function reference count will be 1. When the ISN_FUNCREF
1703 // instruction is deleted the reference count is decremented and the
1704 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001705 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001706 }
1707
1708 func_ptr_unref(ufunc);
1709 return FAIL;
1710}
1711
1712/*
1713 * Get a lambda and compile it. Uses Vim9 syntax.
1714 */
1715 int
1716get_lambda_tv_and_compile(
1717 char_u **arg,
1718 typval_T *rettv,
1719 int types_optional,
1720 evalarg_T *evalarg)
1721{
1722 int r;
1723 ufunc_T *ufunc;
1724 int save_sc_version = current_sctx.sc_version;
1725
1726 // Get the funcref in "rettv".
1727 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1728 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1729 current_sctx.sc_version = save_sc_version;
1730 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001731 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732
1733 // "rettv" will now be a partial referencing the function.
1734 ufunc = rettv->vval.v_partial->pt_func;
1735
1736 // Compile it here to get the return type. The return type is optional,
1737 // when it's missing use t_unknown. This is recognized in
1738 // compile_return().
1739 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1740 ufunc->uf_ret_type = &t_unknown;
1741 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1742
1743 if (ufunc->uf_def_status == UF_COMPILED)
1744 {
1745 // The return type will now be known.
1746 set_function_type(ufunc);
1747 return OK;
1748 }
1749 clear_tv(rettv);
1750 return FAIL;
1751}
1752
1753/*
1754 * parse a dict: {key: val, [key]: val}
1755 * "*arg" points to the '{'.
1756 * ppconst->pp_is_const is set if all item values are a constant.
1757 */
1758 static int
1759compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1760{
1761 garray_T *instr = &cctx->ctx_instr;
1762 int count = 0;
1763 dict_T *d = dict_alloc();
1764 dictitem_T *item;
1765 char_u *whitep = *arg + 1;
1766 char_u *p;
1767 int is_const;
1768 int is_all_const = TRUE; // reset when non-const encountered
1769
1770 if (d == NULL)
1771 return FAIL;
1772 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001773 {
1774 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001776 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 for (;;)
1778 {
1779 char_u *key = NULL;
1780
1781 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1782 {
1783 *arg = NULL;
1784 goto failret;
1785 }
1786
1787 if (**arg == '}')
1788 break;
1789
1790 if (**arg == '[')
1791 {
1792 isn_T *isn;
1793
1794 // {[expr]: value} uses an evaluated key.
1795 *arg = skipwhite(*arg + 1);
1796 if (compile_expr0(arg, cctx) == FAIL)
1797 return FAIL;
1798 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1799 if (isn->isn_type == ISN_PUSHNR)
1800 {
1801 char buf[NUMBUFLEN];
1802
1803 // Convert to string at compile time.
1804 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1805 isn->isn_type = ISN_PUSHS;
1806 isn->isn_arg.string = vim_strsave((char_u *)buf);
1807 }
1808 if (isn->isn_type == ISN_PUSHS)
1809 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001810 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001811 return FAIL;
1812 *arg = skipwhite(*arg);
1813 if (**arg != ']')
1814 {
1815 emsg(_(e_missing_matching_bracket_after_dict_key));
1816 return FAIL;
1817 }
1818 ++*arg;
1819 }
1820 else
1821 {
1822 // {"name": value},
1823 // {'name': value},
1824 // {name: value} use "name" as a literal key
1825 key = get_literal_key(arg);
1826 if (key == NULL)
1827 return FAIL;
1828 if (generate_PUSHS(cctx, &key) == FAIL)
1829 return FAIL;
1830 }
1831
1832 // Check for duplicate keys, if using string keys.
1833 if (key != NULL)
1834 {
1835 item = dict_find(d, key, -1);
1836 if (item != NULL)
1837 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001838 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839 goto failret;
1840 }
1841 item = dictitem_alloc(key);
1842 if (item != NULL)
1843 {
1844 item->di_tv.v_type = VAR_UNKNOWN;
1845 item->di_tv.v_lock = 0;
1846 if (dict_add(d, item) == FAIL)
1847 dictitem_free(item);
1848 }
1849 }
1850
1851 if (**arg != ':')
1852 {
1853 if (*skipwhite(*arg) == ':')
1854 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1855 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001856 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001857 return FAIL;
1858 }
1859 whitep = *arg + 1;
1860 if (!IS_WHITE_OR_NUL(*whitep))
1861 {
1862 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1863 return FAIL;
1864 }
1865
1866 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1867 {
1868 *arg = NULL;
1869 goto failret;
1870 }
1871
1872 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1873 return FAIL;
1874 if (!is_const)
1875 is_all_const = FALSE;
1876 ++count;
1877
1878 whitep = *arg;
1879 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1880 {
1881 *arg = NULL;
1882 goto failret;
1883 }
1884 if (**arg == '}')
1885 break;
1886 if (**arg != ',')
1887 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001888 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889 goto failret;
1890 }
1891 if (IS_WHITE_OR_NUL(*whitep))
1892 {
1893 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1894 return FAIL;
1895 }
1896 whitep = *arg + 1;
1897 if (!IS_WHITE_OR_NUL(*whitep))
1898 {
1899 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1900 return FAIL;
1901 }
1902 *arg = skipwhite(whitep);
1903 }
1904
1905 *arg = *arg + 1;
1906
1907 // Allow for following comment, after at least one space.
1908 p = skipwhite(*arg);
1909 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1910 *arg += STRLEN(*arg);
1911
1912 dict_unref(d);
1913 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001914 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001915
1916failret:
1917 if (*arg == NULL)
1918 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001919 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920 *arg = (char_u *)"";
1921 }
1922 dict_unref(d);
1923 return FAIL;
1924}
1925
1926/*
1927 * Compile "&option".
1928 */
1929 static int
1930compile_get_option(char_u **arg, cctx_T *cctx)
1931{
1932 typval_T rettv;
1933 char_u *start = *arg;
1934 int ret;
1935
1936 // parse the option and get the current value to get the type.
1937 rettv.v_type = VAR_UNKNOWN;
1938 ret = eval_option(arg, &rettv, TRUE);
1939 if (ret == OK)
1940 {
1941 // include the '&' in the name, eval_option() expects it.
1942 char_u *name = vim_strnsave(start, *arg - start);
1943 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1944 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1945
1946 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1947 vim_free(name);
1948 }
1949 clear_tv(&rettv);
1950
1951 return ret;
1952}
1953
1954/*
1955 * Compile "$VAR".
1956 */
1957 static int
1958compile_get_env(char_u **arg, cctx_T *cctx)
1959{
1960 char_u *start = *arg;
1961 int len;
1962 int ret;
1963 char_u *name;
1964
1965 ++*arg;
1966 len = get_env_len(arg);
1967 if (len == 0)
1968 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001969 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001970 return FAIL;
1971 }
1972
1973 // include the '$' in the name, eval_env_var() expects it.
1974 name = vim_strnsave(start, len + 1);
1975 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1976 vim_free(name);
1977 return ret;
1978}
1979
1980/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001981 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001982 */
1983 static int
1984compile_interp_string(char_u **arg, cctx_T *cctx)
1985{
1986 typval_T tv;
1987 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001988 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001989 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001990 int count = 0;
1991 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001992
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001993 // *arg is on the '$' character, move it to the first string character.
1994 ++*arg;
1995 quote = **arg;
1996 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001997
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001998 for (;;)
1999 {
2000 // Get the string up to the matching quote or to a single '{'.
2001 // "arg" is advanced to either the quote or the '{'.
2002 if (quote == '"')
2003 ret = eval_string(arg, &tv, evaluate, TRUE);
2004 else
2005 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2006 if (ret == FAIL)
2007 break;
2008 if (evaluate)
2009 {
2010 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
2011 || (**arg != '{' && count == 0))
2012 {
2013 // generate non-empty string or empty string if it's the only
2014 // one
2015 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
2016 return FAIL;
2017 tv.vval.v_string = NULL; // don't free it now
2018 ++count;
2019 }
2020 clear_tv(&tv);
2021 }
2022
2023 if (**arg != '{')
2024 {
2025 // found terminating quote
2026 ++*arg;
2027 break;
2028 }
2029
2030 p = compile_one_expr_in_str(*arg, cctx);
2031 if (p == NULL)
2032 {
2033 ret = FAIL;
2034 break;
2035 }
2036 ++count;
2037 *arg = p;
2038 }
LemonBoy2eaef102022-05-06 13:14:50 +01002039
2040 if (ret == FAIL || !evaluate)
2041 return ret;
2042
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002043 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
2044 if (count > 1)
2045 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01002046
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002047 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002048}
2049
2050/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002051 * Compile "@r".
2052 */
2053 static int
2054compile_get_register(char_u **arg, cctx_T *cctx)
2055{
2056 int ret;
2057
2058 ++*arg;
2059 if (**arg == NUL)
2060 {
2061 semsg(_(e_syntax_error_at_str), *arg - 1);
2062 return FAIL;
2063 }
2064 if (!valid_yank_reg(**arg, FALSE))
2065 {
2066 emsg_invreg(**arg);
2067 return FAIL;
2068 }
2069 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2070 ++*arg;
2071 return ret;
2072}
2073
2074/*
2075 * Apply leading '!', '-' and '+' to constant "rettv".
2076 * When "numeric_only" is TRUE do not apply '!'.
2077 */
2078 static int
2079apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
2080{
2081 char_u *p = *end;
2082
2083 // this works from end to start
2084 while (p > start)
2085 {
2086 --p;
2087 if (*p == '-' || *p == '+')
2088 {
2089 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002090 if (rettv->v_type == VAR_FLOAT)
2091 {
2092 if (*p == '-')
2093 rettv->vval.v_float = -rettv->vval.v_float;
2094 }
2095 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 {
2097 varnumber_T val;
2098 int error = FALSE;
2099
2100 // tv_get_number_chk() accepts a string, but we don't want that
2101 // here
2102 if (check_not_string(rettv) == FAIL)
2103 return FAIL;
2104 val = tv_get_number_chk(rettv, &error);
2105 clear_tv(rettv);
2106 if (error)
2107 return FAIL;
2108 if (*p == '-')
2109 val = -val;
2110 rettv->v_type = VAR_NUMBER;
2111 rettv->vval.v_number = val;
2112 }
2113 }
2114 else if (numeric_only)
2115 {
2116 ++p;
2117 break;
2118 }
2119 else if (*p == '!')
2120 {
2121 int v = tv2bool(rettv);
2122
2123 // '!' is permissive in the type.
2124 clear_tv(rettv);
2125 rettv->v_type = VAR_BOOL;
2126 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2127 }
2128 }
2129 *end = p;
2130 return OK;
2131}
2132
2133/*
2134 * Recognize v: variables that are constants and set "rettv".
2135 */
2136 static void
2137get_vim_constant(char_u **arg, typval_T *rettv)
2138{
2139 if (STRNCMP(*arg, "v:true", 6) == 0)
2140 {
2141 rettv->v_type = VAR_BOOL;
2142 rettv->vval.v_number = VVAL_TRUE;
2143 *arg += 6;
2144 }
2145 else if (STRNCMP(*arg, "v:false", 7) == 0)
2146 {
2147 rettv->v_type = VAR_BOOL;
2148 rettv->vval.v_number = VVAL_FALSE;
2149 *arg += 7;
2150 }
2151 else if (STRNCMP(*arg, "v:null", 6) == 0)
2152 {
2153 rettv->v_type = VAR_SPECIAL;
2154 rettv->vval.v_number = VVAL_NULL;
2155 *arg += 6;
2156 }
2157 else if (STRNCMP(*arg, "v:none", 6) == 0)
2158 {
2159 rettv->v_type = VAR_SPECIAL;
2160 rettv->vval.v_number = VVAL_NONE;
2161 *arg += 6;
2162 }
2163}
2164
2165 exprtype_T
2166get_compare_type(char_u *p, int *len, int *type_is)
2167{
2168 exprtype_T type = EXPR_UNKNOWN;
2169 int i;
2170
2171 switch (p[0])
2172 {
2173 case '=': if (p[1] == '=')
2174 type = EXPR_EQUAL;
2175 else if (p[1] == '~')
2176 type = EXPR_MATCH;
2177 break;
2178 case '!': if (p[1] == '=')
2179 type = EXPR_NEQUAL;
2180 else if (p[1] == '~')
2181 type = EXPR_NOMATCH;
2182 break;
2183 case '>': if (p[1] != '=')
2184 {
2185 type = EXPR_GREATER;
2186 *len = 1;
2187 }
2188 else
2189 type = EXPR_GEQUAL;
2190 break;
2191 case '<': if (p[1] != '=')
2192 {
2193 type = EXPR_SMALLER;
2194 *len = 1;
2195 }
2196 else
2197 type = EXPR_SEQUAL;
2198 break;
2199 case 'i': if (p[1] == 's')
2200 {
2201 // "is" and "isnot"; but not a prefix of a name
2202 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2203 *len = 5;
2204 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002205 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 {
2207 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2208 *type_is = TRUE;
2209 }
2210 }
2211 break;
2212 }
2213 return type;
2214}
2215
2216/*
2217 * Skip over an expression, ignoring most errors.
2218 */
2219 void
2220skip_expr_cctx(char_u **arg, cctx_T *cctx)
2221{
2222 evalarg_T evalarg;
2223
2224 init_evalarg(&evalarg);
2225 evalarg.eval_cctx = cctx;
2226 skip_expr(arg, &evalarg);
2227 clear_evalarg(&evalarg, NULL);
2228}
2229
2230/*
2231 * Check that the top of the type stack has a type that can be used as a
2232 * condition. Give an error and return FAIL if not.
2233 */
2234 int
2235bool_on_stack(cctx_T *cctx)
2236{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237 type_T *type;
2238
Bram Moolenaar078a4612022-01-04 15:17:03 +00002239 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002240 if (type == &t_bool)
2241 return OK;
2242
Bram Moolenaar4913d422022-10-17 13:13:32 +01002243 if (type->tt_type == VAR_ANY
2244 || type->tt_type == VAR_UNKNOWN
2245 || type->tt_type == VAR_NUMBER
2246 || type == &t_number_bool
2247 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2249 // This requires a runtime type check.
2250 return generate_COND2BOOL(cctx);
2251
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002252 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002253}
2254
2255/*
2256 * Give the "white on both sides" error, taking the operator from "p[len]".
2257 */
2258 void
2259error_white_both(char_u *op, int len)
2260{
2261 char_u buf[10];
2262
2263 vim_strncpy(buf, op, len);
2264 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2265}
2266
2267/*
2268 * Compile code to apply '-', '+' and '!'.
2269 * When "numeric_only" is TRUE do not apply '!'.
2270 */
2271 static int
2272compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2273{
2274 char_u *p = *end;
2275
2276 // this works from end to start
2277 while (p > start)
2278 {
2279 --p;
2280 while (VIM_ISWHITE(*p))
2281 --p;
2282 if (*p == '-' || *p == '+')
2283 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002284 type_T *type = get_type_on_stack(cctx, 0);
2285 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002286 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 return FAIL;
2288
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002290 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2291 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002292 }
2293 else if (numeric_only)
2294 {
2295 ++p;
2296 break;
2297 }
2298 else
2299 {
2300 int invert = *p == '!';
2301
2302 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2303 {
2304 if (p[-1] == '!')
2305 invert = !invert;
2306 --p;
2307 }
2308 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2309 return FAIL;
2310 }
2311 }
2312 *end = p;
2313 return OK;
2314}
2315
2316/*
2317 * Compile "(expression)": recursive!
2318 * Return FAIL/OK.
2319 */
2320 static int
2321compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2322{
2323 int ret;
2324 char_u *p = *arg + 1;
2325
2326 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2327 return FAIL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002328
2329 if (**arg == ')')
2330 // empty tuple
2331 return compile_tuple(arg, cctx, ppconst, FALSE);
2332
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002333 if (ppconst->pp_used <= PPSIZE - 10)
2334 {
2335 ret = compile_expr1(arg, cctx, ppconst);
2336 }
2337 else
2338 {
2339 // Not enough space in ppconst, flush constants.
2340 if (generate_ppconst(cctx, ppconst) == FAIL)
2341 return FAIL;
2342 ret = compile_expr0(arg, cctx);
2343 }
2344 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2345 return FAIL;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002346 if (ret == OK && **arg == ',')
2347 {
2348 // tuple
2349 int is_const = ppconst->pp_used > 0 || ppconst->pp_is_const;
2350 if (generate_ppconst(cctx, ppconst) == FAIL)
2351 return FAIL;
2352 return compile_tuple(arg, cctx, ppconst, is_const);
2353 }
2354
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002355 if (**arg == ')')
2356 ++*arg;
2357 else if (ret == OK)
2358 {
2359 emsg(_(e_missing_closing_paren));
2360 ret = FAIL;
2361 }
2362 return ret;
2363}
2364
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002365static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002366
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002367/*
2368 * Compile whatever comes after "name" or "name()".
2369 * Advances "*arg" only when something was recognized.
2370 */
2371 static int
2372compile_subscript(
2373 char_u **arg,
2374 cctx_T *cctx,
2375 char_u *start_leader,
2376 char_u **end_leader,
2377 ppconst_T *ppconst)
2378{
2379 char_u *name_start = *end_leader;
2380 int keeping_dict = FALSE;
2381
2382 for (;;)
2383 {
2384 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002385 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002386
2387 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2388 {
2389 char_u *next = peek_next_line_from_context(cctx);
2390
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002391 // If a following line starts with "->{", "->(" or "->X" advance to
2392 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 // Also if a following line starts with ".x".
2394 if (next != NULL &&
2395 ((next[0] == '-' && next[1] == '>'
2396 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002397 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002398 || ASCII_ISALPHA(*skipwhite(next + 2))))
2399 || (next[0] == '.' && eval_isdictc(next[1]))))
2400 {
2401 next = next_line_from_context(cctx, TRUE);
2402 if (next == NULL)
2403 return FAIL;
2404 *arg = next;
2405 p = skipwhite(*arg);
2406 }
2407 }
2408
2409 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2410 // is not a function call.
2411 if (**arg == '(')
2412 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002413 int argcount = 0;
2414
2415 if (generate_ppconst(cctx, ppconst) == FAIL)
2416 return FAIL;
2417 ppconst->pp_is_const = FALSE;
2418
2419 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002420 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421
2422 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002423 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424 return FAIL;
2425 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2426 return FAIL;
2427 if (keeping_dict)
2428 {
2429 keeping_dict = FALSE;
2430 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2431 return FAIL;
2432 }
2433 }
2434 else if (*p == '-' && p[1] == '>')
2435 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002436 char_u *pstart = p;
2437 int alt;
2438 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002439
Bram Moolenaarc7349932022-01-16 20:59:39 +00002440 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 if (generate_ppconst(cctx, ppconst) == FAIL)
2442 return FAIL;
2443 ppconst->pp_is_const = FALSE;
2444
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002445 // Apply the '!', '-' and '+' first:
2446 // -1.0->func() works like (-1.0)->func()
2447 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2448 return FAIL;
2449
2450 p += 2;
2451 *arg = skipwhite(p);
2452 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002453
2454 // Three alternatives handled here:
2455 // 1. "base->name(" only a name, use compile_call()
2456 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2457 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002458 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002459 alt = 2;
2460 else
2461 {
2462 // alternative 1 or 3
2463 p = *arg;
2464 if (!eval_isnamec1(*p))
2465 {
2466 semsg(_(e_trailing_characters_str), pstart);
2467 return FAIL;
2468 }
2469 if (ASCII_ISALPHA(*p) && p[1] == ':')
2470 p += 2;
2471 for ( ; eval_isnamec(*p); ++p)
2472 ;
2473 if (*p == '(')
2474 {
2475 // alternative 1
2476 alt = 1;
2477 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2478 return FAIL;
2479 }
2480 else
2481 {
2482 // Must be alternative 3, find the "(". Only works within
2483 // one line.
2484 alt = 3;
2485 paren = vim_strchr(p, '(');
2486 if (paren == NULL)
2487 {
2488 semsg(_(e_missing_parenthesis_str), *arg);
2489 return FAIL;
2490 }
2491 }
2492 }
2493
2494 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002495 {
2496 int argcount = 1;
2497 garray_T *stack = &cctx->ctx_type_stack;
2498 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002499 int expr_isn_start = cctx->ctx_instr.ga_len;
2500 int expr_isn_end;
2501 int arg_isn_count;
2502
Bram Moolenaarc7349932022-01-16 20:59:39 +00002503 if (alt == 2)
2504 {
2505 // Funcref call: list->(Refs[2])(arg)
2506 // or lambda: list->((arg) => expr)(arg)
2507 //
2508 // Fist compile the function expression.
2509 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2510 return FAIL;
2511 }
2512 else
2513 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002514 int fail;
2515 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002516 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002517
Bram Moolenaarc7349932022-01-16 20:59:39 +00002518 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002519
2520 // instead of using LOADG for "import.Func" use PUSHFUNC
2521 ++paren_follows_after_expr;
2522
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002523 // do not look in the next line
2524 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002525
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002526 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002527 || *skipwhite(*arg) != NUL;
2528 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002529 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002530 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002531
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002532 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002533 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002534 if (did_emsg == prev_did_emsg)
2535 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002536 return FAIL;
2537 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002538 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002540 // Compile the arguments.
2541 if (**arg != '(')
2542 {
2543 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002544 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 else
2546 semsg(_(e_missing_parenthesis_str), *arg);
2547 return FAIL;
2548 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002549
2550 // Remember the next instruction index, where the instructions
2551 // for arguments are being written.
2552 expr_isn_end = cctx->ctx_instr.ga_len;
2553
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002554 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002555 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2556 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002557 return FAIL;
2558
2559 // Move the instructions for the arguments to before the
2560 // instructions of the expression and move the type of the
2561 // expression after the argument types. This is what ISN_PCALL
2562 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2564 if (arg_isn_count > 0)
2565 {
2566 int expr_isn_count = expr_isn_end - expr_isn_start;
2567 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002568 type_T *decl_type;
2569 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570
2571 if (isn == NULL)
2572 return FAIL;
2573 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2574 + expr_isn_start,
2575 sizeof(isn_T) * expr_isn_count);
2576 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2577 + expr_isn_start,
2578 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2579 sizeof(isn_T) * arg_isn_count);
2580 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2581 + expr_isn_start + arg_isn_count,
2582 isn, sizeof(isn_T) * expr_isn_count);
2583 vim_free(isn);
2584
Bram Moolenaar078a4612022-01-04 15:17:03 +00002585 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2586 type = typep->type_curr;
2587 decl_type = typep->type_decl;
2588 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2589 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2590 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002592 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2593 typep->type_curr = type;
2594 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002595 }
2596
Bram Moolenaar078a4612022-01-04 15:17:03 +00002597 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002598 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2599 return FAIL;
2600 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002601
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002602 if (keeping_dict)
2603 {
2604 keeping_dict = FALSE;
2605 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2606 return FAIL;
2607 }
2608 }
2609 else if (**arg == '[')
2610 {
2611 int is_slice = FALSE;
2612
2613 // list index: list[123]
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002614 // tuple index: tuple[123]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615 // dict member: dict[key]
2616 // string index: text[123]
2617 // blob index: blob[123]
2618 if (generate_ppconst(cctx, ppconst) == FAIL)
2619 return FAIL;
2620 ppconst->pp_is_const = FALSE;
2621
2622 ++p;
2623 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2624 return FAIL;
2625 if (**arg == ':')
2626 {
2627 // missing first index is equal to zero
2628 generate_PUSHNR(cctx, 0);
2629 }
2630 else
2631 {
2632 if (compile_expr0(arg, cctx) == FAIL)
2633 return FAIL;
2634 if (**arg == ':')
2635 {
2636 semsg(_(e_white_space_required_before_and_after_str_at_str),
2637 ":", *arg);
2638 return FAIL;
2639 }
2640 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2641 return FAIL;
2642 *arg = skipwhite(*arg);
2643 }
2644 if (**arg == ':')
2645 {
2646 is_slice = TRUE;
2647 ++*arg;
2648 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2649 {
2650 semsg(_(e_white_space_required_before_and_after_str_at_str),
2651 ":", *arg);
2652 return FAIL;
2653 }
2654 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2655 return FAIL;
2656 if (**arg == ']')
2657 // missing second index is equal to end of string
2658 generate_PUSHNR(cctx, -1);
2659 else
2660 {
2661 if (compile_expr0(arg, cctx) == FAIL)
2662 return FAIL;
2663 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2664 return FAIL;
2665 *arg = skipwhite(*arg);
2666 }
2667 }
2668
2669 if (**arg != ']')
2670 {
2671 emsg(_(e_missing_closing_square_brace));
2672 return FAIL;
2673 }
2674 *arg = *arg + 1;
2675
2676 if (keeping_dict)
2677 {
2678 keeping_dict = FALSE;
2679 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2680 return FAIL;
2681 }
2682 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2683 return FAIL;
2684 }
2685 else if (*p == '.' && p[1] != '.')
2686 {
2687 // dictionary member: dict.name
2688 if (generate_ppconst(cctx, ppconst) == FAIL)
2689 return FAIL;
2690 ppconst->pp_is_const = FALSE;
2691
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002692 type = get_type_on_stack(cctx, 0);
2693 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002694 && (type->tt_type == VAR_CLASS
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002695 || (type->tt_type == VAR_OBJECT
2696 && type != &t_object_any)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002698 // class member: SomeClass.varname
2699 // class method: SomeClass.SomeMethod()
2700 // class constructor: SomeClass.new()
2701 // object member: someObject.varname, this.varname
2702 // object method: someObject.SomeMethod(), this.SomeMethod()
2703 *arg = p;
2704 if (compile_class_object_index(cctx, arg, type) == FAIL)
2705 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002706 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002707 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002709 *arg = p + 1;
2710 if (IS_WHITE_OR_NUL(**arg))
2711 {
2712 emsg(_(e_missing_name_after_dot));
2713 return FAIL;
2714 }
2715 p = *arg;
2716 if (eval_isdictc(*p))
2717 while (eval_isnamec(*p))
2718 MB_PTR_ADV(p);
2719 if (p == *arg)
2720 {
2721 semsg(_(e_syntax_error_at_str), *arg);
2722 return FAIL;
2723 }
2724 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2725 return FAIL;
2726 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2727 return FAIL;
2728 keeping_dict = TRUE;
2729 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002730 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 }
2732 else
2733 break;
2734 }
2735
2736 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2737 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002738 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2739 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002740 return FAIL;
2741
2742 return OK;
2743}
2744
2745/*
2746 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2747 * "arg" is advanced until after the expression, skipping white space.
2748 *
2749 * If the value is a constant "ppconst->pp_used" will be non-zero.
2750 * Before instructions are generated, any values in "ppconst" will generated.
2751 *
2752 * This is the compiling equivalent of eval1(), eval2(), etc.
2753 */
2754
2755/*
2756 * number number constant
2757 * 0zFFFFFFFF Blob constant
2758 * "string" string constant
2759 * 'string' literal string constant
2760 * &option-name option value
2761 * @r register contents
2762 * identifier variable value
2763 * function() function call
2764 * $VAR environment variable
2765 * (expression) nested expression
2766 * [expr, expr] List
2767 * {key: val, [key]: val} Dictionary
2768 *
2769 * Also handle:
2770 * ! in front logical NOT
2771 * - in front unary minus
2772 * + in front unary plus (ignored)
2773 * trailing (arg) funcref/partial call
2774 * trailing [] subscript in String or List
2775 * trailing .name entry in Dictionary
2776 * trailing ->name() method call
2777 */
2778 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002779compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002780 char_u **arg,
2781 cctx_T *cctx,
2782 ppconst_T *ppconst)
2783{
2784 char_u *start_leader, *end_leader;
2785 int ret = OK;
2786 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2787 int used_before = ppconst->pp_used;
2788
2789 ppconst->pp_is_const = FALSE;
2790
2791 /*
2792 * Skip '!', '-' and '+' characters. They are handled later.
2793 */
2794 start_leader = *arg;
2795 if (eval_leader(arg, TRUE) == FAIL)
2796 return FAIL;
2797 end_leader = *arg;
2798
2799 rettv->v_type = VAR_UNKNOWN;
2800 switch (**arg)
2801 {
2802 /*
2803 * Number constant.
2804 */
2805 case '0': // also for blob starting with 0z
2806 case '1':
2807 case '2':
2808 case '3':
2809 case '4':
2810 case '5':
2811 case '6':
2812 case '7':
2813 case '8':
2814 case '9':
2815 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2816 return FAIL;
2817 // Apply "-" and "+" just before the number now, right to
2818 // left. Matters especially when "->" follows. Stops at
2819 // '!'.
2820 if (apply_leader(rettv, TRUE,
2821 start_leader, &end_leader) == FAIL)
2822 {
2823 clear_tv(rettv);
2824 return FAIL;
2825 }
2826 break;
2827
2828 /*
2829 * String constant: "string".
2830 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002831 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002832 return FAIL;
2833 break;
2834
2835 /*
2836 * Literal string constant: 'str''ing'.
2837 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002838 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002839 return FAIL;
2840 break;
2841
2842 /*
2843 * Constant Vim variable.
2844 */
2845 case 'v': get_vim_constant(arg, rettv);
2846 ret = NOTDONE;
2847 break;
2848
2849 /*
2850 * "true" constant
2851 */
2852 case 't': if (STRNCMP(*arg, "true", 4) == 0
2853 && !eval_isnamec((*arg)[4]))
2854 {
2855 *arg += 4;
2856 rettv->v_type = VAR_BOOL;
2857 rettv->vval.v_number = VVAL_TRUE;
2858 }
2859 else
2860 ret = NOTDONE;
2861 break;
2862
2863 /*
2864 * "false" constant
2865 */
2866 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2867 && !eval_isnamec((*arg)[5]))
2868 {
2869 *arg += 5;
2870 rettv->v_type = VAR_BOOL;
2871 rettv->vval.v_number = VVAL_FALSE;
2872 }
2873 else
2874 ret = NOTDONE;
2875 break;
2876
2877 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002878 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002879 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002880 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002881 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002882 char_u *p = *arg + 4;
2883 int len;
2884
2885 for (len = 0; eval_isnamec(p[len]); ++len)
2886 ;
2887 ret = handle_predefined(*arg, len + 4, rettv);
2888 if (ret == FAIL)
2889 ret = NOTDONE;
2890 else
2891 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002892 }
2893 else
2894 ret = NOTDONE;
2895 break;
2896
2897 /*
2898 * List: [expr, expr]
2899 */
2900 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2901 return FAIL;
2902 ret = compile_list(arg, cctx, ppconst);
2903 break;
2904
2905 /*
2906 * Dictionary: {'key': val, 'key': val}
2907 */
2908 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2909 return FAIL;
2910 ret = compile_dict(arg, cctx, ppconst);
2911 break;
2912
2913 /*
2914 * Option value: &name
2915 */
2916 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2917 return FAIL;
2918 ret = compile_get_option(arg, cctx);
2919 break;
2920
2921 /*
2922 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002923 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002924 */
2925 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2926 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002927 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2928 ret = compile_interp_string(arg, cctx);
2929 else
2930 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002931 break;
2932
2933 /*
2934 * Register contents: @r.
2935 */
2936 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2937 return FAIL;
2938 ret = compile_get_register(arg, cctx);
2939 break;
2940 /*
2941 * nested expression: (expression).
2942 * lambda: (arg, arg) => expr
2943 * funcref: (arg, arg) => { statement }
2944 */
2945 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2946 ret = compile_lambda(arg, cctx);
2947 if (ret == NOTDONE)
2948 ret = compile_parenthesis(arg, cctx, ppconst);
2949 break;
2950
2951 default: ret = NOTDONE;
2952 break;
2953 }
2954 if (ret == FAIL)
2955 return FAIL;
2956
2957 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2958 {
2959 if (cctx->ctx_skip == SKIP_YES)
2960 clear_tv(rettv);
2961 else
2962 // A constant expression can possibly be handled compile time,
2963 // return the value instead of generating code.
2964 ++ppconst->pp_used;
2965 }
2966 else if (ret == NOTDONE)
2967 {
2968 char_u *p;
2969 int r;
2970
2971 if (!eval_isnamec1(**arg))
2972 {
2973 if (!vim9_bad_comment(*arg))
2974 {
2975 if (ends_excmd(*skipwhite(*arg)))
2976 semsg(_(e_empty_expression_str), *arg);
2977 else
2978 semsg(_(e_name_expected_str), *arg);
2979 }
2980 return FAIL;
2981 }
2982
2983 // "name" or "name()"
2984 p = to_name_end(*arg, TRUE);
2985 if (p - *arg == (size_t)1 && **arg == '_')
2986 {
2987 emsg(_(e_cannot_use_underscore_here));
2988 return FAIL;
2989 }
2990
2991 if (*p == '(')
2992 {
2993 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2994 }
2995 else
2996 {
2997 if (cctx->ctx_skip != SKIP_YES
2998 && generate_ppconst(cctx, ppconst) == FAIL)
2999 return FAIL;
3000 r = compile_load(arg, p, cctx, TRUE, TRUE);
3001 }
3002 if (r == FAIL)
3003 return FAIL;
3004 }
3005
3006 // Handle following "[]", ".member", etc.
3007 // Then deal with prefixed '-', '+' and '!', if not done already.
3008 if (compile_subscript(arg, cctx, start_leader, &end_leader,
3009 ppconst) == FAIL)
3010 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01003011 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003012 {
3013 // apply the '!', '-' and '+' before the constant
3014 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
3015 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
3016 return FAIL;
3017 return OK;
3018 }
3019 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
3020 return FAIL;
3021 return OK;
3022}
3023
3024/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003025 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003026 */
3027 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003028compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003029{
3030 type_T *want_type = NULL;
3031
3032 // Recognize <type>
3033 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3034 {
3035 ++*arg;
3036 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
3037 if (want_type == NULL)
3038 return FAIL;
3039
3040 if (**arg != '>')
3041 {
3042 if (*skipwhite(*arg) == '>')
3043 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3044 else
3045 emsg(_(e_missing_gt));
3046 return FAIL;
3047 }
3048 ++*arg;
3049 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3050 return FAIL;
3051 }
3052
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003053 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003054 return FAIL;
3055
3056 if (want_type != NULL)
3057 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003058 type_T *actual;
3059 where_T where = WHERE_INIT;
3060
LemonBoy50d48542024-07-04 17:03:17 +02003061 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003062 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00003063 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00003064 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003065 {
LemonBoy50d48542024-07-04 17:03:17 +02003066 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
3067 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003068 return FAIL;
3069 }
3070 }
3071
3072 return OK;
3073}
3074
3075/*
3076 * * number multiplication
3077 * / number division
3078 * % number modulo
3079 */
3080 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003081compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003082{
3083 char_u *op;
3084 char_u *next;
3085 int ppconst_used = ppconst->pp_used;
3086
3087 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003088 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003089 return FAIL;
3090
3091 /*
3092 * Repeat computing, until no "*", "/" or "%" is following.
3093 */
3094 for (;;)
3095 {
3096 op = may_peek_next_line(cctx, *arg, &next);
3097 if (*op != '*' && *op != '/' && *op != '%')
3098 break;
3099 if (next != NULL)
3100 {
3101 *arg = next_line_from_context(cctx, TRUE);
3102 op = skipwhite(*arg);
3103 }
3104
3105 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
3106 {
3107 error_white_both(op, 1);
3108 return FAIL;
3109 }
3110 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
3111 return FAIL;
3112
3113 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003114 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003115 return FAIL;
3116
3117 if (ppconst->pp_used == ppconst_used + 2
3118 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3119 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
3120 {
3121 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3122 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3123 varnumber_T res = 0;
3124 int failed = FALSE;
3125
3126 // both are numbers: compute the result
3127 switch (*op)
3128 {
3129 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
3130 break;
3131 case '/': res = num_divide(tv1->vval.v_number,
3132 tv2->vval.v_number, &failed);
3133 break;
3134 case '%': res = num_modulus(tv1->vval.v_number,
3135 tv2->vval.v_number, &failed);
3136 break;
3137 }
3138 if (failed)
3139 return FAIL;
3140 tv1->vval.v_number = res;
3141 --ppconst->pp_used;
3142 }
3143 else
3144 {
3145 generate_ppconst(cctx, ppconst);
3146 generate_two_op(cctx, op);
3147 }
3148 }
3149
3150 return OK;
3151}
3152
3153/*
3154 * + number addition or list/blobl concatenation
3155 * - number subtraction
3156 * .. string concatenation
3157 */
3158 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003159compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003160{
3161 char_u *op;
3162 char_u *next;
3163 int oplen;
3164 int ppconst_used = ppconst->pp_used;
3165
3166 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003167 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003168 return FAIL;
3169
3170 /*
3171 * Repeat computing, until no "+", "-" or ".." is following.
3172 */
3173 for (;;)
3174 {
3175 op = may_peek_next_line(cctx, *arg, &next);
3176 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
3177 break;
3178 if (op[0] == op[1] && *op != '.' && next)
3179 // Finding "++" or "--" on the next line is a separate command.
3180 // But ".." is concatenation.
3181 break;
3182 oplen = (*op == '.' ? 2 : 1);
3183 if (next != NULL)
3184 {
3185 *arg = next_line_from_context(cctx, TRUE);
3186 op = skipwhite(*arg);
3187 }
3188
3189 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3190 {
3191 error_white_both(op, oplen);
3192 return FAIL;
3193 }
3194
3195 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3196 return FAIL;
3197
3198 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003199 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003200 return FAIL;
3201
3202 if (ppconst->pp_used == ppconst_used + 2
3203 && (*op == '.'
3204 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3205 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3206 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3207 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3208 {
3209 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3210 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3211
3212 // concat/subtract/add constant numbers
3213 if (*op == '+')
3214 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3215 else if (*op == '-')
3216 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3217 else
3218 {
3219 // concatenate constant strings
3220 char_u *s1 = tv1->vval.v_string;
3221 char_u *s2 = tv2->vval.v_string;
3222 size_t len1 = STRLEN(s1);
3223
3224 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3225 if (tv1->vval.v_string == NULL)
3226 {
3227 clear_ppconst(ppconst);
3228 return FAIL;
3229 }
3230 mch_memmove(tv1->vval.v_string, s1, len1);
3231 STRCPY(tv1->vval.v_string + len1, s2);
3232 vim_free(s1);
3233 vim_free(s2);
3234 }
3235 --ppconst->pp_used;
3236 }
3237 else
3238 {
3239 generate_ppconst(cctx, ppconst);
3240 ppconst->pp_is_const = FALSE;
3241 if (*op == '.')
3242 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003243 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3244 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003245 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003246 if (generate_CONCAT(cctx, 2) == FAIL)
3247 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003248 }
3249 else
3250 generate_two_op(cctx, op);
3251 }
3252 }
3253
3254 return OK;
3255}
3256
3257/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003258 * expr6a >> expr6b
3259 * expr6a << expr6b
3260 *
3261 * Produces instructions:
3262 * OPNR bitwise left or right shift
3263 */
3264 static int
3265compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3266{
3267 exprtype_T type = EXPR_UNKNOWN;
3268 char_u *p;
3269 char_u *next;
3270 int len = 2;
3271 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003272 isn_T *isn;
3273
3274 // get the first variable
3275 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3276 return FAIL;
3277
3278 /*
3279 * Repeat computing, until no "+", "-" or ".." is following.
3280 */
3281 for (;;)
3282 {
3283 type = EXPR_UNKNOWN;
3284
3285 p = may_peek_next_line(cctx, *arg, &next);
3286 if (p[0] == '<' && p[1] == '<')
3287 type = EXPR_LSHIFT;
3288 else if (p[0] == '>' && p[1] == '>')
3289 type = EXPR_RSHIFT;
3290
3291 if (type == EXPR_UNKNOWN)
3292 return OK;
3293
3294 // Handle a bitwise left or right shift operator
3295 if (ppconst->pp_used == ppconst_used + 1)
3296 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003297 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003298 {
3299 // left operand should be a number
3300 emsg(_(e_bitshift_ops_must_be_number));
3301 return FAIL;
3302 }
3303 }
3304 else
3305 {
3306 type_T *t = get_type_on_stack(cctx, 0);
3307
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003308 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003309 {
3310 emsg(_(e_bitshift_ops_must_be_number));
3311 return FAIL;
3312 }
3313 }
3314
3315 if (next != NULL)
3316 {
3317 *arg = next_line_from_context(cctx, TRUE);
3318 p = skipwhite(*arg);
3319 }
3320
3321 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3322 {
3323 error_white_both(p, len);
3324 return FAIL;
3325 }
3326
3327 // get the second variable
3328 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3329 return FAIL;
3330
3331 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3332 return FAIL;
3333
3334 if (ppconst->pp_used == ppconst_used + 2)
3335 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003336 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3337 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3338
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003339 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003340 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3341 {
3342 // right operand should be a positive number
3343 if (tv2->v_type != VAR_NUMBER)
3344 emsg(_(e_bitshift_ops_must_be_number));
3345 else
dundargocc57b5bc2022-11-02 13:30:51 +00003346 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003347 return FAIL;
3348 }
3349
3350 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3351 tv1->vval.v_number = 0;
3352 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003353 tv1->vval.v_number =
3354 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003355 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003356 tv1->vval.v_number =
3357 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003358 clear_tv(tv2);
3359 --ppconst->pp_used;
3360 }
3361 else
3362 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003363 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3364 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003365 {
3366 emsg(_(e_bitshift_ops_must_be_number));
3367 return FAIL;
3368 }
3369
3370 generate_ppconst(cctx, ppconst);
3371
3372 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3373 if (isn == NULL)
3374 return FAIL;
3375
3376 if (isn != NULL)
3377 isn->isn_arg.op.op_type = type;
3378 }
3379 }
3380
3381 return OK;
3382}
3383
3384/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003385 * expr5a == expr5b
3386 * expr5a =~ expr5b
3387 * expr5a != expr5b
3388 * expr5a !~ expr5b
3389 * expr5a > expr5b
3390 * expr5a >= expr5b
3391 * expr5a < expr5b
3392 * expr5a <= expr5b
3393 * expr5a is expr5b
3394 * expr5a isnot expr5b
3395 *
3396 * Produces instructions:
3397 * EVAL expr5a Push result of "expr5a"
3398 * EVAL expr5b Push result of "expr5b"
3399 * COMPARE one of the compare instructions
3400 */
3401 static int
3402compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3403{
3404 exprtype_T type = EXPR_UNKNOWN;
3405 char_u *p;
3406 char_u *next;
3407 int len = 2;
3408 int type_is = FALSE;
3409 int ppconst_used = ppconst->pp_used;
3410
3411 // get the first variable
3412 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3413 return FAIL;
3414
3415 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003416
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003417 type = get_compare_type(p, &len, &type_is);
3418
3419 /*
3420 * If there is a comparative operator, use it.
3421 */
3422 if (type != EXPR_UNKNOWN)
3423 {
3424 int ic = FALSE; // Default: do not ignore case
3425
3426 if (next != NULL)
3427 {
3428 *arg = next_line_from_context(cctx, TRUE);
3429 p = skipwhite(*arg);
3430 }
3431 if (type_is && (p[len] == '?' || p[len] == '#'))
3432 {
3433 semsg(_(e_invalid_expression_str), *arg);
3434 return FAIL;
3435 }
3436 // extra question mark appended: ignore case
3437 if (p[len] == '?')
3438 {
3439 ic = TRUE;
3440 ++len;
3441 }
3442 // extra '#' appended: match case (ignored)
3443 else if (p[len] == '#')
3444 ++len;
3445 // nothing appended: match case
3446
3447 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3448 {
3449 error_white_both(p, len);
3450 return FAIL;
3451 }
3452
3453 // get the second variable
3454 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3455 return FAIL;
3456
3457 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3458 return FAIL;
3459
3460 if (ppconst->pp_used == ppconst_used + 2)
3461 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003462 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003463 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3464 int ret;
3465
3466 // Both sides are a constant, compute the result now.
3467 // First check for a valid combination of types, this is more
3468 // strict than typval_compare().
3469 if (check_compare_types(type, tv1, tv2) == FAIL)
3470 ret = FAIL;
3471 else
3472 {
3473 ret = typval_compare(tv1, tv2, type, ic);
3474 tv1->v_type = VAR_BOOL;
3475 tv1->vval.v_number = tv1->vval.v_number
3476 ? VVAL_TRUE : VVAL_FALSE;
3477 clear_tv(tv2);
3478 --ppconst->pp_used;
3479 }
3480 return ret;
3481 }
3482
3483 generate_ppconst(cctx, ppconst);
3484 return generate_COMPARE(cctx, type, ic);
3485 }
3486
3487 return OK;
3488}
3489
3490static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3491
3492/*
3493 * Compile || or &&.
3494 */
3495 static int
3496compile_and_or(
3497 char_u **arg,
3498 cctx_T *cctx,
3499 char *op,
3500 ppconst_T *ppconst,
3501 int ppconst_used UNUSED)
3502{
3503 char_u *next;
3504 char_u *p = may_peek_next_line(cctx, *arg, &next);
3505 int opchar = *op;
3506
3507 if (p[0] == opchar && p[1] == opchar)
3508 {
3509 garray_T *instr = &cctx->ctx_instr;
3510 garray_T end_ga;
3511 int save_skip = cctx->ctx_skip;
3512
3513 /*
3514 * Repeat until there is no following "||" or "&&"
3515 */
3516 ga_init2(&end_ga, sizeof(int), 10);
3517 while (p[0] == opchar && p[1] == opchar)
3518 {
3519 long start_lnum = SOURCING_LNUM;
3520 long save_sourcing_lnum;
3521 int start_ctx_lnum = cctx->ctx_lnum;
3522 int save_lnum;
3523 int const_used;
3524 int status;
3525 jumpwhen_T jump_when = opchar == '|'
3526 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3527
3528 if (next != NULL)
3529 {
3530 *arg = next_line_from_context(cctx, TRUE);
3531 p = skipwhite(*arg);
3532 }
3533
3534 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3535 {
3536 semsg(_(e_white_space_required_before_and_after_str_at_str),
3537 op, p);
3538 ga_clear(&end_ga);
3539 return FAIL;
3540 }
3541
3542 save_sourcing_lnum = SOURCING_LNUM;
3543 SOURCING_LNUM = start_lnum;
3544 save_lnum = cctx->ctx_lnum;
3545 cctx->ctx_lnum = start_ctx_lnum;
3546
3547 status = check_ppconst_bool(ppconst);
3548 if (status != FAIL)
3549 {
3550 // Use the last ppconst if possible.
3551 if (ppconst->pp_used > 0)
3552 {
3553 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3554 int is_true = tv2bool(tv);
3555
3556 if ((is_true && opchar == '|')
3557 || (!is_true && opchar == '&'))
3558 {
3559 // For "false && expr" and "true || expr" the "expr"
3560 // does not need to be evaluated.
3561 cctx->ctx_skip = SKIP_YES;
3562 clear_tv(tv);
3563 tv->v_type = VAR_BOOL;
3564 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3565 }
3566 else
3567 {
3568 // For "true && expr" and "false || expr" only "expr"
3569 // needs to be evaluated.
3570 --ppconst->pp_used;
3571 jump_when = JUMP_NEVER;
3572 }
3573 }
3574 else
3575 {
3576 // Every part must evaluate to a bool.
3577 status = bool_on_stack(cctx);
3578 }
3579 }
3580 if (status != FAIL)
3581 status = ga_grow(&end_ga, 1);
3582 cctx->ctx_lnum = save_lnum;
3583 if (status == FAIL)
3584 {
3585 ga_clear(&end_ga);
3586 return FAIL;
3587 }
3588
3589 if (jump_when != JUMP_NEVER)
3590 {
3591 if (cctx->ctx_skip != SKIP_YES)
3592 {
3593 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3594 ++end_ga.ga_len;
3595 }
3596 generate_JUMP(cctx, jump_when, 0);
3597 }
3598
3599 // eval the next expression
3600 SOURCING_LNUM = save_sourcing_lnum;
3601 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3602 {
3603 ga_clear(&end_ga);
3604 return FAIL;
3605 }
3606
3607 const_used = ppconst->pp_used;
3608 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3609 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3610 {
3611 ga_clear(&end_ga);
3612 return FAIL;
3613 }
3614
3615 // "0 || 1" results in true, "1 && 0" results in false.
3616 if (ppconst->pp_used == const_used + 1)
3617 {
3618 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3619
3620 if (tv->v_type == VAR_NUMBER
3621 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3622 {
3623 tv->vval.v_number = tv->vval.v_number == 1
3624 ? VVAL_TRUE : VVAL_FALSE;
3625 tv->v_type = VAR_BOOL;
3626 }
3627 }
3628
3629 p = may_peek_next_line(cctx, *arg, &next);
3630 }
3631
3632 if (check_ppconst_bool(ppconst) == FAIL)
3633 {
3634 ga_clear(&end_ga);
3635 return FAIL;
3636 }
3637
3638 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3639 // Every part must evaluate to a bool.
3640 if (bool_on_stack(cctx) == FAIL)
3641 {
3642 ga_clear(&end_ga);
3643 return FAIL;
3644 }
3645
3646 if (end_ga.ga_len > 0)
3647 {
3648 // Fill in the end label in all jumps.
3649 generate_ppconst(cctx, ppconst);
3650 while (end_ga.ga_len > 0)
3651 {
3652 isn_T *isn;
3653
3654 --end_ga.ga_len;
3655 isn = ((isn_T *)instr->ga_data)
3656 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3657 isn->isn_arg.jump.jump_where = instr->ga_len;
3658 }
3659 }
3660 ga_clear(&end_ga);
3661
3662 cctx->ctx_skip = save_skip;
3663 }
3664
3665 return OK;
3666}
3667
3668/*
3669 * expr4a && expr4a && expr4a logical AND
3670 *
3671 * Produces instructions:
3672 * EVAL expr4a Push result of "expr4a"
3673 * COND2BOOL convert to bool if needed
3674 * JUMP_IF_COND_FALSE end
3675 * EVAL expr4b Push result of "expr4b"
3676 * JUMP_IF_COND_FALSE end
3677 * EVAL expr4c Push result of "expr4c"
3678 * end:
3679 */
3680 static int
3681compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3682{
3683 int ppconst_used = ppconst->pp_used;
3684
3685 // get the first variable
3686 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3687 return FAIL;
3688
3689 // || and && work almost the same
3690 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3691}
3692
3693/*
3694 * expr3a || expr3b || expr3c logical OR
3695 *
3696 * Produces instructions:
3697 * EVAL expr3a Push result of "expr3a"
3698 * COND2BOOL convert to bool if needed
3699 * JUMP_IF_COND_TRUE end
3700 * EVAL expr3b Push result of "expr3b"
3701 * JUMP_IF_COND_TRUE end
3702 * EVAL expr3c Push result of "expr3c"
3703 * end:
3704 */
3705 static int
3706compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3707{
3708 int ppconst_used = ppconst->pp_used;
3709
3710 // eval the first expression
3711 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3712 return FAIL;
3713
3714 // || and && work almost the same
3715 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3716}
3717
3718/*
3719 * Toplevel expression: expr2 ? expr1a : expr1b
3720 * Produces instructions:
3721 * EVAL expr2 Push result of "expr2"
3722 * JUMP_IF_FALSE alt jump if false
3723 * EVAL expr1a
3724 * JUMP_ALWAYS end
3725 * alt: EVAL expr1b
3726 * end:
3727 *
3728 * Toplevel expression: expr2 ?? expr1
3729 * Produces instructions:
3730 * EVAL expr2 Push result of "expr2"
3731 * JUMP_AND_KEEP_IF_TRUE end jump if true
3732 * EVAL expr1
3733 * end:
3734 */
3735 int
3736compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3737{
3738 char_u *p;
3739 int ppconst_used = ppconst->pp_used;
3740 char_u *next;
3741
3742 // Ignore all kinds of errors when not producing code.
3743 if (cctx->ctx_skip == SKIP_YES)
3744 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003745 int prev_did_emsg = did_emsg;
3746
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003747 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003748 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003749 }
3750
3751 // Evaluate the first expression.
3752 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3753 return FAIL;
3754
3755 p = may_peek_next_line(cctx, *arg, &next);
3756 if (*p == '?')
3757 {
3758 int op_falsy = p[1] == '?';
3759 garray_T *instr = &cctx->ctx_instr;
3760 garray_T *stack = &cctx->ctx_type_stack;
3761 int alt_idx = instr->ga_len;
3762 int end_idx = 0;
3763 isn_T *isn;
3764 type_T *type1 = NULL;
3765 int has_const_expr = FALSE;
3766 int const_value = FALSE;
3767 int save_skip = cctx->ctx_skip;
3768
3769 if (next != NULL)
3770 {
3771 *arg = next_line_from_context(cctx, TRUE);
3772 p = skipwhite(*arg);
3773 }
3774
3775 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3776 {
3777 semsg(_(e_white_space_required_before_and_after_str_at_str),
3778 op_falsy ? "??" : "?", p);
3779 return FAIL;
3780 }
3781
3782 if (ppconst->pp_used == ppconst_used + 1)
3783 {
3784 // the condition is a constant, we know whether the ? or the :
3785 // expression is to be evaluated.
3786 has_const_expr = TRUE;
3787 if (op_falsy)
3788 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3789 else
3790 {
3791 int error = FALSE;
3792
3793 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3794 &error);
3795 if (error)
3796 return FAIL;
3797 }
3798 cctx->ctx_skip = save_skip == SKIP_YES ||
3799 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3800
3801 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3802 // "left ?? right" and "left" is truthy: produce "left"
3803 generate_ppconst(cctx, ppconst);
3804 else
3805 {
3806 clear_tv(&ppconst->pp_tv[ppconst_used]);
3807 --ppconst->pp_used;
3808 }
3809 }
3810 else
3811 {
3812 generate_ppconst(cctx, ppconst);
3813 if (op_falsy)
3814 end_idx = instr->ga_len;
3815 generate_JUMP(cctx, op_falsy
3816 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3817 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003818 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003819 }
3820
3821 // evaluate the second expression; any type is accepted
3822 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3823 return FAIL;
3824 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3825 return FAIL;
3826
3827 if (!has_const_expr)
3828 {
3829 generate_ppconst(cctx, ppconst);
3830
3831 if (!op_falsy)
3832 {
3833 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003834 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003835 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003836
3837 end_idx = instr->ga_len;
3838 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3839
3840 // jump here from JUMP_IF_FALSE
3841 isn = ((isn_T *)instr->ga_data) + alt_idx;
3842 isn->isn_arg.jump.jump_where = instr->ga_len;
3843 }
3844 }
3845
3846 if (!op_falsy)
3847 {
3848 // Check for the ":".
3849 p = may_peek_next_line(cctx, *arg, &next);
3850 if (*p != ':')
3851 {
3852 emsg(_(e_missing_colon_after_questionmark));
3853 return FAIL;
3854 }
3855 if (next != NULL)
3856 {
3857 *arg = next_line_from_context(cctx, TRUE);
3858 p = skipwhite(*arg);
3859 }
3860
3861 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3862 {
3863 semsg(_(e_white_space_required_before_and_after_str_at_str),
3864 ":", p);
3865 return FAIL;
3866 }
3867
3868 // evaluate the third expression
3869 if (has_const_expr)
3870 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3871 ? SKIP_YES : SKIP_NOT;
3872 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3873 return FAIL;
3874 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3875 return FAIL;
3876 }
3877
3878 if (!has_const_expr)
3879 {
3880 type_T **typep;
3881
3882 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003883 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003884
3885 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003886 typep = &((((type2_T *)stack->ga_data)
3887 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003888 common_type(type1, *typep, typep, cctx->ctx_type_list);
3889
3890 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3891 isn = ((isn_T *)instr->ga_data) + end_idx;
3892 isn->isn_arg.jump.jump_where = instr->ga_len;
3893 }
3894
3895 cctx->ctx_skip = save_skip;
3896 }
3897 return OK;
3898}
3899
3900/*
3901 * Toplevel expression.
3902 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3903 * Returns OK or FAIL.
3904 */
3905 int
3906compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3907{
3908 ppconst_T ppconst;
3909
3910 CLEAR_FIELD(ppconst);
3911 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3912 {
3913 clear_ppconst(&ppconst);
3914 return FAIL;
3915 }
3916 if (is_const != NULL)
3917 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3918 if (generate_ppconst(cctx, &ppconst) == FAIL)
3919 return FAIL;
3920 return OK;
3921}
3922
3923/*
3924 * Toplevel expression.
3925 */
3926 int
3927compile_expr0(char_u **arg, cctx_T *cctx)
3928{
3929 return compile_expr0_ext(arg, cctx, NULL);
3930}
3931
3932
3933#endif // defined(FEAT_EVAL)