blob: 9a96034543f7a1e9b1067bb69d77cd6da4f6427b [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 {
Yegappan Lakshmananc10342d2025-01-11 09:39:01 +0100395 int status;
396
397 if (IS_INTERFACE(cl))
398 status = generate_GET_ITF_MEMBER(cctx, cl, m_idx,
399 ocm->ocm_type);
400 else
401 status = generate_GET_OBJ_MEMBER(cctx, m_idx,
402 ocm->ocm_type);
403 if (status == FAIL)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200404 return FAIL;
405 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000406 }
407
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200408 // A private object method can be used only inside the class where it
409 // is defined or in one of the child classes.
410 // A private class method can be used only in the class where it is
411 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200412 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200413 ((type->tt_type == VAR_OBJECT
414 && !inside_class_hierarchy(cctx, cl))
415 || (type->tt_type == VAR_CLASS
416 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200417 {
Ernie Rael03042a22023-11-11 08:53:32 +0100418 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200419 return FAIL;
420 }
421
Bram Moolenaar574950d2023-01-03 19:08:50 +0000422 // Compile the arguments and call the class function or object method.
423 // The object method will know that the object is on the stack, just
424 // before the arguments.
425 *arg = skipwhite(name_end + 1);
426 int argcount = 0;
427 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
428 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000429
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200430 if (ocm != NULL)
431 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000432 if (type->tt_type == VAR_OBJECT
433 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
Ernie Rael58c95792024-08-13 23:27:22 +0200434 return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super);
435 return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000436 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000437
438 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000439 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200440 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200441 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000442 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200443 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000444 {
Ernie Rael03042a22023-11-11 08:53:32 +0100445 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200446 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200447 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200448 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200449
450 *arg = name_end;
451 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200452 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
453 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200454 }
455
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200456 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200457 m_idx = object_method_idx(cl, name, len);
458 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000459 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200460 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100461 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200462 if (*name == '_' && !inside_class(cctx, cl))
463 {
Ernie Rael03042a22023-11-11 08:53:32 +0100464 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200465 return FAIL;
466 }
467 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200468 // Remove the object type from the stack
469 --cctx->ctx_type_stack.ga_len;
470 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000471 }
472
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200473 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000474 }
475 else
476 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000477 // load class member
478 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200479 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
480 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000481 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200482 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200483 // A private class variable can be accessed only in the class where
484 // it is defined.
485 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200486 {
Ernie Rael03042a22023-11-11 08:53:32 +0100487 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200488 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200489 return FAIL;
490 }
491
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000492 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200493 // Remove the class type from the stack
494 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000495 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
496 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200497
498 // Could be a class method reference: "class.Func".
499 m_idx = class_method_idx(cl, name, len);
500 if (m_idx >= 0)
501 {
502 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100503 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200504 if (*name == '_' && !inside_class(cctx, cl))
505 {
Ernie Rael03042a22023-11-11 08:53:32 +0100506 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200507 return FAIL;
508 }
509 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200510 // Remove the class type from the stack
511 --cctx->ctx_type_stack.ga_len;
512 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200513 }
514
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200515 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000516 }
517
518 return FAIL;
519}
520
521/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000522 * Generate an instruction to load script-local variable "name", without the
523 * leading "s:".
524 * Also finds imported variables.
525 */
526 int
527compile_load_scriptvar(
528 cctx_T *cctx,
529 char_u *name, // variable NUL terminated
530 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100531 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000532{
533 scriptitem_T *si;
534 int idx;
535 imported_T *import;
536
537 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
538 return FAIL;
539 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000540 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000541 if (idx >= 0)
542 {
543 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
544
545 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
546 current_sctx.sc_sid, idx, sv->sv_type);
547 return OK;
548 }
549
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000550 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000551 if (import != NULL)
552 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000553 char_u *p = skipwhite(*end);
554 char_u *exp_name;
555 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100556 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000557 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000558 int done = FALSE;
559 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000560
Ernie Rael9a901792024-04-16 22:11:56 +0200561 check_script_symlink(import->imp_sid);
562 import_check_sourced_sid(&import->imp_sid);
563
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000564 // Need to lookup the member.
565 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000566 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000567 semsg(_(e_expected_dot_after_name_str), start);
568 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000570 ++p;
571 if (VIM_ISWHITE(*p))
572 {
573 emsg(_(e_no_white_space_allowed_after_dot));
574 return FAIL;
575 }
576
577 // isolate one name
578 exp_name = p;
579 while (eval_isnamec(*p))
580 ++p;
581 cc = *p;
582 *p = NUL;
583
Bram Moolenaard041f422022-01-12 19:54:00 +0000584 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100585 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
586 // "import autoload './dir/script.vim'" or
587 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100588 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100589
590 if (res == OK)
591 {
592 if (si->sn_autoload_prefix != NULL
593 && si->sn_state == SN_STATE_NOT_LOADED)
594 {
595 char_u *auto_name =
596 concat_str(si->sn_autoload_prefix, exp_name);
597
598 // autoload script must be loaded later, access by the autoload
599 // name. If a '(' follows it must be a function. Otherwise we
600 // don't know, it can be "script.Func".
601 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100602 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100603 else
604 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
605 vim_free(auto_name);
606 done = TRUE;
607 }
608 else if (si->sn_import_autoload
609 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100610 {
611 // If a '(' follows it must be a function. Otherwise we don't
612 // know, it can be "script.Func".
613 if (cc == '(' || paren_follows_after_expr)
614 {
615 char_u sid_name[MAX_FUNC_NAME_LEN];
616
617 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100618 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100619 }
620 else
621 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
622 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100623 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100624 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100625 else
626 {
627 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
628 cctx, NULL, TRUE);
629 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100630 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100631
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000632 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000633 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000634 if (done)
635 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000636
637 if (idx < 0)
638 {
639 if (ufunc != NULL)
640 {
641 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100642 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000643 return OK;
644 }
645 return FAIL;
646 }
647
648 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
649 import->imp_sid,
650 idx,
651 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000652 return OK;
653 }
654
Bram Moolenaard0132f42022-05-12 11:05:40 +0100655 // Can only get here if we know "name" is a script variable and not in a
656 // Vim9 script (variable is not in sn_var_vals): old style script.
657 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000658 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659}
660
661 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000662generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000663{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000664 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000665 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000667 // Reject a global non-autoload function found without the "g:" prefix.
668 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669 return FAIL;
670
671 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000672 compile_type = get_compile_type(ufunc);
673 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000674 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000675 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100676 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000677}
678
679/*
680 * Compile a variable name into a load instruction.
681 * "end" points to just after the name.
682 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
683 * When "error" is FALSE do not give an error when not found.
684 */
685 int
686compile_load(
687 char_u **arg,
688 char_u *end_arg,
689 cctx_T *cctx,
690 int is_expr,
691 int error)
692{
693 type_T *type;
694 char_u *name = NULL;
695 char_u *end = end_arg;
696 int res = FAIL;
697 int prev_called_emsg = called_emsg;
698
699 if (*(*arg + 1) == ':')
700 {
701 if (end <= *arg + 2)
702 {
703 isntype_T isn_type;
704
705 // load dictionary of namespace
706 switch (**arg)
707 {
708 case 'g': isn_type = ISN_LOADGDICT; break;
709 case 'w': isn_type = ISN_LOADWDICT; break;
710 case 't': isn_type = ISN_LOADTDICT; break;
711 case 'b': isn_type = ISN_LOADBDICT; break;
712 default:
713 semsg(_(e_namespace_not_supported_str), *arg);
714 goto theend;
715 }
716 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
717 goto theend;
718 res = OK;
719 }
720 else
721 {
722 isntype_T isn_type = ISN_DROP;
723
724 // load namespaced variable
725 name = vim_strnsave(*arg + 2, end - (*arg + 2));
726 if (name == NULL)
727 return FAIL;
728
729 switch (**arg)
730 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100731 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000732 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000733 case 's': if (current_script_is_vim9())
734 {
735 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
736 *arg);
737 vim_free(name);
738 return FAIL;
739 }
Kota Kato948a3892022-08-16 16:09:59 +0100740 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000741 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 else
743 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100744 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 break;
746 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
747 {
748 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000749 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000750 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751 else
752 isn_type = ISN_LOADG;
753 }
754 else
755 {
756 isn_type = ISN_LOADAUTO;
757 vim_free(name);
758 name = vim_strnsave(*arg, end - *arg);
759 if (name == NULL)
760 return FAIL;
761 }
762 break;
763 case 'w': isn_type = ISN_LOADW; break;
764 case 't': isn_type = ISN_LOADT; break;
765 case 'b': isn_type = ISN_LOADB; break;
766 default: // cannot happen, just in case
767 semsg(_(e_namespace_not_supported_str), *arg);
768 goto theend;
769 }
770 if (isn_type != ISN_DROP)
771 {
772 // Global, Buffer-local, Window-local and Tabpage-local
773 // variables can be defined later, thus we don't check if it
774 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000775 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776 }
777 }
778 }
779 else
780 {
781 size_t len = end - *arg;
782 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200783 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 int gen_load = FALSE;
785 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100786 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100787 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788
789 name = vim_strnsave(*arg, end - *arg);
790 if (name == NULL)
791 return FAIL;
792
Bram Moolenaar58b40092023-01-11 15:59:05 +0000793 if (STRCMP(name, "super") == 0
794 && cctx->ctx_ufunc != NULL
795 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
796 {
797 // super.SomeFunc() in a class function: push &t_super type, this
798 // is recognized in compile_subscript().
799 res = push_type_stack(cctx, &t_super);
800 if (*end != '.')
801 emsg(_(e_super_must_be_followed_by_dot));
802 }
803 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000804 {
805 script_autoload(name, FALSE);
806 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
807 }
808 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
809 == OK)
810 {
811 if (gen_load_outer == 0)
812 gen_load = TRUE;
813 }
814 else
815 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000816 lvar_T lvar;
817 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818
819 if (lookup_local(*arg, len, &lvar, cctx) == OK)
820 {
821 type = lvar.lv_type;
822 idx = lvar.lv_idx;
823 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100824 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100826 outer_loop_depth = lvar.lv_loop_depth;
827 outer_loop_idx = lvar.lv_loop_idx;
828 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000829 else
830 gen_load = TRUE;
831 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200832 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
833 (((idx =
834 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
835 || ((method_idx =
836 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000837 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200838 // Referencing a class variable or method without the class
839 // name. A class variable or method can be referenced without
840 // the class name only in the class where the function is
841 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200842 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200843 {
844 if (idx >= 0)
845 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
846 else
847 {
848 ufunc_T *fp = cl->class_class_functions[method_idx];
849 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
850 NULL);
851 }
852 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200853 else
854 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200855 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200856 name, cl->class_name);
857 res = FAIL;
858 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000859 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860 else
861 {
862 // "var" can be script-local even without using "s:" if it
863 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000864 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000865 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100866 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867
868 // When evaluating an expression and the name starts with an
869 // uppercase letter it can be a user defined function.
870 // generate_funcref() will fail if the function can't be found.
871 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000872 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 }
874 }
875 if (gen_load)
876 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
877 if (gen_load_outer > 0)
878 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100879 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
880 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 cctx->ctx_outer_used = TRUE;
882 }
883 }
884
885 *arg = end;
886
887theend:
888 if (res == FAIL && error && called_emsg == prev_called_emsg)
889 semsg(_(e_variable_not_found_str), name);
890 vim_free(name);
891 return res;
892}
893
894/*
895 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100896 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 * Returns FAIL if compilation fails.
898 */
899 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100900compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901{
LemonBoyf3b48952022-05-05 13:53:03 +0100902 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 garray_T save_ga = cctx->ctx_instr;
904 int expr_res;
905 int trailing_error;
906 int instr_count;
907 isn_T *instr = NULL;
908
909 // Remove the string type from the stack.
910 --cctx->ctx_type_stack.ga_len;
911
912 // Temporarily reset the list of instructions so that the jump labels are
913 // correct.
914 cctx->ctx_instr.ga_len = 0;
915 cctx->ctx_instr.ga_maxlen = 0;
916 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000917
918 // avoid peeking a next line
919 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
920 cctx->ctx_ufunc->uf_lines.ga_len = 0;
921
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000923
924 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
925
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926 s = skipwhite(s);
927 trailing_error = *s != NUL;
928
929 if (expr_res == FAIL || trailing_error
930 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
931 {
932 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000933 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000934 clear_instr_ga(&cctx->ctx_instr);
935 cctx->ctx_instr = save_ga;
936 ++cctx->ctx_type_stack.ga_len;
937 return FAIL;
938 }
939
940 // Move the generated instructions into the ISN_INSTR instruction, then
941 // restore the list of instructions.
942 instr_count = cctx->ctx_instr.ga_len;
943 instr = cctx->ctx_instr.ga_data;
944 instr[instr_count].isn_type = ISN_FINISH;
945
946 cctx->ctx_instr = save_ga;
947 vim_free(isn->isn_arg.string);
948 isn->isn_type = ISN_INSTR;
949 isn->isn_arg.instr = instr;
950 return OK;
951}
952
953/*
954 * Compile the argument expressions.
955 * "arg" points to just after the "(" and is advanced to after the ")"
956 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100957 int
LemonBoyf3b48952022-05-05 13:53:03 +0100958compile_arguments(
959 char_u **arg,
960 cctx_T *cctx,
961 int *argcount,
962 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963{
964 char_u *p = *arg;
965 char_u *whitep = *arg;
966 int must_end = FALSE;
967 int instr_count;
968
969 for (;;)
970 {
971 if (may_get_next_line(whitep, &p, cctx) == FAIL)
972 goto failret;
973 if (*p == ')')
974 {
975 *arg = p + 1;
976 return OK;
977 }
978 if (must_end)
979 {
980 semsg(_(e_missing_comma_before_argument_str), p);
981 return FAIL;
982 }
983
984 instr_count = cctx->ctx_instr.ga_len;
985 if (compile_expr0(&p, cctx) == FAIL)
986 return FAIL;
987 ++*argcount;
988
LemonBoyf3b48952022-05-05 13:53:03 +0100989 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 && cctx->ctx_instr.ga_len == instr_count + 1)
991 {
992 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
993
994 // {skip} argument of searchpair() can be compiled if not empty
995 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100996 compile_string(isn, cctx, 0);
997 }
998 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
999 && cctx->ctx_instr.ga_len == instr_count + 1)
1000 {
1001 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1002
1003 // {sub} argument of substitute() can be compiled if it starts
1004 // with \=
1005 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001006 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001007 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 }
1009
1010 if (*p != ',' && *skipwhite(p) == ',')
1011 {
1012 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1013 p = skipwhite(p);
1014 }
1015 if (*p == ',')
1016 {
1017 ++p;
1018 if (*p != NUL && !VIM_ISWHITE(*p))
1019 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1020 }
1021 else
1022 must_end = TRUE;
1023 whitep = p;
1024 p = skipwhite(p);
1025 }
1026failret:
1027 emsg(_(e_missing_closing_paren));
1028 return FAIL;
1029}
1030
1031/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001032 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1033 * etc.) if the class implements it.
1034 */
1035 static int
1036compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1037{
1038 type_T *type = get_decl_type_on_stack(cctx, 0);
1039 int res = FAIL;
1040
1041 // If the built in function is invoked on an object and the class
1042 // implements the corresponding built in method, then invoke the object
1043 // method.
1044 if (type->tt_type == VAR_OBJECT)
1045 {
1046 int method_idx;
1047 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1048 &method_idx);
1049 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001050 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001051 }
1052
1053 return res;
1054}
1055
1056
1057/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001058 * Compile a function call: name(arg1, arg2)
1059 * "arg" points to "name", "arg + varlen" to the "(".
1060 * "argcount_init" is 1 for "value->method()"
1061 * Instructions:
1062 * EVAL arg1
1063 * EVAL arg2
1064 * BCALL / DCALL / UCALL
1065 */
1066 static int
1067compile_call(
1068 char_u **arg,
1069 size_t varlen,
1070 cctx_T *cctx,
1071 ppconst_T *ppconst,
1072 int argcount_init)
1073{
1074 char_u *name = *arg;
1075 char_u *p;
1076 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001077 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001078 char_u fname_buf[FLEN_FIXED + 1];
1079 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001080 ufunc_T *ufunc = NULL;
1081 int res = FAIL;
1082 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001083 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001084 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001085 imported_T *import;
1086
1087 if (varlen >= sizeof(namebuf))
1088 {
1089 semsg(_(e_name_too_long_str), name);
1090 return FAIL;
1091 }
1092 vim_strncpy(namebuf, *arg, varlen);
1093
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001094 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001095 if (import != NULL)
1096 {
1097 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1098 return FAIL;
1099 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100
1101 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001102 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001104 if ((varlen == 3
1105 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1107 {
1108 char_u *s = skipwhite(*arg + varlen + 1);
1109 typval_T argvars[2];
1110 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001111 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001112
1113 argvars[0].v_type = VAR_UNKNOWN;
1114 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001115 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001117 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001118 s = skipwhite(s);
1119 if (*s == ')' && argvars[0].v_type == VAR_STRING
1120 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1121 || !is_has))
1122 {
1123 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1124
1125 *arg = s + 1;
1126 argvars[1].v_type = VAR_UNKNOWN;
1127 tv->v_type = VAR_NUMBER;
1128 tv->vval.v_number = 0;
1129 if (is_has)
1130 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001131 else if (is_len)
1132 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001133 else
1134 f_exists(argvars, tv);
1135 clear_tv(&argvars[0]);
1136 ++ppconst->pp_used;
1137 return OK;
1138 }
1139 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001140 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 {
1142 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1143 return FAIL;
1144 }
1145 }
1146
1147 if (generate_ppconst(cctx, ppconst) == FAIL)
1148 return FAIL;
1149
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001150 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001151 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1152
1153 // We handle the "skip" argument of searchpair() and searchpairpos()
1154 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001155 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1156 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1157 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1158 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1159 special_fn = CA_SEARCHPAIR;
1160 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1161 special_fn = CA_SUBSTITUTE;
1162 else
1163 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164
1165 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001166 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167 goto theend;
1168
1169 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1170 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1171 {
1172 int idx;
1173
1174 // builtin function
1175 idx = find_internal_func(name);
1176 if (idx >= 0)
1177 {
1178 if (STRCMP(name, "flatten") == 0)
1179 {
1180 emsg(_(e_cannot_use_flatten_in_vim9_script));
1181 goto theend;
1182 }
1183
1184 if (STRCMP(name, "add") == 0 && argcount == 2)
1185 {
h-eastb895b0f2023-09-24 15:46:31 +02001186 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001187 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1188 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001189
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190 // add() can be compiled to instructions if we know the type
1191 if (type->tt_type == VAR_LIST)
1192 {
1193 // inline "add(list, item)" so that the type can be checked
1194 res = generate_LISTAPPEND(cctx);
1195 idx = -1;
1196 }
1197 else if (type->tt_type == VAR_BLOB)
1198 {
1199 // inline "add(blob, nr)" so that the type can be checked
1200 res = generate_BLOBAPPEND(cctx);
1201 idx = -1;
1202 }
1203 }
1204
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001205 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1206 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001207 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001208 // May have the "D" or "R" flag, reserve a variable for a
1209 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001210 if (get_defer_var_idx(cctx) == 0)
1211 idx = -1;
1212 }
1213
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001214 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1215 if (STRCMP(name, "string") == 0)
1216 builtin_method = CLASS_BUILTIN_STRING;
1217 else if (STRCMP(name, "empty") == 0)
1218 builtin_method = CLASS_BUILTIN_EMPTY;
1219 else if (STRCMP(name, "len") == 0)
1220 builtin_method = CLASS_BUILTIN_LEN;
1221 if (builtin_method != CLASS_BUILTIN_INVALID)
1222 {
1223 res = compile_builtin_method_call(cctx, builtin_method);
1224 if (res == OK)
1225 idx = -1;
1226 }
1227
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228 if (idx >= 0)
1229 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1230 }
1231 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001232 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001233 goto theend;
1234 }
1235
Bram Moolenaar62aec932022-01-29 21:45:34 +00001236 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1237
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001238 // An argument or local variable can be a function reference, this
1239 // overrules a function name.
1240 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1241 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1242 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001243 class_T *cl = NULL;
1244 int mi = 0;
1245
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 // If we can find the function by name generate the right call.
1247 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001248 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001249 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001251 if (!func_is_global(ufunc))
1252 {
Ernie Rael58c95792024-08-13 23:27:22 +02001253 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001254 goto theend;
1255 }
1256 if (!has_g_namespace
1257 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1258 {
1259 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001260 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001261 goto theend;
1262 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001264 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001265 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001266 // Class method invocation without the class name.
1267 // A class method can be referenced without the class name only in
1268 // the class where the function is defined.
1269 if (cctx->ctx_ufunc->uf_defclass == cl)
1270 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001271 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001272 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001273 }
1274 else
1275 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001276 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001277 name, cl->class_name);
1278 res = FAIL;
1279 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001280 goto theend;
1281 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282 }
1283
1284 // If the name is a variable, load it and use PCALL.
1285 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001286 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001288 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001289 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1290 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001291 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001292
Christian Brabandtd5475e82023-08-17 23:34:09 +02001293 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 goto theend;
1295 }
1296
1297 // If we can find a global function by name generate the right call.
1298 if (ufunc != NULL)
1299 {
Ernie Rael58c95792024-08-13 23:27:22 +02001300 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001301 goto theend;
1302 }
1303
1304 // A global function may be defined only later. Need to figure out at
1305 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001306 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307 res = generate_UCALL(cctx, name, argcount);
1308 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001309 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310
1311theend:
1312 vim_free(tofree);
1313 return res;
1314}
1315
1316// like NAMESPACE_CHAR but with 'a' and 'l'.
1317#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1318
1319/*
1320 * Find the end of a variable or function name. Unlike find_name_end() this
1321 * does not recognize magic braces.
1322 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1323 * Return a pointer to just after the name. Equal to "arg" if there is no
1324 * valid name.
1325 */
1326 char_u *
1327to_name_end(char_u *arg, int use_namespace)
1328{
1329 char_u *p;
1330
1331 // Quick check for valid starting character.
1332 if (!eval_isnamec1(*arg))
1333 return arg;
1334
1335 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1336 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1337 // and can be used in slice "[n:]".
1338 if (*p == ':' && (p != arg + 1
1339 || !use_namespace
1340 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1341 break;
1342 return p;
1343}
1344
1345/*
1346 * Like to_name_end() but also skip over a list or dict constant.
1347 * Also accept "<SNR>123_Func".
1348 * This intentionally does not handle line continuation.
1349 */
1350 char_u *
1351to_name_const_end(char_u *arg)
1352{
1353 char_u *p = arg;
1354 typval_T rettv;
1355
1356 if (STRNCMP(p, "<SNR>", 5) == 0)
1357 p = skipdigits(p + 5);
1358 p = to_name_end(p, TRUE);
1359 if (p == arg && *arg == '[')
1360 {
1361
1362 // Can be "[1, 2, 3]->Func()".
1363 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1364 p = arg;
1365 }
1366 return p;
1367}
1368
1369/*
1370 * parse a list: [expr, expr]
1371 * "*arg" points to the '['.
1372 * ppconst->pp_is_const is set if all items are a constant.
1373 */
1374 static int
1375compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1376{
1377 char_u *p = skipwhite(*arg + 1);
1378 char_u *whitep = *arg + 1;
1379 int count = 0;
1380 int is_const;
1381 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001382 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001383
1384 for (;;)
1385 {
1386 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001388 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389 return FAIL;
1390 }
1391 if (*p == ',')
1392 {
1393 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1394 return FAIL;
1395 }
1396 if (*p == ']')
1397 {
1398 ++p;
1399 break;
1400 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001401 if (must_end)
1402 {
1403 semsg(_(e_missing_comma_in_list_str), p);
1404 return FAIL;
1405 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001406 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1407 return FAIL;
1408 if (!is_const)
1409 is_all_const = FALSE;
1410 ++count;
1411 if (*p == ',')
1412 {
1413 ++p;
1414 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1415 {
1416 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1417 return FAIL;
1418 }
1419 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001420 else
1421 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422 whitep = p;
1423 p = skipwhite(p);
1424 }
1425 *arg = p;
1426
1427 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001428 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001429}
1430
1431/*
1432 * Parse a lambda: "(arg, arg) => expr"
1433 * "*arg" points to the '('.
1434 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1435 */
1436 static int
1437compile_lambda(char_u **arg, cctx_T *cctx)
1438{
1439 int r;
1440 typval_T rettv;
1441 ufunc_T *ufunc;
1442 evalarg_T evalarg;
1443
1444 init_evalarg(&evalarg);
1445 evalarg.eval_flags = EVAL_EVALUATE;
1446 evalarg.eval_cctx = cctx;
1447
1448 // Get the funcref in "rettv".
1449 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1450 if (r != OK)
1451 {
1452 clear_evalarg(&evalarg, NULL);
1453 return r;
1454 }
1455
1456 // "rettv" will now be a partial referencing the function.
1457 ufunc = rettv.vval.v_partial->pt_func;
1458 ++ufunc->uf_refcount;
1459 clear_tv(&rettv);
1460
1461 // Compile it here to get the return type. The return type is optional,
1462 // when it's missing use t_unknown. This is recognized in
1463 // compile_return().
1464 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1465 ufunc->uf_ret_type = &t_unknown;
1466 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1467
1468 // When the outer function is compiled for profiling or debugging, the
1469 // lambda may be called without profiling or debugging. Compile it here in
1470 // the right context.
1471 if (cctx->ctx_compile_type == CT_DEBUG
1472#ifdef FEAT_PROFILE
1473 || cctx->ctx_compile_type == CT_PROFILE
1474#endif
1475 )
1476 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1477
Bram Moolenaar139575d2022-03-15 19:29:30 +00001478 // if the outer function is not compiled for debugging or profiling, this
1479 // one might be
1480 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001481 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001482 compiletype_T compile_type = get_compile_type(ufunc);
1483
1484 if (compile_type != CT_NONE)
1485 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001486 }
1487
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001488 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1489 // "*arg" may point into it. Point into the original line to avoid a
1490 // dangling pointer.
1491 if (evalarg.eval_using_cmdline)
1492 {
1493 garray_T *gap = &evalarg.eval_tofree_ga;
1494 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1495
1496 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1497 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001498 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001499 }
1500
1501 clear_evalarg(&evalarg, NULL);
1502
1503 if (ufunc->uf_def_status == UF_COMPILED)
1504 {
1505 // The return type will now be known.
1506 set_function_type(ufunc);
1507
1508 // The function reference count will be 1. When the ISN_FUNCREF
1509 // instruction is deleted the reference count is decremented and the
1510 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001511 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001512 }
1513
1514 func_ptr_unref(ufunc);
1515 return FAIL;
1516}
1517
1518/*
1519 * Get a lambda and compile it. Uses Vim9 syntax.
1520 */
1521 int
1522get_lambda_tv_and_compile(
1523 char_u **arg,
1524 typval_T *rettv,
1525 int types_optional,
1526 evalarg_T *evalarg)
1527{
1528 int r;
1529 ufunc_T *ufunc;
1530 int save_sc_version = current_sctx.sc_version;
1531
1532 // Get the funcref in "rettv".
1533 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1534 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1535 current_sctx.sc_version = save_sc_version;
1536 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001537 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001538
1539 // "rettv" will now be a partial referencing the function.
1540 ufunc = rettv->vval.v_partial->pt_func;
1541
1542 // Compile it here to get the return type. The return type is optional,
1543 // when it's missing use t_unknown. This is recognized in
1544 // compile_return().
1545 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1546 ufunc->uf_ret_type = &t_unknown;
1547 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1548
1549 if (ufunc->uf_def_status == UF_COMPILED)
1550 {
1551 // The return type will now be known.
1552 set_function_type(ufunc);
1553 return OK;
1554 }
1555 clear_tv(rettv);
1556 return FAIL;
1557}
1558
1559/*
1560 * parse a dict: {key: val, [key]: val}
1561 * "*arg" points to the '{'.
1562 * ppconst->pp_is_const is set if all item values are a constant.
1563 */
1564 static int
1565compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1566{
1567 garray_T *instr = &cctx->ctx_instr;
1568 int count = 0;
1569 dict_T *d = dict_alloc();
1570 dictitem_T *item;
1571 char_u *whitep = *arg + 1;
1572 char_u *p;
1573 int is_const;
1574 int is_all_const = TRUE; // reset when non-const encountered
1575
1576 if (d == NULL)
1577 return FAIL;
1578 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001579 {
1580 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001581 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001582 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001583 for (;;)
1584 {
1585 char_u *key = NULL;
1586
1587 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1588 {
1589 *arg = NULL;
1590 goto failret;
1591 }
1592
1593 if (**arg == '}')
1594 break;
1595
1596 if (**arg == '[')
1597 {
1598 isn_T *isn;
1599
1600 // {[expr]: value} uses an evaluated key.
1601 *arg = skipwhite(*arg + 1);
1602 if (compile_expr0(arg, cctx) == FAIL)
1603 return FAIL;
1604 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1605 if (isn->isn_type == ISN_PUSHNR)
1606 {
1607 char buf[NUMBUFLEN];
1608
1609 // Convert to string at compile time.
1610 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1611 isn->isn_type = ISN_PUSHS;
1612 isn->isn_arg.string = vim_strsave((char_u *)buf);
1613 }
1614 if (isn->isn_type == ISN_PUSHS)
1615 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001616 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001617 return FAIL;
1618 *arg = skipwhite(*arg);
1619 if (**arg != ']')
1620 {
1621 emsg(_(e_missing_matching_bracket_after_dict_key));
1622 return FAIL;
1623 }
1624 ++*arg;
1625 }
1626 else
1627 {
1628 // {"name": value},
1629 // {'name': value},
1630 // {name: value} use "name" as a literal key
1631 key = get_literal_key(arg);
1632 if (key == NULL)
1633 return FAIL;
1634 if (generate_PUSHS(cctx, &key) == FAIL)
1635 return FAIL;
1636 }
1637
1638 // Check for duplicate keys, if using string keys.
1639 if (key != NULL)
1640 {
1641 item = dict_find(d, key, -1);
1642 if (item != NULL)
1643 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001644 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645 goto failret;
1646 }
1647 item = dictitem_alloc(key);
1648 if (item != NULL)
1649 {
1650 item->di_tv.v_type = VAR_UNKNOWN;
1651 item->di_tv.v_lock = 0;
1652 if (dict_add(d, item) == FAIL)
1653 dictitem_free(item);
1654 }
1655 }
1656
1657 if (**arg != ':')
1658 {
1659 if (*skipwhite(*arg) == ':')
1660 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1661 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001662 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001663 return FAIL;
1664 }
1665 whitep = *arg + 1;
1666 if (!IS_WHITE_OR_NUL(*whitep))
1667 {
1668 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1669 return FAIL;
1670 }
1671
1672 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1673 {
1674 *arg = NULL;
1675 goto failret;
1676 }
1677
1678 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1679 return FAIL;
1680 if (!is_const)
1681 is_all_const = FALSE;
1682 ++count;
1683
1684 whitep = *arg;
1685 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1686 {
1687 *arg = NULL;
1688 goto failret;
1689 }
1690 if (**arg == '}')
1691 break;
1692 if (**arg != ',')
1693 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001694 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695 goto failret;
1696 }
1697 if (IS_WHITE_OR_NUL(*whitep))
1698 {
1699 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1700 return FAIL;
1701 }
1702 whitep = *arg + 1;
1703 if (!IS_WHITE_OR_NUL(*whitep))
1704 {
1705 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1706 return FAIL;
1707 }
1708 *arg = skipwhite(whitep);
1709 }
1710
1711 *arg = *arg + 1;
1712
1713 // Allow for following comment, after at least one space.
1714 p = skipwhite(*arg);
1715 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1716 *arg += STRLEN(*arg);
1717
1718 dict_unref(d);
1719 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001720 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001721
1722failret:
1723 if (*arg == NULL)
1724 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001725 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726 *arg = (char_u *)"";
1727 }
1728 dict_unref(d);
1729 return FAIL;
1730}
1731
1732/*
1733 * Compile "&option".
1734 */
1735 static int
1736compile_get_option(char_u **arg, cctx_T *cctx)
1737{
1738 typval_T rettv;
1739 char_u *start = *arg;
1740 int ret;
1741
1742 // parse the option and get the current value to get the type.
1743 rettv.v_type = VAR_UNKNOWN;
1744 ret = eval_option(arg, &rettv, TRUE);
1745 if (ret == OK)
1746 {
1747 // include the '&' in the name, eval_option() expects it.
1748 char_u *name = vim_strnsave(start, *arg - start);
1749 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1750 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1751
1752 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1753 vim_free(name);
1754 }
1755 clear_tv(&rettv);
1756
1757 return ret;
1758}
1759
1760/*
1761 * Compile "$VAR".
1762 */
1763 static int
1764compile_get_env(char_u **arg, cctx_T *cctx)
1765{
1766 char_u *start = *arg;
1767 int len;
1768 int ret;
1769 char_u *name;
1770
1771 ++*arg;
1772 len = get_env_len(arg);
1773 if (len == 0)
1774 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001775 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001776 return FAIL;
1777 }
1778
1779 // include the '$' in the name, eval_env_var() expects it.
1780 name = vim_strnsave(start, len + 1);
1781 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1782 vim_free(name);
1783 return ret;
1784}
1785
1786/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001787 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001788 */
1789 static int
1790compile_interp_string(char_u **arg, cctx_T *cctx)
1791{
1792 typval_T tv;
1793 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001794 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001795 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001796 int count = 0;
1797 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001798
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001799 // *arg is on the '$' character, move it to the first string character.
1800 ++*arg;
1801 quote = **arg;
1802 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001803
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001804 for (;;)
1805 {
1806 // Get the string up to the matching quote or to a single '{'.
1807 // "arg" is advanced to either the quote or the '{'.
1808 if (quote == '"')
1809 ret = eval_string(arg, &tv, evaluate, TRUE);
1810 else
1811 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1812 if (ret == FAIL)
1813 break;
1814 if (evaluate)
1815 {
1816 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1817 || (**arg != '{' && count == 0))
1818 {
1819 // generate non-empty string or empty string if it's the only
1820 // one
1821 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1822 return FAIL;
1823 tv.vval.v_string = NULL; // don't free it now
1824 ++count;
1825 }
1826 clear_tv(&tv);
1827 }
1828
1829 if (**arg != '{')
1830 {
1831 // found terminating quote
1832 ++*arg;
1833 break;
1834 }
1835
1836 p = compile_one_expr_in_str(*arg, cctx);
1837 if (p == NULL)
1838 {
1839 ret = FAIL;
1840 break;
1841 }
1842 ++count;
1843 *arg = p;
1844 }
LemonBoy2eaef102022-05-06 13:14:50 +01001845
1846 if (ret == FAIL || !evaluate)
1847 return ret;
1848
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001849 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1850 if (count > 1)
1851 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001852
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001853 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001854}
1855
1856/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001857 * Compile "@r".
1858 */
1859 static int
1860compile_get_register(char_u **arg, cctx_T *cctx)
1861{
1862 int ret;
1863
1864 ++*arg;
1865 if (**arg == NUL)
1866 {
1867 semsg(_(e_syntax_error_at_str), *arg - 1);
1868 return FAIL;
1869 }
1870 if (!valid_yank_reg(**arg, FALSE))
1871 {
1872 emsg_invreg(**arg);
1873 return FAIL;
1874 }
1875 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1876 ++*arg;
1877 return ret;
1878}
1879
1880/*
1881 * Apply leading '!', '-' and '+' to constant "rettv".
1882 * When "numeric_only" is TRUE do not apply '!'.
1883 */
1884 static int
1885apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1886{
1887 char_u *p = *end;
1888
1889 // this works from end to start
1890 while (p > start)
1891 {
1892 --p;
1893 if (*p == '-' || *p == '+')
1894 {
1895 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001896 if (rettv->v_type == VAR_FLOAT)
1897 {
1898 if (*p == '-')
1899 rettv->vval.v_float = -rettv->vval.v_float;
1900 }
1901 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001902 {
1903 varnumber_T val;
1904 int error = FALSE;
1905
1906 // tv_get_number_chk() accepts a string, but we don't want that
1907 // here
1908 if (check_not_string(rettv) == FAIL)
1909 return FAIL;
1910 val = tv_get_number_chk(rettv, &error);
1911 clear_tv(rettv);
1912 if (error)
1913 return FAIL;
1914 if (*p == '-')
1915 val = -val;
1916 rettv->v_type = VAR_NUMBER;
1917 rettv->vval.v_number = val;
1918 }
1919 }
1920 else if (numeric_only)
1921 {
1922 ++p;
1923 break;
1924 }
1925 else if (*p == '!')
1926 {
1927 int v = tv2bool(rettv);
1928
1929 // '!' is permissive in the type.
1930 clear_tv(rettv);
1931 rettv->v_type = VAR_BOOL;
1932 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1933 }
1934 }
1935 *end = p;
1936 return OK;
1937}
1938
1939/*
1940 * Recognize v: variables that are constants and set "rettv".
1941 */
1942 static void
1943get_vim_constant(char_u **arg, typval_T *rettv)
1944{
1945 if (STRNCMP(*arg, "v:true", 6) == 0)
1946 {
1947 rettv->v_type = VAR_BOOL;
1948 rettv->vval.v_number = VVAL_TRUE;
1949 *arg += 6;
1950 }
1951 else if (STRNCMP(*arg, "v:false", 7) == 0)
1952 {
1953 rettv->v_type = VAR_BOOL;
1954 rettv->vval.v_number = VVAL_FALSE;
1955 *arg += 7;
1956 }
1957 else if (STRNCMP(*arg, "v:null", 6) == 0)
1958 {
1959 rettv->v_type = VAR_SPECIAL;
1960 rettv->vval.v_number = VVAL_NULL;
1961 *arg += 6;
1962 }
1963 else if (STRNCMP(*arg, "v:none", 6) == 0)
1964 {
1965 rettv->v_type = VAR_SPECIAL;
1966 rettv->vval.v_number = VVAL_NONE;
1967 *arg += 6;
1968 }
1969}
1970
1971 exprtype_T
1972get_compare_type(char_u *p, int *len, int *type_is)
1973{
1974 exprtype_T type = EXPR_UNKNOWN;
1975 int i;
1976
1977 switch (p[0])
1978 {
1979 case '=': if (p[1] == '=')
1980 type = EXPR_EQUAL;
1981 else if (p[1] == '~')
1982 type = EXPR_MATCH;
1983 break;
1984 case '!': if (p[1] == '=')
1985 type = EXPR_NEQUAL;
1986 else if (p[1] == '~')
1987 type = EXPR_NOMATCH;
1988 break;
1989 case '>': if (p[1] != '=')
1990 {
1991 type = EXPR_GREATER;
1992 *len = 1;
1993 }
1994 else
1995 type = EXPR_GEQUAL;
1996 break;
1997 case '<': if (p[1] != '=')
1998 {
1999 type = EXPR_SMALLER;
2000 *len = 1;
2001 }
2002 else
2003 type = EXPR_SEQUAL;
2004 break;
2005 case 'i': if (p[1] == 's')
2006 {
2007 // "is" and "isnot"; but not a prefix of a name
2008 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2009 *len = 5;
2010 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002011 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012 {
2013 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2014 *type_is = TRUE;
2015 }
2016 }
2017 break;
2018 }
2019 return type;
2020}
2021
2022/*
2023 * Skip over an expression, ignoring most errors.
2024 */
2025 void
2026skip_expr_cctx(char_u **arg, cctx_T *cctx)
2027{
2028 evalarg_T evalarg;
2029
2030 init_evalarg(&evalarg);
2031 evalarg.eval_cctx = cctx;
2032 skip_expr(arg, &evalarg);
2033 clear_evalarg(&evalarg, NULL);
2034}
2035
2036/*
2037 * Check that the top of the type stack has a type that can be used as a
2038 * condition. Give an error and return FAIL if not.
2039 */
2040 int
2041bool_on_stack(cctx_T *cctx)
2042{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002043 type_T *type;
2044
Bram Moolenaar078a4612022-01-04 15:17:03 +00002045 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002046 if (type == &t_bool)
2047 return OK;
2048
Bram Moolenaar4913d422022-10-17 13:13:32 +01002049 if (type->tt_type == VAR_ANY
2050 || type->tt_type == VAR_UNKNOWN
2051 || type->tt_type == VAR_NUMBER
2052 || type == &t_number_bool
2053 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002054 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2055 // This requires a runtime type check.
2056 return generate_COND2BOOL(cctx);
2057
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002058 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059}
2060
2061/*
2062 * Give the "white on both sides" error, taking the operator from "p[len]".
2063 */
2064 void
2065error_white_both(char_u *op, int len)
2066{
2067 char_u buf[10];
2068
2069 vim_strncpy(buf, op, len);
2070 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2071}
2072
2073/*
2074 * Compile code to apply '-', '+' and '!'.
2075 * When "numeric_only" is TRUE do not apply '!'.
2076 */
2077 static int
2078compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2079{
2080 char_u *p = *end;
2081
2082 // this works from end to start
2083 while (p > start)
2084 {
2085 --p;
2086 while (VIM_ISWHITE(*p))
2087 --p;
2088 if (*p == '-' || *p == '+')
2089 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002090 type_T *type = get_type_on_stack(cctx, 0);
2091 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002092 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002093 return FAIL;
2094
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002095 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002096 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2097 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098 }
2099 else if (numeric_only)
2100 {
2101 ++p;
2102 break;
2103 }
2104 else
2105 {
2106 int invert = *p == '!';
2107
2108 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2109 {
2110 if (p[-1] == '!')
2111 invert = !invert;
2112 --p;
2113 }
2114 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2115 return FAIL;
2116 }
2117 }
2118 *end = p;
2119 return OK;
2120}
2121
2122/*
2123 * Compile "(expression)": recursive!
2124 * Return FAIL/OK.
2125 */
2126 static int
2127compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2128{
2129 int ret;
2130 char_u *p = *arg + 1;
2131
2132 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2133 return FAIL;
2134 if (ppconst->pp_used <= PPSIZE - 10)
2135 {
2136 ret = compile_expr1(arg, cctx, ppconst);
2137 }
2138 else
2139 {
2140 // Not enough space in ppconst, flush constants.
2141 if (generate_ppconst(cctx, ppconst) == FAIL)
2142 return FAIL;
2143 ret = compile_expr0(arg, cctx);
2144 }
2145 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2146 return FAIL;
2147 if (**arg == ')')
2148 ++*arg;
2149 else if (ret == OK)
2150 {
2151 emsg(_(e_missing_closing_paren));
2152 ret = FAIL;
2153 }
2154 return ret;
2155}
2156
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002157static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002158
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159/*
2160 * Compile whatever comes after "name" or "name()".
2161 * Advances "*arg" only when something was recognized.
2162 */
2163 static int
2164compile_subscript(
2165 char_u **arg,
2166 cctx_T *cctx,
2167 char_u *start_leader,
2168 char_u **end_leader,
2169 ppconst_T *ppconst)
2170{
2171 char_u *name_start = *end_leader;
2172 int keeping_dict = FALSE;
2173
2174 for (;;)
2175 {
2176 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002177 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178
2179 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2180 {
2181 char_u *next = peek_next_line_from_context(cctx);
2182
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002183 // If a following line starts with "->{", "->(" or "->X" advance to
2184 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002185 // Also if a following line starts with ".x".
2186 if (next != NULL &&
2187 ((next[0] == '-' && next[1] == '>'
2188 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002189 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002190 || ASCII_ISALPHA(*skipwhite(next + 2))))
2191 || (next[0] == '.' && eval_isdictc(next[1]))))
2192 {
2193 next = next_line_from_context(cctx, TRUE);
2194 if (next == NULL)
2195 return FAIL;
2196 *arg = next;
2197 p = skipwhite(*arg);
2198 }
2199 }
2200
2201 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2202 // is not a function call.
2203 if (**arg == '(')
2204 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002205 int argcount = 0;
2206
2207 if (generate_ppconst(cctx, ppconst) == FAIL)
2208 return FAIL;
2209 ppconst->pp_is_const = FALSE;
2210
2211 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002212 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002213
2214 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002215 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002216 return FAIL;
2217 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2218 return FAIL;
2219 if (keeping_dict)
2220 {
2221 keeping_dict = FALSE;
2222 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2223 return FAIL;
2224 }
2225 }
2226 else if (*p == '-' && p[1] == '>')
2227 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002228 char_u *pstart = p;
2229 int alt;
2230 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002231
Bram Moolenaarc7349932022-01-16 20:59:39 +00002232 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233 if (generate_ppconst(cctx, ppconst) == FAIL)
2234 return FAIL;
2235 ppconst->pp_is_const = FALSE;
2236
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237 // Apply the '!', '-' and '+' first:
2238 // -1.0->func() works like (-1.0)->func()
2239 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2240 return FAIL;
2241
2242 p += 2;
2243 *arg = skipwhite(p);
2244 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002245
2246 // Three alternatives handled here:
2247 // 1. "base->name(" only a name, use compile_call()
2248 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2249 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002250 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002251 alt = 2;
2252 else
2253 {
2254 // alternative 1 or 3
2255 p = *arg;
2256 if (!eval_isnamec1(*p))
2257 {
2258 semsg(_(e_trailing_characters_str), pstart);
2259 return FAIL;
2260 }
2261 if (ASCII_ISALPHA(*p) && p[1] == ':')
2262 p += 2;
2263 for ( ; eval_isnamec(*p); ++p)
2264 ;
2265 if (*p == '(')
2266 {
2267 // alternative 1
2268 alt = 1;
2269 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2270 return FAIL;
2271 }
2272 else
2273 {
2274 // Must be alternative 3, find the "(". Only works within
2275 // one line.
2276 alt = 3;
2277 paren = vim_strchr(p, '(');
2278 if (paren == NULL)
2279 {
2280 semsg(_(e_missing_parenthesis_str), *arg);
2281 return FAIL;
2282 }
2283 }
2284 }
2285
2286 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 {
2288 int argcount = 1;
2289 garray_T *stack = &cctx->ctx_type_stack;
2290 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002291 int expr_isn_start = cctx->ctx_instr.ga_len;
2292 int expr_isn_end;
2293 int arg_isn_count;
2294
Bram Moolenaarc7349932022-01-16 20:59:39 +00002295 if (alt == 2)
2296 {
2297 // Funcref call: list->(Refs[2])(arg)
2298 // or lambda: list->((arg) => expr)(arg)
2299 //
2300 // Fist compile the function expression.
2301 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2302 return FAIL;
2303 }
2304 else
2305 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002306 int fail;
2307 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002308 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002309
Bram Moolenaarc7349932022-01-16 20:59:39 +00002310 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002311
2312 // instead of using LOADG for "import.Func" use PUSHFUNC
2313 ++paren_follows_after_expr;
2314
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002315 // do not look in the next line
2316 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002317
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002318 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002319 || *skipwhite(*arg) != NUL;
2320 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002321 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002322 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002323
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002324 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002325 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002326 if (did_emsg == prev_did_emsg)
2327 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002328 return FAIL;
2329 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002330 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002331
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002332 // Compile the arguments.
2333 if (**arg != '(')
2334 {
2335 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002336 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337 else
2338 semsg(_(e_missing_parenthesis_str), *arg);
2339 return FAIL;
2340 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002341
2342 // Remember the next instruction index, where the instructions
2343 // for arguments are being written.
2344 expr_isn_end = cctx->ctx_instr.ga_len;
2345
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002346 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002347 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2348 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349 return FAIL;
2350
2351 // Move the instructions for the arguments to before the
2352 // instructions of the expression and move the type of the
2353 // expression after the argument types. This is what ISN_PCALL
2354 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002355 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2356 if (arg_isn_count > 0)
2357 {
2358 int expr_isn_count = expr_isn_end - expr_isn_start;
2359 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002360 type_T *decl_type;
2361 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362
2363 if (isn == NULL)
2364 return FAIL;
2365 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2366 + expr_isn_start,
2367 sizeof(isn_T) * expr_isn_count);
2368 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2369 + expr_isn_start,
2370 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2371 sizeof(isn_T) * arg_isn_count);
2372 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2373 + expr_isn_start + arg_isn_count,
2374 isn, sizeof(isn_T) * expr_isn_count);
2375 vim_free(isn);
2376
Bram Moolenaar078a4612022-01-04 15:17:03 +00002377 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2378 type = typep->type_curr;
2379 decl_type = typep->type_decl;
2380 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2381 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2382 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002384 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2385 typep->type_curr = type;
2386 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 }
2388
Bram Moolenaar078a4612022-01-04 15:17:03 +00002389 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002390 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2391 return FAIL;
2392 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002393
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 if (keeping_dict)
2395 {
2396 keeping_dict = FALSE;
2397 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2398 return FAIL;
2399 }
2400 }
2401 else if (**arg == '[')
2402 {
2403 int is_slice = FALSE;
2404
2405 // list index: list[123]
2406 // dict member: dict[key]
2407 // string index: text[123]
2408 // blob index: blob[123]
2409 if (generate_ppconst(cctx, ppconst) == FAIL)
2410 return FAIL;
2411 ppconst->pp_is_const = FALSE;
2412
2413 ++p;
2414 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2415 return FAIL;
2416 if (**arg == ':')
2417 {
2418 // missing first index is equal to zero
2419 generate_PUSHNR(cctx, 0);
2420 }
2421 else
2422 {
2423 if (compile_expr0(arg, cctx) == FAIL)
2424 return FAIL;
2425 if (**arg == ':')
2426 {
2427 semsg(_(e_white_space_required_before_and_after_str_at_str),
2428 ":", *arg);
2429 return FAIL;
2430 }
2431 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2432 return FAIL;
2433 *arg = skipwhite(*arg);
2434 }
2435 if (**arg == ':')
2436 {
2437 is_slice = TRUE;
2438 ++*arg;
2439 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2440 {
2441 semsg(_(e_white_space_required_before_and_after_str_at_str),
2442 ":", *arg);
2443 return FAIL;
2444 }
2445 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2446 return FAIL;
2447 if (**arg == ']')
2448 // missing second index is equal to end of string
2449 generate_PUSHNR(cctx, -1);
2450 else
2451 {
2452 if (compile_expr0(arg, cctx) == FAIL)
2453 return FAIL;
2454 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2455 return FAIL;
2456 *arg = skipwhite(*arg);
2457 }
2458 }
2459
2460 if (**arg != ']')
2461 {
2462 emsg(_(e_missing_closing_square_brace));
2463 return FAIL;
2464 }
2465 *arg = *arg + 1;
2466
2467 if (keeping_dict)
2468 {
2469 keeping_dict = FALSE;
2470 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2471 return FAIL;
2472 }
2473 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2474 return FAIL;
2475 }
2476 else if (*p == '.' && p[1] != '.')
2477 {
2478 // dictionary member: dict.name
2479 if (generate_ppconst(cctx, ppconst) == FAIL)
2480 return FAIL;
2481 ppconst->pp_is_const = FALSE;
2482
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002483 type = get_type_on_stack(cctx, 0);
2484 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002485 && (type->tt_type == VAR_CLASS
2486 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002488 // class member: SomeClass.varname
2489 // class method: SomeClass.SomeMethod()
2490 // class constructor: SomeClass.new()
2491 // object member: someObject.varname, this.varname
2492 // object method: someObject.SomeMethod(), this.SomeMethod()
2493 *arg = p;
2494 if (compile_class_object_index(cctx, arg, type) == FAIL)
2495 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002497 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002498 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002499 *arg = p + 1;
2500 if (IS_WHITE_OR_NUL(**arg))
2501 {
2502 emsg(_(e_missing_name_after_dot));
2503 return FAIL;
2504 }
2505 p = *arg;
2506 if (eval_isdictc(*p))
2507 while (eval_isnamec(*p))
2508 MB_PTR_ADV(p);
2509 if (p == *arg)
2510 {
2511 semsg(_(e_syntax_error_at_str), *arg);
2512 return FAIL;
2513 }
2514 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2515 return FAIL;
2516 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2517 return FAIL;
2518 keeping_dict = TRUE;
2519 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002521 }
2522 else
2523 break;
2524 }
2525
2526 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2527 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002528 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2529 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002530 return FAIL;
2531
2532 return OK;
2533}
2534
2535/*
2536 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2537 * "arg" is advanced until after the expression, skipping white space.
2538 *
2539 * If the value is a constant "ppconst->pp_used" will be non-zero.
2540 * Before instructions are generated, any values in "ppconst" will generated.
2541 *
2542 * This is the compiling equivalent of eval1(), eval2(), etc.
2543 */
2544
2545/*
2546 * number number constant
2547 * 0zFFFFFFFF Blob constant
2548 * "string" string constant
2549 * 'string' literal string constant
2550 * &option-name option value
2551 * @r register contents
2552 * identifier variable value
2553 * function() function call
2554 * $VAR environment variable
2555 * (expression) nested expression
2556 * [expr, expr] List
2557 * {key: val, [key]: val} Dictionary
2558 *
2559 * Also handle:
2560 * ! in front logical NOT
2561 * - in front unary minus
2562 * + in front unary plus (ignored)
2563 * trailing (arg) funcref/partial call
2564 * trailing [] subscript in String or List
2565 * trailing .name entry in Dictionary
2566 * trailing ->name() method call
2567 */
2568 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002569compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 char_u **arg,
2571 cctx_T *cctx,
2572 ppconst_T *ppconst)
2573{
2574 char_u *start_leader, *end_leader;
2575 int ret = OK;
2576 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2577 int used_before = ppconst->pp_used;
2578
2579 ppconst->pp_is_const = FALSE;
2580
2581 /*
2582 * Skip '!', '-' and '+' characters. They are handled later.
2583 */
2584 start_leader = *arg;
2585 if (eval_leader(arg, TRUE) == FAIL)
2586 return FAIL;
2587 end_leader = *arg;
2588
2589 rettv->v_type = VAR_UNKNOWN;
2590 switch (**arg)
2591 {
2592 /*
2593 * Number constant.
2594 */
2595 case '0': // also for blob starting with 0z
2596 case '1':
2597 case '2':
2598 case '3':
2599 case '4':
2600 case '5':
2601 case '6':
2602 case '7':
2603 case '8':
2604 case '9':
2605 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2606 return FAIL;
2607 // Apply "-" and "+" just before the number now, right to
2608 // left. Matters especially when "->" follows. Stops at
2609 // '!'.
2610 if (apply_leader(rettv, TRUE,
2611 start_leader, &end_leader) == FAIL)
2612 {
2613 clear_tv(rettv);
2614 return FAIL;
2615 }
2616 break;
2617
2618 /*
2619 * String constant: "string".
2620 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002621 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 return FAIL;
2623 break;
2624
2625 /*
2626 * Literal string constant: 'str''ing'.
2627 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002628 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002629 return FAIL;
2630 break;
2631
2632 /*
2633 * Constant Vim variable.
2634 */
2635 case 'v': get_vim_constant(arg, rettv);
2636 ret = NOTDONE;
2637 break;
2638
2639 /*
2640 * "true" constant
2641 */
2642 case 't': if (STRNCMP(*arg, "true", 4) == 0
2643 && !eval_isnamec((*arg)[4]))
2644 {
2645 *arg += 4;
2646 rettv->v_type = VAR_BOOL;
2647 rettv->vval.v_number = VVAL_TRUE;
2648 }
2649 else
2650 ret = NOTDONE;
2651 break;
2652
2653 /*
2654 * "false" constant
2655 */
2656 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2657 && !eval_isnamec((*arg)[5]))
2658 {
2659 *arg += 5;
2660 rettv->v_type = VAR_BOOL;
2661 rettv->vval.v_number = VVAL_FALSE;
2662 }
2663 else
2664 ret = NOTDONE;
2665 break;
2666
2667 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002668 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002669 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002670 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002671 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002672 char_u *p = *arg + 4;
2673 int len;
2674
2675 for (len = 0; eval_isnamec(p[len]); ++len)
2676 ;
2677 ret = handle_predefined(*arg, len + 4, rettv);
2678 if (ret == FAIL)
2679 ret = NOTDONE;
2680 else
2681 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002682 }
2683 else
2684 ret = NOTDONE;
2685 break;
2686
2687 /*
2688 * List: [expr, expr]
2689 */
2690 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2691 return FAIL;
2692 ret = compile_list(arg, cctx, ppconst);
2693 break;
2694
2695 /*
2696 * Dictionary: {'key': val, 'key': val}
2697 */
2698 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2699 return FAIL;
2700 ret = compile_dict(arg, cctx, ppconst);
2701 break;
2702
2703 /*
2704 * Option value: &name
2705 */
2706 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2707 return FAIL;
2708 ret = compile_get_option(arg, cctx);
2709 break;
2710
2711 /*
2712 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002713 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002714 */
2715 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2716 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002717 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2718 ret = compile_interp_string(arg, cctx);
2719 else
2720 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002721 break;
2722
2723 /*
2724 * Register contents: @r.
2725 */
2726 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2727 return FAIL;
2728 ret = compile_get_register(arg, cctx);
2729 break;
2730 /*
2731 * nested expression: (expression).
2732 * lambda: (arg, arg) => expr
2733 * funcref: (arg, arg) => { statement }
2734 */
2735 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2736 ret = compile_lambda(arg, cctx);
2737 if (ret == NOTDONE)
2738 ret = compile_parenthesis(arg, cctx, ppconst);
2739 break;
2740
2741 default: ret = NOTDONE;
2742 break;
2743 }
2744 if (ret == FAIL)
2745 return FAIL;
2746
2747 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2748 {
2749 if (cctx->ctx_skip == SKIP_YES)
2750 clear_tv(rettv);
2751 else
2752 // A constant expression can possibly be handled compile time,
2753 // return the value instead of generating code.
2754 ++ppconst->pp_used;
2755 }
2756 else if (ret == NOTDONE)
2757 {
2758 char_u *p;
2759 int r;
2760
2761 if (!eval_isnamec1(**arg))
2762 {
2763 if (!vim9_bad_comment(*arg))
2764 {
2765 if (ends_excmd(*skipwhite(*arg)))
2766 semsg(_(e_empty_expression_str), *arg);
2767 else
2768 semsg(_(e_name_expected_str), *arg);
2769 }
2770 return FAIL;
2771 }
2772
2773 // "name" or "name()"
2774 p = to_name_end(*arg, TRUE);
2775 if (p - *arg == (size_t)1 && **arg == '_')
2776 {
2777 emsg(_(e_cannot_use_underscore_here));
2778 return FAIL;
2779 }
2780
2781 if (*p == '(')
2782 {
2783 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2784 }
2785 else
2786 {
2787 if (cctx->ctx_skip != SKIP_YES
2788 && generate_ppconst(cctx, ppconst) == FAIL)
2789 return FAIL;
2790 r = compile_load(arg, p, cctx, TRUE, TRUE);
2791 }
2792 if (r == FAIL)
2793 return FAIL;
2794 }
2795
2796 // Handle following "[]", ".member", etc.
2797 // Then deal with prefixed '-', '+' and '!', if not done already.
2798 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2799 ppconst) == FAIL)
2800 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002801 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002802 {
2803 // apply the '!', '-' and '+' before the constant
2804 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2805 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2806 return FAIL;
2807 return OK;
2808 }
2809 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2810 return FAIL;
2811 return OK;
2812}
2813
2814/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002815 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002816 */
2817 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002818compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002819{
2820 type_T *want_type = NULL;
2821
2822 // Recognize <type>
2823 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2824 {
2825 ++*arg;
2826 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2827 if (want_type == NULL)
2828 return FAIL;
2829
2830 if (**arg != '>')
2831 {
2832 if (*skipwhite(*arg) == '>')
2833 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2834 else
2835 emsg(_(e_missing_gt));
2836 return FAIL;
2837 }
2838 ++*arg;
2839 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2840 return FAIL;
2841 }
2842
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002843 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002844 return FAIL;
2845
2846 if (want_type != NULL)
2847 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002848 type_T *actual;
2849 where_T where = WHERE_INIT;
2850
LemonBoy50d48542024-07-04 17:03:17 +02002851 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002852 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002853 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002854 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002855 {
LemonBoy50d48542024-07-04 17:03:17 +02002856 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2857 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002858 return FAIL;
2859 }
2860 }
2861
2862 return OK;
2863}
2864
2865/*
2866 * * number multiplication
2867 * / number division
2868 * % number modulo
2869 */
2870 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002871compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002872{
2873 char_u *op;
2874 char_u *next;
2875 int ppconst_used = ppconst->pp_used;
2876
2877 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002878 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002879 return FAIL;
2880
2881 /*
2882 * Repeat computing, until no "*", "/" or "%" is following.
2883 */
2884 for (;;)
2885 {
2886 op = may_peek_next_line(cctx, *arg, &next);
2887 if (*op != '*' && *op != '/' && *op != '%')
2888 break;
2889 if (next != NULL)
2890 {
2891 *arg = next_line_from_context(cctx, TRUE);
2892 op = skipwhite(*arg);
2893 }
2894
2895 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2896 {
2897 error_white_both(op, 1);
2898 return FAIL;
2899 }
2900 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2901 return FAIL;
2902
2903 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002904 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002905 return FAIL;
2906
2907 if (ppconst->pp_used == ppconst_used + 2
2908 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2909 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2910 {
2911 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2912 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2913 varnumber_T res = 0;
2914 int failed = FALSE;
2915
2916 // both are numbers: compute the result
2917 switch (*op)
2918 {
2919 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2920 break;
2921 case '/': res = num_divide(tv1->vval.v_number,
2922 tv2->vval.v_number, &failed);
2923 break;
2924 case '%': res = num_modulus(tv1->vval.v_number,
2925 tv2->vval.v_number, &failed);
2926 break;
2927 }
2928 if (failed)
2929 return FAIL;
2930 tv1->vval.v_number = res;
2931 --ppconst->pp_used;
2932 }
2933 else
2934 {
2935 generate_ppconst(cctx, ppconst);
2936 generate_two_op(cctx, op);
2937 }
2938 }
2939
2940 return OK;
2941}
2942
2943/*
2944 * + number addition or list/blobl concatenation
2945 * - number subtraction
2946 * .. string concatenation
2947 */
2948 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002949compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002950{
2951 char_u *op;
2952 char_u *next;
2953 int oplen;
2954 int ppconst_used = ppconst->pp_used;
2955
2956 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002957 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002958 return FAIL;
2959
2960 /*
2961 * Repeat computing, until no "+", "-" or ".." is following.
2962 */
2963 for (;;)
2964 {
2965 op = may_peek_next_line(cctx, *arg, &next);
2966 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2967 break;
2968 if (op[0] == op[1] && *op != '.' && next)
2969 // Finding "++" or "--" on the next line is a separate command.
2970 // But ".." is concatenation.
2971 break;
2972 oplen = (*op == '.' ? 2 : 1);
2973 if (next != NULL)
2974 {
2975 *arg = next_line_from_context(cctx, TRUE);
2976 op = skipwhite(*arg);
2977 }
2978
2979 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2980 {
2981 error_white_both(op, oplen);
2982 return FAIL;
2983 }
2984
2985 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2986 return FAIL;
2987
2988 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002989 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002990 return FAIL;
2991
2992 if (ppconst->pp_used == ppconst_used + 2
2993 && (*op == '.'
2994 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2995 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2996 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2997 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2998 {
2999 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3000 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3001
3002 // concat/subtract/add constant numbers
3003 if (*op == '+')
3004 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3005 else if (*op == '-')
3006 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3007 else
3008 {
3009 // concatenate constant strings
3010 char_u *s1 = tv1->vval.v_string;
3011 char_u *s2 = tv2->vval.v_string;
3012 size_t len1 = STRLEN(s1);
3013
3014 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3015 if (tv1->vval.v_string == NULL)
3016 {
3017 clear_ppconst(ppconst);
3018 return FAIL;
3019 }
3020 mch_memmove(tv1->vval.v_string, s1, len1);
3021 STRCPY(tv1->vval.v_string + len1, s2);
3022 vim_free(s1);
3023 vim_free(s2);
3024 }
3025 --ppconst->pp_used;
3026 }
3027 else
3028 {
3029 generate_ppconst(cctx, ppconst);
3030 ppconst->pp_is_const = FALSE;
3031 if (*op == '.')
3032 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003033 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3034 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003035 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003036 if (generate_CONCAT(cctx, 2) == FAIL)
3037 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003038 }
3039 else
3040 generate_two_op(cctx, op);
3041 }
3042 }
3043
3044 return OK;
3045}
3046
3047/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003048 * expr6a >> expr6b
3049 * expr6a << expr6b
3050 *
3051 * Produces instructions:
3052 * OPNR bitwise left or right shift
3053 */
3054 static int
3055compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3056{
3057 exprtype_T type = EXPR_UNKNOWN;
3058 char_u *p;
3059 char_u *next;
3060 int len = 2;
3061 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003062 isn_T *isn;
3063
3064 // get the first variable
3065 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3066 return FAIL;
3067
3068 /*
3069 * Repeat computing, until no "+", "-" or ".." is following.
3070 */
3071 for (;;)
3072 {
3073 type = EXPR_UNKNOWN;
3074
3075 p = may_peek_next_line(cctx, *arg, &next);
3076 if (p[0] == '<' && p[1] == '<')
3077 type = EXPR_LSHIFT;
3078 else if (p[0] == '>' && p[1] == '>')
3079 type = EXPR_RSHIFT;
3080
3081 if (type == EXPR_UNKNOWN)
3082 return OK;
3083
3084 // Handle a bitwise left or right shift operator
3085 if (ppconst->pp_used == ppconst_used + 1)
3086 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003087 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003088 {
3089 // left operand should be a number
3090 emsg(_(e_bitshift_ops_must_be_number));
3091 return FAIL;
3092 }
3093 }
3094 else
3095 {
3096 type_T *t = get_type_on_stack(cctx, 0);
3097
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003098 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003099 {
3100 emsg(_(e_bitshift_ops_must_be_number));
3101 return FAIL;
3102 }
3103 }
3104
3105 if (next != NULL)
3106 {
3107 *arg = next_line_from_context(cctx, TRUE);
3108 p = skipwhite(*arg);
3109 }
3110
3111 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3112 {
3113 error_white_both(p, len);
3114 return FAIL;
3115 }
3116
3117 // get the second variable
3118 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3119 return FAIL;
3120
3121 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3122 return FAIL;
3123
3124 if (ppconst->pp_used == ppconst_used + 2)
3125 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003126 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3127 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3128
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003129 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003130 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3131 {
3132 // right operand should be a positive number
3133 if (tv2->v_type != VAR_NUMBER)
3134 emsg(_(e_bitshift_ops_must_be_number));
3135 else
dundargocc57b5bc2022-11-02 13:30:51 +00003136 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003137 return FAIL;
3138 }
3139
3140 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3141 tv1->vval.v_number = 0;
3142 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003143 tv1->vval.v_number =
3144 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003145 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003146 tv1->vval.v_number =
3147 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003148 clear_tv(tv2);
3149 --ppconst->pp_used;
3150 }
3151 else
3152 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003153 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3154 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003155 {
3156 emsg(_(e_bitshift_ops_must_be_number));
3157 return FAIL;
3158 }
3159
3160 generate_ppconst(cctx, ppconst);
3161
3162 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3163 if (isn == NULL)
3164 return FAIL;
3165
3166 if (isn != NULL)
3167 isn->isn_arg.op.op_type = type;
3168 }
3169 }
3170
3171 return OK;
3172}
3173
3174/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003175 * expr5a == expr5b
3176 * expr5a =~ expr5b
3177 * expr5a != expr5b
3178 * expr5a !~ expr5b
3179 * expr5a > expr5b
3180 * expr5a >= expr5b
3181 * expr5a < expr5b
3182 * expr5a <= expr5b
3183 * expr5a is expr5b
3184 * expr5a isnot expr5b
3185 *
3186 * Produces instructions:
3187 * EVAL expr5a Push result of "expr5a"
3188 * EVAL expr5b Push result of "expr5b"
3189 * COMPARE one of the compare instructions
3190 */
3191 static int
3192compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3193{
3194 exprtype_T type = EXPR_UNKNOWN;
3195 char_u *p;
3196 char_u *next;
3197 int len = 2;
3198 int type_is = FALSE;
3199 int ppconst_used = ppconst->pp_used;
3200
3201 // get the first variable
3202 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3203 return FAIL;
3204
3205 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003206
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003207 type = get_compare_type(p, &len, &type_is);
3208
3209 /*
3210 * If there is a comparative operator, use it.
3211 */
3212 if (type != EXPR_UNKNOWN)
3213 {
3214 int ic = FALSE; // Default: do not ignore case
3215
3216 if (next != NULL)
3217 {
3218 *arg = next_line_from_context(cctx, TRUE);
3219 p = skipwhite(*arg);
3220 }
3221 if (type_is && (p[len] == '?' || p[len] == '#'))
3222 {
3223 semsg(_(e_invalid_expression_str), *arg);
3224 return FAIL;
3225 }
3226 // extra question mark appended: ignore case
3227 if (p[len] == '?')
3228 {
3229 ic = TRUE;
3230 ++len;
3231 }
3232 // extra '#' appended: match case (ignored)
3233 else if (p[len] == '#')
3234 ++len;
3235 // nothing appended: match case
3236
3237 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3238 {
3239 error_white_both(p, len);
3240 return FAIL;
3241 }
3242
3243 // get the second variable
3244 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3245 return FAIL;
3246
3247 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3248 return FAIL;
3249
3250 if (ppconst->pp_used == ppconst_used + 2)
3251 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003252 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003253 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3254 int ret;
3255
3256 // Both sides are a constant, compute the result now.
3257 // First check for a valid combination of types, this is more
3258 // strict than typval_compare().
3259 if (check_compare_types(type, tv1, tv2) == FAIL)
3260 ret = FAIL;
3261 else
3262 {
3263 ret = typval_compare(tv1, tv2, type, ic);
3264 tv1->v_type = VAR_BOOL;
3265 tv1->vval.v_number = tv1->vval.v_number
3266 ? VVAL_TRUE : VVAL_FALSE;
3267 clear_tv(tv2);
3268 --ppconst->pp_used;
3269 }
3270 return ret;
3271 }
3272
3273 generate_ppconst(cctx, ppconst);
3274 return generate_COMPARE(cctx, type, ic);
3275 }
3276
3277 return OK;
3278}
3279
3280static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3281
3282/*
3283 * Compile || or &&.
3284 */
3285 static int
3286compile_and_or(
3287 char_u **arg,
3288 cctx_T *cctx,
3289 char *op,
3290 ppconst_T *ppconst,
3291 int ppconst_used UNUSED)
3292{
3293 char_u *next;
3294 char_u *p = may_peek_next_line(cctx, *arg, &next);
3295 int opchar = *op;
3296
3297 if (p[0] == opchar && p[1] == opchar)
3298 {
3299 garray_T *instr = &cctx->ctx_instr;
3300 garray_T end_ga;
3301 int save_skip = cctx->ctx_skip;
3302
3303 /*
3304 * Repeat until there is no following "||" or "&&"
3305 */
3306 ga_init2(&end_ga, sizeof(int), 10);
3307 while (p[0] == opchar && p[1] == opchar)
3308 {
3309 long start_lnum = SOURCING_LNUM;
3310 long save_sourcing_lnum;
3311 int start_ctx_lnum = cctx->ctx_lnum;
3312 int save_lnum;
3313 int const_used;
3314 int status;
3315 jumpwhen_T jump_when = opchar == '|'
3316 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3317
3318 if (next != NULL)
3319 {
3320 *arg = next_line_from_context(cctx, TRUE);
3321 p = skipwhite(*arg);
3322 }
3323
3324 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3325 {
3326 semsg(_(e_white_space_required_before_and_after_str_at_str),
3327 op, p);
3328 ga_clear(&end_ga);
3329 return FAIL;
3330 }
3331
3332 save_sourcing_lnum = SOURCING_LNUM;
3333 SOURCING_LNUM = start_lnum;
3334 save_lnum = cctx->ctx_lnum;
3335 cctx->ctx_lnum = start_ctx_lnum;
3336
3337 status = check_ppconst_bool(ppconst);
3338 if (status != FAIL)
3339 {
3340 // Use the last ppconst if possible.
3341 if (ppconst->pp_used > 0)
3342 {
3343 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3344 int is_true = tv2bool(tv);
3345
3346 if ((is_true && opchar == '|')
3347 || (!is_true && opchar == '&'))
3348 {
3349 // For "false && expr" and "true || expr" the "expr"
3350 // does not need to be evaluated.
3351 cctx->ctx_skip = SKIP_YES;
3352 clear_tv(tv);
3353 tv->v_type = VAR_BOOL;
3354 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3355 }
3356 else
3357 {
3358 // For "true && expr" and "false || expr" only "expr"
3359 // needs to be evaluated.
3360 --ppconst->pp_used;
3361 jump_when = JUMP_NEVER;
3362 }
3363 }
3364 else
3365 {
3366 // Every part must evaluate to a bool.
3367 status = bool_on_stack(cctx);
3368 }
3369 }
3370 if (status != FAIL)
3371 status = ga_grow(&end_ga, 1);
3372 cctx->ctx_lnum = save_lnum;
3373 if (status == FAIL)
3374 {
3375 ga_clear(&end_ga);
3376 return FAIL;
3377 }
3378
3379 if (jump_when != JUMP_NEVER)
3380 {
3381 if (cctx->ctx_skip != SKIP_YES)
3382 {
3383 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3384 ++end_ga.ga_len;
3385 }
3386 generate_JUMP(cctx, jump_when, 0);
3387 }
3388
3389 // eval the next expression
3390 SOURCING_LNUM = save_sourcing_lnum;
3391 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3392 {
3393 ga_clear(&end_ga);
3394 return FAIL;
3395 }
3396
3397 const_used = ppconst->pp_used;
3398 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3399 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3400 {
3401 ga_clear(&end_ga);
3402 return FAIL;
3403 }
3404
3405 // "0 || 1" results in true, "1 && 0" results in false.
3406 if (ppconst->pp_used == const_used + 1)
3407 {
3408 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3409
3410 if (tv->v_type == VAR_NUMBER
3411 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3412 {
3413 tv->vval.v_number = tv->vval.v_number == 1
3414 ? VVAL_TRUE : VVAL_FALSE;
3415 tv->v_type = VAR_BOOL;
3416 }
3417 }
3418
3419 p = may_peek_next_line(cctx, *arg, &next);
3420 }
3421
3422 if (check_ppconst_bool(ppconst) == FAIL)
3423 {
3424 ga_clear(&end_ga);
3425 return FAIL;
3426 }
3427
3428 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3429 // Every part must evaluate to a bool.
3430 if (bool_on_stack(cctx) == FAIL)
3431 {
3432 ga_clear(&end_ga);
3433 return FAIL;
3434 }
3435
3436 if (end_ga.ga_len > 0)
3437 {
3438 // Fill in the end label in all jumps.
3439 generate_ppconst(cctx, ppconst);
3440 while (end_ga.ga_len > 0)
3441 {
3442 isn_T *isn;
3443
3444 --end_ga.ga_len;
3445 isn = ((isn_T *)instr->ga_data)
3446 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3447 isn->isn_arg.jump.jump_where = instr->ga_len;
3448 }
3449 }
3450 ga_clear(&end_ga);
3451
3452 cctx->ctx_skip = save_skip;
3453 }
3454
3455 return OK;
3456}
3457
3458/*
3459 * expr4a && expr4a && expr4a logical AND
3460 *
3461 * Produces instructions:
3462 * EVAL expr4a Push result of "expr4a"
3463 * COND2BOOL convert to bool if needed
3464 * JUMP_IF_COND_FALSE end
3465 * EVAL expr4b Push result of "expr4b"
3466 * JUMP_IF_COND_FALSE end
3467 * EVAL expr4c Push result of "expr4c"
3468 * end:
3469 */
3470 static int
3471compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3472{
3473 int ppconst_used = ppconst->pp_used;
3474
3475 // get the first variable
3476 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3477 return FAIL;
3478
3479 // || and && work almost the same
3480 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3481}
3482
3483/*
3484 * expr3a || expr3b || expr3c logical OR
3485 *
3486 * Produces instructions:
3487 * EVAL expr3a Push result of "expr3a"
3488 * COND2BOOL convert to bool if needed
3489 * JUMP_IF_COND_TRUE end
3490 * EVAL expr3b Push result of "expr3b"
3491 * JUMP_IF_COND_TRUE end
3492 * EVAL expr3c Push result of "expr3c"
3493 * end:
3494 */
3495 static int
3496compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3497{
3498 int ppconst_used = ppconst->pp_used;
3499
3500 // eval the first expression
3501 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3502 return FAIL;
3503
3504 // || and && work almost the same
3505 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3506}
3507
3508/*
3509 * Toplevel expression: expr2 ? expr1a : expr1b
3510 * Produces instructions:
3511 * EVAL expr2 Push result of "expr2"
3512 * JUMP_IF_FALSE alt jump if false
3513 * EVAL expr1a
3514 * JUMP_ALWAYS end
3515 * alt: EVAL expr1b
3516 * end:
3517 *
3518 * Toplevel expression: expr2 ?? expr1
3519 * Produces instructions:
3520 * EVAL expr2 Push result of "expr2"
3521 * JUMP_AND_KEEP_IF_TRUE end jump if true
3522 * EVAL expr1
3523 * end:
3524 */
3525 int
3526compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3527{
3528 char_u *p;
3529 int ppconst_used = ppconst->pp_used;
3530 char_u *next;
3531
3532 // Ignore all kinds of errors when not producing code.
3533 if (cctx->ctx_skip == SKIP_YES)
3534 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003535 int prev_did_emsg = did_emsg;
3536
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003537 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003538 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003539 }
3540
3541 // Evaluate the first expression.
3542 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3543 return FAIL;
3544
3545 p = may_peek_next_line(cctx, *arg, &next);
3546 if (*p == '?')
3547 {
3548 int op_falsy = p[1] == '?';
3549 garray_T *instr = &cctx->ctx_instr;
3550 garray_T *stack = &cctx->ctx_type_stack;
3551 int alt_idx = instr->ga_len;
3552 int end_idx = 0;
3553 isn_T *isn;
3554 type_T *type1 = NULL;
3555 int has_const_expr = FALSE;
3556 int const_value = FALSE;
3557 int save_skip = cctx->ctx_skip;
3558
3559 if (next != NULL)
3560 {
3561 *arg = next_line_from_context(cctx, TRUE);
3562 p = skipwhite(*arg);
3563 }
3564
3565 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3566 {
3567 semsg(_(e_white_space_required_before_and_after_str_at_str),
3568 op_falsy ? "??" : "?", p);
3569 return FAIL;
3570 }
3571
3572 if (ppconst->pp_used == ppconst_used + 1)
3573 {
3574 // the condition is a constant, we know whether the ? or the :
3575 // expression is to be evaluated.
3576 has_const_expr = TRUE;
3577 if (op_falsy)
3578 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3579 else
3580 {
3581 int error = FALSE;
3582
3583 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3584 &error);
3585 if (error)
3586 return FAIL;
3587 }
3588 cctx->ctx_skip = save_skip == SKIP_YES ||
3589 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3590
3591 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3592 // "left ?? right" and "left" is truthy: produce "left"
3593 generate_ppconst(cctx, ppconst);
3594 else
3595 {
3596 clear_tv(&ppconst->pp_tv[ppconst_used]);
3597 --ppconst->pp_used;
3598 }
3599 }
3600 else
3601 {
3602 generate_ppconst(cctx, ppconst);
3603 if (op_falsy)
3604 end_idx = instr->ga_len;
3605 generate_JUMP(cctx, op_falsy
3606 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3607 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003608 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003609 }
3610
3611 // evaluate the second expression; any type is accepted
3612 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3613 return FAIL;
3614 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3615 return FAIL;
3616
3617 if (!has_const_expr)
3618 {
3619 generate_ppconst(cctx, ppconst);
3620
3621 if (!op_falsy)
3622 {
3623 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003624 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003625 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003626
3627 end_idx = instr->ga_len;
3628 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3629
3630 // jump here from JUMP_IF_FALSE
3631 isn = ((isn_T *)instr->ga_data) + alt_idx;
3632 isn->isn_arg.jump.jump_where = instr->ga_len;
3633 }
3634 }
3635
3636 if (!op_falsy)
3637 {
3638 // Check for the ":".
3639 p = may_peek_next_line(cctx, *arg, &next);
3640 if (*p != ':')
3641 {
3642 emsg(_(e_missing_colon_after_questionmark));
3643 return FAIL;
3644 }
3645 if (next != NULL)
3646 {
3647 *arg = next_line_from_context(cctx, TRUE);
3648 p = skipwhite(*arg);
3649 }
3650
3651 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3652 {
3653 semsg(_(e_white_space_required_before_and_after_str_at_str),
3654 ":", p);
3655 return FAIL;
3656 }
3657
3658 // evaluate the third expression
3659 if (has_const_expr)
3660 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3661 ? SKIP_YES : SKIP_NOT;
3662 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3663 return FAIL;
3664 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3665 return FAIL;
3666 }
3667
3668 if (!has_const_expr)
3669 {
3670 type_T **typep;
3671
3672 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003673 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003674
3675 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003676 typep = &((((type2_T *)stack->ga_data)
3677 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003678 common_type(type1, *typep, typep, cctx->ctx_type_list);
3679
3680 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3681 isn = ((isn_T *)instr->ga_data) + end_idx;
3682 isn->isn_arg.jump.jump_where = instr->ga_len;
3683 }
3684
3685 cctx->ctx_skip = save_skip;
3686 }
3687 return OK;
3688}
3689
3690/*
3691 * Toplevel expression.
3692 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3693 * Returns OK or FAIL.
3694 */
3695 int
3696compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3697{
3698 ppconst_T ppconst;
3699
3700 CLEAR_FIELD(ppconst);
3701 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3702 {
3703 clear_ppconst(&ppconst);
3704 return FAIL;
3705 }
3706 if (is_const != NULL)
3707 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3708 if (generate_ppconst(cctx, &ppconst) == FAIL)
3709 return FAIL;
3710 return OK;
3711}
3712
3713/*
3714 * Toplevel expression.
3715 */
3716 int
3717compile_expr0(char_u **arg, cctx_T *cctx)
3718{
3719 return compile_expr0_ext(arg, cctx, NULL);
3720}
3721
3722
3723#endif // defined(FEAT_EVAL)