blob: f875bc404ac19321308c082d60aa6143f46aeccd [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;
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100294 int is_super = ((type->tt_flags & TTFLAG_SUPER) == TTFLAG_SUPER);
Bram Moolenaar58b40092023-01-11 15:59:05 +0000295 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 }
Ernie Raelbce60c42025-01-19 10:03:00 +0100376
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200377 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000378 if (ufunc == NULL)
379 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200380 // could be a funcref in a member variable
381 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
382 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
383 {
384 method_not_found_msg(cl, type->tt_type, name, len);
385 return FAIL;
386 }
387 if (type->tt_type == VAR_CLASS)
388 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200389 // Remove the class type from the stack
390 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200391 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
392 return FAIL;
393 }
394 else
395 {
Yegappan Lakshmananc10342d2025-01-11 09:39:01 +0100396 int status;
397
398 if (IS_INTERFACE(cl))
399 status = generate_GET_ITF_MEMBER(cctx, cl, m_idx,
400 ocm->ocm_type);
401 else
402 status = generate_GET_OBJ_MEMBER(cctx, m_idx,
403 ocm->ocm_type);
404 if (status == FAIL)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200405 return FAIL;
406 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000407 }
408
Yegappan Lakshmanancb848b62025-01-20 21:38:09 +0100409 if (is_super && ufunc != NULL && IS_ABSTRACT_METHOD(ufunc))
Ernie Raelbce60c42025-01-19 10:03:00 +0100410 {
411 // Trying to invoke an abstract method in a super class is not
412 // allowed.
413 semsg(_(e_abstract_method_str_direct), ufunc->uf_name,
414 ufunc->uf_defclass->class_name);
415 return FAIL;
416 }
417
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200418 // A private object method can be used only inside the class where it
419 // is defined or in one of the child classes.
420 // A private class method can be used only in the class where it is
421 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200422 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200423 ((type->tt_type == VAR_OBJECT
424 && !inside_class_hierarchy(cctx, cl))
425 || (type->tt_type == VAR_CLASS
426 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200427 {
Ernie Rael03042a22023-11-11 08:53:32 +0100428 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200429 return FAIL;
430 }
431
Bram Moolenaar574950d2023-01-03 19:08:50 +0000432 // Compile the arguments and call the class function or object method.
433 // The object method will know that the object is on the stack, just
434 // before the arguments.
435 *arg = skipwhite(name_end + 1);
436 int argcount = 0;
437 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
438 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000439
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200440 if (ocm != NULL)
441 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000442 if (type->tt_type == VAR_OBJECT
443 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
Ernie Rael58c95792024-08-13 23:27:22 +0200444 return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super);
445 return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000446 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000447
448 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000449 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200450 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200451 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000452 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200453 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000454 {
Ernie Rael03042a22023-11-11 08:53:32 +0100455 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200456 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200457 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200458 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200459
460 *arg = name_end;
461 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200462 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
463 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200464 }
465
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200466 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200467 m_idx = object_method_idx(cl, name, len);
468 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000469 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200470 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100471 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200472 if (*name == '_' && !inside_class(cctx, cl))
473 {
Ernie Rael03042a22023-11-11 08:53:32 +0100474 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200475 return FAIL;
476 }
477 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200478 // Remove the object type from the stack
479 --cctx->ctx_type_stack.ga_len;
480 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000481 }
482
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200483 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000484 }
485 else
486 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000487 // load class member
488 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200489 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
490 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000491 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200492 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200493 // A private class variable can be accessed only in the class where
494 // it is defined.
495 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200496 {
Ernie Rael03042a22023-11-11 08:53:32 +0100497 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200498 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200499 return FAIL;
500 }
501
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000502 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200503 // Remove the class type from the stack
504 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000505 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
506 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200507
508 // Could be a class method reference: "class.Func".
509 m_idx = class_method_idx(cl, name, len);
510 if (m_idx >= 0)
511 {
512 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100513 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200514 if (*name == '_' && !inside_class(cctx, cl))
515 {
Ernie Rael03042a22023-11-11 08:53:32 +0100516 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200517 return FAIL;
518 }
519 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200520 // Remove the class type from the stack
521 --cctx->ctx_type_stack.ga_len;
522 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200523 }
524
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200525 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000526 }
527
528 return FAIL;
529}
530
531/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000532 * Generate an instruction to load script-local variable "name", without the
533 * leading "s:".
534 * Also finds imported variables.
535 */
536 int
537compile_load_scriptvar(
538 cctx_T *cctx,
539 char_u *name, // variable NUL terminated
540 char_u *start, // start of variable
Hirohito Higashi6c012832025-02-25 20:29:50 +0100541 char_u **end, // end of variable, may be NULL
542 imported_T *import) // found imported item, can be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543{
544 scriptitem_T *si;
545 int idx;
Hirohito Higashi6c012832025-02-25 20:29:50 +0100546 imported_T *imp;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547
548 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
549 return FAIL;
550 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000551 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000552 if (idx >= 0)
553 {
554 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
555
556 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
557 current_sctx.sc_sid, idx, sv->sv_type);
558 return OK;
559 }
560
Hirohito Higashi6c012832025-02-25 20:29:50 +0100561 if (end == NULL)
562 imp = NULL;
563 else if (import == NULL)
564 imp = find_imported(name, 0, FALSE);
565 else
566 imp = import;
567
568 if (imp != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000570 char_u *p = skipwhite(*end);
571 char_u *exp_name;
572 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100573 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000574 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000575 int done = FALSE;
576 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000577
Hirohito Higashi6c012832025-02-25 20:29:50 +0100578 check_script_symlink(imp->imp_sid);
579 import_check_sourced_sid(&imp->imp_sid);
Ernie Rael9a901792024-04-16 22:11:56 +0200580
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000581 // Need to lookup the member.
582 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000583 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000584 semsg(_(e_expected_dot_after_name_str), start);
585 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000586 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000587 ++p;
588 if (VIM_ISWHITE(*p))
589 {
590 emsg(_(e_no_white_space_allowed_after_dot));
591 return FAIL;
592 }
593
594 // isolate one name
595 exp_name = p;
596 while (eval_isnamec(*p))
597 ++p;
598 cc = *p;
599 *p = NUL;
600
Hirohito Higashi6c012832025-02-25 20:29:50 +0100601 si = SCRIPT_ITEM(imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100602 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
603 // "import autoload './dir/script.vim'" or
604 // "import autoload './autoload/script.vim'" - load script first
Hirohito Higashi6c012832025-02-25 20:29:50 +0100605 res = generate_SOURCE(cctx, imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100606
607 if (res == OK)
608 {
609 if (si->sn_autoload_prefix != NULL
610 && si->sn_state == SN_STATE_NOT_LOADED)
611 {
612 char_u *auto_name =
613 concat_str(si->sn_autoload_prefix, exp_name);
614
615 // autoload script must be loaded later, access by the autoload
616 // name. If a '(' follows it must be a function. Otherwise we
617 // don't know, it can be "script.Func".
618 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100619 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100620 else
621 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
622 vim_free(auto_name);
623 done = TRUE;
624 }
625 else if (si->sn_import_autoload
626 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100627 {
628 // If a '(' follows it must be a function. Otherwise we don't
629 // know, it can be "script.Func".
630 if (cc == '(' || paren_follows_after_expr)
631 {
632 char_u sid_name[MAX_FUNC_NAME_LEN];
633
Hirohito Higashi6c012832025-02-25 20:29:50 +0100634 func_name_with_sid(exp_name, imp->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100635 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100636 }
637 else
638 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100639 imp->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100640 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100641 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100642 else
643 {
Hirohito Higashi6c012832025-02-25 20:29:50 +0100644 idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100645 cctx, NULL, TRUE);
646 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100647 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100648
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000649 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000650 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000651 if (done)
652 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000653
654 if (idx < 0)
655 {
656 if (ufunc != NULL)
657 {
658 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100659 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000660 return OK;
661 }
662 return FAIL;
663 }
664
665 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100666 imp->imp_sid,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000667 idx,
668 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669 return OK;
670 }
671
Bram Moolenaard0132f42022-05-12 11:05:40 +0100672 // Can only get here if we know "name" is a script variable and not in a
673 // Vim9 script (variable is not in sn_var_vals): old style script.
674 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000675 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676}
677
678 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000679generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000680{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000681 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000682 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000683
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000684 // Reject a global non-autoload function found without the "g:" prefix.
685 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686 return FAIL;
687
688 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000689 compile_type = get_compile_type(ufunc);
690 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000691 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100693 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694}
695
696/*
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100697 * Returns TRUE if compiling a class method.
698 */
699 static int
700compiling_a_class_method(cctx_T *cctx)
701{
702 // For an object method, the FC_OBJECT flag will be set.
703 // For a constructor method, the FC_NEW flag will be set.
704 // Excluding these methods, the others are class methods.
705 // When compiling a closure function inside an object method,
706 // cctx->ctx_outer->ctx_func will point to the object method.
707 return cctx->ctx_ufunc != NULL
708 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0
709 && (cctx->ctx_outer == NULL
710 || cctx->ctx_outer->ctx_ufunc == NULL
711 || cctx->ctx_outer->ctx_ufunc->uf_class == NULL
712 || (cctx->ctx_outer->ctx_ufunc->uf_flags
713 & (FC_OBJECT|FC_NEW)) == 0);
714}
715
716/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717 * Compile a variable name into a load instruction.
718 * "end" points to just after the name.
719 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
720 * When "error" is FALSE do not give an error when not found.
721 */
722 int
723compile_load(
724 char_u **arg,
725 char_u *end_arg,
726 cctx_T *cctx,
727 int is_expr,
728 int error)
729{
730 type_T *type;
731 char_u *name = NULL;
732 char_u *end = end_arg;
733 int res = FAIL;
734 int prev_called_emsg = called_emsg;
735
736 if (*(*arg + 1) == ':')
737 {
738 if (end <= *arg + 2)
739 {
740 isntype_T isn_type;
741
742 // load dictionary of namespace
743 switch (**arg)
744 {
745 case 'g': isn_type = ISN_LOADGDICT; break;
746 case 'w': isn_type = ISN_LOADWDICT; break;
747 case 't': isn_type = ISN_LOADTDICT; break;
748 case 'b': isn_type = ISN_LOADBDICT; break;
749 default:
750 semsg(_(e_namespace_not_supported_str), *arg);
751 goto theend;
752 }
753 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
754 goto theend;
755 res = OK;
756 }
757 else
758 {
759 isntype_T isn_type = ISN_DROP;
760
761 // load namespaced variable
762 name = vim_strnsave(*arg + 2, end - (*arg + 2));
763 if (name == NULL)
764 return FAIL;
765
766 switch (**arg)
767 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100768 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000770 case 's': if (current_script_is_vim9())
771 {
772 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
773 *arg);
774 vim_free(name);
775 return FAIL;
776 }
Kota Kato948a3892022-08-16 16:09:59 +0100777 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000778 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 else
780 res = compile_load_scriptvar(cctx, name,
Hirohito Higashi6c012832025-02-25 20:29:50 +0100781 NULL, &end, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 break;
783 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
784 {
785 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000786 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000787 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 else
789 isn_type = ISN_LOADG;
790 }
791 else
792 {
793 isn_type = ISN_LOADAUTO;
794 vim_free(name);
795 name = vim_strnsave(*arg, end - *arg);
796 if (name == NULL)
797 return FAIL;
798 }
799 break;
800 case 'w': isn_type = ISN_LOADW; break;
801 case 't': isn_type = ISN_LOADT; break;
802 case 'b': isn_type = ISN_LOADB; break;
803 default: // cannot happen, just in case
804 semsg(_(e_namespace_not_supported_str), *arg);
805 goto theend;
806 }
807 if (isn_type != ISN_DROP)
808 {
809 // Global, Buffer-local, Window-local and Tabpage-local
810 // variables can be defined later, thus we don't check if it
811 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000812 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000813 }
814 }
815 }
816 else
817 {
818 size_t len = end - *arg;
819 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200820 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 int gen_load = FALSE;
822 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100823 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100824 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825
Hirohito Higashif7cb9f92025-02-04 16:37:19 +0100826 name = vim_strnsave(*arg, len);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 if (name == NULL)
828 return FAIL;
829
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100830 if (STRCMP(name, "super") == 0 && compiling_a_class_method(cctx))
Bram Moolenaar58b40092023-01-11 15:59:05 +0000831 {
832 // super.SomeFunc() in a class function: push &t_super type, this
833 // is recognized in compile_subscript().
834 res = push_type_stack(cctx, &t_super);
835 if (*end != '.')
836 emsg(_(e_super_must_be_followed_by_dot));
837 }
838 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000839 {
840 script_autoload(name, FALSE);
841 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
842 }
843 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
844 == OK)
845 {
846 if (gen_load_outer == 0)
847 gen_load = TRUE;
848 }
849 else
850 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000851 lvar_T lvar;
852 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853
854 if (lookup_local(*arg, len, &lvar, cctx) == OK)
855 {
856 type = lvar.lv_type;
857 idx = lvar.lv_idx;
858 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100859 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100861 outer_loop_depth = lvar.lv_loop_depth;
862 outer_loop_idx = lvar.lv_loop_idx;
863 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 else
865 gen_load = TRUE;
866 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200867 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
868 (((idx =
869 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
870 || ((method_idx =
871 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000872 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200873 // Referencing a class variable or method without the class
874 // name. A class variable or method can be referenced without
875 // the class name only in the class where the function is
876 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200877 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200878 {
879 if (idx >= 0)
880 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
881 else
882 {
883 ufunc_T *fp = cl->class_class_functions[method_idx];
884 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
885 NULL);
886 }
887 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200888 else
889 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200890 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200891 name, cl->class_name);
892 res = FAIL;
893 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000894 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 else
896 {
Hirohito Higashi6c012832025-02-25 20:29:50 +0100897 imported_T *imp = NULL;
898
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000899 // "var" can be script-local even without using "s:" if it
900 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000901 if (script_var_exists(*arg, len, cctx, NULL) == OK
Hirohito Higashi6c012832025-02-25 20:29:50 +0100902 || (imp = find_imported(name, 0, FALSE)) != NULL)
903 res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904
905 // When evaluating an expression and the name starts with an
906 // uppercase letter it can be a user defined function.
907 // generate_funcref() will fail if the function can't be found.
908 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000909 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 }
911 }
912 if (gen_load)
913 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
914 if (gen_load_outer > 0)
915 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100916 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
917 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 cctx->ctx_outer_used = TRUE;
919 }
920 }
921
922 *arg = end;
923
924theend:
925 if (res == FAIL && error && called_emsg == prev_called_emsg)
926 semsg(_(e_variable_not_found_str), name);
927 vim_free(name);
928 return res;
929}
930
931/*
932 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100933 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000934 * Returns FAIL if compilation fails.
935 */
936 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100937compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938{
LemonBoyf3b48952022-05-05 13:53:03 +0100939 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000940 garray_T save_ga = cctx->ctx_instr;
941 int expr_res;
942 int trailing_error;
943 int instr_count;
944 isn_T *instr = NULL;
945
946 // Remove the string type from the stack.
947 --cctx->ctx_type_stack.ga_len;
948
949 // Temporarily reset the list of instructions so that the jump labels are
950 // correct.
951 cctx->ctx_instr.ga_len = 0;
952 cctx->ctx_instr.ga_maxlen = 0;
953 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000954
955 // avoid peeking a next line
956 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
957 cctx->ctx_ufunc->uf_lines.ga_len = 0;
958
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000959 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000960
961 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
962
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963 s = skipwhite(s);
964 trailing_error = *s != NUL;
965
966 if (expr_res == FAIL || trailing_error
967 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
968 {
969 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000970 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971 clear_instr_ga(&cctx->ctx_instr);
972 cctx->ctx_instr = save_ga;
973 ++cctx->ctx_type_stack.ga_len;
974 return FAIL;
975 }
976
977 // Move the generated instructions into the ISN_INSTR instruction, then
978 // restore the list of instructions.
979 instr_count = cctx->ctx_instr.ga_len;
980 instr = cctx->ctx_instr.ga_data;
981 instr[instr_count].isn_type = ISN_FINISH;
982
983 cctx->ctx_instr = save_ga;
984 vim_free(isn->isn_arg.string);
985 isn->isn_type = ISN_INSTR;
986 isn->isn_arg.instr = instr;
987 return OK;
988}
989
990/*
991 * Compile the argument expressions.
992 * "arg" points to just after the "(" and is advanced to after the ")"
993 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100994 int
LemonBoyf3b48952022-05-05 13:53:03 +0100995compile_arguments(
996 char_u **arg,
997 cctx_T *cctx,
998 int *argcount,
999 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001000{
1001 char_u *p = *arg;
1002 char_u *whitep = *arg;
1003 int must_end = FALSE;
1004 int instr_count;
1005
1006 for (;;)
1007 {
1008 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1009 goto failret;
1010 if (*p == ')')
1011 {
1012 *arg = p + 1;
1013 return OK;
1014 }
1015 if (must_end)
1016 {
1017 semsg(_(e_missing_comma_before_argument_str), p);
1018 return FAIL;
1019 }
1020
1021 instr_count = cctx->ctx_instr.ga_len;
1022 if (compile_expr0(&p, cctx) == FAIL)
1023 return FAIL;
1024 ++*argcount;
1025
LemonBoyf3b48952022-05-05 13:53:03 +01001026 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001027 && cctx->ctx_instr.ga_len == instr_count + 1)
1028 {
1029 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1030
1031 // {skip} argument of searchpair() can be compiled if not empty
1032 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +01001033 compile_string(isn, cctx, 0);
1034 }
1035 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
1036 && cctx->ctx_instr.ga_len == instr_count + 1)
1037 {
1038 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1039
1040 // {sub} argument of substitute() can be compiled if it starts
1041 // with \=
1042 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001043 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001044 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001045 }
1046
1047 if (*p != ',' && *skipwhite(p) == ',')
1048 {
1049 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1050 p = skipwhite(p);
1051 }
1052 if (*p == ',')
1053 {
1054 ++p;
1055 if (*p != NUL && !VIM_ISWHITE(*p))
1056 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1057 }
1058 else
1059 must_end = TRUE;
1060 whitep = p;
1061 p = skipwhite(p);
1062 }
1063failret:
1064 emsg(_(e_missing_closing_paren));
1065 return FAIL;
1066}
1067
1068/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001069 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1070 * etc.) if the class implements it.
1071 */
1072 static int
1073compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1074{
1075 type_T *type = get_decl_type_on_stack(cctx, 0);
1076 int res = FAIL;
1077
1078 // If the built in function is invoked on an object and the class
1079 // implements the corresponding built in method, then invoke the object
1080 // method.
1081 if (type->tt_type == VAR_OBJECT)
1082 {
1083 int method_idx;
1084 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1085 &method_idx);
1086 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001087 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001088 }
1089
1090 return res;
1091}
1092
1093
1094/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001095 * Compile a function call: name(arg1, arg2)
1096 * "arg" points to "name", "arg + varlen" to the "(".
1097 * "argcount_init" is 1 for "value->method()"
1098 * Instructions:
1099 * EVAL arg1
1100 * EVAL arg2
1101 * BCALL / DCALL / UCALL
1102 */
1103 static int
1104compile_call(
1105 char_u **arg,
1106 size_t varlen,
1107 cctx_T *cctx,
1108 ppconst_T *ppconst,
1109 int argcount_init)
1110{
1111 char_u *name = *arg;
1112 char_u *p;
1113 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001114 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 char_u fname_buf[FLEN_FIXED + 1];
1116 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 ufunc_T *ufunc = NULL;
1118 int res = FAIL;
1119 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001120 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001121 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001122 imported_T *import;
1123
1124 if (varlen >= sizeof(namebuf))
1125 {
1126 semsg(_(e_name_too_long_str), name);
1127 return FAIL;
1128 }
1129 vim_strncpy(namebuf, *arg, varlen);
1130
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001131 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001132 if (import != NULL)
1133 {
1134 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1135 return FAIL;
1136 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001137
1138 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001139 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001141 if ((varlen == 3
1142 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1144 {
1145 char_u *s = skipwhite(*arg + varlen + 1);
1146 typval_T argvars[2];
1147 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001148 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149
1150 argvars[0].v_type = VAR_UNKNOWN;
1151 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001152 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001154 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001155 s = skipwhite(s);
1156 if (*s == ')' && argvars[0].v_type == VAR_STRING
1157 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1158 || !is_has))
1159 {
1160 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1161
1162 *arg = s + 1;
1163 argvars[1].v_type = VAR_UNKNOWN;
1164 tv->v_type = VAR_NUMBER;
1165 tv->vval.v_number = 0;
1166 if (is_has)
1167 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001168 else if (is_len)
1169 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170 else
1171 f_exists(argvars, tv);
1172 clear_tv(&argvars[0]);
1173 ++ppconst->pp_used;
1174 return OK;
1175 }
1176 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001177 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 {
1179 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1180 return FAIL;
1181 }
1182 }
1183
1184 if (generate_ppconst(cctx, ppconst) == FAIL)
1185 return FAIL;
1186
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001187 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1189
1190 // We handle the "skip" argument of searchpair() and searchpairpos()
1191 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001192 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1193 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1194 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1195 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1196 special_fn = CA_SEARCHPAIR;
1197 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1198 special_fn = CA_SUBSTITUTE;
1199 else
1200 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201
1202 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001203 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204 goto theend;
1205
1206 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1207 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1208 {
1209 int idx;
1210
1211 // builtin function
1212 idx = find_internal_func(name);
1213 if (idx >= 0)
1214 {
1215 if (STRCMP(name, "flatten") == 0)
1216 {
1217 emsg(_(e_cannot_use_flatten_in_vim9_script));
1218 goto theend;
1219 }
1220
1221 if (STRCMP(name, "add") == 0 && argcount == 2)
1222 {
h-eastb895b0f2023-09-24 15:46:31 +02001223 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001224 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1225 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001226
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001227 // add() can be compiled to instructions if we know the type
1228 if (type->tt_type == VAR_LIST)
1229 {
1230 // inline "add(list, item)" so that the type can be checked
1231 res = generate_LISTAPPEND(cctx);
1232 idx = -1;
1233 }
1234 else if (type->tt_type == VAR_BLOB)
1235 {
1236 // inline "add(blob, nr)" so that the type can be checked
1237 res = generate_BLOBAPPEND(cctx);
1238 idx = -1;
1239 }
1240 }
1241
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001242 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1243 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001244 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001245 // May have the "D" or "R" flag, reserve a variable for a
1246 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001247 if (get_defer_var_idx(cctx) == 0)
1248 idx = -1;
1249 }
1250
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001251 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1252 if (STRCMP(name, "string") == 0)
1253 builtin_method = CLASS_BUILTIN_STRING;
1254 else if (STRCMP(name, "empty") == 0)
1255 builtin_method = CLASS_BUILTIN_EMPTY;
1256 else if (STRCMP(name, "len") == 0)
1257 builtin_method = CLASS_BUILTIN_LEN;
1258 if (builtin_method != CLASS_BUILTIN_INVALID)
1259 {
1260 res = compile_builtin_method_call(cctx, builtin_method);
1261 if (res == OK)
1262 idx = -1;
1263 }
1264
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001265 if (idx >= 0)
1266 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1267 }
1268 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001269 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 goto theend;
1271 }
1272
Bram Moolenaar62aec932022-01-29 21:45:34 +00001273 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1274
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001275 // An argument or local variable can be a function reference, this
1276 // overrules a function name.
1277 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1278 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1279 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001280 class_T *cl = NULL;
1281 int mi = 0;
1282
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001283 // If we can find the function by name generate the right call.
1284 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001285 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001286 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001288 if (!func_is_global(ufunc))
1289 {
Ernie Rael58c95792024-08-13 23:27:22 +02001290 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001291 goto theend;
1292 }
1293 if (!has_g_namespace
1294 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1295 {
1296 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001297 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001298 goto theend;
1299 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001301 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001302 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001303 // Class method invocation without the class name.
1304 // A class method can be referenced without the class name only in
1305 // the class where the function is defined.
1306 if (cctx->ctx_ufunc->uf_defclass == cl)
1307 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001308 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001309 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001310 }
1311 else
1312 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001313 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001314 name, cl->class_name);
1315 res = FAIL;
1316 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001317 goto theend;
1318 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 }
1320
1321 // If the name is a variable, load it and use PCALL.
1322 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001323 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001325 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1327 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001328 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001329
Christian Brabandtd5475e82023-08-17 23:34:09 +02001330 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331 goto theend;
1332 }
1333
1334 // If we can find a global function by name generate the right call.
1335 if (ufunc != NULL)
1336 {
Ernie Rael58c95792024-08-13 23:27:22 +02001337 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338 goto theend;
1339 }
1340
1341 // A global function may be defined only later. Need to figure out at
1342 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001343 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344 res = generate_UCALL(cctx, name, argcount);
1345 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001346 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347
1348theend:
1349 vim_free(tofree);
1350 return res;
1351}
1352
1353// like NAMESPACE_CHAR but with 'a' and 'l'.
1354#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1355
1356/*
1357 * Find the end of a variable or function name. Unlike find_name_end() this
1358 * does not recognize magic braces.
1359 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1360 * Return a pointer to just after the name. Equal to "arg" if there is no
1361 * valid name.
1362 */
1363 char_u *
1364to_name_end(char_u *arg, int use_namespace)
1365{
1366 char_u *p;
1367
1368 // Quick check for valid starting character.
1369 if (!eval_isnamec1(*arg))
1370 return arg;
1371
1372 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1373 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1374 // and can be used in slice "[n:]".
1375 if (*p == ':' && (p != arg + 1
1376 || !use_namespace
1377 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1378 break;
1379 return p;
1380}
1381
1382/*
1383 * Like to_name_end() but also skip over a list or dict constant.
1384 * Also accept "<SNR>123_Func".
1385 * This intentionally does not handle line continuation.
1386 */
1387 char_u *
1388to_name_const_end(char_u *arg)
1389{
1390 char_u *p = arg;
1391 typval_T rettv;
1392
1393 if (STRNCMP(p, "<SNR>", 5) == 0)
1394 p = skipdigits(p + 5);
1395 p = to_name_end(p, TRUE);
1396 if (p == arg && *arg == '[')
1397 {
1398
1399 // Can be "[1, 2, 3]->Func()".
1400 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1401 p = arg;
1402 }
1403 return p;
1404}
1405
1406/*
1407 * parse a list: [expr, expr]
1408 * "*arg" points to the '['.
1409 * ppconst->pp_is_const is set if all items are a constant.
1410 */
1411 static int
1412compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1413{
1414 char_u *p = skipwhite(*arg + 1);
1415 char_u *whitep = *arg + 1;
1416 int count = 0;
1417 int is_const;
1418 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001419 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420
1421 for (;;)
1422 {
1423 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1424 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001425 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426 return FAIL;
1427 }
1428 if (*p == ',')
1429 {
1430 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1431 return FAIL;
1432 }
1433 if (*p == ']')
1434 {
1435 ++p;
1436 break;
1437 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001438 if (must_end)
1439 {
1440 semsg(_(e_missing_comma_in_list_str), p);
1441 return FAIL;
1442 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001443 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1444 return FAIL;
1445 if (!is_const)
1446 is_all_const = FALSE;
1447 ++count;
1448 if (*p == ',')
1449 {
1450 ++p;
1451 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1452 {
1453 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1454 return FAIL;
1455 }
1456 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001457 else
1458 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459 whitep = p;
1460 p = skipwhite(p);
1461 }
1462 *arg = p;
1463
1464 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001465 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001466}
1467
1468/*
1469 * Parse a lambda: "(arg, arg) => expr"
1470 * "*arg" points to the '('.
1471 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1472 */
1473 static int
1474compile_lambda(char_u **arg, cctx_T *cctx)
1475{
1476 int r;
1477 typval_T rettv;
1478 ufunc_T *ufunc;
1479 evalarg_T evalarg;
1480
1481 init_evalarg(&evalarg);
1482 evalarg.eval_flags = EVAL_EVALUATE;
1483 evalarg.eval_cctx = cctx;
1484
1485 // Get the funcref in "rettv".
1486 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1487 if (r != OK)
1488 {
1489 clear_evalarg(&evalarg, NULL);
1490 return r;
1491 }
1492
1493 // "rettv" will now be a partial referencing the function.
1494 ufunc = rettv.vval.v_partial->pt_func;
1495 ++ufunc->uf_refcount;
1496 clear_tv(&rettv);
1497
1498 // Compile it here to get the return type. The return type is optional,
1499 // when it's missing use t_unknown. This is recognized in
1500 // compile_return().
1501 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1502 ufunc->uf_ret_type = &t_unknown;
1503 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1504
1505 // When the outer function is compiled for profiling or debugging, the
1506 // lambda may be called without profiling or debugging. Compile it here in
1507 // the right context.
1508 if (cctx->ctx_compile_type == CT_DEBUG
1509#ifdef FEAT_PROFILE
1510 || cctx->ctx_compile_type == CT_PROFILE
1511#endif
1512 )
1513 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1514
Bram Moolenaar139575d2022-03-15 19:29:30 +00001515 // if the outer function is not compiled for debugging or profiling, this
1516 // one might be
1517 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001518 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001519 compiletype_T compile_type = get_compile_type(ufunc);
1520
1521 if (compile_type != CT_NONE)
1522 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001523 }
1524
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001525 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1526 // "*arg" may point into it. Point into the original line to avoid a
1527 // dangling pointer.
1528 if (evalarg.eval_using_cmdline)
1529 {
1530 garray_T *gap = &evalarg.eval_tofree_ga;
1531 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1532
1533 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1534 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001535 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536 }
1537
1538 clear_evalarg(&evalarg, NULL);
1539
1540 if (ufunc->uf_def_status == UF_COMPILED)
1541 {
1542 // The return type will now be known.
1543 set_function_type(ufunc);
1544
1545 // The function reference count will be 1. When the ISN_FUNCREF
1546 // instruction is deleted the reference count is decremented and the
1547 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001548 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001549 }
1550
1551 func_ptr_unref(ufunc);
1552 return FAIL;
1553}
1554
1555/*
1556 * Get a lambda and compile it. Uses Vim9 syntax.
1557 */
1558 int
1559get_lambda_tv_and_compile(
1560 char_u **arg,
1561 typval_T *rettv,
1562 int types_optional,
1563 evalarg_T *evalarg)
1564{
1565 int r;
1566 ufunc_T *ufunc;
1567 int save_sc_version = current_sctx.sc_version;
1568
1569 // Get the funcref in "rettv".
1570 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1571 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1572 current_sctx.sc_version = save_sc_version;
1573 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001574 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001575
1576 // "rettv" will now be a partial referencing the function.
1577 ufunc = rettv->vval.v_partial->pt_func;
1578
1579 // Compile it here to get the return type. The return type is optional,
1580 // when it's missing use t_unknown. This is recognized in
1581 // compile_return().
1582 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1583 ufunc->uf_ret_type = &t_unknown;
1584 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1585
1586 if (ufunc->uf_def_status == UF_COMPILED)
1587 {
1588 // The return type will now be known.
1589 set_function_type(ufunc);
1590 return OK;
1591 }
1592 clear_tv(rettv);
1593 return FAIL;
1594}
1595
1596/*
1597 * parse a dict: {key: val, [key]: val}
1598 * "*arg" points to the '{'.
1599 * ppconst->pp_is_const is set if all item values are a constant.
1600 */
1601 static int
1602compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1603{
1604 garray_T *instr = &cctx->ctx_instr;
1605 int count = 0;
1606 dict_T *d = dict_alloc();
1607 dictitem_T *item;
1608 char_u *whitep = *arg + 1;
1609 char_u *p;
1610 int is_const;
1611 int is_all_const = TRUE; // reset when non-const encountered
1612
1613 if (d == NULL)
1614 return FAIL;
1615 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001616 {
1617 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001618 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001619 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001620 for (;;)
1621 {
1622 char_u *key = NULL;
1623
1624 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1625 {
1626 *arg = NULL;
1627 goto failret;
1628 }
1629
1630 if (**arg == '}')
1631 break;
1632
1633 if (**arg == '[')
1634 {
1635 isn_T *isn;
1636
1637 // {[expr]: value} uses an evaluated key.
1638 *arg = skipwhite(*arg + 1);
1639 if (compile_expr0(arg, cctx) == FAIL)
1640 return FAIL;
1641 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1642 if (isn->isn_type == ISN_PUSHNR)
1643 {
1644 char buf[NUMBUFLEN];
1645
1646 // Convert to string at compile time.
1647 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1648 isn->isn_type = ISN_PUSHS;
1649 isn->isn_arg.string = vim_strsave((char_u *)buf);
1650 }
1651 if (isn->isn_type == ISN_PUSHS)
1652 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001653 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654 return FAIL;
1655 *arg = skipwhite(*arg);
1656 if (**arg != ']')
1657 {
1658 emsg(_(e_missing_matching_bracket_after_dict_key));
1659 return FAIL;
1660 }
1661 ++*arg;
1662 }
1663 else
1664 {
1665 // {"name": value},
1666 // {'name': value},
1667 // {name: value} use "name" as a literal key
1668 key = get_literal_key(arg);
1669 if (key == NULL)
1670 return FAIL;
1671 if (generate_PUSHS(cctx, &key) == FAIL)
1672 return FAIL;
1673 }
1674
1675 // Check for duplicate keys, if using string keys.
1676 if (key != NULL)
1677 {
1678 item = dict_find(d, key, -1);
1679 if (item != NULL)
1680 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001681 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001682 goto failret;
1683 }
1684 item = dictitem_alloc(key);
1685 if (item != NULL)
1686 {
1687 item->di_tv.v_type = VAR_UNKNOWN;
1688 item->di_tv.v_lock = 0;
1689 if (dict_add(d, item) == FAIL)
1690 dictitem_free(item);
1691 }
1692 }
1693
1694 if (**arg != ':')
1695 {
1696 if (*skipwhite(*arg) == ':')
1697 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1698 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001699 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001700 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
1709 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1710 {
1711 *arg = NULL;
1712 goto failret;
1713 }
1714
1715 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1716 return FAIL;
1717 if (!is_const)
1718 is_all_const = FALSE;
1719 ++count;
1720
1721 whitep = *arg;
1722 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1723 {
1724 *arg = NULL;
1725 goto failret;
1726 }
1727 if (**arg == '}')
1728 break;
1729 if (**arg != ',')
1730 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001731 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 goto failret;
1733 }
1734 if (IS_WHITE_OR_NUL(*whitep))
1735 {
1736 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1737 return FAIL;
1738 }
1739 whitep = *arg + 1;
1740 if (!IS_WHITE_OR_NUL(*whitep))
1741 {
1742 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1743 return FAIL;
1744 }
1745 *arg = skipwhite(whitep);
1746 }
1747
1748 *arg = *arg + 1;
1749
1750 // Allow for following comment, after at least one space.
1751 p = skipwhite(*arg);
1752 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1753 *arg += STRLEN(*arg);
1754
1755 dict_unref(d);
1756 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001757 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758
1759failret:
1760 if (*arg == NULL)
1761 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001762 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001763 *arg = (char_u *)"";
1764 }
1765 dict_unref(d);
1766 return FAIL;
1767}
1768
1769/*
1770 * Compile "&option".
1771 */
1772 static int
1773compile_get_option(char_u **arg, cctx_T *cctx)
1774{
1775 typval_T rettv;
1776 char_u *start = *arg;
1777 int ret;
1778
1779 // parse the option and get the current value to get the type.
1780 rettv.v_type = VAR_UNKNOWN;
1781 ret = eval_option(arg, &rettv, TRUE);
1782 if (ret == OK)
1783 {
1784 // include the '&' in the name, eval_option() expects it.
1785 char_u *name = vim_strnsave(start, *arg - start);
1786 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1787 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1788
1789 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1790 vim_free(name);
1791 }
1792 clear_tv(&rettv);
1793
1794 return ret;
1795}
1796
1797/*
1798 * Compile "$VAR".
1799 */
1800 static int
1801compile_get_env(char_u **arg, cctx_T *cctx)
1802{
1803 char_u *start = *arg;
1804 int len;
1805 int ret;
1806 char_u *name;
1807
1808 ++*arg;
1809 len = get_env_len(arg);
1810 if (len == 0)
1811 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001812 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813 return FAIL;
1814 }
1815
1816 // include the '$' in the name, eval_env_var() expects it.
1817 name = vim_strnsave(start, len + 1);
1818 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1819 vim_free(name);
1820 return ret;
1821}
1822
1823/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001824 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001825 */
1826 static int
1827compile_interp_string(char_u **arg, cctx_T *cctx)
1828{
1829 typval_T tv;
1830 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001831 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001832 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001833 int count = 0;
1834 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001835
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001836 // *arg is on the '$' character, move it to the first string character.
1837 ++*arg;
1838 quote = **arg;
1839 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001840
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001841 for (;;)
1842 {
1843 // Get the string up to the matching quote or to a single '{'.
1844 // "arg" is advanced to either the quote or the '{'.
1845 if (quote == '"')
1846 ret = eval_string(arg, &tv, evaluate, TRUE);
1847 else
1848 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1849 if (ret == FAIL)
1850 break;
1851 if (evaluate)
1852 {
1853 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1854 || (**arg != '{' && count == 0))
1855 {
1856 // generate non-empty string or empty string if it's the only
1857 // one
1858 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1859 return FAIL;
1860 tv.vval.v_string = NULL; // don't free it now
1861 ++count;
1862 }
1863 clear_tv(&tv);
1864 }
1865
1866 if (**arg != '{')
1867 {
1868 // found terminating quote
1869 ++*arg;
1870 break;
1871 }
1872
1873 p = compile_one_expr_in_str(*arg, cctx);
1874 if (p == NULL)
1875 {
1876 ret = FAIL;
1877 break;
1878 }
1879 ++count;
1880 *arg = p;
1881 }
LemonBoy2eaef102022-05-06 13:14:50 +01001882
1883 if (ret == FAIL || !evaluate)
1884 return ret;
1885
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001886 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1887 if (count > 1)
1888 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001889
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001890 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001891}
1892
1893/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001894 * Compile "@r".
1895 */
1896 static int
1897compile_get_register(char_u **arg, cctx_T *cctx)
1898{
1899 int ret;
1900
1901 ++*arg;
1902 if (**arg == NUL)
1903 {
1904 semsg(_(e_syntax_error_at_str), *arg - 1);
1905 return FAIL;
1906 }
1907 if (!valid_yank_reg(**arg, FALSE))
1908 {
1909 emsg_invreg(**arg);
1910 return FAIL;
1911 }
1912 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1913 ++*arg;
1914 return ret;
1915}
1916
1917/*
1918 * Apply leading '!', '-' and '+' to constant "rettv".
1919 * When "numeric_only" is TRUE do not apply '!'.
1920 */
1921 static int
1922apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1923{
1924 char_u *p = *end;
1925
1926 // this works from end to start
1927 while (p > start)
1928 {
1929 --p;
1930 if (*p == '-' || *p == '+')
1931 {
1932 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001933 if (rettv->v_type == VAR_FLOAT)
1934 {
1935 if (*p == '-')
1936 rettv->vval.v_float = -rettv->vval.v_float;
1937 }
1938 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001939 {
1940 varnumber_T val;
1941 int error = FALSE;
1942
1943 // tv_get_number_chk() accepts a string, but we don't want that
1944 // here
1945 if (check_not_string(rettv) == FAIL)
1946 return FAIL;
1947 val = tv_get_number_chk(rettv, &error);
1948 clear_tv(rettv);
1949 if (error)
1950 return FAIL;
1951 if (*p == '-')
1952 val = -val;
1953 rettv->v_type = VAR_NUMBER;
1954 rettv->vval.v_number = val;
1955 }
1956 }
1957 else if (numeric_only)
1958 {
1959 ++p;
1960 break;
1961 }
1962 else if (*p == '!')
1963 {
1964 int v = tv2bool(rettv);
1965
1966 // '!' is permissive in the type.
1967 clear_tv(rettv);
1968 rettv->v_type = VAR_BOOL;
1969 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1970 }
1971 }
1972 *end = p;
1973 return OK;
1974}
1975
1976/*
1977 * Recognize v: variables that are constants and set "rettv".
1978 */
1979 static void
1980get_vim_constant(char_u **arg, typval_T *rettv)
1981{
1982 if (STRNCMP(*arg, "v:true", 6) == 0)
1983 {
1984 rettv->v_type = VAR_BOOL;
1985 rettv->vval.v_number = VVAL_TRUE;
1986 *arg += 6;
1987 }
1988 else if (STRNCMP(*arg, "v:false", 7) == 0)
1989 {
1990 rettv->v_type = VAR_BOOL;
1991 rettv->vval.v_number = VVAL_FALSE;
1992 *arg += 7;
1993 }
1994 else if (STRNCMP(*arg, "v:null", 6) == 0)
1995 {
1996 rettv->v_type = VAR_SPECIAL;
1997 rettv->vval.v_number = VVAL_NULL;
1998 *arg += 6;
1999 }
2000 else if (STRNCMP(*arg, "v:none", 6) == 0)
2001 {
2002 rettv->v_type = VAR_SPECIAL;
2003 rettv->vval.v_number = VVAL_NONE;
2004 *arg += 6;
2005 }
2006}
2007
2008 exprtype_T
2009get_compare_type(char_u *p, int *len, int *type_is)
2010{
2011 exprtype_T type = EXPR_UNKNOWN;
2012 int i;
2013
2014 switch (p[0])
2015 {
2016 case '=': if (p[1] == '=')
2017 type = EXPR_EQUAL;
2018 else if (p[1] == '~')
2019 type = EXPR_MATCH;
2020 break;
2021 case '!': if (p[1] == '=')
2022 type = EXPR_NEQUAL;
2023 else if (p[1] == '~')
2024 type = EXPR_NOMATCH;
2025 break;
2026 case '>': if (p[1] != '=')
2027 {
2028 type = EXPR_GREATER;
2029 *len = 1;
2030 }
2031 else
2032 type = EXPR_GEQUAL;
2033 break;
2034 case '<': if (p[1] != '=')
2035 {
2036 type = EXPR_SMALLER;
2037 *len = 1;
2038 }
2039 else
2040 type = EXPR_SEQUAL;
2041 break;
2042 case 'i': if (p[1] == 's')
2043 {
2044 // "is" and "isnot"; but not a prefix of a name
2045 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2046 *len = 5;
2047 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002048 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002049 {
2050 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2051 *type_is = TRUE;
2052 }
2053 }
2054 break;
2055 }
2056 return type;
2057}
2058
2059/*
2060 * Skip over an expression, ignoring most errors.
2061 */
2062 void
2063skip_expr_cctx(char_u **arg, cctx_T *cctx)
2064{
2065 evalarg_T evalarg;
2066
2067 init_evalarg(&evalarg);
2068 evalarg.eval_cctx = cctx;
2069 skip_expr(arg, &evalarg);
2070 clear_evalarg(&evalarg, NULL);
2071}
2072
2073/*
2074 * Check that the top of the type stack has a type that can be used as a
2075 * condition. Give an error and return FAIL if not.
2076 */
2077 int
2078bool_on_stack(cctx_T *cctx)
2079{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002080 type_T *type;
2081
Bram Moolenaar078a4612022-01-04 15:17:03 +00002082 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002083 if (type == &t_bool)
2084 return OK;
2085
Bram Moolenaar4913d422022-10-17 13:13:32 +01002086 if (type->tt_type == VAR_ANY
2087 || type->tt_type == VAR_UNKNOWN
2088 || type->tt_type == VAR_NUMBER
2089 || type == &t_number_bool
2090 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2092 // This requires a runtime type check.
2093 return generate_COND2BOOL(cctx);
2094
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002095 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096}
2097
2098/*
2099 * Give the "white on both sides" error, taking the operator from "p[len]".
2100 */
2101 void
2102error_white_both(char_u *op, int len)
2103{
2104 char_u buf[10];
2105
2106 vim_strncpy(buf, op, len);
2107 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2108}
2109
2110/*
2111 * Compile code to apply '-', '+' and '!'.
2112 * When "numeric_only" is TRUE do not apply '!'.
2113 */
2114 static int
2115compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2116{
2117 char_u *p = *end;
2118
2119 // this works from end to start
2120 while (p > start)
2121 {
2122 --p;
2123 while (VIM_ISWHITE(*p))
2124 --p;
2125 if (*p == '-' || *p == '+')
2126 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002127 type_T *type = get_type_on_stack(cctx, 0);
2128 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002129 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002130 return FAIL;
2131
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002133 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2134 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002135 }
2136 else if (numeric_only)
2137 {
2138 ++p;
2139 break;
2140 }
2141 else
2142 {
2143 int invert = *p == '!';
2144
2145 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2146 {
2147 if (p[-1] == '!')
2148 invert = !invert;
2149 --p;
2150 }
2151 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2152 return FAIL;
2153 }
2154 }
2155 *end = p;
2156 return OK;
2157}
2158
2159/*
2160 * Compile "(expression)": recursive!
2161 * Return FAIL/OK.
2162 */
2163 static int
2164compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2165{
2166 int ret;
2167 char_u *p = *arg + 1;
2168
2169 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2170 return FAIL;
2171 if (ppconst->pp_used <= PPSIZE - 10)
2172 {
2173 ret = compile_expr1(arg, cctx, ppconst);
2174 }
2175 else
2176 {
2177 // Not enough space in ppconst, flush constants.
2178 if (generate_ppconst(cctx, ppconst) == FAIL)
2179 return FAIL;
2180 ret = compile_expr0(arg, cctx);
2181 }
2182 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2183 return FAIL;
2184 if (**arg == ')')
2185 ++*arg;
2186 else if (ret == OK)
2187 {
2188 emsg(_(e_missing_closing_paren));
2189 ret = FAIL;
2190 }
2191 return ret;
2192}
2193
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002194static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002195
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002196/*
2197 * Compile whatever comes after "name" or "name()".
2198 * Advances "*arg" only when something was recognized.
2199 */
2200 static int
2201compile_subscript(
2202 char_u **arg,
2203 cctx_T *cctx,
2204 char_u *start_leader,
2205 char_u **end_leader,
2206 ppconst_T *ppconst)
2207{
2208 char_u *name_start = *end_leader;
2209 int keeping_dict = FALSE;
2210
2211 for (;;)
2212 {
2213 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002214 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002215
2216 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2217 {
2218 char_u *next = peek_next_line_from_context(cctx);
2219
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002220 // If a following line starts with "->{", "->(" or "->X" advance to
2221 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002222 // Also if a following line starts with ".x".
2223 if (next != NULL &&
2224 ((next[0] == '-' && next[1] == '>'
2225 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002226 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002227 || ASCII_ISALPHA(*skipwhite(next + 2))))
2228 || (next[0] == '.' && eval_isdictc(next[1]))))
2229 {
2230 next = next_line_from_context(cctx, TRUE);
2231 if (next == NULL)
2232 return FAIL;
2233 *arg = next;
2234 p = skipwhite(*arg);
2235 }
2236 }
2237
2238 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2239 // is not a function call.
2240 if (**arg == '(')
2241 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002242 int argcount = 0;
2243
2244 if (generate_ppconst(cctx, ppconst) == FAIL)
2245 return FAIL;
2246 ppconst->pp_is_const = FALSE;
2247
2248 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002249 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002250
2251 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002252 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002253 return FAIL;
2254 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2255 return FAIL;
2256 if (keeping_dict)
2257 {
2258 keeping_dict = FALSE;
2259 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2260 return FAIL;
2261 }
2262 }
2263 else if (*p == '-' && p[1] == '>')
2264 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002265 char_u *pstart = p;
2266 int alt;
2267 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002268
Bram Moolenaarc7349932022-01-16 20:59:39 +00002269 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270 if (generate_ppconst(cctx, ppconst) == FAIL)
2271 return FAIL;
2272 ppconst->pp_is_const = FALSE;
2273
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274 // Apply the '!', '-' and '+' first:
2275 // -1.0->func() works like (-1.0)->func()
2276 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2277 return FAIL;
2278
2279 p += 2;
2280 *arg = skipwhite(p);
2281 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002282
2283 // Three alternatives handled here:
2284 // 1. "base->name(" only a name, use compile_call()
2285 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2286 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002288 alt = 2;
2289 else
2290 {
2291 // alternative 1 or 3
2292 p = *arg;
2293 if (!eval_isnamec1(*p))
2294 {
2295 semsg(_(e_trailing_characters_str), pstart);
2296 return FAIL;
2297 }
2298 if (ASCII_ISALPHA(*p) && p[1] == ':')
2299 p += 2;
2300 for ( ; eval_isnamec(*p); ++p)
2301 ;
2302 if (*p == '(')
2303 {
2304 // alternative 1
2305 alt = 1;
2306 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2307 return FAIL;
2308 }
2309 else
2310 {
2311 // Must be alternative 3, find the "(". Only works within
2312 // one line.
2313 alt = 3;
2314 paren = vim_strchr(p, '(');
2315 if (paren == NULL)
2316 {
2317 semsg(_(e_missing_parenthesis_str), *arg);
2318 return FAIL;
2319 }
2320 }
2321 }
2322
2323 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002324 {
2325 int argcount = 1;
2326 garray_T *stack = &cctx->ctx_type_stack;
2327 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 int expr_isn_start = cctx->ctx_instr.ga_len;
2329 int expr_isn_end;
2330 int arg_isn_count;
2331
Bram Moolenaarc7349932022-01-16 20:59:39 +00002332 if (alt == 2)
2333 {
2334 // Funcref call: list->(Refs[2])(arg)
2335 // or lambda: list->((arg) => expr)(arg)
2336 //
2337 // Fist compile the function expression.
2338 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2339 return FAIL;
2340 }
2341 else
2342 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002343 int fail;
2344 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002345 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002346
Bram Moolenaarc7349932022-01-16 20:59:39 +00002347 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002348
2349 // instead of using LOADG for "import.Func" use PUSHFUNC
2350 ++paren_follows_after_expr;
2351
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002352 // do not look in the next line
2353 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002354
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002355 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002356 || *skipwhite(*arg) != NUL;
2357 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002358 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002359 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002360
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002361 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002362 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002363 if (did_emsg == prev_did_emsg)
2364 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002365 return FAIL;
2366 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002367 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002368
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 // Compile the arguments.
2370 if (**arg != '(')
2371 {
2372 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002373 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002374 else
2375 semsg(_(e_missing_parenthesis_str), *arg);
2376 return FAIL;
2377 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002378
2379 // Remember the next instruction index, where the instructions
2380 // for arguments are being written.
2381 expr_isn_end = cctx->ctx_instr.ga_len;
2382
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002384 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2385 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002386 return FAIL;
2387
2388 // Move the instructions for the arguments to before the
2389 // instructions of the expression and move the type of the
2390 // expression after the argument types. This is what ISN_PCALL
2391 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002392 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2393 if (arg_isn_count > 0)
2394 {
2395 int expr_isn_count = expr_isn_end - expr_isn_start;
2396 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002397 type_T *decl_type;
2398 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002399
2400 if (isn == NULL)
2401 return FAIL;
2402 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2403 + expr_isn_start,
2404 sizeof(isn_T) * expr_isn_count);
2405 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2406 + expr_isn_start,
2407 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2408 sizeof(isn_T) * arg_isn_count);
2409 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2410 + expr_isn_start + arg_isn_count,
2411 isn, sizeof(isn_T) * expr_isn_count);
2412 vim_free(isn);
2413
Bram Moolenaar078a4612022-01-04 15:17:03 +00002414 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2415 type = typep->type_curr;
2416 decl_type = typep->type_decl;
2417 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2418 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2419 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002421 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2422 typep->type_curr = type;
2423 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424 }
2425
Bram Moolenaar078a4612022-01-04 15:17:03 +00002426 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002427 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2428 return FAIL;
2429 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002430
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431 if (keeping_dict)
2432 {
2433 keeping_dict = FALSE;
2434 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2435 return FAIL;
2436 }
2437 }
2438 else if (**arg == '[')
2439 {
2440 int is_slice = FALSE;
2441
2442 // list index: list[123]
2443 // dict member: dict[key]
2444 // string index: text[123]
2445 // blob index: blob[123]
2446 if (generate_ppconst(cctx, ppconst) == FAIL)
2447 return FAIL;
2448 ppconst->pp_is_const = FALSE;
2449
2450 ++p;
2451 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2452 return FAIL;
2453 if (**arg == ':')
2454 {
2455 // missing first index is equal to zero
2456 generate_PUSHNR(cctx, 0);
2457 }
2458 else
2459 {
2460 if (compile_expr0(arg, cctx) == FAIL)
2461 return FAIL;
2462 if (**arg == ':')
2463 {
2464 semsg(_(e_white_space_required_before_and_after_str_at_str),
2465 ":", *arg);
2466 return FAIL;
2467 }
2468 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2469 return FAIL;
2470 *arg = skipwhite(*arg);
2471 }
2472 if (**arg == ':')
2473 {
2474 is_slice = TRUE;
2475 ++*arg;
2476 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2477 {
2478 semsg(_(e_white_space_required_before_and_after_str_at_str),
2479 ":", *arg);
2480 return FAIL;
2481 }
2482 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2483 return FAIL;
2484 if (**arg == ']')
2485 // missing second index is equal to end of string
2486 generate_PUSHNR(cctx, -1);
2487 else
2488 {
2489 if (compile_expr0(arg, cctx) == FAIL)
2490 return FAIL;
2491 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2492 return FAIL;
2493 *arg = skipwhite(*arg);
2494 }
2495 }
2496
2497 if (**arg != ']')
2498 {
2499 emsg(_(e_missing_closing_square_brace));
2500 return FAIL;
2501 }
2502 *arg = *arg + 1;
2503
2504 if (keeping_dict)
2505 {
2506 keeping_dict = FALSE;
2507 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2508 return FAIL;
2509 }
2510 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2511 return FAIL;
2512 }
2513 else if (*p == '.' && p[1] != '.')
2514 {
2515 // dictionary member: dict.name
2516 if (generate_ppconst(cctx, ppconst) == FAIL)
2517 return FAIL;
2518 ppconst->pp_is_const = FALSE;
2519
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002520 type = get_type_on_stack(cctx, 0);
2521 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002522 && (type->tt_type == VAR_CLASS
2523 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002525 // class member: SomeClass.varname
2526 // class method: SomeClass.SomeMethod()
2527 // class constructor: SomeClass.new()
2528 // object member: someObject.varname, this.varname
2529 // object method: someObject.SomeMethod(), this.SomeMethod()
2530 *arg = p;
2531 if (compile_class_object_index(cctx, arg, type) == FAIL)
2532 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002533 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002534 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002536 *arg = p + 1;
2537 if (IS_WHITE_OR_NUL(**arg))
2538 {
2539 emsg(_(e_missing_name_after_dot));
2540 return FAIL;
2541 }
2542 p = *arg;
2543 if (eval_isdictc(*p))
2544 while (eval_isnamec(*p))
2545 MB_PTR_ADV(p);
2546 if (p == *arg)
2547 {
2548 semsg(_(e_syntax_error_at_str), *arg);
2549 return FAIL;
2550 }
2551 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2552 return FAIL;
2553 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2554 return FAIL;
2555 keeping_dict = TRUE;
2556 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002557 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002558 }
2559 else
2560 break;
2561 }
2562
2563 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2564 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002565 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2566 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002567 return FAIL;
2568
2569 return OK;
2570}
2571
2572/*
2573 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2574 * "arg" is advanced until after the expression, skipping white space.
2575 *
2576 * If the value is a constant "ppconst->pp_used" will be non-zero.
2577 * Before instructions are generated, any values in "ppconst" will generated.
2578 *
2579 * This is the compiling equivalent of eval1(), eval2(), etc.
2580 */
2581
2582/*
2583 * number number constant
2584 * 0zFFFFFFFF Blob constant
2585 * "string" string constant
2586 * 'string' literal string constant
2587 * &option-name option value
2588 * @r register contents
2589 * identifier variable value
2590 * function() function call
2591 * $VAR environment variable
2592 * (expression) nested expression
2593 * [expr, expr] List
2594 * {key: val, [key]: val} Dictionary
2595 *
2596 * Also handle:
2597 * ! in front logical NOT
2598 * - in front unary minus
2599 * + in front unary plus (ignored)
2600 * trailing (arg) funcref/partial call
2601 * trailing [] subscript in String or List
2602 * trailing .name entry in Dictionary
2603 * trailing ->name() method call
2604 */
2605 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002606compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002607 char_u **arg,
2608 cctx_T *cctx,
2609 ppconst_T *ppconst)
2610{
2611 char_u *start_leader, *end_leader;
2612 int ret = OK;
2613 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2614 int used_before = ppconst->pp_used;
2615
2616 ppconst->pp_is_const = FALSE;
2617
2618 /*
2619 * Skip '!', '-' and '+' characters. They are handled later.
2620 */
2621 start_leader = *arg;
2622 if (eval_leader(arg, TRUE) == FAIL)
2623 return FAIL;
2624 end_leader = *arg;
2625
2626 rettv->v_type = VAR_UNKNOWN;
2627 switch (**arg)
2628 {
2629 /*
2630 * Number constant.
2631 */
2632 case '0': // also for blob starting with 0z
2633 case '1':
2634 case '2':
2635 case '3':
2636 case '4':
2637 case '5':
2638 case '6':
2639 case '7':
2640 case '8':
2641 case '9':
2642 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2643 return FAIL;
2644 // Apply "-" and "+" just before the number now, right to
2645 // left. Matters especially when "->" follows. Stops at
2646 // '!'.
2647 if (apply_leader(rettv, TRUE,
2648 start_leader, &end_leader) == FAIL)
2649 {
2650 clear_tv(rettv);
2651 return FAIL;
2652 }
2653 break;
2654
2655 /*
2656 * String constant: "string".
2657 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002658 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659 return FAIL;
2660 break;
2661
2662 /*
2663 * Literal string constant: 'str''ing'.
2664 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002665 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002666 return FAIL;
2667 break;
2668
2669 /*
2670 * Constant Vim variable.
2671 */
2672 case 'v': get_vim_constant(arg, rettv);
2673 ret = NOTDONE;
2674 break;
2675
2676 /*
2677 * "true" constant
2678 */
2679 case 't': if (STRNCMP(*arg, "true", 4) == 0
2680 && !eval_isnamec((*arg)[4]))
2681 {
2682 *arg += 4;
2683 rettv->v_type = VAR_BOOL;
2684 rettv->vval.v_number = VVAL_TRUE;
2685 }
2686 else
2687 ret = NOTDONE;
2688 break;
2689
2690 /*
2691 * "false" constant
2692 */
2693 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2694 && !eval_isnamec((*arg)[5]))
2695 {
2696 *arg += 5;
2697 rettv->v_type = VAR_BOOL;
2698 rettv->vval.v_number = VVAL_FALSE;
2699 }
2700 else
2701 ret = NOTDONE;
2702 break;
2703
2704 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002705 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002706 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002707 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002709 char_u *p = *arg + 4;
2710 int len;
2711
2712 for (len = 0; eval_isnamec(p[len]); ++len)
2713 ;
2714 ret = handle_predefined(*arg, len + 4, rettv);
2715 if (ret == FAIL)
2716 ret = NOTDONE;
2717 else
2718 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002719 }
2720 else
2721 ret = NOTDONE;
2722 break;
2723
2724 /*
2725 * List: [expr, expr]
2726 */
2727 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2728 return FAIL;
2729 ret = compile_list(arg, cctx, ppconst);
2730 break;
2731
2732 /*
2733 * Dictionary: {'key': val, 'key': val}
2734 */
2735 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2736 return FAIL;
2737 ret = compile_dict(arg, cctx, ppconst);
2738 break;
2739
2740 /*
2741 * Option value: &name
2742 */
2743 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2744 return FAIL;
2745 ret = compile_get_option(arg, cctx);
2746 break;
2747
2748 /*
2749 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002750 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002751 */
2752 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2753 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002754 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2755 ret = compile_interp_string(arg, cctx);
2756 else
2757 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002758 break;
2759
2760 /*
2761 * Register contents: @r.
2762 */
2763 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2764 return FAIL;
2765 ret = compile_get_register(arg, cctx);
2766 break;
2767 /*
2768 * nested expression: (expression).
2769 * lambda: (arg, arg) => expr
2770 * funcref: (arg, arg) => { statement }
2771 */
2772 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2773 ret = compile_lambda(arg, cctx);
2774 if (ret == NOTDONE)
2775 ret = compile_parenthesis(arg, cctx, ppconst);
2776 break;
2777
2778 default: ret = NOTDONE;
2779 break;
2780 }
2781 if (ret == FAIL)
2782 return FAIL;
2783
2784 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2785 {
2786 if (cctx->ctx_skip == SKIP_YES)
2787 clear_tv(rettv);
2788 else
2789 // A constant expression can possibly be handled compile time,
2790 // return the value instead of generating code.
2791 ++ppconst->pp_used;
2792 }
2793 else if (ret == NOTDONE)
2794 {
2795 char_u *p;
2796 int r;
2797
2798 if (!eval_isnamec1(**arg))
2799 {
2800 if (!vim9_bad_comment(*arg))
2801 {
2802 if (ends_excmd(*skipwhite(*arg)))
2803 semsg(_(e_empty_expression_str), *arg);
2804 else
2805 semsg(_(e_name_expected_str), *arg);
2806 }
2807 return FAIL;
2808 }
2809
2810 // "name" or "name()"
2811 p = to_name_end(*arg, TRUE);
2812 if (p - *arg == (size_t)1 && **arg == '_')
2813 {
2814 emsg(_(e_cannot_use_underscore_here));
2815 return FAIL;
2816 }
2817
2818 if (*p == '(')
2819 {
2820 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2821 }
2822 else
2823 {
2824 if (cctx->ctx_skip != SKIP_YES
2825 && generate_ppconst(cctx, ppconst) == FAIL)
2826 return FAIL;
2827 r = compile_load(arg, p, cctx, TRUE, TRUE);
2828 }
2829 if (r == FAIL)
2830 return FAIL;
2831 }
2832
2833 // Handle following "[]", ".member", etc.
2834 // Then deal with prefixed '-', '+' and '!', if not done already.
2835 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2836 ppconst) == FAIL)
2837 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002838 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002839 {
2840 // apply the '!', '-' and '+' before the constant
2841 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2842 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2843 return FAIL;
2844 return OK;
2845 }
2846 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2847 return FAIL;
2848 return OK;
2849}
2850
2851/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002852 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002853 */
2854 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002855compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002856{
2857 type_T *want_type = NULL;
2858
2859 // Recognize <type>
2860 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2861 {
2862 ++*arg;
2863 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2864 if (want_type == NULL)
2865 return FAIL;
2866
2867 if (**arg != '>')
2868 {
2869 if (*skipwhite(*arg) == '>')
2870 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2871 else
2872 emsg(_(e_missing_gt));
2873 return FAIL;
2874 }
2875 ++*arg;
2876 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2877 return FAIL;
2878 }
2879
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002880 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002881 return FAIL;
2882
2883 if (want_type != NULL)
2884 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002885 type_T *actual;
2886 where_T where = WHERE_INIT;
2887
LemonBoy50d48542024-07-04 17:03:17 +02002888 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002889 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002890 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002891 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002892 {
LemonBoy50d48542024-07-04 17:03:17 +02002893 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2894 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002895 return FAIL;
2896 }
2897 }
2898
2899 return OK;
2900}
2901
2902/*
2903 * * number multiplication
2904 * / number division
2905 * % number modulo
2906 */
2907 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002908compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002909{
2910 char_u *op;
2911 char_u *next;
2912 int ppconst_used = ppconst->pp_used;
2913
2914 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002915 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002916 return FAIL;
2917
2918 /*
2919 * Repeat computing, until no "*", "/" or "%" is following.
2920 */
2921 for (;;)
2922 {
2923 op = may_peek_next_line(cctx, *arg, &next);
2924 if (*op != '*' && *op != '/' && *op != '%')
2925 break;
2926 if (next != NULL)
2927 {
2928 *arg = next_line_from_context(cctx, TRUE);
2929 op = skipwhite(*arg);
2930 }
2931
2932 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2933 {
2934 error_white_both(op, 1);
2935 return FAIL;
2936 }
2937 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2938 return FAIL;
2939
2940 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002941 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002942 return FAIL;
2943
2944 if (ppconst->pp_used == ppconst_used + 2
2945 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2946 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2947 {
2948 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2949 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2950 varnumber_T res = 0;
2951 int failed = FALSE;
2952
2953 // both are numbers: compute the result
2954 switch (*op)
2955 {
2956 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2957 break;
2958 case '/': res = num_divide(tv1->vval.v_number,
2959 tv2->vval.v_number, &failed);
2960 break;
2961 case '%': res = num_modulus(tv1->vval.v_number,
2962 tv2->vval.v_number, &failed);
2963 break;
2964 }
2965 if (failed)
2966 return FAIL;
2967 tv1->vval.v_number = res;
2968 --ppconst->pp_used;
2969 }
2970 else
2971 {
2972 generate_ppconst(cctx, ppconst);
2973 generate_two_op(cctx, op);
2974 }
2975 }
2976
2977 return OK;
2978}
2979
2980/*
2981 * + number addition or list/blobl concatenation
2982 * - number subtraction
2983 * .. string concatenation
2984 */
2985 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002986compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002987{
2988 char_u *op;
2989 char_u *next;
2990 int oplen;
2991 int ppconst_used = ppconst->pp_used;
2992
2993 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002994 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002995 return FAIL;
2996
2997 /*
2998 * Repeat computing, until no "+", "-" or ".." is following.
2999 */
3000 for (;;)
3001 {
3002 op = may_peek_next_line(cctx, *arg, &next);
3003 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
3004 break;
3005 if (op[0] == op[1] && *op != '.' && next)
3006 // Finding "++" or "--" on the next line is a separate command.
3007 // But ".." is concatenation.
3008 break;
3009 oplen = (*op == '.' ? 2 : 1);
3010 if (next != NULL)
3011 {
3012 *arg = next_line_from_context(cctx, TRUE);
3013 op = skipwhite(*arg);
3014 }
3015
3016 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3017 {
3018 error_white_both(op, oplen);
3019 return FAIL;
3020 }
3021
3022 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3023 return FAIL;
3024
3025 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003026 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003027 return FAIL;
3028
3029 if (ppconst->pp_used == ppconst_used + 2
3030 && (*op == '.'
3031 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3032 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3033 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3034 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3035 {
3036 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3037 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3038
3039 // concat/subtract/add constant numbers
3040 if (*op == '+')
3041 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3042 else if (*op == '-')
3043 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3044 else
3045 {
3046 // concatenate constant strings
3047 char_u *s1 = tv1->vval.v_string;
3048 char_u *s2 = tv2->vval.v_string;
3049 size_t len1 = STRLEN(s1);
3050
3051 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3052 if (tv1->vval.v_string == NULL)
3053 {
3054 clear_ppconst(ppconst);
3055 return FAIL;
3056 }
3057 mch_memmove(tv1->vval.v_string, s1, len1);
3058 STRCPY(tv1->vval.v_string + len1, s2);
3059 vim_free(s1);
3060 vim_free(s2);
3061 }
3062 --ppconst->pp_used;
3063 }
3064 else
3065 {
3066 generate_ppconst(cctx, ppconst);
3067 ppconst->pp_is_const = FALSE;
3068 if (*op == '.')
3069 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003070 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3071 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003072 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003073 if (generate_CONCAT(cctx, 2) == FAIL)
3074 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003075 }
3076 else
3077 generate_two_op(cctx, op);
3078 }
3079 }
3080
3081 return OK;
3082}
3083
3084/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003085 * expr6a >> expr6b
3086 * expr6a << expr6b
3087 *
3088 * Produces instructions:
3089 * OPNR bitwise left or right shift
3090 */
3091 static int
3092compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3093{
3094 exprtype_T type = EXPR_UNKNOWN;
3095 char_u *p;
3096 char_u *next;
3097 int len = 2;
3098 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003099 isn_T *isn;
3100
3101 // get the first variable
3102 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3103 return FAIL;
3104
3105 /*
3106 * Repeat computing, until no "+", "-" or ".." is following.
3107 */
3108 for (;;)
3109 {
3110 type = EXPR_UNKNOWN;
3111
3112 p = may_peek_next_line(cctx, *arg, &next);
3113 if (p[0] == '<' && p[1] == '<')
3114 type = EXPR_LSHIFT;
3115 else if (p[0] == '>' && p[1] == '>')
3116 type = EXPR_RSHIFT;
3117
3118 if (type == EXPR_UNKNOWN)
3119 return OK;
3120
3121 // Handle a bitwise left or right shift operator
3122 if (ppconst->pp_used == ppconst_used + 1)
3123 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003124 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003125 {
3126 // left operand should be a number
3127 emsg(_(e_bitshift_ops_must_be_number));
3128 return FAIL;
3129 }
3130 }
3131 else
3132 {
3133 type_T *t = get_type_on_stack(cctx, 0);
3134
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003135 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003136 {
3137 emsg(_(e_bitshift_ops_must_be_number));
3138 return FAIL;
3139 }
3140 }
3141
3142 if (next != NULL)
3143 {
3144 *arg = next_line_from_context(cctx, TRUE);
3145 p = skipwhite(*arg);
3146 }
3147
3148 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3149 {
3150 error_white_both(p, len);
3151 return FAIL;
3152 }
3153
3154 // get the second variable
3155 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3156 return FAIL;
3157
3158 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3159 return FAIL;
3160
3161 if (ppconst->pp_used == ppconst_used + 2)
3162 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003163 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3164 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3165
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003166 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003167 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3168 {
3169 // right operand should be a positive number
3170 if (tv2->v_type != VAR_NUMBER)
3171 emsg(_(e_bitshift_ops_must_be_number));
3172 else
dundargocc57b5bc2022-11-02 13:30:51 +00003173 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003174 return FAIL;
3175 }
3176
3177 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3178 tv1->vval.v_number = 0;
3179 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003180 tv1->vval.v_number =
3181 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003182 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003183 tv1->vval.v_number =
3184 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003185 clear_tv(tv2);
3186 --ppconst->pp_used;
3187 }
3188 else
3189 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003190 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3191 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003192 {
3193 emsg(_(e_bitshift_ops_must_be_number));
3194 return FAIL;
3195 }
3196
3197 generate_ppconst(cctx, ppconst);
3198
3199 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3200 if (isn == NULL)
3201 return FAIL;
3202
3203 if (isn != NULL)
3204 isn->isn_arg.op.op_type = type;
3205 }
3206 }
3207
3208 return OK;
3209}
3210
3211/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003212 * expr5a == expr5b
3213 * expr5a =~ expr5b
3214 * expr5a != expr5b
3215 * expr5a !~ expr5b
3216 * expr5a > expr5b
3217 * expr5a >= expr5b
3218 * expr5a < expr5b
3219 * expr5a <= expr5b
3220 * expr5a is expr5b
3221 * expr5a isnot expr5b
3222 *
3223 * Produces instructions:
3224 * EVAL expr5a Push result of "expr5a"
3225 * EVAL expr5b Push result of "expr5b"
3226 * COMPARE one of the compare instructions
3227 */
3228 static int
3229compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3230{
3231 exprtype_T type = EXPR_UNKNOWN;
3232 char_u *p;
3233 char_u *next;
3234 int len = 2;
3235 int type_is = FALSE;
3236 int ppconst_used = ppconst->pp_used;
3237
3238 // get the first variable
3239 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3240 return FAIL;
3241
3242 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003243
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003244 type = get_compare_type(p, &len, &type_is);
3245
3246 /*
3247 * If there is a comparative operator, use it.
3248 */
3249 if (type != EXPR_UNKNOWN)
3250 {
3251 int ic = FALSE; // Default: do not ignore case
3252
3253 if (next != NULL)
3254 {
3255 *arg = next_line_from_context(cctx, TRUE);
3256 p = skipwhite(*arg);
3257 }
3258 if (type_is && (p[len] == '?' || p[len] == '#'))
3259 {
3260 semsg(_(e_invalid_expression_str), *arg);
3261 return FAIL;
3262 }
3263 // extra question mark appended: ignore case
3264 if (p[len] == '?')
3265 {
3266 ic = TRUE;
3267 ++len;
3268 }
3269 // extra '#' appended: match case (ignored)
3270 else if (p[len] == '#')
3271 ++len;
3272 // nothing appended: match case
3273
3274 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3275 {
3276 error_white_both(p, len);
3277 return FAIL;
3278 }
3279
3280 // get the second variable
3281 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3282 return FAIL;
3283
3284 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3285 return FAIL;
3286
3287 if (ppconst->pp_used == ppconst_used + 2)
3288 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003289 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003290 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3291 int ret;
3292
3293 // Both sides are a constant, compute the result now.
3294 // First check for a valid combination of types, this is more
3295 // strict than typval_compare().
3296 if (check_compare_types(type, tv1, tv2) == FAIL)
3297 ret = FAIL;
3298 else
3299 {
3300 ret = typval_compare(tv1, tv2, type, ic);
3301 tv1->v_type = VAR_BOOL;
3302 tv1->vval.v_number = tv1->vval.v_number
3303 ? VVAL_TRUE : VVAL_FALSE;
3304 clear_tv(tv2);
3305 --ppconst->pp_used;
3306 }
3307 return ret;
3308 }
3309
3310 generate_ppconst(cctx, ppconst);
3311 return generate_COMPARE(cctx, type, ic);
3312 }
3313
3314 return OK;
3315}
3316
3317static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3318
3319/*
3320 * Compile || or &&.
3321 */
3322 static int
3323compile_and_or(
3324 char_u **arg,
3325 cctx_T *cctx,
3326 char *op,
3327 ppconst_T *ppconst,
3328 int ppconst_used UNUSED)
3329{
3330 char_u *next;
3331 char_u *p = may_peek_next_line(cctx, *arg, &next);
3332 int opchar = *op;
3333
3334 if (p[0] == opchar && p[1] == opchar)
3335 {
3336 garray_T *instr = &cctx->ctx_instr;
3337 garray_T end_ga;
3338 int save_skip = cctx->ctx_skip;
3339
3340 /*
3341 * Repeat until there is no following "||" or "&&"
3342 */
3343 ga_init2(&end_ga, sizeof(int), 10);
3344 while (p[0] == opchar && p[1] == opchar)
3345 {
3346 long start_lnum = SOURCING_LNUM;
3347 long save_sourcing_lnum;
3348 int start_ctx_lnum = cctx->ctx_lnum;
3349 int save_lnum;
3350 int const_used;
3351 int status;
3352 jumpwhen_T jump_when = opchar == '|'
3353 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3354
3355 if (next != NULL)
3356 {
3357 *arg = next_line_from_context(cctx, TRUE);
3358 p = skipwhite(*arg);
3359 }
3360
3361 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3362 {
3363 semsg(_(e_white_space_required_before_and_after_str_at_str),
3364 op, p);
3365 ga_clear(&end_ga);
3366 return FAIL;
3367 }
3368
3369 save_sourcing_lnum = SOURCING_LNUM;
3370 SOURCING_LNUM = start_lnum;
3371 save_lnum = cctx->ctx_lnum;
3372 cctx->ctx_lnum = start_ctx_lnum;
3373
3374 status = check_ppconst_bool(ppconst);
3375 if (status != FAIL)
3376 {
3377 // Use the last ppconst if possible.
3378 if (ppconst->pp_used > 0)
3379 {
3380 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3381 int is_true = tv2bool(tv);
3382
3383 if ((is_true && opchar == '|')
3384 || (!is_true && opchar == '&'))
3385 {
3386 // For "false && expr" and "true || expr" the "expr"
3387 // does not need to be evaluated.
3388 cctx->ctx_skip = SKIP_YES;
3389 clear_tv(tv);
3390 tv->v_type = VAR_BOOL;
3391 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3392 }
3393 else
3394 {
3395 // For "true && expr" and "false || expr" only "expr"
3396 // needs to be evaluated.
3397 --ppconst->pp_used;
3398 jump_when = JUMP_NEVER;
3399 }
3400 }
3401 else
3402 {
3403 // Every part must evaluate to a bool.
3404 status = bool_on_stack(cctx);
3405 }
3406 }
3407 if (status != FAIL)
3408 status = ga_grow(&end_ga, 1);
3409 cctx->ctx_lnum = save_lnum;
3410 if (status == FAIL)
3411 {
3412 ga_clear(&end_ga);
3413 return FAIL;
3414 }
3415
3416 if (jump_when != JUMP_NEVER)
3417 {
3418 if (cctx->ctx_skip != SKIP_YES)
3419 {
3420 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3421 ++end_ga.ga_len;
3422 }
3423 generate_JUMP(cctx, jump_when, 0);
3424 }
3425
3426 // eval the next expression
3427 SOURCING_LNUM = save_sourcing_lnum;
3428 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3429 {
3430 ga_clear(&end_ga);
3431 return FAIL;
3432 }
3433
3434 const_used = ppconst->pp_used;
3435 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3436 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3437 {
3438 ga_clear(&end_ga);
3439 return FAIL;
3440 }
3441
3442 // "0 || 1" results in true, "1 && 0" results in false.
3443 if (ppconst->pp_used == const_used + 1)
3444 {
3445 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3446
3447 if (tv->v_type == VAR_NUMBER
3448 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3449 {
3450 tv->vval.v_number = tv->vval.v_number == 1
3451 ? VVAL_TRUE : VVAL_FALSE;
3452 tv->v_type = VAR_BOOL;
3453 }
3454 }
3455
3456 p = may_peek_next_line(cctx, *arg, &next);
3457 }
3458
3459 if (check_ppconst_bool(ppconst) == FAIL)
3460 {
3461 ga_clear(&end_ga);
3462 return FAIL;
3463 }
3464
3465 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3466 // Every part must evaluate to a bool.
3467 if (bool_on_stack(cctx) == FAIL)
3468 {
3469 ga_clear(&end_ga);
3470 return FAIL;
3471 }
3472
3473 if (end_ga.ga_len > 0)
3474 {
3475 // Fill in the end label in all jumps.
3476 generate_ppconst(cctx, ppconst);
3477 while (end_ga.ga_len > 0)
3478 {
3479 isn_T *isn;
3480
3481 --end_ga.ga_len;
3482 isn = ((isn_T *)instr->ga_data)
3483 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3484 isn->isn_arg.jump.jump_where = instr->ga_len;
3485 }
3486 }
3487 ga_clear(&end_ga);
3488
3489 cctx->ctx_skip = save_skip;
3490 }
3491
3492 return OK;
3493}
3494
3495/*
3496 * expr4a && expr4a && expr4a logical AND
3497 *
3498 * Produces instructions:
3499 * EVAL expr4a Push result of "expr4a"
3500 * COND2BOOL convert to bool if needed
3501 * JUMP_IF_COND_FALSE end
3502 * EVAL expr4b Push result of "expr4b"
3503 * JUMP_IF_COND_FALSE end
3504 * EVAL expr4c Push result of "expr4c"
3505 * end:
3506 */
3507 static int
3508compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3509{
3510 int ppconst_used = ppconst->pp_used;
3511
3512 // get the first variable
3513 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3514 return FAIL;
3515
3516 // || and && work almost the same
3517 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3518}
3519
3520/*
3521 * expr3a || expr3b || expr3c logical OR
3522 *
3523 * Produces instructions:
3524 * EVAL expr3a Push result of "expr3a"
3525 * COND2BOOL convert to bool if needed
3526 * JUMP_IF_COND_TRUE end
3527 * EVAL expr3b Push result of "expr3b"
3528 * JUMP_IF_COND_TRUE end
3529 * EVAL expr3c Push result of "expr3c"
3530 * end:
3531 */
3532 static int
3533compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3534{
3535 int ppconst_used = ppconst->pp_used;
3536
3537 // eval the first expression
3538 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3539 return FAIL;
3540
3541 // || and && work almost the same
3542 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3543}
3544
3545/*
3546 * Toplevel expression: expr2 ? expr1a : expr1b
3547 * Produces instructions:
3548 * EVAL expr2 Push result of "expr2"
3549 * JUMP_IF_FALSE alt jump if false
3550 * EVAL expr1a
3551 * JUMP_ALWAYS end
3552 * alt: EVAL expr1b
3553 * end:
3554 *
3555 * Toplevel expression: expr2 ?? expr1
3556 * Produces instructions:
3557 * EVAL expr2 Push result of "expr2"
3558 * JUMP_AND_KEEP_IF_TRUE end jump if true
3559 * EVAL expr1
3560 * end:
3561 */
3562 int
3563compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3564{
3565 char_u *p;
3566 int ppconst_used = ppconst->pp_used;
3567 char_u *next;
3568
3569 // Ignore all kinds of errors when not producing code.
3570 if (cctx->ctx_skip == SKIP_YES)
3571 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003572 int prev_did_emsg = did_emsg;
3573
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003574 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003575 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003576 }
3577
3578 // Evaluate the first expression.
3579 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3580 return FAIL;
3581
3582 p = may_peek_next_line(cctx, *arg, &next);
3583 if (*p == '?')
3584 {
3585 int op_falsy = p[1] == '?';
3586 garray_T *instr = &cctx->ctx_instr;
3587 garray_T *stack = &cctx->ctx_type_stack;
3588 int alt_idx = instr->ga_len;
3589 int end_idx = 0;
3590 isn_T *isn;
3591 type_T *type1 = NULL;
3592 int has_const_expr = FALSE;
3593 int const_value = FALSE;
3594 int save_skip = cctx->ctx_skip;
3595
3596 if (next != NULL)
3597 {
3598 *arg = next_line_from_context(cctx, TRUE);
3599 p = skipwhite(*arg);
3600 }
3601
3602 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3603 {
3604 semsg(_(e_white_space_required_before_and_after_str_at_str),
3605 op_falsy ? "??" : "?", p);
3606 return FAIL;
3607 }
3608
3609 if (ppconst->pp_used == ppconst_used + 1)
3610 {
3611 // the condition is a constant, we know whether the ? or the :
3612 // expression is to be evaluated.
3613 has_const_expr = TRUE;
3614 if (op_falsy)
3615 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3616 else
3617 {
3618 int error = FALSE;
3619
3620 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3621 &error);
3622 if (error)
3623 return FAIL;
3624 }
3625 cctx->ctx_skip = save_skip == SKIP_YES ||
3626 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3627
3628 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3629 // "left ?? right" and "left" is truthy: produce "left"
3630 generate_ppconst(cctx, ppconst);
3631 else
3632 {
3633 clear_tv(&ppconst->pp_tv[ppconst_used]);
3634 --ppconst->pp_used;
3635 }
3636 }
3637 else
3638 {
3639 generate_ppconst(cctx, ppconst);
3640 if (op_falsy)
3641 end_idx = instr->ga_len;
3642 generate_JUMP(cctx, op_falsy
3643 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3644 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003645 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003646 }
3647
3648 // evaluate the second expression; any type is accepted
3649 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3650 return FAIL;
3651 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3652 return FAIL;
3653
3654 if (!has_const_expr)
3655 {
3656 generate_ppconst(cctx, ppconst);
3657
3658 if (!op_falsy)
3659 {
3660 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003661 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003662 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003663
3664 end_idx = instr->ga_len;
3665 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3666
3667 // jump here from JUMP_IF_FALSE
3668 isn = ((isn_T *)instr->ga_data) + alt_idx;
3669 isn->isn_arg.jump.jump_where = instr->ga_len;
3670 }
3671 }
3672
3673 if (!op_falsy)
3674 {
3675 // Check for the ":".
3676 p = may_peek_next_line(cctx, *arg, &next);
3677 if (*p != ':')
3678 {
3679 emsg(_(e_missing_colon_after_questionmark));
3680 return FAIL;
3681 }
3682 if (next != NULL)
3683 {
3684 *arg = next_line_from_context(cctx, TRUE);
3685 p = skipwhite(*arg);
3686 }
3687
3688 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3689 {
3690 semsg(_(e_white_space_required_before_and_after_str_at_str),
3691 ":", p);
3692 return FAIL;
3693 }
3694
3695 // evaluate the third expression
3696 if (has_const_expr)
3697 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3698 ? SKIP_YES : SKIP_NOT;
3699 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3700 return FAIL;
3701 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3702 return FAIL;
3703 }
3704
3705 if (!has_const_expr)
3706 {
3707 type_T **typep;
3708
3709 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003710 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003711
3712 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003713 typep = &((((type2_T *)stack->ga_data)
3714 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003715 common_type(type1, *typep, typep, cctx->ctx_type_list);
3716
3717 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3718 isn = ((isn_T *)instr->ga_data) + end_idx;
3719 isn->isn_arg.jump.jump_where = instr->ga_len;
3720 }
3721
3722 cctx->ctx_skip = save_skip;
3723 }
3724 return OK;
3725}
3726
3727/*
3728 * Toplevel expression.
3729 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3730 * Returns OK or FAIL.
3731 */
3732 int
3733compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3734{
3735 ppconst_T ppconst;
3736
3737 CLEAR_FIELD(ppconst);
3738 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3739 {
3740 clear_ppconst(&ppconst);
3741 return FAIL;
3742 }
3743 if (is_const != NULL)
3744 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3745 if (generate_ppconst(cctx, &ppconst) == FAIL)
3746 return FAIL;
3747 return OK;
3748}
3749
3750/*
3751 * Toplevel expression.
3752 */
3753 int
3754compile_expr0(char_u **arg, cctx_T *cctx)
3755{
3756 return compile_expr0_ext(arg, cctx, NULL);
3757}
3758
3759
3760#endif // defined(FEAT_EVAL)