blob: cfdea7bc41387230c269472cc8710c2beceb08b6 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000102 if (need_type(idxtype, &t_number, FALSE,
103 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000104 return FAIL;
105 if (is_slice)
106 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000107 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000108 if (need_type(idxtype, &t_number, FALSE,
109 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000110 return FAIL;
111 }
112 }
113
114 if (vartype == VAR_DICT)
115 {
116 if (is_slice)
117 {
118 emsg(_(e_cannot_slice_dictionary));
119 return FAIL;
120 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000122 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000123 typep->type_curr = typep->type_curr->tt_member;
124 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000125 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 typep->type_curr = &t_any;
127 if (typep->type_decl->tt_type == VAR_DICT)
128 {
129 typep->type_decl = typep->type_decl->tt_member;
130 if (typep->type_decl == &t_unknown)
131 // empty dict was used
132 typep->type_decl = &t_any;
133 }
134 else
135 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000136 }
137 else
138 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000139 if (need_type(typep->type_curr, &t_dict_any, FALSE,
140 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000141 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000142 typep->type_curr = &t_any;
143 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200145 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL
Bram Moolenaard0132f42022-05-12 11:05:40 +0100146 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147 return FAIL;
148 if (keeping_dict != NULL)
149 *keeping_dict = TRUE;
150 }
151 else if (vartype == VAR_STRING)
152 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000153 typep->type_curr = &t_string;
154 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000155 if ((is_slice
156 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
157 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
158 return FAIL;
159 }
160 else if (vartype == VAR_BLOB)
161 {
162 if (is_slice)
163 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 typep->type_curr = &t_blob;
165 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
167 return FAIL;
168 }
169 else
170 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000171 typep->type_curr = &t_number;
172 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
174 return FAIL;
175 }
176 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100177 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
178 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000179 {
180 if (is_slice)
181 {
182 if (generate_instr_drop(cctx,
183 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
184 2) == FAIL)
185 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000186 // a copy is made so the member type is no longer declared
187 if (typep->type_decl->tt_type == VAR_LIST)
188 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000189
190 // a copy is made, the composite is no longer "const"
191 if (typep->type_curr->tt_flags & TTFLAG_CONST)
192 {
193 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
194
195 if (type != typep->type_curr) // did get a copy
196 {
197 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
198 typep->type_curr = type;
199 }
200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201 }
202 else
203 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 typep->type_curr = typep->type_curr->tt_member;
207 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000209 typep->type_curr = &t_any;
210 if (typep->type_decl->tt_type == VAR_LIST)
211 {
212 typep->type_decl = typep->type_decl->tt_member;
213 if (typep->type_decl == &t_unknown)
214 // empty list was used
215 typep->type_decl = &t_any;
216 }
217 else
218 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 }
220 if (generate_instr_drop(cctx,
221 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
222 == FAIL)
223 return FAIL;
224 }
225 }
226 else
227 {
228 switch (vartype)
229 {
230 case VAR_FUNC:
231 case VAR_PARTIAL:
232 emsg(_(e_cannot_index_a_funcref));
233 break;
234 case VAR_BOOL:
235 case VAR_SPECIAL:
236 case VAR_JOB:
237 case VAR_CHANNEL:
238 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 case VAR_CLASS:
240 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200241 case VAR_TYPEALIAS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000242 case VAR_UNKNOWN:
243 case VAR_ANY:
244 case VAR_VOID:
245 emsg(_(e_cannot_index_special_variable));
246 break;
247 default:
248 emsg(_(e_string_list_dict_or_blob_required));
249 }
250 return FAIL;
251 }
252 return OK;
253}
254
255/*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200256 * Returns TRUE if the current function is inside the class "cl" or one of the
257 * parent classes.
258 */
259 static int
260inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
261{
262 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
263 {
264 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
265 {
266 class_T *clp = cctx->ctx_ufunc->uf_class;
267 while (clp != NULL)
268 {
269 if (clp == cl)
270 return TRUE;
271 clp = clp->class_extends;
272 }
273 }
274 }
275
276 return FALSE;
277}
278
279/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000280 * Compile ".member" coming after an object or class.
281 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000282 static int
283compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
284{
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200285 int m_idx;
286
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000287 if (VIM_ISWHITE((*arg)[1]))
288 {
289 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
290 return FAIL;
291 }
292
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000293 class_T *cl = type->tt_class;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000294 int is_super = type->tt_flags & TTFLAG_SUPER;
295 if (type == &t_super)
296 {
297 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000298 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200299 emsg(_(e_using_super_not_in_class_method));
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000300 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000301 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000302 is_super = TRUE;
303 cl = cctx->ctx_ufunc->uf_class;
304 // Remove &t_super from the stack.
305 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000306 }
307 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000308 {
309 garray_T *instr = &cctx->ctx_instr;
310 if (instr->ga_len > 0)
311 {
312 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
313 if (isn->isn_type == ISN_LOADSCRIPT)
314 {
315 // The class was recognized as a script item. We only need
316 // to know what class it is, drop the instruction.
317 --instr->ga_len;
318 vim_free(isn->isn_arg.script.scriptref);
319 }
320 }
321 }
322
Ernie Raelf77a7f72023-03-03 15:05:30 +0000323 if (cl == NULL)
324 {
325 // TODO: this should not give an error but be handled at runtime
326 emsg(_(e_incomplete_type));
327 return FAIL;
328 }
329
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000330 ++*arg;
331 char_u *name = *arg;
332 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
333 if (name_end == name)
334 return FAIL;
335 size_t len = name_end - name;
336
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000337 if (*name_end == '(')
338 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000339 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000340 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000341 ufunc_T **functions;
342
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000343 if (type->tt_type == VAR_CLASS)
344 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000345 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000346 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000347 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000348 }
349 else
350 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000351 // type->tt_type == VAR_OBJECT: method call
Ernie Rael58c95792024-08-13 23:27:22 +0200352 // When compiling Func and doing "super.SomeFunc()", must be in the
353 // class context that defines Func.
354 if (is_super)
355 cl = cctx->ctx_ufunc->uf_defclass;
356
Bram Moolenaar574950d2023-01-03 19:08:50 +0000357 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000358 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000360 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000361
362 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000363 int fi;
364 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000365 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000366 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000367 // Use a separate pointer to avoid that ASAN complains about
368 // uf_name[] only being 4 characters.
369 char_u *ufname = (char_u *)fp->uf_name;
370 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
371 {
372 ufunc = fp;
373 break;
374 }
375 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200376 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000377 if (ufunc == NULL)
378 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200379 // could be a funcref in a member variable
380 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
381 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
382 {
383 method_not_found_msg(cl, type->tt_type, name, len);
384 return FAIL;
385 }
386 if (type->tt_type == VAR_CLASS)
387 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200388 // Remove the class type from the stack
389 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200390 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
391 return FAIL;
392 }
393 else
394 {
395 if (generate_GET_OBJ_MEMBER(cctx, m_idx, ocm->ocm_type) ==
396 FAIL)
397 return FAIL;
398 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000399 }
400
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200401 // A private object method can be used only inside the class where it
402 // is defined or in one of the child classes.
403 // A private class method can be used only in the class where it is
404 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200405 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200406 ((type->tt_type == VAR_OBJECT
407 && !inside_class_hierarchy(cctx, cl))
408 || (type->tt_type == VAR_CLASS
409 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200410 {
Ernie Rael03042a22023-11-11 08:53:32 +0100411 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200412 return FAIL;
413 }
414
Bram Moolenaar574950d2023-01-03 19:08:50 +0000415 // Compile the arguments and call the class function or object method.
416 // The object method will know that the object is on the stack, just
417 // before the arguments.
418 *arg = skipwhite(name_end + 1);
419 int argcount = 0;
420 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
421 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000422
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200423 if (ocm != NULL)
424 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000425 if (type->tt_type == VAR_OBJECT
426 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
Ernie Rael58c95792024-08-13 23:27:22 +0200427 return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super);
428 return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000429 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000430
431 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000432 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200433 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200434 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000435 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200436 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000437 {
Ernie Rael03042a22023-11-11 08:53:32 +0100438 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200439 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200440 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200441 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200442
443 *arg = name_end;
444 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200445 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
446 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200447 }
448
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200449 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200450 m_idx = object_method_idx(cl, name, len);
451 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000452 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200453 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100454 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200455 if (*name == '_' && !inside_class(cctx, cl))
456 {
Ernie Rael03042a22023-11-11 08:53:32 +0100457 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200458 return FAIL;
459 }
460 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200461 // Remove the object type from the stack
462 --cctx->ctx_type_stack.ga_len;
463 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000464 }
465
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200466 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000467 }
468 else
469 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000470 // load class member
471 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200472 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
473 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000474 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200475 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200476 // A private class variable can be accessed only in the class where
477 // it is defined.
478 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200479 {
Ernie Rael03042a22023-11-11 08:53:32 +0100480 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200481 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200482 return FAIL;
483 }
484
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000485 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200486 // Remove the class type from the stack
487 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000488 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
489 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200490
491 // Could be a class method reference: "class.Func".
492 m_idx = class_method_idx(cl, name, len);
493 if (m_idx >= 0)
494 {
495 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100496 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200497 if (*name == '_' && !inside_class(cctx, cl))
498 {
Ernie Rael03042a22023-11-11 08:53:32 +0100499 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200500 return FAIL;
501 }
502 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200503 // Remove the class type from the stack
504 --cctx->ctx_type_stack.ga_len;
505 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200506 }
507
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200508 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000509 }
510
511 return FAIL;
512}
513
514/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000515 * Generate an instruction to load script-local variable "name", without the
516 * leading "s:".
517 * Also finds imported variables.
518 */
519 int
520compile_load_scriptvar(
521 cctx_T *cctx,
522 char_u *name, // variable NUL terminated
523 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100524 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525{
526 scriptitem_T *si;
527 int idx;
528 imported_T *import;
529
530 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
531 return FAIL;
532 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000533 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000534 if (idx >= 0)
535 {
536 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
537
538 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
539 current_sctx.sc_sid, idx, sv->sv_type);
540 return OK;
541 }
542
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000543 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000544 if (import != NULL)
545 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000546 char_u *p = skipwhite(*end);
547 char_u *exp_name;
548 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100549 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000550 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000551 int done = FALSE;
552 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000553
Ernie Rael9a901792024-04-16 22:11:56 +0200554 check_script_symlink(import->imp_sid);
555 import_check_sourced_sid(&import->imp_sid);
556
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000557 // Need to lookup the member.
558 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000559 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000560 semsg(_(e_expected_dot_after_name_str), start);
561 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000562 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000563 ++p;
564 if (VIM_ISWHITE(*p))
565 {
566 emsg(_(e_no_white_space_allowed_after_dot));
567 return FAIL;
568 }
569
570 // isolate one name
571 exp_name = p;
572 while (eval_isnamec(*p))
573 ++p;
574 cc = *p;
575 *p = NUL;
576
Bram Moolenaard041f422022-01-12 19:54:00 +0000577 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100578 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
579 // "import autoload './dir/script.vim'" or
580 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100581 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100582
583 if (res == OK)
584 {
585 if (si->sn_autoload_prefix != NULL
586 && si->sn_state == SN_STATE_NOT_LOADED)
587 {
588 char_u *auto_name =
589 concat_str(si->sn_autoload_prefix, exp_name);
590
591 // autoload script must be loaded later, access by the autoload
592 // name. If a '(' follows it must be a function. Otherwise we
593 // don't know, it can be "script.Func".
594 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100595 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100596 else
597 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
598 vim_free(auto_name);
599 done = TRUE;
600 }
601 else if (si->sn_import_autoload
602 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100603 {
604 // If a '(' follows it must be a function. Otherwise we don't
605 // know, it can be "script.Func".
606 if (cc == '(' || paren_follows_after_expr)
607 {
608 char_u sid_name[MAX_FUNC_NAME_LEN];
609
610 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100611 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100612 }
613 else
614 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
615 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100616 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100617 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100618 else
619 {
620 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
621 cctx, NULL, TRUE);
622 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100623 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100624
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000625 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000626 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000627 if (done)
628 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000629
630 if (idx < 0)
631 {
632 if (ufunc != NULL)
633 {
634 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100635 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000636 return OK;
637 }
638 return FAIL;
639 }
640
641 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
642 import->imp_sid,
643 idx,
644 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645 return OK;
646 }
647
Bram Moolenaard0132f42022-05-12 11:05:40 +0100648 // Can only get here if we know "name" is a script variable and not in a
649 // Vim9 script (variable is not in sn_var_vals): old style script.
650 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000651 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000652}
653
654 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000655generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000656{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000657 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000658 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000660 // Reject a global non-autoload function found without the "g:" prefix.
661 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662 return FAIL;
663
664 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000665 compile_type = get_compile_type(ufunc);
666 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000667 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000668 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100669 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000670}
671
672/*
673 * Compile a variable name into a load instruction.
674 * "end" points to just after the name.
675 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
676 * When "error" is FALSE do not give an error when not found.
677 */
678 int
679compile_load(
680 char_u **arg,
681 char_u *end_arg,
682 cctx_T *cctx,
683 int is_expr,
684 int error)
685{
686 type_T *type;
687 char_u *name = NULL;
688 char_u *end = end_arg;
689 int res = FAIL;
690 int prev_called_emsg = called_emsg;
691
692 if (*(*arg + 1) == ':')
693 {
694 if (end <= *arg + 2)
695 {
696 isntype_T isn_type;
697
698 // load dictionary of namespace
699 switch (**arg)
700 {
701 case 'g': isn_type = ISN_LOADGDICT; break;
702 case 'w': isn_type = ISN_LOADWDICT; break;
703 case 't': isn_type = ISN_LOADTDICT; break;
704 case 'b': isn_type = ISN_LOADBDICT; break;
705 default:
706 semsg(_(e_namespace_not_supported_str), *arg);
707 goto theend;
708 }
709 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
710 goto theend;
711 res = OK;
712 }
713 else
714 {
715 isntype_T isn_type = ISN_DROP;
716
717 // load namespaced variable
718 name = vim_strnsave(*arg + 2, end - (*arg + 2));
719 if (name == NULL)
720 return FAIL;
721
722 switch (**arg)
723 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100724 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000725 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000726 case 's': if (current_script_is_vim9())
727 {
728 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
729 *arg);
730 vim_free(name);
731 return FAIL;
732 }
Kota Kato948a3892022-08-16 16:09:59 +0100733 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000734 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000735 else
736 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100737 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000738 break;
739 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
740 {
741 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000742 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000743 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744 else
745 isn_type = ISN_LOADG;
746 }
747 else
748 {
749 isn_type = ISN_LOADAUTO;
750 vim_free(name);
751 name = vim_strnsave(*arg, end - *arg);
752 if (name == NULL)
753 return FAIL;
754 }
755 break;
756 case 'w': isn_type = ISN_LOADW; break;
757 case 't': isn_type = ISN_LOADT; break;
758 case 'b': isn_type = ISN_LOADB; break;
759 default: // cannot happen, just in case
760 semsg(_(e_namespace_not_supported_str), *arg);
761 goto theend;
762 }
763 if (isn_type != ISN_DROP)
764 {
765 // Global, Buffer-local, Window-local and Tabpage-local
766 // variables can be defined later, thus we don't check if it
767 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000768 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 }
770 }
771 }
772 else
773 {
774 size_t len = end - *arg;
775 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200776 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000777 int gen_load = FALSE;
778 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100779 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100780 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000781
782 name = vim_strnsave(*arg, end - *arg);
783 if (name == NULL)
784 return FAIL;
785
Bram Moolenaar58b40092023-01-11 15:59:05 +0000786 if (STRCMP(name, "super") == 0
787 && cctx->ctx_ufunc != NULL
788 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
789 {
790 // super.SomeFunc() in a class function: push &t_super type, this
791 // is recognized in compile_subscript().
792 res = push_type_stack(cctx, &t_super);
793 if (*end != '.')
794 emsg(_(e_super_must_be_followed_by_dot));
795 }
796 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797 {
798 script_autoload(name, FALSE);
799 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
800 }
801 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
802 == OK)
803 {
804 if (gen_load_outer == 0)
805 gen_load = TRUE;
806 }
807 else
808 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000809 lvar_T lvar;
810 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811
812 if (lookup_local(*arg, len, &lvar, cctx) == OK)
813 {
814 type = lvar.lv_type;
815 idx = lvar.lv_idx;
816 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100817 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100819 outer_loop_depth = lvar.lv_loop_depth;
820 outer_loop_idx = lvar.lv_loop_idx;
821 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 else
823 gen_load = TRUE;
824 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200825 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
826 (((idx =
827 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
828 || ((method_idx =
829 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000830 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200831 // Referencing a class variable or method without the class
832 // name. A class variable or method can be referenced without
833 // the class name only in the class where the function is
834 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200835 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200836 {
837 if (idx >= 0)
838 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
839 else
840 {
841 ufunc_T *fp = cl->class_class_functions[method_idx];
842 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
843 NULL);
844 }
845 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200846 else
847 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200848 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200849 name, cl->class_name);
850 res = FAIL;
851 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000852 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 else
854 {
855 // "var" can be script-local even without using "s:" if it
856 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000857 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000858 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100859 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860
861 // When evaluating an expression and the name starts with an
862 // uppercase letter it can be a user defined function.
863 // generate_funcref() will fail if the function can't be found.
864 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000865 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 }
867 }
868 if (gen_load)
869 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
870 if (gen_load_outer > 0)
871 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100872 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
873 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874 cctx->ctx_outer_used = TRUE;
875 }
876 }
877
878 *arg = end;
879
880theend:
881 if (res == FAIL && error && called_emsg == prev_called_emsg)
882 semsg(_(e_variable_not_found_str), name);
883 vim_free(name);
884 return res;
885}
886
887/*
888 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100889 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000890 * Returns FAIL if compilation fails.
891 */
892 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100893compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894{
LemonBoyf3b48952022-05-05 13:53:03 +0100895 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896 garray_T save_ga = cctx->ctx_instr;
897 int expr_res;
898 int trailing_error;
899 int instr_count;
900 isn_T *instr = NULL;
901
902 // Remove the string type from the stack.
903 --cctx->ctx_type_stack.ga_len;
904
905 // Temporarily reset the list of instructions so that the jump labels are
906 // correct.
907 cctx->ctx_instr.ga_len = 0;
908 cctx->ctx_instr.ga_maxlen = 0;
909 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000910
911 // avoid peeking a next line
912 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
913 cctx->ctx_ufunc->uf_lines.ga_len = 0;
914
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000915 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000916
917 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
918
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000919 s = skipwhite(s);
920 trailing_error = *s != NUL;
921
922 if (expr_res == FAIL || trailing_error
923 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
924 {
925 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000926 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 clear_instr_ga(&cctx->ctx_instr);
928 cctx->ctx_instr = save_ga;
929 ++cctx->ctx_type_stack.ga_len;
930 return FAIL;
931 }
932
933 // Move the generated instructions into the ISN_INSTR instruction, then
934 // restore the list of instructions.
935 instr_count = cctx->ctx_instr.ga_len;
936 instr = cctx->ctx_instr.ga_data;
937 instr[instr_count].isn_type = ISN_FINISH;
938
939 cctx->ctx_instr = save_ga;
940 vim_free(isn->isn_arg.string);
941 isn->isn_type = ISN_INSTR;
942 isn->isn_arg.instr = instr;
943 return OK;
944}
945
946/*
947 * Compile the argument expressions.
948 * "arg" points to just after the "(" and is advanced to after the ")"
949 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950 int
LemonBoyf3b48952022-05-05 13:53:03 +0100951compile_arguments(
952 char_u **arg,
953 cctx_T *cctx,
954 int *argcount,
955 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000956{
957 char_u *p = *arg;
958 char_u *whitep = *arg;
959 int must_end = FALSE;
960 int instr_count;
961
962 for (;;)
963 {
964 if (may_get_next_line(whitep, &p, cctx) == FAIL)
965 goto failret;
966 if (*p == ')')
967 {
968 *arg = p + 1;
969 return OK;
970 }
971 if (must_end)
972 {
973 semsg(_(e_missing_comma_before_argument_str), p);
974 return FAIL;
975 }
976
977 instr_count = cctx->ctx_instr.ga_len;
978 if (compile_expr0(&p, cctx) == FAIL)
979 return FAIL;
980 ++*argcount;
981
LemonBoyf3b48952022-05-05 13:53:03 +0100982 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 && cctx->ctx_instr.ga_len == instr_count + 1)
984 {
985 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
986
987 // {skip} argument of searchpair() can be compiled if not empty
988 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100989 compile_string(isn, cctx, 0);
990 }
991 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
992 && cctx->ctx_instr.ga_len == instr_count + 1)
993 {
994 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
995
996 // {sub} argument of substitute() can be compiled if it starts
997 // with \=
998 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100999 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001000 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001 }
1002
1003 if (*p != ',' && *skipwhite(p) == ',')
1004 {
1005 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1006 p = skipwhite(p);
1007 }
1008 if (*p == ',')
1009 {
1010 ++p;
1011 if (*p != NUL && !VIM_ISWHITE(*p))
1012 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1013 }
1014 else
1015 must_end = TRUE;
1016 whitep = p;
1017 p = skipwhite(p);
1018 }
1019failret:
1020 emsg(_(e_missing_closing_paren));
1021 return FAIL;
1022}
1023
1024/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001025 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1026 * etc.) if the class implements it.
1027 */
1028 static int
1029compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1030{
1031 type_T *type = get_decl_type_on_stack(cctx, 0);
1032 int res = FAIL;
1033
1034 // If the built in function is invoked on an object and the class
1035 // implements the corresponding built in method, then invoke the object
1036 // method.
1037 if (type->tt_type == VAR_OBJECT)
1038 {
1039 int method_idx;
1040 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1041 &method_idx);
1042 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001043 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001044 }
1045
1046 return res;
1047}
1048
1049
1050/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001051 * Compile a function call: name(arg1, arg2)
1052 * "arg" points to "name", "arg + varlen" to the "(".
1053 * "argcount_init" is 1 for "value->method()"
1054 * Instructions:
1055 * EVAL arg1
1056 * EVAL arg2
1057 * BCALL / DCALL / UCALL
1058 */
1059 static int
1060compile_call(
1061 char_u **arg,
1062 size_t varlen,
1063 cctx_T *cctx,
1064 ppconst_T *ppconst,
1065 int argcount_init)
1066{
1067 char_u *name = *arg;
1068 char_u *p;
1069 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001070 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 char_u fname_buf[FLEN_FIXED + 1];
1072 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001073 ufunc_T *ufunc = NULL;
1074 int res = FAIL;
1075 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001076 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001077 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001078 imported_T *import;
1079
1080 if (varlen >= sizeof(namebuf))
1081 {
1082 semsg(_(e_name_too_long_str), name);
1083 return FAIL;
1084 }
1085 vim_strncpy(namebuf, *arg, varlen);
1086
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001087 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001088 if (import != NULL)
1089 {
1090 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1091 return FAIL;
1092 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001093
1094 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001095 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001097 if ((varlen == 3
1098 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1100 {
1101 char_u *s = skipwhite(*arg + varlen + 1);
1102 typval_T argvars[2];
1103 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001104 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105
1106 argvars[0].v_type = VAR_UNKNOWN;
1107 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001108 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001109 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001110 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001111 s = skipwhite(s);
1112 if (*s == ')' && argvars[0].v_type == VAR_STRING
1113 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1114 || !is_has))
1115 {
1116 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1117
1118 *arg = s + 1;
1119 argvars[1].v_type = VAR_UNKNOWN;
1120 tv->v_type = VAR_NUMBER;
1121 tv->vval.v_number = 0;
1122 if (is_has)
1123 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001124 else if (is_len)
1125 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126 else
1127 f_exists(argvars, tv);
1128 clear_tv(&argvars[0]);
1129 ++ppconst->pp_used;
1130 return OK;
1131 }
1132 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001133 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001134 {
1135 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1136 return FAIL;
1137 }
1138 }
1139
1140 if (generate_ppconst(cctx, ppconst) == FAIL)
1141 return FAIL;
1142
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001143 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1145
1146 // We handle the "skip" argument of searchpair() and searchpairpos()
1147 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001148 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1149 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1150 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1151 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1152 special_fn = CA_SEARCHPAIR;
1153 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1154 special_fn = CA_SUBSTITUTE;
1155 else
1156 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157
1158 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001159 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160 goto theend;
1161
1162 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1163 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1164 {
1165 int idx;
1166
1167 // builtin function
1168 idx = find_internal_func(name);
1169 if (idx >= 0)
1170 {
1171 if (STRCMP(name, "flatten") == 0)
1172 {
1173 emsg(_(e_cannot_use_flatten_in_vim9_script));
1174 goto theend;
1175 }
1176
1177 if (STRCMP(name, "add") == 0 && argcount == 2)
1178 {
h-eastb895b0f2023-09-24 15:46:31 +02001179 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001180 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1181 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001182
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183 // add() can be compiled to instructions if we know the type
1184 if (type->tt_type == VAR_LIST)
1185 {
1186 // inline "add(list, item)" so that the type can be checked
1187 res = generate_LISTAPPEND(cctx);
1188 idx = -1;
1189 }
1190 else if (type->tt_type == VAR_BLOB)
1191 {
1192 // inline "add(blob, nr)" so that the type can be checked
1193 res = generate_BLOBAPPEND(cctx);
1194 idx = -1;
1195 }
1196 }
1197
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001198 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1199 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001200 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001201 // May have the "D" or "R" flag, reserve a variable for a
1202 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001203 if (get_defer_var_idx(cctx) == 0)
1204 idx = -1;
1205 }
1206
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001207 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1208 if (STRCMP(name, "string") == 0)
1209 builtin_method = CLASS_BUILTIN_STRING;
1210 else if (STRCMP(name, "empty") == 0)
1211 builtin_method = CLASS_BUILTIN_EMPTY;
1212 else if (STRCMP(name, "len") == 0)
1213 builtin_method = CLASS_BUILTIN_LEN;
1214 if (builtin_method != CLASS_BUILTIN_INVALID)
1215 {
1216 res = compile_builtin_method_call(cctx, builtin_method);
1217 if (res == OK)
1218 idx = -1;
1219 }
1220
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001221 if (idx >= 0)
1222 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1223 }
1224 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001225 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001226 goto theend;
1227 }
1228
Bram Moolenaar62aec932022-01-29 21:45:34 +00001229 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1230
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231 // An argument or local variable can be a function reference, this
1232 // overrules a function name.
1233 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1234 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1235 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001236 class_T *cl = NULL;
1237 int mi = 0;
1238
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001239 // If we can find the function by name generate the right call.
1240 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001241 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001242 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001244 if (!func_is_global(ufunc))
1245 {
Ernie Rael58c95792024-08-13 23:27:22 +02001246 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001247 goto theend;
1248 }
1249 if (!has_g_namespace
1250 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1251 {
1252 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001253 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001254 goto theend;
1255 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001256 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001257 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001258 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001259 // Class method invocation without the class name.
1260 // A class method can be referenced without the class name only in
1261 // the class where the function is defined.
1262 if (cctx->ctx_ufunc->uf_defclass == cl)
1263 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001264 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001265 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001266 }
1267 else
1268 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001269 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001270 name, cl->class_name);
1271 res = FAIL;
1272 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001273 goto theend;
1274 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001275 }
1276
1277 // If the name is a variable, load it and use PCALL.
1278 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001279 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001281 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1283 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001284 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001285
Christian Brabandtd5475e82023-08-17 23:34:09 +02001286 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287 goto theend;
1288 }
1289
1290 // If we can find a global function by name generate the right call.
1291 if (ufunc != NULL)
1292 {
Ernie Rael58c95792024-08-13 23:27:22 +02001293 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 goto theend;
1295 }
1296
1297 // A global function may be defined only later. Need to figure out at
1298 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001299 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 res = generate_UCALL(cctx, name, argcount);
1301 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001302 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001303
1304theend:
1305 vim_free(tofree);
1306 return res;
1307}
1308
1309// like NAMESPACE_CHAR but with 'a' and 'l'.
1310#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1311
1312/*
1313 * Find the end of a variable or function name. Unlike find_name_end() this
1314 * does not recognize magic braces.
1315 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1316 * Return a pointer to just after the name. Equal to "arg" if there is no
1317 * valid name.
1318 */
1319 char_u *
1320to_name_end(char_u *arg, int use_namespace)
1321{
1322 char_u *p;
1323
1324 // Quick check for valid starting character.
1325 if (!eval_isnamec1(*arg))
1326 return arg;
1327
1328 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1329 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1330 // and can be used in slice "[n:]".
1331 if (*p == ':' && (p != arg + 1
1332 || !use_namespace
1333 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1334 break;
1335 return p;
1336}
1337
1338/*
1339 * Like to_name_end() but also skip over a list or dict constant.
1340 * Also accept "<SNR>123_Func".
1341 * This intentionally does not handle line continuation.
1342 */
1343 char_u *
1344to_name_const_end(char_u *arg)
1345{
1346 char_u *p = arg;
1347 typval_T rettv;
1348
1349 if (STRNCMP(p, "<SNR>", 5) == 0)
1350 p = skipdigits(p + 5);
1351 p = to_name_end(p, TRUE);
1352 if (p == arg && *arg == '[')
1353 {
1354
1355 // Can be "[1, 2, 3]->Func()".
1356 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1357 p = arg;
1358 }
1359 return p;
1360}
1361
1362/*
1363 * parse a list: [expr, expr]
1364 * "*arg" points to the '['.
1365 * ppconst->pp_is_const is set if all items are a constant.
1366 */
1367 static int
1368compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1369{
1370 char_u *p = skipwhite(*arg + 1);
1371 char_u *whitep = *arg + 1;
1372 int count = 0;
1373 int is_const;
1374 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001375 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001376
1377 for (;;)
1378 {
1379 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1380 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001381 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 return FAIL;
1383 }
1384 if (*p == ',')
1385 {
1386 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1387 return FAIL;
1388 }
1389 if (*p == ']')
1390 {
1391 ++p;
1392 break;
1393 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001394 if (must_end)
1395 {
1396 semsg(_(e_missing_comma_in_list_str), p);
1397 return FAIL;
1398 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1400 return FAIL;
1401 if (!is_const)
1402 is_all_const = FALSE;
1403 ++count;
1404 if (*p == ',')
1405 {
1406 ++p;
1407 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1408 {
1409 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1410 return FAIL;
1411 }
1412 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001413 else
1414 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001415 whitep = p;
1416 p = skipwhite(p);
1417 }
1418 *arg = p;
1419
1420 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001421 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422}
1423
1424/*
1425 * Parse a lambda: "(arg, arg) => expr"
1426 * "*arg" points to the '('.
1427 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1428 */
1429 static int
1430compile_lambda(char_u **arg, cctx_T *cctx)
1431{
1432 int r;
1433 typval_T rettv;
1434 ufunc_T *ufunc;
1435 evalarg_T evalarg;
1436
1437 init_evalarg(&evalarg);
1438 evalarg.eval_flags = EVAL_EVALUATE;
1439 evalarg.eval_cctx = cctx;
1440
1441 // Get the funcref in "rettv".
1442 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1443 if (r != OK)
1444 {
1445 clear_evalarg(&evalarg, NULL);
1446 return r;
1447 }
1448
1449 // "rettv" will now be a partial referencing the function.
1450 ufunc = rettv.vval.v_partial->pt_func;
1451 ++ufunc->uf_refcount;
1452 clear_tv(&rettv);
1453
1454 // Compile it here to get the return type. The return type is optional,
1455 // when it's missing use t_unknown. This is recognized in
1456 // compile_return().
1457 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1458 ufunc->uf_ret_type = &t_unknown;
1459 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1460
1461 // When the outer function is compiled for profiling or debugging, the
1462 // lambda may be called without profiling or debugging. Compile it here in
1463 // the right context.
1464 if (cctx->ctx_compile_type == CT_DEBUG
1465#ifdef FEAT_PROFILE
1466 || cctx->ctx_compile_type == CT_PROFILE
1467#endif
1468 )
1469 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1470
Bram Moolenaar139575d2022-03-15 19:29:30 +00001471 // if the outer function is not compiled for debugging or profiling, this
1472 // one might be
1473 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001474 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001475 compiletype_T compile_type = get_compile_type(ufunc);
1476
1477 if (compile_type != CT_NONE)
1478 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001479 }
1480
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001481 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1482 // "*arg" may point into it. Point into the original line to avoid a
1483 // dangling pointer.
1484 if (evalarg.eval_using_cmdline)
1485 {
1486 garray_T *gap = &evalarg.eval_tofree_ga;
1487 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1488
1489 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1490 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001491 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492 }
1493
1494 clear_evalarg(&evalarg, NULL);
1495
1496 if (ufunc->uf_def_status == UF_COMPILED)
1497 {
1498 // The return type will now be known.
1499 set_function_type(ufunc);
1500
1501 // The function reference count will be 1. When the ISN_FUNCREF
1502 // instruction is deleted the reference count is decremented and the
1503 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001504 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001505 }
1506
1507 func_ptr_unref(ufunc);
1508 return FAIL;
1509}
1510
1511/*
1512 * Get a lambda and compile it. Uses Vim9 syntax.
1513 */
1514 int
1515get_lambda_tv_and_compile(
1516 char_u **arg,
1517 typval_T *rettv,
1518 int types_optional,
1519 evalarg_T *evalarg)
1520{
1521 int r;
1522 ufunc_T *ufunc;
1523 int save_sc_version = current_sctx.sc_version;
1524
1525 // Get the funcref in "rettv".
1526 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1527 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1528 current_sctx.sc_version = save_sc_version;
1529 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001530 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001531
1532 // "rettv" will now be a partial referencing the function.
1533 ufunc = rettv->vval.v_partial->pt_func;
1534
1535 // Compile it here to get the return type. The return type is optional,
1536 // when it's missing use t_unknown. This is recognized in
1537 // compile_return().
1538 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1539 ufunc->uf_ret_type = &t_unknown;
1540 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1541
1542 if (ufunc->uf_def_status == UF_COMPILED)
1543 {
1544 // The return type will now be known.
1545 set_function_type(ufunc);
1546 return OK;
1547 }
1548 clear_tv(rettv);
1549 return FAIL;
1550}
1551
1552/*
1553 * parse a dict: {key: val, [key]: val}
1554 * "*arg" points to the '{'.
1555 * ppconst->pp_is_const is set if all item values are a constant.
1556 */
1557 static int
1558compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1559{
1560 garray_T *instr = &cctx->ctx_instr;
1561 int count = 0;
1562 dict_T *d = dict_alloc();
1563 dictitem_T *item;
1564 char_u *whitep = *arg + 1;
1565 char_u *p;
1566 int is_const;
1567 int is_all_const = TRUE; // reset when non-const encountered
1568
1569 if (d == NULL)
1570 return FAIL;
1571 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001572 {
1573 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001574 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001575 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 for (;;)
1577 {
1578 char_u *key = NULL;
1579
1580 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1581 {
1582 *arg = NULL;
1583 goto failret;
1584 }
1585
1586 if (**arg == '}')
1587 break;
1588
1589 if (**arg == '[')
1590 {
1591 isn_T *isn;
1592
1593 // {[expr]: value} uses an evaluated key.
1594 *arg = skipwhite(*arg + 1);
1595 if (compile_expr0(arg, cctx) == FAIL)
1596 return FAIL;
1597 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1598 if (isn->isn_type == ISN_PUSHNR)
1599 {
1600 char buf[NUMBUFLEN];
1601
1602 // Convert to string at compile time.
1603 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1604 isn->isn_type = ISN_PUSHS;
1605 isn->isn_arg.string = vim_strsave((char_u *)buf);
1606 }
1607 if (isn->isn_type == ISN_PUSHS)
1608 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001609 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001610 return FAIL;
1611 *arg = skipwhite(*arg);
1612 if (**arg != ']')
1613 {
1614 emsg(_(e_missing_matching_bracket_after_dict_key));
1615 return FAIL;
1616 }
1617 ++*arg;
1618 }
1619 else
1620 {
1621 // {"name": value},
1622 // {'name': value},
1623 // {name: value} use "name" as a literal key
1624 key = get_literal_key(arg);
1625 if (key == NULL)
1626 return FAIL;
1627 if (generate_PUSHS(cctx, &key) == FAIL)
1628 return FAIL;
1629 }
1630
1631 // Check for duplicate keys, if using string keys.
1632 if (key != NULL)
1633 {
1634 item = dict_find(d, key, -1);
1635 if (item != NULL)
1636 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001637 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001638 goto failret;
1639 }
1640 item = dictitem_alloc(key);
1641 if (item != NULL)
1642 {
1643 item->di_tv.v_type = VAR_UNKNOWN;
1644 item->di_tv.v_lock = 0;
1645 if (dict_add(d, item) == FAIL)
1646 dictitem_free(item);
1647 }
1648 }
1649
1650 if (**arg != ':')
1651 {
1652 if (*skipwhite(*arg) == ':')
1653 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1654 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001655 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001656 return FAIL;
1657 }
1658 whitep = *arg + 1;
1659 if (!IS_WHITE_OR_NUL(*whitep))
1660 {
1661 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1662 return FAIL;
1663 }
1664
1665 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1666 {
1667 *arg = NULL;
1668 goto failret;
1669 }
1670
1671 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1672 return FAIL;
1673 if (!is_const)
1674 is_all_const = FALSE;
1675 ++count;
1676
1677 whitep = *arg;
1678 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1679 {
1680 *arg = NULL;
1681 goto failret;
1682 }
1683 if (**arg == '}')
1684 break;
1685 if (**arg != ',')
1686 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001687 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 goto failret;
1689 }
1690 if (IS_WHITE_OR_NUL(*whitep))
1691 {
1692 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1693 return FAIL;
1694 }
1695 whitep = *arg + 1;
1696 if (!IS_WHITE_OR_NUL(*whitep))
1697 {
1698 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1699 return FAIL;
1700 }
1701 *arg = skipwhite(whitep);
1702 }
1703
1704 *arg = *arg + 1;
1705
1706 // Allow for following comment, after at least one space.
1707 p = skipwhite(*arg);
1708 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1709 *arg += STRLEN(*arg);
1710
1711 dict_unref(d);
1712 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001713 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714
1715failret:
1716 if (*arg == NULL)
1717 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001718 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 *arg = (char_u *)"";
1720 }
1721 dict_unref(d);
1722 return FAIL;
1723}
1724
1725/*
1726 * Compile "&option".
1727 */
1728 static int
1729compile_get_option(char_u **arg, cctx_T *cctx)
1730{
1731 typval_T rettv;
1732 char_u *start = *arg;
1733 int ret;
1734
1735 // parse the option and get the current value to get the type.
1736 rettv.v_type = VAR_UNKNOWN;
1737 ret = eval_option(arg, &rettv, TRUE);
1738 if (ret == OK)
1739 {
1740 // include the '&' in the name, eval_option() expects it.
1741 char_u *name = vim_strnsave(start, *arg - start);
1742 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1743 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1744
1745 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1746 vim_free(name);
1747 }
1748 clear_tv(&rettv);
1749
1750 return ret;
1751}
1752
1753/*
1754 * Compile "$VAR".
1755 */
1756 static int
1757compile_get_env(char_u **arg, cctx_T *cctx)
1758{
1759 char_u *start = *arg;
1760 int len;
1761 int ret;
1762 char_u *name;
1763
1764 ++*arg;
1765 len = get_env_len(arg);
1766 if (len == 0)
1767 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001768 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 return FAIL;
1770 }
1771
1772 // include the '$' in the name, eval_env_var() expects it.
1773 name = vim_strnsave(start, len + 1);
1774 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1775 vim_free(name);
1776 return ret;
1777}
1778
1779/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001780 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001781 */
1782 static int
1783compile_interp_string(char_u **arg, cctx_T *cctx)
1784{
1785 typval_T tv;
1786 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001787 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001788 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001789 int count = 0;
1790 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001791
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001792 // *arg is on the '$' character, move it to the first string character.
1793 ++*arg;
1794 quote = **arg;
1795 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001796
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001797 for (;;)
1798 {
1799 // Get the string up to the matching quote or to a single '{'.
1800 // "arg" is advanced to either the quote or the '{'.
1801 if (quote == '"')
1802 ret = eval_string(arg, &tv, evaluate, TRUE);
1803 else
1804 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1805 if (ret == FAIL)
1806 break;
1807 if (evaluate)
1808 {
1809 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1810 || (**arg != '{' && count == 0))
1811 {
1812 // generate non-empty string or empty string if it's the only
1813 // one
1814 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1815 return FAIL;
1816 tv.vval.v_string = NULL; // don't free it now
1817 ++count;
1818 }
1819 clear_tv(&tv);
1820 }
1821
1822 if (**arg != '{')
1823 {
1824 // found terminating quote
1825 ++*arg;
1826 break;
1827 }
1828
1829 p = compile_one_expr_in_str(*arg, cctx);
1830 if (p == NULL)
1831 {
1832 ret = FAIL;
1833 break;
1834 }
1835 ++count;
1836 *arg = p;
1837 }
LemonBoy2eaef102022-05-06 13:14:50 +01001838
1839 if (ret == FAIL || !evaluate)
1840 return ret;
1841
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001842 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1843 if (count > 1)
1844 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001845
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001846 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001847}
1848
1849/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850 * Compile "@r".
1851 */
1852 static int
1853compile_get_register(char_u **arg, cctx_T *cctx)
1854{
1855 int ret;
1856
1857 ++*arg;
1858 if (**arg == NUL)
1859 {
1860 semsg(_(e_syntax_error_at_str), *arg - 1);
1861 return FAIL;
1862 }
1863 if (!valid_yank_reg(**arg, FALSE))
1864 {
1865 emsg_invreg(**arg);
1866 return FAIL;
1867 }
1868 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1869 ++*arg;
1870 return ret;
1871}
1872
1873/*
1874 * Apply leading '!', '-' and '+' to constant "rettv".
1875 * When "numeric_only" is TRUE do not apply '!'.
1876 */
1877 static int
1878apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1879{
1880 char_u *p = *end;
1881
1882 // this works from end to start
1883 while (p > start)
1884 {
1885 --p;
1886 if (*p == '-' || *p == '+')
1887 {
1888 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889 if (rettv->v_type == VAR_FLOAT)
1890 {
1891 if (*p == '-')
1892 rettv->vval.v_float = -rettv->vval.v_float;
1893 }
1894 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895 {
1896 varnumber_T val;
1897 int error = FALSE;
1898
1899 // tv_get_number_chk() accepts a string, but we don't want that
1900 // here
1901 if (check_not_string(rettv) == FAIL)
1902 return FAIL;
1903 val = tv_get_number_chk(rettv, &error);
1904 clear_tv(rettv);
1905 if (error)
1906 return FAIL;
1907 if (*p == '-')
1908 val = -val;
1909 rettv->v_type = VAR_NUMBER;
1910 rettv->vval.v_number = val;
1911 }
1912 }
1913 else if (numeric_only)
1914 {
1915 ++p;
1916 break;
1917 }
1918 else if (*p == '!')
1919 {
1920 int v = tv2bool(rettv);
1921
1922 // '!' is permissive in the type.
1923 clear_tv(rettv);
1924 rettv->v_type = VAR_BOOL;
1925 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1926 }
1927 }
1928 *end = p;
1929 return OK;
1930}
1931
1932/*
1933 * Recognize v: variables that are constants and set "rettv".
1934 */
1935 static void
1936get_vim_constant(char_u **arg, typval_T *rettv)
1937{
1938 if (STRNCMP(*arg, "v:true", 6) == 0)
1939 {
1940 rettv->v_type = VAR_BOOL;
1941 rettv->vval.v_number = VVAL_TRUE;
1942 *arg += 6;
1943 }
1944 else if (STRNCMP(*arg, "v:false", 7) == 0)
1945 {
1946 rettv->v_type = VAR_BOOL;
1947 rettv->vval.v_number = VVAL_FALSE;
1948 *arg += 7;
1949 }
1950 else if (STRNCMP(*arg, "v:null", 6) == 0)
1951 {
1952 rettv->v_type = VAR_SPECIAL;
1953 rettv->vval.v_number = VVAL_NULL;
1954 *arg += 6;
1955 }
1956 else if (STRNCMP(*arg, "v:none", 6) == 0)
1957 {
1958 rettv->v_type = VAR_SPECIAL;
1959 rettv->vval.v_number = VVAL_NONE;
1960 *arg += 6;
1961 }
1962}
1963
1964 exprtype_T
1965get_compare_type(char_u *p, int *len, int *type_is)
1966{
1967 exprtype_T type = EXPR_UNKNOWN;
1968 int i;
1969
1970 switch (p[0])
1971 {
1972 case '=': if (p[1] == '=')
1973 type = EXPR_EQUAL;
1974 else if (p[1] == '~')
1975 type = EXPR_MATCH;
1976 break;
1977 case '!': if (p[1] == '=')
1978 type = EXPR_NEQUAL;
1979 else if (p[1] == '~')
1980 type = EXPR_NOMATCH;
1981 break;
1982 case '>': if (p[1] != '=')
1983 {
1984 type = EXPR_GREATER;
1985 *len = 1;
1986 }
1987 else
1988 type = EXPR_GEQUAL;
1989 break;
1990 case '<': if (p[1] != '=')
1991 {
1992 type = EXPR_SMALLER;
1993 *len = 1;
1994 }
1995 else
1996 type = EXPR_SEQUAL;
1997 break;
1998 case 'i': if (p[1] == 's')
1999 {
2000 // "is" and "isnot"; but not a prefix of a name
2001 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2002 *len = 5;
2003 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002004 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005 {
2006 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2007 *type_is = TRUE;
2008 }
2009 }
2010 break;
2011 }
2012 return type;
2013}
2014
2015/*
2016 * Skip over an expression, ignoring most errors.
2017 */
2018 void
2019skip_expr_cctx(char_u **arg, cctx_T *cctx)
2020{
2021 evalarg_T evalarg;
2022
2023 init_evalarg(&evalarg);
2024 evalarg.eval_cctx = cctx;
2025 skip_expr(arg, &evalarg);
2026 clear_evalarg(&evalarg, NULL);
2027}
2028
2029/*
2030 * Check that the top of the type stack has a type that can be used as a
2031 * condition. Give an error and return FAIL if not.
2032 */
2033 int
2034bool_on_stack(cctx_T *cctx)
2035{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002036 type_T *type;
2037
Bram Moolenaar078a4612022-01-04 15:17:03 +00002038 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002039 if (type == &t_bool)
2040 return OK;
2041
Bram Moolenaar4913d422022-10-17 13:13:32 +01002042 if (type->tt_type == VAR_ANY
2043 || type->tt_type == VAR_UNKNOWN
2044 || type->tt_type == VAR_NUMBER
2045 || type == &t_number_bool
2046 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2048 // This requires a runtime type check.
2049 return generate_COND2BOOL(cctx);
2050
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002051 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002052}
2053
2054/*
2055 * Give the "white on both sides" error, taking the operator from "p[len]".
2056 */
2057 void
2058error_white_both(char_u *op, int len)
2059{
2060 char_u buf[10];
2061
2062 vim_strncpy(buf, op, len);
2063 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2064}
2065
2066/*
2067 * Compile code to apply '-', '+' and '!'.
2068 * When "numeric_only" is TRUE do not apply '!'.
2069 */
2070 static int
2071compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2072{
2073 char_u *p = *end;
2074
2075 // this works from end to start
2076 while (p > start)
2077 {
2078 --p;
2079 while (VIM_ISWHITE(*p))
2080 --p;
2081 if (*p == '-' || *p == '+')
2082 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002083 type_T *type = get_type_on_stack(cctx, 0);
2084 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002085 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002086 return FAIL;
2087
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002088 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002089 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2090 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 }
2092 else if (numeric_only)
2093 {
2094 ++p;
2095 break;
2096 }
2097 else
2098 {
2099 int invert = *p == '!';
2100
2101 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2102 {
2103 if (p[-1] == '!')
2104 invert = !invert;
2105 --p;
2106 }
2107 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2108 return FAIL;
2109 }
2110 }
2111 *end = p;
2112 return OK;
2113}
2114
2115/*
2116 * Compile "(expression)": recursive!
2117 * Return FAIL/OK.
2118 */
2119 static int
2120compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2121{
2122 int ret;
2123 char_u *p = *arg + 1;
2124
2125 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2126 return FAIL;
2127 if (ppconst->pp_used <= PPSIZE - 10)
2128 {
2129 ret = compile_expr1(arg, cctx, ppconst);
2130 }
2131 else
2132 {
2133 // Not enough space in ppconst, flush constants.
2134 if (generate_ppconst(cctx, ppconst) == FAIL)
2135 return FAIL;
2136 ret = compile_expr0(arg, cctx);
2137 }
2138 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2139 return FAIL;
2140 if (**arg == ')')
2141 ++*arg;
2142 else if (ret == OK)
2143 {
2144 emsg(_(e_missing_closing_paren));
2145 ret = FAIL;
2146 }
2147 return ret;
2148}
2149
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002150static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002151
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152/*
2153 * Compile whatever comes after "name" or "name()".
2154 * Advances "*arg" only when something was recognized.
2155 */
2156 static int
2157compile_subscript(
2158 char_u **arg,
2159 cctx_T *cctx,
2160 char_u *start_leader,
2161 char_u **end_leader,
2162 ppconst_T *ppconst)
2163{
2164 char_u *name_start = *end_leader;
2165 int keeping_dict = FALSE;
2166
2167 for (;;)
2168 {
2169 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002170 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002171
2172 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2173 {
2174 char_u *next = peek_next_line_from_context(cctx);
2175
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002176 // If a following line starts with "->{", "->(" or "->X" advance to
2177 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178 // Also if a following line starts with ".x".
2179 if (next != NULL &&
2180 ((next[0] == '-' && next[1] == '>'
2181 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002182 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002183 || ASCII_ISALPHA(*skipwhite(next + 2))))
2184 || (next[0] == '.' && eval_isdictc(next[1]))))
2185 {
2186 next = next_line_from_context(cctx, TRUE);
2187 if (next == NULL)
2188 return FAIL;
2189 *arg = next;
2190 p = skipwhite(*arg);
2191 }
2192 }
2193
2194 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2195 // is not a function call.
2196 if (**arg == '(')
2197 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002198 int argcount = 0;
2199
2200 if (generate_ppconst(cctx, ppconst) == FAIL)
2201 return FAIL;
2202 ppconst->pp_is_const = FALSE;
2203
2204 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002205 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206
2207 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002208 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002209 return FAIL;
2210 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2211 return FAIL;
2212 if (keeping_dict)
2213 {
2214 keeping_dict = FALSE;
2215 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2216 return FAIL;
2217 }
2218 }
2219 else if (*p == '-' && p[1] == '>')
2220 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002221 char_u *pstart = p;
2222 int alt;
2223 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224
Bram Moolenaarc7349932022-01-16 20:59:39 +00002225 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 if (generate_ppconst(cctx, ppconst) == FAIL)
2227 return FAIL;
2228 ppconst->pp_is_const = FALSE;
2229
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002230 // Apply the '!', '-' and '+' first:
2231 // -1.0->func() works like (-1.0)->func()
2232 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2233 return FAIL;
2234
2235 p += 2;
2236 *arg = skipwhite(p);
2237 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002238
2239 // Three alternatives handled here:
2240 // 1. "base->name(" only a name, use compile_call()
2241 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2242 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002243 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002244 alt = 2;
2245 else
2246 {
2247 // alternative 1 or 3
2248 p = *arg;
2249 if (!eval_isnamec1(*p))
2250 {
2251 semsg(_(e_trailing_characters_str), pstart);
2252 return FAIL;
2253 }
2254 if (ASCII_ISALPHA(*p) && p[1] == ':')
2255 p += 2;
2256 for ( ; eval_isnamec(*p); ++p)
2257 ;
2258 if (*p == '(')
2259 {
2260 // alternative 1
2261 alt = 1;
2262 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2263 return FAIL;
2264 }
2265 else
2266 {
2267 // Must be alternative 3, find the "(". Only works within
2268 // one line.
2269 alt = 3;
2270 paren = vim_strchr(p, '(');
2271 if (paren == NULL)
2272 {
2273 semsg(_(e_missing_parenthesis_str), *arg);
2274 return FAIL;
2275 }
2276 }
2277 }
2278
2279 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002280 {
2281 int argcount = 1;
2282 garray_T *stack = &cctx->ctx_type_stack;
2283 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002284 int expr_isn_start = cctx->ctx_instr.ga_len;
2285 int expr_isn_end;
2286 int arg_isn_count;
2287
Bram Moolenaarc7349932022-01-16 20:59:39 +00002288 if (alt == 2)
2289 {
2290 // Funcref call: list->(Refs[2])(arg)
2291 // or lambda: list->((arg) => expr)(arg)
2292 //
2293 // Fist compile the function expression.
2294 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2295 return FAIL;
2296 }
2297 else
2298 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002299 int fail;
2300 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002301 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002302
Bram Moolenaarc7349932022-01-16 20:59:39 +00002303 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002304
2305 // instead of using LOADG for "import.Func" use PUSHFUNC
2306 ++paren_follows_after_expr;
2307
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002308 // do not look in the next line
2309 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002310
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002311 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002312 || *skipwhite(*arg) != NUL;
2313 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002314 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002315 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002316
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002317 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002318 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002319 if (did_emsg == prev_did_emsg)
2320 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002321 return FAIL;
2322 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002323 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002324
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 // Compile the arguments.
2326 if (**arg != '(')
2327 {
2328 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002329 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002330 else
2331 semsg(_(e_missing_parenthesis_str), *arg);
2332 return FAIL;
2333 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002334
2335 // Remember the next instruction index, where the instructions
2336 // for arguments are being written.
2337 expr_isn_end = cctx->ctx_instr.ga_len;
2338
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002339 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002340 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2341 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002342 return FAIL;
2343
2344 // Move the instructions for the arguments to before the
2345 // instructions of the expression and move the type of the
2346 // expression after the argument types. This is what ISN_PCALL
2347 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002348 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2349 if (arg_isn_count > 0)
2350 {
2351 int expr_isn_count = expr_isn_end - expr_isn_start;
2352 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002353 type_T *decl_type;
2354 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002355
2356 if (isn == NULL)
2357 return FAIL;
2358 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2359 + expr_isn_start,
2360 sizeof(isn_T) * expr_isn_count);
2361 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2362 + expr_isn_start,
2363 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2364 sizeof(isn_T) * arg_isn_count);
2365 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2366 + expr_isn_start + arg_isn_count,
2367 isn, sizeof(isn_T) * expr_isn_count);
2368 vim_free(isn);
2369
Bram Moolenaar078a4612022-01-04 15:17:03 +00002370 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2371 type = typep->type_curr;
2372 decl_type = typep->type_decl;
2373 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2374 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2375 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002376 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002377 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2378 typep->type_curr = type;
2379 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002380 }
2381
Bram Moolenaar078a4612022-01-04 15:17:03 +00002382 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2384 return FAIL;
2385 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002386
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 if (keeping_dict)
2388 {
2389 keeping_dict = FALSE;
2390 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2391 return FAIL;
2392 }
2393 }
2394 else if (**arg == '[')
2395 {
2396 int is_slice = FALSE;
2397
2398 // list index: list[123]
2399 // dict member: dict[key]
2400 // string index: text[123]
2401 // blob index: blob[123]
2402 if (generate_ppconst(cctx, ppconst) == FAIL)
2403 return FAIL;
2404 ppconst->pp_is_const = FALSE;
2405
2406 ++p;
2407 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2408 return FAIL;
2409 if (**arg == ':')
2410 {
2411 // missing first index is equal to zero
2412 generate_PUSHNR(cctx, 0);
2413 }
2414 else
2415 {
2416 if (compile_expr0(arg, cctx) == FAIL)
2417 return FAIL;
2418 if (**arg == ':')
2419 {
2420 semsg(_(e_white_space_required_before_and_after_str_at_str),
2421 ":", *arg);
2422 return FAIL;
2423 }
2424 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2425 return FAIL;
2426 *arg = skipwhite(*arg);
2427 }
2428 if (**arg == ':')
2429 {
2430 is_slice = TRUE;
2431 ++*arg;
2432 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2433 {
2434 semsg(_(e_white_space_required_before_and_after_str_at_str),
2435 ":", *arg);
2436 return FAIL;
2437 }
2438 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2439 return FAIL;
2440 if (**arg == ']')
2441 // missing second index is equal to end of string
2442 generate_PUSHNR(cctx, -1);
2443 else
2444 {
2445 if (compile_expr0(arg, cctx) == FAIL)
2446 return FAIL;
2447 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2448 return FAIL;
2449 *arg = skipwhite(*arg);
2450 }
2451 }
2452
2453 if (**arg != ']')
2454 {
2455 emsg(_(e_missing_closing_square_brace));
2456 return FAIL;
2457 }
2458 *arg = *arg + 1;
2459
2460 if (keeping_dict)
2461 {
2462 keeping_dict = FALSE;
2463 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2464 return FAIL;
2465 }
2466 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2467 return FAIL;
2468 }
2469 else if (*p == '.' && p[1] != '.')
2470 {
2471 // dictionary member: dict.name
2472 if (generate_ppconst(cctx, ppconst) == FAIL)
2473 return FAIL;
2474 ppconst->pp_is_const = FALSE;
2475
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002476 type = get_type_on_stack(cctx, 0);
2477 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002478 && (type->tt_type == VAR_CLASS
2479 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002480 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002481 // class member: SomeClass.varname
2482 // class method: SomeClass.SomeMethod()
2483 // class constructor: SomeClass.new()
2484 // object member: someObject.varname, this.varname
2485 // object method: someObject.SomeMethod(), this.SomeMethod()
2486 *arg = p;
2487 if (compile_class_object_index(cctx, arg, type) == FAIL)
2488 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002490 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002492 *arg = p + 1;
2493 if (IS_WHITE_OR_NUL(**arg))
2494 {
2495 emsg(_(e_missing_name_after_dot));
2496 return FAIL;
2497 }
2498 p = *arg;
2499 if (eval_isdictc(*p))
2500 while (eval_isnamec(*p))
2501 MB_PTR_ADV(p);
2502 if (p == *arg)
2503 {
2504 semsg(_(e_syntax_error_at_str), *arg);
2505 return FAIL;
2506 }
2507 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2508 return FAIL;
2509 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2510 return FAIL;
2511 keeping_dict = TRUE;
2512 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002513 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002514 }
2515 else
2516 break;
2517 }
2518
2519 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2520 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002521 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2522 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002523 return FAIL;
2524
2525 return OK;
2526}
2527
2528/*
2529 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2530 * "arg" is advanced until after the expression, skipping white space.
2531 *
2532 * If the value is a constant "ppconst->pp_used" will be non-zero.
2533 * Before instructions are generated, any values in "ppconst" will generated.
2534 *
2535 * This is the compiling equivalent of eval1(), eval2(), etc.
2536 */
2537
2538/*
2539 * number number constant
2540 * 0zFFFFFFFF Blob constant
2541 * "string" string constant
2542 * 'string' literal string constant
2543 * &option-name option value
2544 * @r register contents
2545 * identifier variable value
2546 * function() function call
2547 * $VAR environment variable
2548 * (expression) nested expression
2549 * [expr, expr] List
2550 * {key: val, [key]: val} Dictionary
2551 *
2552 * Also handle:
2553 * ! in front logical NOT
2554 * - in front unary minus
2555 * + in front unary plus (ignored)
2556 * trailing (arg) funcref/partial call
2557 * trailing [] subscript in String or List
2558 * trailing .name entry in Dictionary
2559 * trailing ->name() method call
2560 */
2561 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002562compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 char_u **arg,
2564 cctx_T *cctx,
2565 ppconst_T *ppconst)
2566{
2567 char_u *start_leader, *end_leader;
2568 int ret = OK;
2569 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2570 int used_before = ppconst->pp_used;
2571
2572 ppconst->pp_is_const = FALSE;
2573
2574 /*
2575 * Skip '!', '-' and '+' characters. They are handled later.
2576 */
2577 start_leader = *arg;
2578 if (eval_leader(arg, TRUE) == FAIL)
2579 return FAIL;
2580 end_leader = *arg;
2581
2582 rettv->v_type = VAR_UNKNOWN;
2583 switch (**arg)
2584 {
2585 /*
2586 * Number constant.
2587 */
2588 case '0': // also for blob starting with 0z
2589 case '1':
2590 case '2':
2591 case '3':
2592 case '4':
2593 case '5':
2594 case '6':
2595 case '7':
2596 case '8':
2597 case '9':
2598 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2599 return FAIL;
2600 // Apply "-" and "+" just before the number now, right to
2601 // left. Matters especially when "->" follows. Stops at
2602 // '!'.
2603 if (apply_leader(rettv, TRUE,
2604 start_leader, &end_leader) == FAIL)
2605 {
2606 clear_tv(rettv);
2607 return FAIL;
2608 }
2609 break;
2610
2611 /*
2612 * String constant: "string".
2613 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002614 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615 return FAIL;
2616 break;
2617
2618 /*
2619 * Literal string constant: 'str''ing'.
2620 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002621 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 return FAIL;
2623 break;
2624
2625 /*
2626 * Constant Vim variable.
2627 */
2628 case 'v': get_vim_constant(arg, rettv);
2629 ret = NOTDONE;
2630 break;
2631
2632 /*
2633 * "true" constant
2634 */
2635 case 't': if (STRNCMP(*arg, "true", 4) == 0
2636 && !eval_isnamec((*arg)[4]))
2637 {
2638 *arg += 4;
2639 rettv->v_type = VAR_BOOL;
2640 rettv->vval.v_number = VVAL_TRUE;
2641 }
2642 else
2643 ret = NOTDONE;
2644 break;
2645
2646 /*
2647 * "false" constant
2648 */
2649 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2650 && !eval_isnamec((*arg)[5]))
2651 {
2652 *arg += 5;
2653 rettv->v_type = VAR_BOOL;
2654 rettv->vval.v_number = VVAL_FALSE;
2655 }
2656 else
2657 ret = NOTDONE;
2658 break;
2659
2660 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002661 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002662 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002663 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002664 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002665 char_u *p = *arg + 4;
2666 int len;
2667
2668 for (len = 0; eval_isnamec(p[len]); ++len)
2669 ;
2670 ret = handle_predefined(*arg, len + 4, rettv);
2671 if (ret == FAIL)
2672 ret = NOTDONE;
2673 else
2674 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002675 }
2676 else
2677 ret = NOTDONE;
2678 break;
2679
2680 /*
2681 * List: [expr, expr]
2682 */
2683 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2684 return FAIL;
2685 ret = compile_list(arg, cctx, ppconst);
2686 break;
2687
2688 /*
2689 * Dictionary: {'key': val, 'key': val}
2690 */
2691 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2692 return FAIL;
2693 ret = compile_dict(arg, cctx, ppconst);
2694 break;
2695
2696 /*
2697 * Option value: &name
2698 */
2699 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2700 return FAIL;
2701 ret = compile_get_option(arg, cctx);
2702 break;
2703
2704 /*
2705 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002706 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002707 */
2708 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2709 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002710 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2711 ret = compile_interp_string(arg, cctx);
2712 else
2713 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002714 break;
2715
2716 /*
2717 * Register contents: @r.
2718 */
2719 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2720 return FAIL;
2721 ret = compile_get_register(arg, cctx);
2722 break;
2723 /*
2724 * nested expression: (expression).
2725 * lambda: (arg, arg) => expr
2726 * funcref: (arg, arg) => { statement }
2727 */
2728 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2729 ret = compile_lambda(arg, cctx);
2730 if (ret == NOTDONE)
2731 ret = compile_parenthesis(arg, cctx, ppconst);
2732 break;
2733
2734 default: ret = NOTDONE;
2735 break;
2736 }
2737 if (ret == FAIL)
2738 return FAIL;
2739
2740 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2741 {
2742 if (cctx->ctx_skip == SKIP_YES)
2743 clear_tv(rettv);
2744 else
2745 // A constant expression can possibly be handled compile time,
2746 // return the value instead of generating code.
2747 ++ppconst->pp_used;
2748 }
2749 else if (ret == NOTDONE)
2750 {
2751 char_u *p;
2752 int r;
2753
2754 if (!eval_isnamec1(**arg))
2755 {
2756 if (!vim9_bad_comment(*arg))
2757 {
2758 if (ends_excmd(*skipwhite(*arg)))
2759 semsg(_(e_empty_expression_str), *arg);
2760 else
2761 semsg(_(e_name_expected_str), *arg);
2762 }
2763 return FAIL;
2764 }
2765
2766 // "name" or "name()"
2767 p = to_name_end(*arg, TRUE);
2768 if (p - *arg == (size_t)1 && **arg == '_')
2769 {
2770 emsg(_(e_cannot_use_underscore_here));
2771 return FAIL;
2772 }
2773
2774 if (*p == '(')
2775 {
2776 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2777 }
2778 else
2779 {
2780 if (cctx->ctx_skip != SKIP_YES
2781 && generate_ppconst(cctx, ppconst) == FAIL)
2782 return FAIL;
2783 r = compile_load(arg, p, cctx, TRUE, TRUE);
2784 }
2785 if (r == FAIL)
2786 return FAIL;
2787 }
2788
2789 // Handle following "[]", ".member", etc.
2790 // Then deal with prefixed '-', '+' and '!', if not done already.
2791 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2792 ppconst) == FAIL)
2793 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002794 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002795 {
2796 // apply the '!', '-' and '+' before the constant
2797 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2798 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2799 return FAIL;
2800 return OK;
2801 }
2802 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2803 return FAIL;
2804 return OK;
2805}
2806
2807/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002808 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002809 */
2810 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002811compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002812{
2813 type_T *want_type = NULL;
2814
2815 // Recognize <type>
2816 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2817 {
2818 ++*arg;
2819 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2820 if (want_type == NULL)
2821 return FAIL;
2822
2823 if (**arg != '>')
2824 {
2825 if (*skipwhite(*arg) == '>')
2826 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2827 else
2828 emsg(_(e_missing_gt));
2829 return FAIL;
2830 }
2831 ++*arg;
2832 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2833 return FAIL;
2834 }
2835
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002836 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002837 return FAIL;
2838
2839 if (want_type != NULL)
2840 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002841 type_T *actual;
2842 where_T where = WHERE_INIT;
2843
LemonBoy50d48542024-07-04 17:03:17 +02002844 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002845 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002846 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002847 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002848 {
LemonBoy50d48542024-07-04 17:03:17 +02002849 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2850 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002851 return FAIL;
2852 }
2853 }
2854
2855 return OK;
2856}
2857
2858/*
2859 * * number multiplication
2860 * / number division
2861 * % number modulo
2862 */
2863 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002864compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002865{
2866 char_u *op;
2867 char_u *next;
2868 int ppconst_used = ppconst->pp_used;
2869
2870 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002871 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002872 return FAIL;
2873
2874 /*
2875 * Repeat computing, until no "*", "/" or "%" is following.
2876 */
2877 for (;;)
2878 {
2879 op = may_peek_next_line(cctx, *arg, &next);
2880 if (*op != '*' && *op != '/' && *op != '%')
2881 break;
2882 if (next != NULL)
2883 {
2884 *arg = next_line_from_context(cctx, TRUE);
2885 op = skipwhite(*arg);
2886 }
2887
2888 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2889 {
2890 error_white_both(op, 1);
2891 return FAIL;
2892 }
2893 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2894 return FAIL;
2895
2896 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002897 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002898 return FAIL;
2899
2900 if (ppconst->pp_used == ppconst_used + 2
2901 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2902 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2903 {
2904 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2905 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2906 varnumber_T res = 0;
2907 int failed = FALSE;
2908
2909 // both are numbers: compute the result
2910 switch (*op)
2911 {
2912 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2913 break;
2914 case '/': res = num_divide(tv1->vval.v_number,
2915 tv2->vval.v_number, &failed);
2916 break;
2917 case '%': res = num_modulus(tv1->vval.v_number,
2918 tv2->vval.v_number, &failed);
2919 break;
2920 }
2921 if (failed)
2922 return FAIL;
2923 tv1->vval.v_number = res;
2924 --ppconst->pp_used;
2925 }
2926 else
2927 {
2928 generate_ppconst(cctx, ppconst);
2929 generate_two_op(cctx, op);
2930 }
2931 }
2932
2933 return OK;
2934}
2935
2936/*
2937 * + number addition or list/blobl concatenation
2938 * - number subtraction
2939 * .. string concatenation
2940 */
2941 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002942compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002943{
2944 char_u *op;
2945 char_u *next;
2946 int oplen;
2947 int ppconst_used = ppconst->pp_used;
2948
2949 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002950 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002951 return FAIL;
2952
2953 /*
2954 * Repeat computing, until no "+", "-" or ".." is following.
2955 */
2956 for (;;)
2957 {
2958 op = may_peek_next_line(cctx, *arg, &next);
2959 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2960 break;
2961 if (op[0] == op[1] && *op != '.' && next)
2962 // Finding "++" or "--" on the next line is a separate command.
2963 // But ".." is concatenation.
2964 break;
2965 oplen = (*op == '.' ? 2 : 1);
2966 if (next != NULL)
2967 {
2968 *arg = next_line_from_context(cctx, TRUE);
2969 op = skipwhite(*arg);
2970 }
2971
2972 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2973 {
2974 error_white_both(op, oplen);
2975 return FAIL;
2976 }
2977
2978 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2979 return FAIL;
2980
2981 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002982 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002983 return FAIL;
2984
2985 if (ppconst->pp_used == ppconst_used + 2
2986 && (*op == '.'
2987 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2988 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2989 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2990 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2991 {
2992 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2993 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2994
2995 // concat/subtract/add constant numbers
2996 if (*op == '+')
2997 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2998 else if (*op == '-')
2999 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3000 else
3001 {
3002 // concatenate constant strings
3003 char_u *s1 = tv1->vval.v_string;
3004 char_u *s2 = tv2->vval.v_string;
3005 size_t len1 = STRLEN(s1);
3006
3007 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3008 if (tv1->vval.v_string == NULL)
3009 {
3010 clear_ppconst(ppconst);
3011 return FAIL;
3012 }
3013 mch_memmove(tv1->vval.v_string, s1, len1);
3014 STRCPY(tv1->vval.v_string + len1, s2);
3015 vim_free(s1);
3016 vim_free(s2);
3017 }
3018 --ppconst->pp_used;
3019 }
3020 else
3021 {
3022 generate_ppconst(cctx, ppconst);
3023 ppconst->pp_is_const = FALSE;
3024 if (*op == '.')
3025 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003026 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3027 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003028 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003029 if (generate_CONCAT(cctx, 2) == FAIL)
3030 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003031 }
3032 else
3033 generate_two_op(cctx, op);
3034 }
3035 }
3036
3037 return OK;
3038}
3039
3040/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003041 * expr6a >> expr6b
3042 * expr6a << expr6b
3043 *
3044 * Produces instructions:
3045 * OPNR bitwise left or right shift
3046 */
3047 static int
3048compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3049{
3050 exprtype_T type = EXPR_UNKNOWN;
3051 char_u *p;
3052 char_u *next;
3053 int len = 2;
3054 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003055 isn_T *isn;
3056
3057 // get the first variable
3058 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3059 return FAIL;
3060
3061 /*
3062 * Repeat computing, until no "+", "-" or ".." is following.
3063 */
3064 for (;;)
3065 {
3066 type = EXPR_UNKNOWN;
3067
3068 p = may_peek_next_line(cctx, *arg, &next);
3069 if (p[0] == '<' && p[1] == '<')
3070 type = EXPR_LSHIFT;
3071 else if (p[0] == '>' && p[1] == '>')
3072 type = EXPR_RSHIFT;
3073
3074 if (type == EXPR_UNKNOWN)
3075 return OK;
3076
3077 // Handle a bitwise left or right shift operator
3078 if (ppconst->pp_used == ppconst_used + 1)
3079 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003080 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003081 {
3082 // left operand should be a number
3083 emsg(_(e_bitshift_ops_must_be_number));
3084 return FAIL;
3085 }
3086 }
3087 else
3088 {
3089 type_T *t = get_type_on_stack(cctx, 0);
3090
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003091 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003092 {
3093 emsg(_(e_bitshift_ops_must_be_number));
3094 return FAIL;
3095 }
3096 }
3097
3098 if (next != NULL)
3099 {
3100 *arg = next_line_from_context(cctx, TRUE);
3101 p = skipwhite(*arg);
3102 }
3103
3104 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3105 {
3106 error_white_both(p, len);
3107 return FAIL;
3108 }
3109
3110 // get the second variable
3111 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3112 return FAIL;
3113
3114 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3115 return FAIL;
3116
3117 if (ppconst->pp_used == ppconst_used + 2)
3118 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003119 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3120 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3121
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003122 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003123 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3124 {
3125 // right operand should be a positive number
3126 if (tv2->v_type != VAR_NUMBER)
3127 emsg(_(e_bitshift_ops_must_be_number));
3128 else
dundargocc57b5bc2022-11-02 13:30:51 +00003129 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003130 return FAIL;
3131 }
3132
3133 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3134 tv1->vval.v_number = 0;
3135 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003136 tv1->vval.v_number =
3137 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003138 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003139 tv1->vval.v_number =
3140 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003141 clear_tv(tv2);
3142 --ppconst->pp_used;
3143 }
3144 else
3145 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003146 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3147 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003148 {
3149 emsg(_(e_bitshift_ops_must_be_number));
3150 return FAIL;
3151 }
3152
3153 generate_ppconst(cctx, ppconst);
3154
3155 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3156 if (isn == NULL)
3157 return FAIL;
3158
3159 if (isn != NULL)
3160 isn->isn_arg.op.op_type = type;
3161 }
3162 }
3163
3164 return OK;
3165}
3166
3167/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003168 * expr5a == expr5b
3169 * expr5a =~ expr5b
3170 * expr5a != expr5b
3171 * expr5a !~ expr5b
3172 * expr5a > expr5b
3173 * expr5a >= expr5b
3174 * expr5a < expr5b
3175 * expr5a <= expr5b
3176 * expr5a is expr5b
3177 * expr5a isnot expr5b
3178 *
3179 * Produces instructions:
3180 * EVAL expr5a Push result of "expr5a"
3181 * EVAL expr5b Push result of "expr5b"
3182 * COMPARE one of the compare instructions
3183 */
3184 static int
3185compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3186{
3187 exprtype_T type = EXPR_UNKNOWN;
3188 char_u *p;
3189 char_u *next;
3190 int len = 2;
3191 int type_is = FALSE;
3192 int ppconst_used = ppconst->pp_used;
3193
3194 // get the first variable
3195 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3196 return FAIL;
3197
3198 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003199
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003200 type = get_compare_type(p, &len, &type_is);
3201
3202 /*
3203 * If there is a comparative operator, use it.
3204 */
3205 if (type != EXPR_UNKNOWN)
3206 {
3207 int ic = FALSE; // Default: do not ignore case
3208
3209 if (next != NULL)
3210 {
3211 *arg = next_line_from_context(cctx, TRUE);
3212 p = skipwhite(*arg);
3213 }
3214 if (type_is && (p[len] == '?' || p[len] == '#'))
3215 {
3216 semsg(_(e_invalid_expression_str), *arg);
3217 return FAIL;
3218 }
3219 // extra question mark appended: ignore case
3220 if (p[len] == '?')
3221 {
3222 ic = TRUE;
3223 ++len;
3224 }
3225 // extra '#' appended: match case (ignored)
3226 else if (p[len] == '#')
3227 ++len;
3228 // nothing appended: match case
3229
3230 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3231 {
3232 error_white_both(p, len);
3233 return FAIL;
3234 }
3235
3236 // get the second variable
3237 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3238 return FAIL;
3239
3240 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3241 return FAIL;
3242
3243 if (ppconst->pp_used == ppconst_used + 2)
3244 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003245 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003246 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3247 int ret;
3248
3249 // Both sides are a constant, compute the result now.
3250 // First check for a valid combination of types, this is more
3251 // strict than typval_compare().
3252 if (check_compare_types(type, tv1, tv2) == FAIL)
3253 ret = FAIL;
3254 else
3255 {
3256 ret = typval_compare(tv1, tv2, type, ic);
3257 tv1->v_type = VAR_BOOL;
3258 tv1->vval.v_number = tv1->vval.v_number
3259 ? VVAL_TRUE : VVAL_FALSE;
3260 clear_tv(tv2);
3261 --ppconst->pp_used;
3262 }
3263 return ret;
3264 }
3265
3266 generate_ppconst(cctx, ppconst);
3267 return generate_COMPARE(cctx, type, ic);
3268 }
3269
3270 return OK;
3271}
3272
3273static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3274
3275/*
3276 * Compile || or &&.
3277 */
3278 static int
3279compile_and_or(
3280 char_u **arg,
3281 cctx_T *cctx,
3282 char *op,
3283 ppconst_T *ppconst,
3284 int ppconst_used UNUSED)
3285{
3286 char_u *next;
3287 char_u *p = may_peek_next_line(cctx, *arg, &next);
3288 int opchar = *op;
3289
3290 if (p[0] == opchar && p[1] == opchar)
3291 {
3292 garray_T *instr = &cctx->ctx_instr;
3293 garray_T end_ga;
3294 int save_skip = cctx->ctx_skip;
3295
3296 /*
3297 * Repeat until there is no following "||" or "&&"
3298 */
3299 ga_init2(&end_ga, sizeof(int), 10);
3300 while (p[0] == opchar && p[1] == opchar)
3301 {
3302 long start_lnum = SOURCING_LNUM;
3303 long save_sourcing_lnum;
3304 int start_ctx_lnum = cctx->ctx_lnum;
3305 int save_lnum;
3306 int const_used;
3307 int status;
3308 jumpwhen_T jump_when = opchar == '|'
3309 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3310
3311 if (next != NULL)
3312 {
3313 *arg = next_line_from_context(cctx, TRUE);
3314 p = skipwhite(*arg);
3315 }
3316
3317 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3318 {
3319 semsg(_(e_white_space_required_before_and_after_str_at_str),
3320 op, p);
3321 ga_clear(&end_ga);
3322 return FAIL;
3323 }
3324
3325 save_sourcing_lnum = SOURCING_LNUM;
3326 SOURCING_LNUM = start_lnum;
3327 save_lnum = cctx->ctx_lnum;
3328 cctx->ctx_lnum = start_ctx_lnum;
3329
3330 status = check_ppconst_bool(ppconst);
3331 if (status != FAIL)
3332 {
3333 // Use the last ppconst if possible.
3334 if (ppconst->pp_used > 0)
3335 {
3336 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3337 int is_true = tv2bool(tv);
3338
3339 if ((is_true && opchar == '|')
3340 || (!is_true && opchar == '&'))
3341 {
3342 // For "false && expr" and "true || expr" the "expr"
3343 // does not need to be evaluated.
3344 cctx->ctx_skip = SKIP_YES;
3345 clear_tv(tv);
3346 tv->v_type = VAR_BOOL;
3347 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3348 }
3349 else
3350 {
3351 // For "true && expr" and "false || expr" only "expr"
3352 // needs to be evaluated.
3353 --ppconst->pp_used;
3354 jump_when = JUMP_NEVER;
3355 }
3356 }
3357 else
3358 {
3359 // Every part must evaluate to a bool.
3360 status = bool_on_stack(cctx);
3361 }
3362 }
3363 if (status != FAIL)
3364 status = ga_grow(&end_ga, 1);
3365 cctx->ctx_lnum = save_lnum;
3366 if (status == FAIL)
3367 {
3368 ga_clear(&end_ga);
3369 return FAIL;
3370 }
3371
3372 if (jump_when != JUMP_NEVER)
3373 {
3374 if (cctx->ctx_skip != SKIP_YES)
3375 {
3376 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3377 ++end_ga.ga_len;
3378 }
3379 generate_JUMP(cctx, jump_when, 0);
3380 }
3381
3382 // eval the next expression
3383 SOURCING_LNUM = save_sourcing_lnum;
3384 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3385 {
3386 ga_clear(&end_ga);
3387 return FAIL;
3388 }
3389
3390 const_used = ppconst->pp_used;
3391 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3392 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3393 {
3394 ga_clear(&end_ga);
3395 return FAIL;
3396 }
3397
3398 // "0 || 1" results in true, "1 && 0" results in false.
3399 if (ppconst->pp_used == const_used + 1)
3400 {
3401 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3402
3403 if (tv->v_type == VAR_NUMBER
3404 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3405 {
3406 tv->vval.v_number = tv->vval.v_number == 1
3407 ? VVAL_TRUE : VVAL_FALSE;
3408 tv->v_type = VAR_BOOL;
3409 }
3410 }
3411
3412 p = may_peek_next_line(cctx, *arg, &next);
3413 }
3414
3415 if (check_ppconst_bool(ppconst) == FAIL)
3416 {
3417 ga_clear(&end_ga);
3418 return FAIL;
3419 }
3420
3421 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3422 // Every part must evaluate to a bool.
3423 if (bool_on_stack(cctx) == FAIL)
3424 {
3425 ga_clear(&end_ga);
3426 return FAIL;
3427 }
3428
3429 if (end_ga.ga_len > 0)
3430 {
3431 // Fill in the end label in all jumps.
3432 generate_ppconst(cctx, ppconst);
3433 while (end_ga.ga_len > 0)
3434 {
3435 isn_T *isn;
3436
3437 --end_ga.ga_len;
3438 isn = ((isn_T *)instr->ga_data)
3439 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3440 isn->isn_arg.jump.jump_where = instr->ga_len;
3441 }
3442 }
3443 ga_clear(&end_ga);
3444
3445 cctx->ctx_skip = save_skip;
3446 }
3447
3448 return OK;
3449}
3450
3451/*
3452 * expr4a && expr4a && expr4a logical AND
3453 *
3454 * Produces instructions:
3455 * EVAL expr4a Push result of "expr4a"
3456 * COND2BOOL convert to bool if needed
3457 * JUMP_IF_COND_FALSE end
3458 * EVAL expr4b Push result of "expr4b"
3459 * JUMP_IF_COND_FALSE end
3460 * EVAL expr4c Push result of "expr4c"
3461 * end:
3462 */
3463 static int
3464compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3465{
3466 int ppconst_used = ppconst->pp_used;
3467
3468 // get the first variable
3469 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3470 return FAIL;
3471
3472 // || and && work almost the same
3473 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3474}
3475
3476/*
3477 * expr3a || expr3b || expr3c logical OR
3478 *
3479 * Produces instructions:
3480 * EVAL expr3a Push result of "expr3a"
3481 * COND2BOOL convert to bool if needed
3482 * JUMP_IF_COND_TRUE end
3483 * EVAL expr3b Push result of "expr3b"
3484 * JUMP_IF_COND_TRUE end
3485 * EVAL expr3c Push result of "expr3c"
3486 * end:
3487 */
3488 static int
3489compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3490{
3491 int ppconst_used = ppconst->pp_used;
3492
3493 // eval the first expression
3494 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3495 return FAIL;
3496
3497 // || and && work almost the same
3498 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3499}
3500
3501/*
3502 * Toplevel expression: expr2 ? expr1a : expr1b
3503 * Produces instructions:
3504 * EVAL expr2 Push result of "expr2"
3505 * JUMP_IF_FALSE alt jump if false
3506 * EVAL expr1a
3507 * JUMP_ALWAYS end
3508 * alt: EVAL expr1b
3509 * end:
3510 *
3511 * Toplevel expression: expr2 ?? expr1
3512 * Produces instructions:
3513 * EVAL expr2 Push result of "expr2"
3514 * JUMP_AND_KEEP_IF_TRUE end jump if true
3515 * EVAL expr1
3516 * end:
3517 */
3518 int
3519compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3520{
3521 char_u *p;
3522 int ppconst_used = ppconst->pp_used;
3523 char_u *next;
3524
3525 // Ignore all kinds of errors when not producing code.
3526 if (cctx->ctx_skip == SKIP_YES)
3527 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003528 int prev_did_emsg = did_emsg;
3529
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003530 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003531 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003532 }
3533
3534 // Evaluate the first expression.
3535 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3536 return FAIL;
3537
3538 p = may_peek_next_line(cctx, *arg, &next);
3539 if (*p == '?')
3540 {
3541 int op_falsy = p[1] == '?';
3542 garray_T *instr = &cctx->ctx_instr;
3543 garray_T *stack = &cctx->ctx_type_stack;
3544 int alt_idx = instr->ga_len;
3545 int end_idx = 0;
3546 isn_T *isn;
3547 type_T *type1 = NULL;
3548 int has_const_expr = FALSE;
3549 int const_value = FALSE;
3550 int save_skip = cctx->ctx_skip;
3551
3552 if (next != NULL)
3553 {
3554 *arg = next_line_from_context(cctx, TRUE);
3555 p = skipwhite(*arg);
3556 }
3557
3558 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3559 {
3560 semsg(_(e_white_space_required_before_and_after_str_at_str),
3561 op_falsy ? "??" : "?", p);
3562 return FAIL;
3563 }
3564
3565 if (ppconst->pp_used == ppconst_used + 1)
3566 {
3567 // the condition is a constant, we know whether the ? or the :
3568 // expression is to be evaluated.
3569 has_const_expr = TRUE;
3570 if (op_falsy)
3571 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3572 else
3573 {
3574 int error = FALSE;
3575
3576 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3577 &error);
3578 if (error)
3579 return FAIL;
3580 }
3581 cctx->ctx_skip = save_skip == SKIP_YES ||
3582 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3583
3584 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3585 // "left ?? right" and "left" is truthy: produce "left"
3586 generate_ppconst(cctx, ppconst);
3587 else
3588 {
3589 clear_tv(&ppconst->pp_tv[ppconst_used]);
3590 --ppconst->pp_used;
3591 }
3592 }
3593 else
3594 {
3595 generate_ppconst(cctx, ppconst);
3596 if (op_falsy)
3597 end_idx = instr->ga_len;
3598 generate_JUMP(cctx, op_falsy
3599 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3600 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003601 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003602 }
3603
3604 // evaluate the second expression; any type is accepted
3605 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3606 return FAIL;
3607 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3608 return FAIL;
3609
3610 if (!has_const_expr)
3611 {
3612 generate_ppconst(cctx, ppconst);
3613
3614 if (!op_falsy)
3615 {
3616 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003617 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003618 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003619
3620 end_idx = instr->ga_len;
3621 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3622
3623 // jump here from JUMP_IF_FALSE
3624 isn = ((isn_T *)instr->ga_data) + alt_idx;
3625 isn->isn_arg.jump.jump_where = instr->ga_len;
3626 }
3627 }
3628
3629 if (!op_falsy)
3630 {
3631 // Check for the ":".
3632 p = may_peek_next_line(cctx, *arg, &next);
3633 if (*p != ':')
3634 {
3635 emsg(_(e_missing_colon_after_questionmark));
3636 return FAIL;
3637 }
3638 if (next != NULL)
3639 {
3640 *arg = next_line_from_context(cctx, TRUE);
3641 p = skipwhite(*arg);
3642 }
3643
3644 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3645 {
3646 semsg(_(e_white_space_required_before_and_after_str_at_str),
3647 ":", p);
3648 return FAIL;
3649 }
3650
3651 // evaluate the third expression
3652 if (has_const_expr)
3653 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3654 ? SKIP_YES : SKIP_NOT;
3655 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3656 return FAIL;
3657 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3658 return FAIL;
3659 }
3660
3661 if (!has_const_expr)
3662 {
3663 type_T **typep;
3664
3665 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003666 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003667
3668 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003669 typep = &((((type2_T *)stack->ga_data)
3670 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003671 common_type(type1, *typep, typep, cctx->ctx_type_list);
3672
3673 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3674 isn = ((isn_T *)instr->ga_data) + end_idx;
3675 isn->isn_arg.jump.jump_where = instr->ga_len;
3676 }
3677
3678 cctx->ctx_skip = save_skip;
3679 }
3680 return OK;
3681}
3682
3683/*
3684 * Toplevel expression.
3685 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3686 * Returns OK or FAIL.
3687 */
3688 int
3689compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3690{
3691 ppconst_T ppconst;
3692
3693 CLEAR_FIELD(ppconst);
3694 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3695 {
3696 clear_ppconst(&ppconst);
3697 return FAIL;
3698 }
3699 if (is_const != NULL)
3700 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3701 if (generate_ppconst(cctx, &ppconst) == FAIL)
3702 return FAIL;
3703 return OK;
3704}
3705
3706/*
3707 * Toplevel expression.
3708 */
3709 int
3710compile_expr0(char_u **arg, cctx_T *cctx)
3711{
3712 return compile_expr0_ext(arg, cctx, NULL);
3713}
3714
3715
3716#endif // defined(FEAT_EVAL)