blob: af12fb69cce677fa89343ec4a1621bcfa4294533 [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
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100541 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000542{
543 scriptitem_T *si;
544 int idx;
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100545 imported_T *import;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546
547 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
548 return FAIL;
549 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000550 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000551 if (idx >= 0)
552 {
553 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
554
555 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
556 current_sctx.sc_sid, idx, sv->sv_type);
557 return OK;
558 }
559
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100560 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
561 if (import != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000562 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000563 char_u *p = skipwhite(*end);
564 char_u *exp_name;
565 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100566 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000567 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000568 int done = FALSE;
569 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000570
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100571 check_script_symlink(import->imp_sid);
572 import_check_sourced_sid(&import->imp_sid);
Ernie Rael9a901792024-04-16 22:11:56 +0200573
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000574 // Need to lookup the member.
575 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000577 semsg(_(e_expected_dot_after_name_str), start);
578 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000579 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000580 ++p;
581 if (VIM_ISWHITE(*p))
582 {
583 emsg(_(e_no_white_space_allowed_after_dot));
584 return FAIL;
585 }
586
587 // isolate one name
588 exp_name = p;
589 while (eval_isnamec(*p))
590 ++p;
591 cc = *p;
592 *p = NUL;
593
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100594 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100595 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
596 // "import autoload './dir/script.vim'" or
597 // "import autoload './autoload/script.vim'" - load script first
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100598 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100599
600 if (res == OK)
601 {
602 if (si->sn_autoload_prefix != NULL
603 && si->sn_state == SN_STATE_NOT_LOADED)
604 {
605 char_u *auto_name =
606 concat_str(si->sn_autoload_prefix, exp_name);
607
608 // autoload script must be loaded later, access by the autoload
609 // name. If a '(' follows it must be a function. Otherwise we
610 // don't know, it can be "script.Func".
611 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100612 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100613 else
614 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
615 vim_free(auto_name);
616 done = TRUE;
617 }
618 else if (si->sn_import_autoload
619 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100620 {
621 // If a '(' follows it must be a function. Otherwise we don't
622 // know, it can be "script.Func".
623 if (cc == '(' || paren_follows_after_expr)
624 {
625 char_u sid_name[MAX_FUNC_NAME_LEN];
626
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100627 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100628 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100629 }
630 else
631 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100632 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100633 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100634 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100635 else
636 {
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100637 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100638 cctx, NULL, TRUE);
639 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100640 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100641
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000642 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000643 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000644 if (done)
645 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000646
647 if (idx < 0)
648 {
649 if (ufunc != NULL)
650 {
651 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100652 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000653 return OK;
654 }
655 return FAIL;
656 }
657
658 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100659 import->imp_sid,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000660 idx,
661 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662 return OK;
663 }
664
Bram Moolenaard0132f42022-05-12 11:05:40 +0100665 // Can only get here if we know "name" is a script variable and not in a
666 // Vim9 script (variable is not in sn_var_vals): old style script.
667 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000668 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669}
670
671 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000672generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000673{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000674 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000675 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000677 // Reject a global non-autoload function found without the "g:" prefix.
678 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000679 return FAIL;
680
681 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000682 compile_type = get_compile_type(ufunc);
683 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000684 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000685 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100686 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687}
688
689/*
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100690 * Returns TRUE if compiling a class method.
691 */
692 static int
693compiling_a_class_method(cctx_T *cctx)
694{
695 // For an object method, the FC_OBJECT flag will be set.
696 // For a constructor method, the FC_NEW flag will be set.
697 // Excluding these methods, the others are class methods.
698 // When compiling a closure function inside an object method,
699 // cctx->ctx_outer->ctx_func will point to the object method.
700 return cctx->ctx_ufunc != NULL
701 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0
702 && (cctx->ctx_outer == NULL
703 || cctx->ctx_outer->ctx_ufunc == NULL
704 || cctx->ctx_outer->ctx_ufunc->uf_class == NULL
705 || (cctx->ctx_outer->ctx_ufunc->uf_flags
706 & (FC_OBJECT|FC_NEW)) == 0);
707}
708
709/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000710 * Compile a variable name into a load instruction.
711 * "end" points to just after the name.
712 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
713 * When "error" is FALSE do not give an error when not found.
714 */
715 int
716compile_load(
717 char_u **arg,
718 char_u *end_arg,
719 cctx_T *cctx,
720 int is_expr,
721 int error)
722{
723 type_T *type;
724 char_u *name = NULL;
725 char_u *end = end_arg;
726 int res = FAIL;
727 int prev_called_emsg = called_emsg;
728
729 if (*(*arg + 1) == ':')
730 {
731 if (end <= *arg + 2)
732 {
733 isntype_T isn_type;
734
735 // load dictionary of namespace
736 switch (**arg)
737 {
738 case 'g': isn_type = ISN_LOADGDICT; break;
739 case 'w': isn_type = ISN_LOADWDICT; break;
740 case 't': isn_type = ISN_LOADTDICT; break;
741 case 'b': isn_type = ISN_LOADBDICT; break;
742 default:
743 semsg(_(e_namespace_not_supported_str), *arg);
744 goto theend;
745 }
746 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
747 goto theend;
748 res = OK;
749 }
750 else
751 {
752 isntype_T isn_type = ISN_DROP;
753
754 // load namespaced variable
755 name = vim_strnsave(*arg + 2, end - (*arg + 2));
756 if (name == NULL)
757 return FAIL;
758
759 switch (**arg)
760 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100761 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000763 case 's': if (current_script_is_vim9())
764 {
765 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
766 *arg);
767 vim_free(name);
768 return FAIL;
769 }
Kota Kato948a3892022-08-16 16:09:59 +0100770 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000771 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 else
773 res = compile_load_scriptvar(cctx, name,
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100774 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000775 break;
776 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
777 {
778 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000779 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000780 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000781 else
782 isn_type = ISN_LOADG;
783 }
784 else
785 {
786 isn_type = ISN_LOADAUTO;
787 vim_free(name);
788 name = vim_strnsave(*arg, end - *arg);
789 if (name == NULL)
790 return FAIL;
791 }
792 break;
793 case 'w': isn_type = ISN_LOADW; break;
794 case 't': isn_type = ISN_LOADT; break;
795 case 'b': isn_type = ISN_LOADB; break;
796 default: // cannot happen, just in case
797 semsg(_(e_namespace_not_supported_str), *arg);
798 goto theend;
799 }
800 if (isn_type != ISN_DROP)
801 {
802 // Global, Buffer-local, Window-local and Tabpage-local
803 // variables can be defined later, thus we don't check if it
804 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000805 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 }
807 }
808 }
809 else
810 {
811 size_t len = end - *arg;
812 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200813 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 int gen_load = FALSE;
815 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100816 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100817 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818
Hirohito Higashif7cb9f92025-02-04 16:37:19 +0100819 name = vim_strnsave(*arg, len);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820 if (name == NULL)
821 return FAIL;
822
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100823 if (STRCMP(name, "super") == 0 && compiling_a_class_method(cctx))
Bram Moolenaar58b40092023-01-11 15:59:05 +0000824 {
825 // super.SomeFunc() in a class function: push &t_super type, this
826 // is recognized in compile_subscript().
827 res = push_type_stack(cctx, &t_super);
828 if (*end != '.')
829 emsg(_(e_super_must_be_followed_by_dot));
830 }
831 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000832 {
833 script_autoload(name, FALSE);
834 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
835 }
836 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
837 == OK)
838 {
839 if (gen_load_outer == 0)
840 gen_load = TRUE;
841 }
842 else
843 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000844 lvar_T lvar;
845 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846
847 if (lookup_local(*arg, len, &lvar, cctx) == OK)
848 {
849 type = lvar.lv_type;
850 idx = lvar.lv_idx;
851 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100852 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100854 outer_loop_depth = lvar.lv_loop_depth;
855 outer_loop_idx = lvar.lv_loop_idx;
856 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857 else
858 gen_load = TRUE;
859 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200860 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
861 (((idx =
862 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
863 || ((method_idx =
864 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000865 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200866 // Referencing a class variable or method without the class
867 // name. A class variable or method can be referenced without
868 // the class name only in the class where the function is
869 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200870 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200871 {
872 if (idx >= 0)
873 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
874 else
875 {
876 ufunc_T *fp = cl->class_class_functions[method_idx];
877 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
878 NULL);
879 }
880 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200881 else
882 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200883 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200884 name, cl->class_name);
885 res = FAIL;
886 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000887 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 else
889 {
890 // "var" can be script-local even without using "s:" if it
891 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000892 if (script_var_exists(*arg, len, cctx, NULL) == OK
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +0100893 || find_imported(name, 0, FALSE) != NULL)
894 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895
896 // When evaluating an expression and the name starts with an
897 // uppercase letter it can be a user defined function.
898 // generate_funcref() will fail if the function can't be found.
899 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000900 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901 }
902 }
903 if (gen_load)
904 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
905 if (gen_load_outer > 0)
906 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100907 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
908 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000909 cctx->ctx_outer_used = TRUE;
910 }
911 }
912
913 *arg = end;
914
915theend:
916 if (res == FAIL && error && called_emsg == prev_called_emsg)
917 semsg(_(e_variable_not_found_str), name);
918 vim_free(name);
919 return res;
920}
921
922/*
923 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100924 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000925 * Returns FAIL if compilation fails.
926 */
927 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100928compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929{
LemonBoyf3b48952022-05-05 13:53:03 +0100930 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000931 garray_T save_ga = cctx->ctx_instr;
932 int expr_res;
933 int trailing_error;
934 int instr_count;
935 isn_T *instr = NULL;
936
937 // Remove the string type from the stack.
938 --cctx->ctx_type_stack.ga_len;
939
940 // Temporarily reset the list of instructions so that the jump labels are
941 // correct.
942 cctx->ctx_instr.ga_len = 0;
943 cctx->ctx_instr.ga_maxlen = 0;
944 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000945
946 // avoid peeking a next line
947 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
948 cctx->ctx_ufunc->uf_lines.ga_len = 0;
949
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000951
952 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
953
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000954 s = skipwhite(s);
955 trailing_error = *s != NUL;
956
957 if (expr_res == FAIL || trailing_error
958 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
959 {
960 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000961 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000962 clear_instr_ga(&cctx->ctx_instr);
963 cctx->ctx_instr = save_ga;
964 ++cctx->ctx_type_stack.ga_len;
965 return FAIL;
966 }
967
968 // Move the generated instructions into the ISN_INSTR instruction, then
969 // restore the list of instructions.
970 instr_count = cctx->ctx_instr.ga_len;
971 instr = cctx->ctx_instr.ga_data;
972 instr[instr_count].isn_type = ISN_FINISH;
973
974 cctx->ctx_instr = save_ga;
975 vim_free(isn->isn_arg.string);
976 isn->isn_type = ISN_INSTR;
977 isn->isn_arg.instr = instr;
978 return OK;
979}
980
981/*
982 * Compile the argument expressions.
983 * "arg" points to just after the "(" and is advanced to after the ")"
984 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100985 int
LemonBoyf3b48952022-05-05 13:53:03 +0100986compile_arguments(
987 char_u **arg,
988 cctx_T *cctx,
989 int *argcount,
990 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000991{
992 char_u *p = *arg;
993 char_u *whitep = *arg;
994 int must_end = FALSE;
995 int instr_count;
996
997 for (;;)
998 {
999 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1000 goto failret;
1001 if (*p == ')')
1002 {
1003 *arg = p + 1;
1004 return OK;
1005 }
1006 if (must_end)
1007 {
1008 semsg(_(e_missing_comma_before_argument_str), p);
1009 return FAIL;
1010 }
1011
1012 instr_count = cctx->ctx_instr.ga_len;
1013 if (compile_expr0(&p, cctx) == FAIL)
1014 return FAIL;
1015 ++*argcount;
1016
LemonBoyf3b48952022-05-05 13:53:03 +01001017 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001018 && cctx->ctx_instr.ga_len == instr_count + 1)
1019 {
1020 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1021
1022 // {skip} argument of searchpair() can be compiled if not empty
1023 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +01001024 compile_string(isn, cctx, 0);
1025 }
1026 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
1027 && cctx->ctx_instr.ga_len == instr_count + 1)
1028 {
1029 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1030
1031 // {sub} argument of substitute() can be compiled if it starts
1032 // with \=
1033 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001034 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001035 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 }
1037
1038 if (*p != ',' && *skipwhite(p) == ',')
1039 {
1040 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1041 p = skipwhite(p);
1042 }
1043 if (*p == ',')
1044 {
1045 ++p;
1046 if (*p != NUL && !VIM_ISWHITE(*p))
1047 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1048 }
1049 else
1050 must_end = TRUE;
1051 whitep = p;
1052 p = skipwhite(p);
1053 }
1054failret:
1055 emsg(_(e_missing_closing_paren));
1056 return FAIL;
1057}
1058
1059/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001060 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1061 * etc.) if the class implements it.
1062 */
1063 static int
1064compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1065{
1066 type_T *type = get_decl_type_on_stack(cctx, 0);
1067 int res = FAIL;
1068
1069 // If the built in function is invoked on an object and the class
1070 // implements the corresponding built in method, then invoke the object
1071 // method.
1072 if (type->tt_type == VAR_OBJECT)
1073 {
1074 int method_idx;
1075 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1076 &method_idx);
1077 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001078 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001079 }
1080
1081 return res;
1082}
1083
1084
1085/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086 * Compile a function call: name(arg1, arg2)
1087 * "arg" points to "name", "arg + varlen" to the "(".
1088 * "argcount_init" is 1 for "value->method()"
1089 * Instructions:
1090 * EVAL arg1
1091 * EVAL arg2
1092 * BCALL / DCALL / UCALL
1093 */
1094 static int
1095compile_call(
1096 char_u **arg,
1097 size_t varlen,
1098 cctx_T *cctx,
1099 ppconst_T *ppconst,
1100 int argcount_init)
1101{
1102 char_u *name = *arg;
1103 char_u *p;
1104 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001105 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 char_u fname_buf[FLEN_FIXED + 1];
1107 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001108 ufunc_T *ufunc = NULL;
1109 int res = FAIL;
1110 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001111 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001112 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001113 imported_T *import;
1114
1115 if (varlen >= sizeof(namebuf))
1116 {
1117 semsg(_(e_name_too_long_str), name);
1118 return FAIL;
1119 }
1120 vim_strncpy(namebuf, *arg, varlen);
1121
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001122 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001123 if (import != NULL)
1124 {
1125 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1126 return FAIL;
1127 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128
1129 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001130 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001132 if ((varlen == 3
1133 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001134 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1135 {
1136 char_u *s = skipwhite(*arg + varlen + 1);
1137 typval_T argvars[2];
1138 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001139 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140
1141 argvars[0].v_type = VAR_UNKNOWN;
1142 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001143 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001145 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001146 s = skipwhite(s);
1147 if (*s == ')' && argvars[0].v_type == VAR_STRING
1148 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1149 || !is_has))
1150 {
1151 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1152
1153 *arg = s + 1;
1154 argvars[1].v_type = VAR_UNKNOWN;
1155 tv->v_type = VAR_NUMBER;
1156 tv->vval.v_number = 0;
1157 if (is_has)
1158 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001159 else if (is_len)
1160 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 else
1162 f_exists(argvars, tv);
1163 clear_tv(&argvars[0]);
1164 ++ppconst->pp_used;
1165 return OK;
1166 }
1167 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001168 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001169 {
1170 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1171 return FAIL;
1172 }
1173 }
1174
1175 if (generate_ppconst(cctx, ppconst) == FAIL)
1176 return FAIL;
1177
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001178 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1180
1181 // We handle the "skip" argument of searchpair() and searchpairpos()
1182 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001183 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1184 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1185 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1186 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1187 special_fn = CA_SEARCHPAIR;
1188 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1189 special_fn = CA_SUBSTITUTE;
1190 else
1191 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001192
1193 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001194 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195 goto theend;
1196
1197 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1198 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1199 {
1200 int idx;
1201
1202 // builtin function
1203 idx = find_internal_func(name);
1204 if (idx >= 0)
1205 {
1206 if (STRCMP(name, "flatten") == 0)
1207 {
1208 emsg(_(e_cannot_use_flatten_in_vim9_script));
1209 goto theend;
1210 }
1211
1212 if (STRCMP(name, "add") == 0 && argcount == 2)
1213 {
h-eastb895b0f2023-09-24 15:46:31 +02001214 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001215 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1216 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001217
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 // add() can be compiled to instructions if we know the type
1219 if (type->tt_type == VAR_LIST)
1220 {
1221 // inline "add(list, item)" so that the type can be checked
1222 res = generate_LISTAPPEND(cctx);
1223 idx = -1;
1224 }
1225 else if (type->tt_type == VAR_BLOB)
1226 {
1227 // inline "add(blob, nr)" so that the type can be checked
1228 res = generate_BLOBAPPEND(cctx);
1229 idx = -1;
1230 }
1231 }
1232
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001233 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1234 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001235 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001236 // May have the "D" or "R" flag, reserve a variable for a
1237 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001238 if (get_defer_var_idx(cctx) == 0)
1239 idx = -1;
1240 }
1241
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001242 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1243 if (STRCMP(name, "string") == 0)
1244 builtin_method = CLASS_BUILTIN_STRING;
1245 else if (STRCMP(name, "empty") == 0)
1246 builtin_method = CLASS_BUILTIN_EMPTY;
1247 else if (STRCMP(name, "len") == 0)
1248 builtin_method = CLASS_BUILTIN_LEN;
1249 if (builtin_method != CLASS_BUILTIN_INVALID)
1250 {
1251 res = compile_builtin_method_call(cctx, builtin_method);
1252 if (res == OK)
1253 idx = -1;
1254 }
1255
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001256 if (idx >= 0)
1257 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1258 }
1259 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001260 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001261 goto theend;
1262 }
1263
Bram Moolenaar62aec932022-01-29 21:45:34 +00001264 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1265
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001266 // An argument or local variable can be a function reference, this
1267 // overrules a function name.
1268 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1269 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1270 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001271 class_T *cl = NULL;
1272 int mi = 0;
1273
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001274 // If we can find the function by name generate the right call.
1275 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001276 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001277 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001279 if (!func_is_global(ufunc))
1280 {
Ernie Rael58c95792024-08-13 23:27:22 +02001281 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001282 goto theend;
1283 }
1284 if (!has_g_namespace
1285 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1286 {
1287 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001288 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001289 goto theend;
1290 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001291 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001292 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001293 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001294 // Class method invocation without the class name.
1295 // A class method can be referenced without the class name only in
1296 // the class where the function is defined.
1297 if (cctx->ctx_ufunc->uf_defclass == cl)
1298 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001299 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001300 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001301 }
1302 else
1303 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001304 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001305 name, cl->class_name);
1306 res = FAIL;
1307 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001308 goto theend;
1309 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310 }
1311
1312 // If the name is a variable, load it and use PCALL.
1313 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001314 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001316 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1318 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001319 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320
Christian Brabandtd5475e82023-08-17 23:34:09 +02001321 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322 goto theend;
1323 }
1324
1325 // If we can find a global function by name generate the right call.
1326 if (ufunc != NULL)
1327 {
Ernie Rael58c95792024-08-13 23:27:22 +02001328 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001329 goto theend;
1330 }
1331
1332 // A global function may be defined only later. Need to figure out at
1333 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001334 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001335 res = generate_UCALL(cctx, name, argcount);
1336 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001337 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338
1339theend:
1340 vim_free(tofree);
1341 return res;
1342}
1343
1344// like NAMESPACE_CHAR but with 'a' and 'l'.
1345#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1346
1347/*
1348 * Find the end of a variable or function name. Unlike find_name_end() this
1349 * does not recognize magic braces.
1350 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1351 * Return a pointer to just after the name. Equal to "arg" if there is no
1352 * valid name.
1353 */
1354 char_u *
1355to_name_end(char_u *arg, int use_namespace)
1356{
1357 char_u *p;
1358
1359 // Quick check for valid starting character.
1360 if (!eval_isnamec1(*arg))
1361 return arg;
1362
1363 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1364 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1365 // and can be used in slice "[n:]".
1366 if (*p == ':' && (p != arg + 1
1367 || !use_namespace
1368 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1369 break;
1370 return p;
1371}
1372
1373/*
1374 * Like to_name_end() but also skip over a list or dict constant.
1375 * Also accept "<SNR>123_Func".
1376 * This intentionally does not handle line continuation.
1377 */
1378 char_u *
1379to_name_const_end(char_u *arg)
1380{
1381 char_u *p = arg;
1382 typval_T rettv;
1383
1384 if (STRNCMP(p, "<SNR>", 5) == 0)
1385 p = skipdigits(p + 5);
1386 p = to_name_end(p, TRUE);
1387 if (p == arg && *arg == '[')
1388 {
1389
1390 // Can be "[1, 2, 3]->Func()".
1391 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1392 p = arg;
1393 }
1394 return p;
1395}
1396
1397/*
1398 * parse a list: [expr, expr]
1399 * "*arg" points to the '['.
1400 * ppconst->pp_is_const is set if all items are a constant.
1401 */
1402 static int
1403compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1404{
1405 char_u *p = skipwhite(*arg + 1);
1406 char_u *whitep = *arg + 1;
1407 int count = 0;
1408 int is_const;
1409 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001410 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001411
1412 for (;;)
1413 {
1414 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1415 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001416 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001417 return FAIL;
1418 }
1419 if (*p == ',')
1420 {
1421 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1422 return FAIL;
1423 }
1424 if (*p == ']')
1425 {
1426 ++p;
1427 break;
1428 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001429 if (must_end)
1430 {
1431 semsg(_(e_missing_comma_in_list_str), p);
1432 return FAIL;
1433 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001434 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1435 return FAIL;
1436 if (!is_const)
1437 is_all_const = FALSE;
1438 ++count;
1439 if (*p == ',')
1440 {
1441 ++p;
1442 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1443 {
1444 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1445 return FAIL;
1446 }
1447 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001448 else
1449 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450 whitep = p;
1451 p = skipwhite(p);
1452 }
1453 *arg = p;
1454
1455 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001456 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001457}
1458
1459/*
1460 * Parse a lambda: "(arg, arg) => expr"
1461 * "*arg" points to the '('.
1462 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1463 */
1464 static int
1465compile_lambda(char_u **arg, cctx_T *cctx)
1466{
1467 int r;
1468 typval_T rettv;
1469 ufunc_T *ufunc;
1470 evalarg_T evalarg;
1471
1472 init_evalarg(&evalarg);
1473 evalarg.eval_flags = EVAL_EVALUATE;
1474 evalarg.eval_cctx = cctx;
1475
1476 // Get the funcref in "rettv".
1477 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1478 if (r != OK)
1479 {
1480 clear_evalarg(&evalarg, NULL);
1481 return r;
1482 }
1483
1484 // "rettv" will now be a partial referencing the function.
1485 ufunc = rettv.vval.v_partial->pt_func;
1486 ++ufunc->uf_refcount;
1487 clear_tv(&rettv);
1488
1489 // Compile it here to get the return type. The return type is optional,
1490 // when it's missing use t_unknown. This is recognized in
1491 // compile_return().
1492 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1493 ufunc->uf_ret_type = &t_unknown;
1494 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1495
1496 // When the outer function is compiled for profiling or debugging, the
1497 // lambda may be called without profiling or debugging. Compile it here in
1498 // the right context.
1499 if (cctx->ctx_compile_type == CT_DEBUG
1500#ifdef FEAT_PROFILE
1501 || cctx->ctx_compile_type == CT_PROFILE
1502#endif
1503 )
1504 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1505
Bram Moolenaar139575d2022-03-15 19:29:30 +00001506 // if the outer function is not compiled for debugging or profiling, this
1507 // one might be
1508 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001509 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001510 compiletype_T compile_type = get_compile_type(ufunc);
1511
1512 if (compile_type != CT_NONE)
1513 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001514 }
1515
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001516 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1517 // "*arg" may point into it. Point into the original line to avoid a
1518 // dangling pointer.
1519 if (evalarg.eval_using_cmdline)
1520 {
1521 garray_T *gap = &evalarg.eval_tofree_ga;
1522 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1523
1524 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1525 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001526 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001527 }
1528
1529 clear_evalarg(&evalarg, NULL);
1530
1531 if (ufunc->uf_def_status == UF_COMPILED)
1532 {
1533 // The return type will now be known.
1534 set_function_type(ufunc);
1535
1536 // The function reference count will be 1. When the ISN_FUNCREF
1537 // instruction is deleted the reference count is decremented and the
1538 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001539 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001540 }
1541
1542 func_ptr_unref(ufunc);
1543 return FAIL;
1544}
1545
1546/*
1547 * Get a lambda and compile it. Uses Vim9 syntax.
1548 */
1549 int
1550get_lambda_tv_and_compile(
1551 char_u **arg,
1552 typval_T *rettv,
1553 int types_optional,
1554 evalarg_T *evalarg)
1555{
1556 int r;
1557 ufunc_T *ufunc;
1558 int save_sc_version = current_sctx.sc_version;
1559
1560 // Get the funcref in "rettv".
1561 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1562 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1563 current_sctx.sc_version = save_sc_version;
1564 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001565 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566
1567 // "rettv" will now be a partial referencing the function.
1568 ufunc = rettv->vval.v_partial->pt_func;
1569
1570 // Compile it here to get the return type. The return type is optional,
1571 // when it's missing use t_unknown. This is recognized in
1572 // compile_return().
1573 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1574 ufunc->uf_ret_type = &t_unknown;
1575 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1576
1577 if (ufunc->uf_def_status == UF_COMPILED)
1578 {
1579 // The return type will now be known.
1580 set_function_type(ufunc);
1581 return OK;
1582 }
1583 clear_tv(rettv);
1584 return FAIL;
1585}
1586
1587/*
1588 * parse a dict: {key: val, [key]: val}
1589 * "*arg" points to the '{'.
1590 * ppconst->pp_is_const is set if all item values are a constant.
1591 */
1592 static int
1593compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1594{
1595 garray_T *instr = &cctx->ctx_instr;
1596 int count = 0;
1597 dict_T *d = dict_alloc();
1598 dictitem_T *item;
1599 char_u *whitep = *arg + 1;
1600 char_u *p;
1601 int is_const;
1602 int is_all_const = TRUE; // reset when non-const encountered
1603
1604 if (d == NULL)
1605 return FAIL;
1606 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001607 {
1608 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001609 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001610 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001611 for (;;)
1612 {
1613 char_u *key = NULL;
1614
1615 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1616 {
1617 *arg = NULL;
1618 goto failret;
1619 }
1620
1621 if (**arg == '}')
1622 break;
1623
1624 if (**arg == '[')
1625 {
1626 isn_T *isn;
1627
1628 // {[expr]: value} uses an evaluated key.
1629 *arg = skipwhite(*arg + 1);
1630 if (compile_expr0(arg, cctx) == FAIL)
1631 return FAIL;
1632 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1633 if (isn->isn_type == ISN_PUSHNR)
1634 {
1635 char buf[NUMBUFLEN];
1636
1637 // Convert to string at compile time.
1638 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1639 isn->isn_type = ISN_PUSHS;
1640 isn->isn_arg.string = vim_strsave((char_u *)buf);
1641 }
1642 if (isn->isn_type == ISN_PUSHS)
1643 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001644 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645 return FAIL;
1646 *arg = skipwhite(*arg);
1647 if (**arg != ']')
1648 {
1649 emsg(_(e_missing_matching_bracket_after_dict_key));
1650 return FAIL;
1651 }
1652 ++*arg;
1653 }
1654 else
1655 {
1656 // {"name": value},
1657 // {'name': value},
1658 // {name: value} use "name" as a literal key
1659 key = get_literal_key(arg);
1660 if (key == NULL)
1661 return FAIL;
1662 if (generate_PUSHS(cctx, &key) == FAIL)
1663 return FAIL;
1664 }
1665
1666 // Check for duplicate keys, if using string keys.
1667 if (key != NULL)
1668 {
1669 item = dict_find(d, key, -1);
1670 if (item != NULL)
1671 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001672 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001673 goto failret;
1674 }
1675 item = dictitem_alloc(key);
1676 if (item != NULL)
1677 {
1678 item->di_tv.v_type = VAR_UNKNOWN;
1679 item->di_tv.v_lock = 0;
1680 if (dict_add(d, item) == FAIL)
1681 dictitem_free(item);
1682 }
1683 }
1684
1685 if (**arg != ':')
1686 {
1687 if (*skipwhite(*arg) == ':')
1688 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1689 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001690 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691 return FAIL;
1692 }
1693 whitep = *arg + 1;
1694 if (!IS_WHITE_OR_NUL(*whitep))
1695 {
1696 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1697 return FAIL;
1698 }
1699
1700 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1701 {
1702 *arg = NULL;
1703 goto failret;
1704 }
1705
1706 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1707 return FAIL;
1708 if (!is_const)
1709 is_all_const = FALSE;
1710 ++count;
1711
1712 whitep = *arg;
1713 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1714 {
1715 *arg = NULL;
1716 goto failret;
1717 }
1718 if (**arg == '}')
1719 break;
1720 if (**arg != ',')
1721 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001722 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001723 goto failret;
1724 }
1725 if (IS_WHITE_OR_NUL(*whitep))
1726 {
1727 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1728 return FAIL;
1729 }
1730 whitep = *arg + 1;
1731 if (!IS_WHITE_OR_NUL(*whitep))
1732 {
1733 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1734 return FAIL;
1735 }
1736 *arg = skipwhite(whitep);
1737 }
1738
1739 *arg = *arg + 1;
1740
1741 // Allow for following comment, after at least one space.
1742 p = skipwhite(*arg);
1743 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1744 *arg += STRLEN(*arg);
1745
1746 dict_unref(d);
1747 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001748 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001749
1750failret:
1751 if (*arg == NULL)
1752 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001753 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001754 *arg = (char_u *)"";
1755 }
1756 dict_unref(d);
1757 return FAIL;
1758}
1759
1760/*
1761 * Compile "&option".
1762 */
1763 static int
1764compile_get_option(char_u **arg, cctx_T *cctx)
1765{
1766 typval_T rettv;
1767 char_u *start = *arg;
1768 int ret;
1769
1770 // parse the option and get the current value to get the type.
1771 rettv.v_type = VAR_UNKNOWN;
1772 ret = eval_option(arg, &rettv, TRUE);
1773 if (ret == OK)
1774 {
1775 // include the '&' in the name, eval_option() expects it.
1776 char_u *name = vim_strnsave(start, *arg - start);
1777 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1778 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1779
1780 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1781 vim_free(name);
1782 }
1783 clear_tv(&rettv);
1784
1785 return ret;
1786}
1787
1788/*
1789 * Compile "$VAR".
1790 */
1791 static int
1792compile_get_env(char_u **arg, cctx_T *cctx)
1793{
1794 char_u *start = *arg;
1795 int len;
1796 int ret;
1797 char_u *name;
1798
1799 ++*arg;
1800 len = get_env_len(arg);
1801 if (len == 0)
1802 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001803 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804 return FAIL;
1805 }
1806
1807 // include the '$' in the name, eval_env_var() expects it.
1808 name = vim_strnsave(start, len + 1);
1809 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1810 vim_free(name);
1811 return ret;
1812}
1813
1814/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001815 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001816 */
1817 static int
1818compile_interp_string(char_u **arg, cctx_T *cctx)
1819{
1820 typval_T tv;
1821 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001822 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001823 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001824 int count = 0;
1825 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001826
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001827 // *arg is on the '$' character, move it to the first string character.
1828 ++*arg;
1829 quote = **arg;
1830 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001831
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001832 for (;;)
1833 {
1834 // Get the string up to the matching quote or to a single '{'.
1835 // "arg" is advanced to either the quote or the '{'.
1836 if (quote == '"')
1837 ret = eval_string(arg, &tv, evaluate, TRUE);
1838 else
1839 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1840 if (ret == FAIL)
1841 break;
1842 if (evaluate)
1843 {
1844 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1845 || (**arg != '{' && count == 0))
1846 {
1847 // generate non-empty string or empty string if it's the only
1848 // one
1849 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1850 return FAIL;
1851 tv.vval.v_string = NULL; // don't free it now
1852 ++count;
1853 }
1854 clear_tv(&tv);
1855 }
1856
1857 if (**arg != '{')
1858 {
1859 // found terminating quote
1860 ++*arg;
1861 break;
1862 }
1863
1864 p = compile_one_expr_in_str(*arg, cctx);
1865 if (p == NULL)
1866 {
1867 ret = FAIL;
1868 break;
1869 }
1870 ++count;
1871 *arg = p;
1872 }
LemonBoy2eaef102022-05-06 13:14:50 +01001873
1874 if (ret == FAIL || !evaluate)
1875 return ret;
1876
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001877 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1878 if (count > 1)
1879 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001880
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001881 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001882}
1883
1884/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001885 * Compile "@r".
1886 */
1887 static int
1888compile_get_register(char_u **arg, cctx_T *cctx)
1889{
1890 int ret;
1891
1892 ++*arg;
1893 if (**arg == NUL)
1894 {
1895 semsg(_(e_syntax_error_at_str), *arg - 1);
1896 return FAIL;
1897 }
1898 if (!valid_yank_reg(**arg, FALSE))
1899 {
1900 emsg_invreg(**arg);
1901 return FAIL;
1902 }
1903 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1904 ++*arg;
1905 return ret;
1906}
1907
1908/*
1909 * Apply leading '!', '-' and '+' to constant "rettv".
1910 * When "numeric_only" is TRUE do not apply '!'.
1911 */
1912 static int
1913apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1914{
1915 char_u *p = *end;
1916
1917 // this works from end to start
1918 while (p > start)
1919 {
1920 --p;
1921 if (*p == '-' || *p == '+')
1922 {
1923 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001924 if (rettv->v_type == VAR_FLOAT)
1925 {
1926 if (*p == '-')
1927 rettv->vval.v_float = -rettv->vval.v_float;
1928 }
1929 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001930 {
1931 varnumber_T val;
1932 int error = FALSE;
1933
1934 // tv_get_number_chk() accepts a string, but we don't want that
1935 // here
1936 if (check_not_string(rettv) == FAIL)
1937 return FAIL;
1938 val = tv_get_number_chk(rettv, &error);
1939 clear_tv(rettv);
1940 if (error)
1941 return FAIL;
1942 if (*p == '-')
1943 val = -val;
1944 rettv->v_type = VAR_NUMBER;
1945 rettv->vval.v_number = val;
1946 }
1947 }
1948 else if (numeric_only)
1949 {
1950 ++p;
1951 break;
1952 }
1953 else if (*p == '!')
1954 {
1955 int v = tv2bool(rettv);
1956
1957 // '!' is permissive in the type.
1958 clear_tv(rettv);
1959 rettv->v_type = VAR_BOOL;
1960 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1961 }
1962 }
1963 *end = p;
1964 return OK;
1965}
1966
1967/*
1968 * Recognize v: variables that are constants and set "rettv".
1969 */
1970 static void
1971get_vim_constant(char_u **arg, typval_T *rettv)
1972{
1973 if (STRNCMP(*arg, "v:true", 6) == 0)
1974 {
1975 rettv->v_type = VAR_BOOL;
1976 rettv->vval.v_number = VVAL_TRUE;
1977 *arg += 6;
1978 }
1979 else if (STRNCMP(*arg, "v:false", 7) == 0)
1980 {
1981 rettv->v_type = VAR_BOOL;
1982 rettv->vval.v_number = VVAL_FALSE;
1983 *arg += 7;
1984 }
1985 else if (STRNCMP(*arg, "v:null", 6) == 0)
1986 {
1987 rettv->v_type = VAR_SPECIAL;
1988 rettv->vval.v_number = VVAL_NULL;
1989 *arg += 6;
1990 }
1991 else if (STRNCMP(*arg, "v:none", 6) == 0)
1992 {
1993 rettv->v_type = VAR_SPECIAL;
1994 rettv->vval.v_number = VVAL_NONE;
1995 *arg += 6;
1996 }
1997}
1998
1999 exprtype_T
2000get_compare_type(char_u *p, int *len, int *type_is)
2001{
2002 exprtype_T type = EXPR_UNKNOWN;
2003 int i;
2004
2005 switch (p[0])
2006 {
2007 case '=': if (p[1] == '=')
2008 type = EXPR_EQUAL;
2009 else if (p[1] == '~')
2010 type = EXPR_MATCH;
2011 break;
2012 case '!': if (p[1] == '=')
2013 type = EXPR_NEQUAL;
2014 else if (p[1] == '~')
2015 type = EXPR_NOMATCH;
2016 break;
2017 case '>': if (p[1] != '=')
2018 {
2019 type = EXPR_GREATER;
2020 *len = 1;
2021 }
2022 else
2023 type = EXPR_GEQUAL;
2024 break;
2025 case '<': if (p[1] != '=')
2026 {
2027 type = EXPR_SMALLER;
2028 *len = 1;
2029 }
2030 else
2031 type = EXPR_SEQUAL;
2032 break;
2033 case 'i': if (p[1] == 's')
2034 {
2035 // "is" and "isnot"; but not a prefix of a name
2036 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2037 *len = 5;
2038 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002039 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 {
2041 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2042 *type_is = TRUE;
2043 }
2044 }
2045 break;
2046 }
2047 return type;
2048}
2049
2050/*
2051 * Skip over an expression, ignoring most errors.
2052 */
2053 void
2054skip_expr_cctx(char_u **arg, cctx_T *cctx)
2055{
2056 evalarg_T evalarg;
2057
2058 init_evalarg(&evalarg);
2059 evalarg.eval_cctx = cctx;
2060 skip_expr(arg, &evalarg);
2061 clear_evalarg(&evalarg, NULL);
2062}
2063
2064/*
2065 * Check that the top of the type stack has a type that can be used as a
2066 * condition. Give an error and return FAIL if not.
2067 */
2068 int
2069bool_on_stack(cctx_T *cctx)
2070{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002071 type_T *type;
2072
Bram Moolenaar078a4612022-01-04 15:17:03 +00002073 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002074 if (type == &t_bool)
2075 return OK;
2076
Bram Moolenaar4913d422022-10-17 13:13:32 +01002077 if (type->tt_type == VAR_ANY
2078 || type->tt_type == VAR_UNKNOWN
2079 || type->tt_type == VAR_NUMBER
2080 || type == &t_number_bool
2081 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2083 // This requires a runtime type check.
2084 return generate_COND2BOOL(cctx);
2085
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002086 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002087}
2088
2089/*
2090 * Give the "white on both sides" error, taking the operator from "p[len]".
2091 */
2092 void
2093error_white_both(char_u *op, int len)
2094{
2095 char_u buf[10];
2096
2097 vim_strncpy(buf, op, len);
2098 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2099}
2100
2101/*
2102 * Compile code to apply '-', '+' and '!'.
2103 * When "numeric_only" is TRUE do not apply '!'.
2104 */
2105 static int
2106compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2107{
2108 char_u *p = *end;
2109
2110 // this works from end to start
2111 while (p > start)
2112 {
2113 --p;
2114 while (VIM_ISWHITE(*p))
2115 --p;
2116 if (*p == '-' || *p == '+')
2117 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002118 type_T *type = get_type_on_stack(cctx, 0);
2119 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002120 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002121 return FAIL;
2122
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002124 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2125 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002126 }
2127 else if (numeric_only)
2128 {
2129 ++p;
2130 break;
2131 }
2132 else
2133 {
2134 int invert = *p == '!';
2135
2136 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2137 {
2138 if (p[-1] == '!')
2139 invert = !invert;
2140 --p;
2141 }
2142 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2143 return FAIL;
2144 }
2145 }
2146 *end = p;
2147 return OK;
2148}
2149
2150/*
2151 * Compile "(expression)": recursive!
2152 * Return FAIL/OK.
2153 */
2154 static int
2155compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2156{
2157 int ret;
2158 char_u *p = *arg + 1;
2159
2160 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2161 return FAIL;
2162 if (ppconst->pp_used <= PPSIZE - 10)
2163 {
2164 ret = compile_expr1(arg, cctx, ppconst);
2165 }
2166 else
2167 {
2168 // Not enough space in ppconst, flush constants.
2169 if (generate_ppconst(cctx, ppconst) == FAIL)
2170 return FAIL;
2171 ret = compile_expr0(arg, cctx);
2172 }
2173 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2174 return FAIL;
2175 if (**arg == ')')
2176 ++*arg;
2177 else if (ret == OK)
2178 {
2179 emsg(_(e_missing_closing_paren));
2180 ret = FAIL;
2181 }
2182 return ret;
2183}
2184
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002185static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002186
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187/*
2188 * Compile whatever comes after "name" or "name()".
2189 * Advances "*arg" only when something was recognized.
2190 */
2191 static int
2192compile_subscript(
2193 char_u **arg,
2194 cctx_T *cctx,
2195 char_u *start_leader,
2196 char_u **end_leader,
2197 ppconst_T *ppconst)
2198{
2199 char_u *name_start = *end_leader;
2200 int keeping_dict = FALSE;
2201
2202 for (;;)
2203 {
2204 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002205 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206
2207 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2208 {
2209 char_u *next = peek_next_line_from_context(cctx);
2210
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002211 // If a following line starts with "->{", "->(" or "->X" advance to
2212 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002213 // Also if a following line starts with ".x".
2214 if (next != NULL &&
2215 ((next[0] == '-' && next[1] == '>'
2216 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002217 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002218 || ASCII_ISALPHA(*skipwhite(next + 2))))
2219 || (next[0] == '.' && eval_isdictc(next[1]))))
2220 {
2221 next = next_line_from_context(cctx, TRUE);
2222 if (next == NULL)
2223 return FAIL;
2224 *arg = next;
2225 p = skipwhite(*arg);
2226 }
2227 }
2228
2229 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2230 // is not a function call.
2231 if (**arg == '(')
2232 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233 int argcount = 0;
2234
2235 if (generate_ppconst(cctx, ppconst) == FAIL)
2236 return FAIL;
2237 ppconst->pp_is_const = FALSE;
2238
2239 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002240 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241
2242 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002243 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002244 return FAIL;
2245 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2246 return FAIL;
2247 if (keeping_dict)
2248 {
2249 keeping_dict = FALSE;
2250 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2251 return FAIL;
2252 }
2253 }
2254 else if (*p == '-' && p[1] == '>')
2255 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002256 char_u *pstart = p;
2257 int alt;
2258 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002259
Bram Moolenaarc7349932022-01-16 20:59:39 +00002260 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261 if (generate_ppconst(cctx, ppconst) == FAIL)
2262 return FAIL;
2263 ppconst->pp_is_const = FALSE;
2264
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002265 // Apply the '!', '-' and '+' first:
2266 // -1.0->func() works like (-1.0)->func()
2267 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2268 return FAIL;
2269
2270 p += 2;
2271 *arg = skipwhite(p);
2272 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002273
2274 // Three alternatives handled here:
2275 // 1. "base->name(" only a name, use compile_call()
2276 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2277 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002279 alt = 2;
2280 else
2281 {
2282 // alternative 1 or 3
2283 p = *arg;
2284 if (!eval_isnamec1(*p))
2285 {
2286 semsg(_(e_trailing_characters_str), pstart);
2287 return FAIL;
2288 }
2289 if (ASCII_ISALPHA(*p) && p[1] == ':')
2290 p += 2;
2291 for ( ; eval_isnamec(*p); ++p)
2292 ;
2293 if (*p == '(')
2294 {
2295 // alternative 1
2296 alt = 1;
2297 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2298 return FAIL;
2299 }
2300 else
2301 {
2302 // Must be alternative 3, find the "(". Only works within
2303 // one line.
2304 alt = 3;
2305 paren = vim_strchr(p, '(');
2306 if (paren == NULL)
2307 {
2308 semsg(_(e_missing_parenthesis_str), *arg);
2309 return FAIL;
2310 }
2311 }
2312 }
2313
2314 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002315 {
2316 int argcount = 1;
2317 garray_T *stack = &cctx->ctx_type_stack;
2318 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002319 int expr_isn_start = cctx->ctx_instr.ga_len;
2320 int expr_isn_end;
2321 int arg_isn_count;
2322
Bram Moolenaarc7349932022-01-16 20:59:39 +00002323 if (alt == 2)
2324 {
2325 // Funcref call: list->(Refs[2])(arg)
2326 // or lambda: list->((arg) => expr)(arg)
2327 //
2328 // Fist compile the function expression.
2329 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2330 return FAIL;
2331 }
2332 else
2333 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002334 int fail;
2335 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002336 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002337
Bram Moolenaarc7349932022-01-16 20:59:39 +00002338 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002339
2340 // instead of using LOADG for "import.Func" use PUSHFUNC
2341 ++paren_follows_after_expr;
2342
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002343 // do not look in the next line
2344 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002345
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002346 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002347 || *skipwhite(*arg) != NUL;
2348 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002349 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002350 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002351
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002352 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002353 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002354 if (did_emsg == prev_did_emsg)
2355 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002356 return FAIL;
2357 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002358 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002360 // Compile the arguments.
2361 if (**arg != '(')
2362 {
2363 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002364 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002365 else
2366 semsg(_(e_missing_parenthesis_str), *arg);
2367 return FAIL;
2368 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002369
2370 // Remember the next instruction index, where the instructions
2371 // for arguments are being written.
2372 expr_isn_end = cctx->ctx_instr.ga_len;
2373
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002374 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002375 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2376 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002377 return FAIL;
2378
2379 // Move the instructions for the arguments to before the
2380 // instructions of the expression and move the type of the
2381 // expression after the argument types. This is what ISN_PCALL
2382 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2384 if (arg_isn_count > 0)
2385 {
2386 int expr_isn_count = expr_isn_end - expr_isn_start;
2387 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002388 type_T *decl_type;
2389 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002390
2391 if (isn == NULL)
2392 return FAIL;
2393 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2394 + expr_isn_start,
2395 sizeof(isn_T) * expr_isn_count);
2396 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2397 + expr_isn_start,
2398 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2399 sizeof(isn_T) * arg_isn_count);
2400 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2401 + expr_isn_start + arg_isn_count,
2402 isn, sizeof(isn_T) * expr_isn_count);
2403 vim_free(isn);
2404
Bram Moolenaar078a4612022-01-04 15:17:03 +00002405 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2406 type = typep->type_curr;
2407 decl_type = typep->type_decl;
2408 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2409 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2410 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002411 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002412 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2413 typep->type_curr = type;
2414 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002415 }
2416
Bram Moolenaar078a4612022-01-04 15:17:03 +00002417 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002418 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2419 return FAIL;
2420 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002421
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422 if (keeping_dict)
2423 {
2424 keeping_dict = FALSE;
2425 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2426 return FAIL;
2427 }
2428 }
2429 else if (**arg == '[')
2430 {
2431 int is_slice = FALSE;
2432
2433 // list index: list[123]
2434 // dict member: dict[key]
2435 // string index: text[123]
2436 // blob index: blob[123]
2437 if (generate_ppconst(cctx, ppconst) == FAIL)
2438 return FAIL;
2439 ppconst->pp_is_const = FALSE;
2440
2441 ++p;
2442 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2443 return FAIL;
2444 if (**arg == ':')
2445 {
2446 // missing first index is equal to zero
2447 generate_PUSHNR(cctx, 0);
2448 }
2449 else
2450 {
2451 if (compile_expr0(arg, cctx) == FAIL)
2452 return FAIL;
2453 if (**arg == ':')
2454 {
2455 semsg(_(e_white_space_required_before_and_after_str_at_str),
2456 ":", *arg);
2457 return FAIL;
2458 }
2459 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2460 return FAIL;
2461 *arg = skipwhite(*arg);
2462 }
2463 if (**arg == ':')
2464 {
2465 is_slice = TRUE;
2466 ++*arg;
2467 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2468 {
2469 semsg(_(e_white_space_required_before_and_after_str_at_str),
2470 ":", *arg);
2471 return FAIL;
2472 }
2473 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2474 return FAIL;
2475 if (**arg == ']')
2476 // missing second index is equal to end of string
2477 generate_PUSHNR(cctx, -1);
2478 else
2479 {
2480 if (compile_expr0(arg, cctx) == FAIL)
2481 return FAIL;
2482 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2483 return FAIL;
2484 *arg = skipwhite(*arg);
2485 }
2486 }
2487
2488 if (**arg != ']')
2489 {
2490 emsg(_(e_missing_closing_square_brace));
2491 return FAIL;
2492 }
2493 *arg = *arg + 1;
2494
2495 if (keeping_dict)
2496 {
2497 keeping_dict = FALSE;
2498 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2499 return FAIL;
2500 }
2501 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2502 return FAIL;
2503 }
2504 else if (*p == '.' && p[1] != '.')
2505 {
2506 // dictionary member: dict.name
2507 if (generate_ppconst(cctx, ppconst) == FAIL)
2508 return FAIL;
2509 ppconst->pp_is_const = FALSE;
2510
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002511 type = get_type_on_stack(cctx, 0);
2512 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002513 && (type->tt_type == VAR_CLASS
2514 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002516 // class member: SomeClass.varname
2517 // class method: SomeClass.SomeMethod()
2518 // class constructor: SomeClass.new()
2519 // object member: someObject.varname, this.varname
2520 // object method: someObject.SomeMethod(), this.SomeMethod()
2521 *arg = p;
2522 if (compile_class_object_index(cctx, arg, type) == FAIL)
2523 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002525 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002526 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002527 *arg = p + 1;
2528 if (IS_WHITE_OR_NUL(**arg))
2529 {
2530 emsg(_(e_missing_name_after_dot));
2531 return FAIL;
2532 }
2533 p = *arg;
2534 if (eval_isdictc(*p))
2535 while (eval_isnamec(*p))
2536 MB_PTR_ADV(p);
2537 if (p == *arg)
2538 {
2539 semsg(_(e_syntax_error_at_str), *arg);
2540 return FAIL;
2541 }
2542 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2543 return FAIL;
2544 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2545 return FAIL;
2546 keeping_dict = TRUE;
2547 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002548 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 }
2550 else
2551 break;
2552 }
2553
2554 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2555 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002556 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2557 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002558 return FAIL;
2559
2560 return OK;
2561}
2562
2563/*
2564 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2565 * "arg" is advanced until after the expression, skipping white space.
2566 *
2567 * If the value is a constant "ppconst->pp_used" will be non-zero.
2568 * Before instructions are generated, any values in "ppconst" will generated.
2569 *
2570 * This is the compiling equivalent of eval1(), eval2(), etc.
2571 */
2572
2573/*
2574 * number number constant
2575 * 0zFFFFFFFF Blob constant
2576 * "string" string constant
2577 * 'string' literal string constant
2578 * &option-name option value
2579 * @r register contents
2580 * identifier variable value
2581 * function() function call
2582 * $VAR environment variable
2583 * (expression) nested expression
2584 * [expr, expr] List
2585 * {key: val, [key]: val} Dictionary
2586 *
2587 * Also handle:
2588 * ! in front logical NOT
2589 * - in front unary minus
2590 * + in front unary plus (ignored)
2591 * trailing (arg) funcref/partial call
2592 * trailing [] subscript in String or List
2593 * trailing .name entry in Dictionary
2594 * trailing ->name() method call
2595 */
2596 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002597compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002598 char_u **arg,
2599 cctx_T *cctx,
2600 ppconst_T *ppconst)
2601{
2602 char_u *start_leader, *end_leader;
2603 int ret = OK;
2604 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2605 int used_before = ppconst->pp_used;
2606
2607 ppconst->pp_is_const = FALSE;
2608
2609 /*
2610 * Skip '!', '-' and '+' characters. They are handled later.
2611 */
2612 start_leader = *arg;
2613 if (eval_leader(arg, TRUE) == FAIL)
2614 return FAIL;
2615 end_leader = *arg;
2616
2617 rettv->v_type = VAR_UNKNOWN;
2618 switch (**arg)
2619 {
2620 /*
2621 * Number constant.
2622 */
2623 case '0': // also for blob starting with 0z
2624 case '1':
2625 case '2':
2626 case '3':
2627 case '4':
2628 case '5':
2629 case '6':
2630 case '7':
2631 case '8':
2632 case '9':
2633 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2634 return FAIL;
2635 // Apply "-" and "+" just before the number now, right to
2636 // left. Matters especially when "->" follows. Stops at
2637 // '!'.
2638 if (apply_leader(rettv, TRUE,
2639 start_leader, &end_leader) == FAIL)
2640 {
2641 clear_tv(rettv);
2642 return FAIL;
2643 }
2644 break;
2645
2646 /*
2647 * String constant: "string".
2648 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002649 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650 return FAIL;
2651 break;
2652
2653 /*
2654 * Literal string constant: 'str''ing'.
2655 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002656 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002657 return FAIL;
2658 break;
2659
2660 /*
2661 * Constant Vim variable.
2662 */
2663 case 'v': get_vim_constant(arg, rettv);
2664 ret = NOTDONE;
2665 break;
2666
2667 /*
2668 * "true" constant
2669 */
2670 case 't': if (STRNCMP(*arg, "true", 4) == 0
2671 && !eval_isnamec((*arg)[4]))
2672 {
2673 *arg += 4;
2674 rettv->v_type = VAR_BOOL;
2675 rettv->vval.v_number = VVAL_TRUE;
2676 }
2677 else
2678 ret = NOTDONE;
2679 break;
2680
2681 /*
2682 * "false" constant
2683 */
2684 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2685 && !eval_isnamec((*arg)[5]))
2686 {
2687 *arg += 5;
2688 rettv->v_type = VAR_BOOL;
2689 rettv->vval.v_number = VVAL_FALSE;
2690 }
2691 else
2692 ret = NOTDONE;
2693 break;
2694
2695 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002696 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002698 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002699 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002700 char_u *p = *arg + 4;
2701 int len;
2702
2703 for (len = 0; eval_isnamec(p[len]); ++len)
2704 ;
2705 ret = handle_predefined(*arg, len + 4, rettv);
2706 if (ret == FAIL)
2707 ret = NOTDONE;
2708 else
2709 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002710 }
2711 else
2712 ret = NOTDONE;
2713 break;
2714
2715 /*
2716 * List: [expr, expr]
2717 */
2718 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2719 return FAIL;
2720 ret = compile_list(arg, cctx, ppconst);
2721 break;
2722
2723 /*
2724 * Dictionary: {'key': val, 'key': val}
2725 */
2726 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2727 return FAIL;
2728 ret = compile_dict(arg, cctx, ppconst);
2729 break;
2730
2731 /*
2732 * Option value: &name
2733 */
2734 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2735 return FAIL;
2736 ret = compile_get_option(arg, cctx);
2737 break;
2738
2739 /*
2740 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002741 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002742 */
2743 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2744 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002745 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2746 ret = compile_interp_string(arg, cctx);
2747 else
2748 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002749 break;
2750
2751 /*
2752 * Register contents: @r.
2753 */
2754 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2755 return FAIL;
2756 ret = compile_get_register(arg, cctx);
2757 break;
2758 /*
2759 * nested expression: (expression).
2760 * lambda: (arg, arg) => expr
2761 * funcref: (arg, arg) => { statement }
2762 */
2763 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2764 ret = compile_lambda(arg, cctx);
2765 if (ret == NOTDONE)
2766 ret = compile_parenthesis(arg, cctx, ppconst);
2767 break;
2768
2769 default: ret = NOTDONE;
2770 break;
2771 }
2772 if (ret == FAIL)
2773 return FAIL;
2774
2775 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2776 {
2777 if (cctx->ctx_skip == SKIP_YES)
2778 clear_tv(rettv);
2779 else
2780 // A constant expression can possibly be handled compile time,
2781 // return the value instead of generating code.
2782 ++ppconst->pp_used;
2783 }
2784 else if (ret == NOTDONE)
2785 {
2786 char_u *p;
2787 int r;
2788
2789 if (!eval_isnamec1(**arg))
2790 {
2791 if (!vim9_bad_comment(*arg))
2792 {
2793 if (ends_excmd(*skipwhite(*arg)))
2794 semsg(_(e_empty_expression_str), *arg);
2795 else
2796 semsg(_(e_name_expected_str), *arg);
2797 }
2798 return FAIL;
2799 }
2800
2801 // "name" or "name()"
2802 p = to_name_end(*arg, TRUE);
2803 if (p - *arg == (size_t)1 && **arg == '_')
2804 {
2805 emsg(_(e_cannot_use_underscore_here));
2806 return FAIL;
2807 }
2808
2809 if (*p == '(')
2810 {
2811 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2812 }
2813 else
2814 {
2815 if (cctx->ctx_skip != SKIP_YES
2816 && generate_ppconst(cctx, ppconst) == FAIL)
2817 return FAIL;
2818 r = compile_load(arg, p, cctx, TRUE, TRUE);
2819 }
2820 if (r == FAIL)
2821 return FAIL;
2822 }
2823
2824 // Handle following "[]", ".member", etc.
2825 // Then deal with prefixed '-', '+' and '!', if not done already.
2826 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2827 ppconst) == FAIL)
2828 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002829 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002830 {
2831 // apply the '!', '-' and '+' before the constant
2832 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2833 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2834 return FAIL;
2835 return OK;
2836 }
2837 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2838 return FAIL;
2839 return OK;
2840}
2841
2842/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002843 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002844 */
2845 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002846compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002847{
2848 type_T *want_type = NULL;
2849
2850 // Recognize <type>
2851 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2852 {
2853 ++*arg;
2854 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2855 if (want_type == NULL)
2856 return FAIL;
2857
2858 if (**arg != '>')
2859 {
2860 if (*skipwhite(*arg) == '>')
2861 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2862 else
2863 emsg(_(e_missing_gt));
2864 return FAIL;
2865 }
2866 ++*arg;
2867 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2868 return FAIL;
2869 }
2870
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002871 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002872 return FAIL;
2873
2874 if (want_type != NULL)
2875 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002876 type_T *actual;
2877 where_T where = WHERE_INIT;
2878
LemonBoy50d48542024-07-04 17:03:17 +02002879 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002880 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002881 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002882 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002883 {
LemonBoy50d48542024-07-04 17:03:17 +02002884 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2885 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002886 return FAIL;
2887 }
2888 }
2889
2890 return OK;
2891}
2892
2893/*
2894 * * number multiplication
2895 * / number division
2896 * % number modulo
2897 */
2898 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002899compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002900{
2901 char_u *op;
2902 char_u *next;
2903 int ppconst_used = ppconst->pp_used;
2904
2905 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002906 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002907 return FAIL;
2908
2909 /*
2910 * Repeat computing, until no "*", "/" or "%" is following.
2911 */
2912 for (;;)
2913 {
2914 op = may_peek_next_line(cctx, *arg, &next);
2915 if (*op != '*' && *op != '/' && *op != '%')
2916 break;
2917 if (next != NULL)
2918 {
2919 *arg = next_line_from_context(cctx, TRUE);
2920 op = skipwhite(*arg);
2921 }
2922
2923 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2924 {
2925 error_white_both(op, 1);
2926 return FAIL;
2927 }
2928 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2929 return FAIL;
2930
2931 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002932 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002933 return FAIL;
2934
2935 if (ppconst->pp_used == ppconst_used + 2
2936 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2937 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2938 {
2939 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2940 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2941 varnumber_T res = 0;
2942 int failed = FALSE;
2943
2944 // both are numbers: compute the result
2945 switch (*op)
2946 {
2947 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2948 break;
2949 case '/': res = num_divide(tv1->vval.v_number,
2950 tv2->vval.v_number, &failed);
2951 break;
2952 case '%': res = num_modulus(tv1->vval.v_number,
2953 tv2->vval.v_number, &failed);
2954 break;
2955 }
2956 if (failed)
2957 return FAIL;
2958 tv1->vval.v_number = res;
2959 --ppconst->pp_used;
2960 }
2961 else
2962 {
2963 generate_ppconst(cctx, ppconst);
2964 generate_two_op(cctx, op);
2965 }
2966 }
2967
2968 return OK;
2969}
2970
2971/*
2972 * + number addition or list/blobl concatenation
2973 * - number subtraction
2974 * .. string concatenation
2975 */
2976 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002977compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002978{
2979 char_u *op;
2980 char_u *next;
2981 int oplen;
2982 int ppconst_used = ppconst->pp_used;
2983
2984 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002985 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002986 return FAIL;
2987
2988 /*
2989 * Repeat computing, until no "+", "-" or ".." is following.
2990 */
2991 for (;;)
2992 {
2993 op = may_peek_next_line(cctx, *arg, &next);
2994 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2995 break;
2996 if (op[0] == op[1] && *op != '.' && next)
2997 // Finding "++" or "--" on the next line is a separate command.
2998 // But ".." is concatenation.
2999 break;
3000 oplen = (*op == '.' ? 2 : 1);
3001 if (next != NULL)
3002 {
3003 *arg = next_line_from_context(cctx, TRUE);
3004 op = skipwhite(*arg);
3005 }
3006
3007 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3008 {
3009 error_white_both(op, oplen);
3010 return FAIL;
3011 }
3012
3013 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3014 return FAIL;
3015
3016 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003017 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003018 return FAIL;
3019
3020 if (ppconst->pp_used == ppconst_used + 2
3021 && (*op == '.'
3022 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3023 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3024 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3025 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3026 {
3027 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3028 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3029
3030 // concat/subtract/add constant numbers
3031 if (*op == '+')
3032 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3033 else if (*op == '-')
3034 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3035 else
3036 {
3037 // concatenate constant strings
3038 char_u *s1 = tv1->vval.v_string;
3039 char_u *s2 = tv2->vval.v_string;
3040 size_t len1 = STRLEN(s1);
3041
3042 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3043 if (tv1->vval.v_string == NULL)
3044 {
3045 clear_ppconst(ppconst);
3046 return FAIL;
3047 }
3048 mch_memmove(tv1->vval.v_string, s1, len1);
3049 STRCPY(tv1->vval.v_string + len1, s2);
3050 vim_free(s1);
3051 vim_free(s2);
3052 }
3053 --ppconst->pp_used;
3054 }
3055 else
3056 {
3057 generate_ppconst(cctx, ppconst);
3058 ppconst->pp_is_const = FALSE;
3059 if (*op == '.')
3060 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003061 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3062 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003063 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003064 if (generate_CONCAT(cctx, 2) == FAIL)
3065 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003066 }
3067 else
3068 generate_two_op(cctx, op);
3069 }
3070 }
3071
3072 return OK;
3073}
3074
3075/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003076 * expr6a >> expr6b
3077 * expr6a << expr6b
3078 *
3079 * Produces instructions:
3080 * OPNR bitwise left or right shift
3081 */
3082 static int
3083compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3084{
3085 exprtype_T type = EXPR_UNKNOWN;
3086 char_u *p;
3087 char_u *next;
3088 int len = 2;
3089 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003090 isn_T *isn;
3091
3092 // get the first variable
3093 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3094 return FAIL;
3095
3096 /*
3097 * Repeat computing, until no "+", "-" or ".." is following.
3098 */
3099 for (;;)
3100 {
3101 type = EXPR_UNKNOWN;
3102
3103 p = may_peek_next_line(cctx, *arg, &next);
3104 if (p[0] == '<' && p[1] == '<')
3105 type = EXPR_LSHIFT;
3106 else if (p[0] == '>' && p[1] == '>')
3107 type = EXPR_RSHIFT;
3108
3109 if (type == EXPR_UNKNOWN)
3110 return OK;
3111
3112 // Handle a bitwise left or right shift operator
3113 if (ppconst->pp_used == ppconst_used + 1)
3114 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003115 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003116 {
3117 // left operand should be a number
3118 emsg(_(e_bitshift_ops_must_be_number));
3119 return FAIL;
3120 }
3121 }
3122 else
3123 {
3124 type_T *t = get_type_on_stack(cctx, 0);
3125
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003126 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003127 {
3128 emsg(_(e_bitshift_ops_must_be_number));
3129 return FAIL;
3130 }
3131 }
3132
3133 if (next != NULL)
3134 {
3135 *arg = next_line_from_context(cctx, TRUE);
3136 p = skipwhite(*arg);
3137 }
3138
3139 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3140 {
3141 error_white_both(p, len);
3142 return FAIL;
3143 }
3144
3145 // get the second variable
3146 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3147 return FAIL;
3148
3149 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3150 return FAIL;
3151
3152 if (ppconst->pp_used == ppconst_used + 2)
3153 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003154 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3155 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3156
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003157 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003158 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3159 {
3160 // right operand should be a positive number
3161 if (tv2->v_type != VAR_NUMBER)
3162 emsg(_(e_bitshift_ops_must_be_number));
3163 else
dundargocc57b5bc2022-11-02 13:30:51 +00003164 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003165 return FAIL;
3166 }
3167
3168 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3169 tv1->vval.v_number = 0;
3170 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003171 tv1->vval.v_number =
3172 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003173 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003174 tv1->vval.v_number =
3175 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003176 clear_tv(tv2);
3177 --ppconst->pp_used;
3178 }
3179 else
3180 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003181 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3182 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003183 {
3184 emsg(_(e_bitshift_ops_must_be_number));
3185 return FAIL;
3186 }
3187
3188 generate_ppconst(cctx, ppconst);
3189
3190 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3191 if (isn == NULL)
3192 return FAIL;
3193
3194 if (isn != NULL)
3195 isn->isn_arg.op.op_type = type;
3196 }
3197 }
3198
3199 return OK;
3200}
3201
3202/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003203 * expr5a == expr5b
3204 * expr5a =~ expr5b
3205 * expr5a != expr5b
3206 * expr5a !~ expr5b
3207 * expr5a > expr5b
3208 * expr5a >= expr5b
3209 * expr5a < expr5b
3210 * expr5a <= expr5b
3211 * expr5a is expr5b
3212 * expr5a isnot expr5b
3213 *
3214 * Produces instructions:
3215 * EVAL expr5a Push result of "expr5a"
3216 * EVAL expr5b Push result of "expr5b"
3217 * COMPARE one of the compare instructions
3218 */
3219 static int
3220compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3221{
3222 exprtype_T type = EXPR_UNKNOWN;
3223 char_u *p;
3224 char_u *next;
3225 int len = 2;
3226 int type_is = FALSE;
3227 int ppconst_used = ppconst->pp_used;
3228
3229 // get the first variable
3230 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3231 return FAIL;
3232
3233 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003234
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003235 type = get_compare_type(p, &len, &type_is);
3236
3237 /*
3238 * If there is a comparative operator, use it.
3239 */
3240 if (type != EXPR_UNKNOWN)
3241 {
3242 int ic = FALSE; // Default: do not ignore case
3243
3244 if (next != NULL)
3245 {
3246 *arg = next_line_from_context(cctx, TRUE);
3247 p = skipwhite(*arg);
3248 }
3249 if (type_is && (p[len] == '?' || p[len] == '#'))
3250 {
3251 semsg(_(e_invalid_expression_str), *arg);
3252 return FAIL;
3253 }
3254 // extra question mark appended: ignore case
3255 if (p[len] == '?')
3256 {
3257 ic = TRUE;
3258 ++len;
3259 }
3260 // extra '#' appended: match case (ignored)
3261 else if (p[len] == '#')
3262 ++len;
3263 // nothing appended: match case
3264
3265 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3266 {
3267 error_white_both(p, len);
3268 return FAIL;
3269 }
3270
3271 // get the second variable
3272 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3273 return FAIL;
3274
3275 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3276 return FAIL;
3277
3278 if (ppconst->pp_used == ppconst_used + 2)
3279 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003280 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003281 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3282 int ret;
3283
3284 // Both sides are a constant, compute the result now.
3285 // First check for a valid combination of types, this is more
3286 // strict than typval_compare().
3287 if (check_compare_types(type, tv1, tv2) == FAIL)
3288 ret = FAIL;
3289 else
3290 {
3291 ret = typval_compare(tv1, tv2, type, ic);
3292 tv1->v_type = VAR_BOOL;
3293 tv1->vval.v_number = tv1->vval.v_number
3294 ? VVAL_TRUE : VVAL_FALSE;
3295 clear_tv(tv2);
3296 --ppconst->pp_used;
3297 }
3298 return ret;
3299 }
3300
3301 generate_ppconst(cctx, ppconst);
3302 return generate_COMPARE(cctx, type, ic);
3303 }
3304
3305 return OK;
3306}
3307
3308static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3309
3310/*
3311 * Compile || or &&.
3312 */
3313 static int
3314compile_and_or(
3315 char_u **arg,
3316 cctx_T *cctx,
3317 char *op,
3318 ppconst_T *ppconst,
3319 int ppconst_used UNUSED)
3320{
3321 char_u *next;
3322 char_u *p = may_peek_next_line(cctx, *arg, &next);
3323 int opchar = *op;
3324
3325 if (p[0] == opchar && p[1] == opchar)
3326 {
3327 garray_T *instr = &cctx->ctx_instr;
3328 garray_T end_ga;
3329 int save_skip = cctx->ctx_skip;
3330
3331 /*
3332 * Repeat until there is no following "||" or "&&"
3333 */
3334 ga_init2(&end_ga, sizeof(int), 10);
3335 while (p[0] == opchar && p[1] == opchar)
3336 {
3337 long start_lnum = SOURCING_LNUM;
3338 long save_sourcing_lnum;
3339 int start_ctx_lnum = cctx->ctx_lnum;
3340 int save_lnum;
3341 int const_used;
3342 int status;
3343 jumpwhen_T jump_when = opchar == '|'
3344 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3345
3346 if (next != NULL)
3347 {
3348 *arg = next_line_from_context(cctx, TRUE);
3349 p = skipwhite(*arg);
3350 }
3351
3352 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3353 {
3354 semsg(_(e_white_space_required_before_and_after_str_at_str),
3355 op, p);
3356 ga_clear(&end_ga);
3357 return FAIL;
3358 }
3359
3360 save_sourcing_lnum = SOURCING_LNUM;
3361 SOURCING_LNUM = start_lnum;
3362 save_lnum = cctx->ctx_lnum;
3363 cctx->ctx_lnum = start_ctx_lnum;
3364
3365 status = check_ppconst_bool(ppconst);
3366 if (status != FAIL)
3367 {
3368 // Use the last ppconst if possible.
3369 if (ppconst->pp_used > 0)
3370 {
3371 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3372 int is_true = tv2bool(tv);
3373
3374 if ((is_true && opchar == '|')
3375 || (!is_true && opchar == '&'))
3376 {
3377 // For "false && expr" and "true || expr" the "expr"
3378 // does not need to be evaluated.
3379 cctx->ctx_skip = SKIP_YES;
3380 clear_tv(tv);
3381 tv->v_type = VAR_BOOL;
3382 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3383 }
3384 else
3385 {
3386 // For "true && expr" and "false || expr" only "expr"
3387 // needs to be evaluated.
3388 --ppconst->pp_used;
3389 jump_when = JUMP_NEVER;
3390 }
3391 }
3392 else
3393 {
3394 // Every part must evaluate to a bool.
3395 status = bool_on_stack(cctx);
3396 }
3397 }
3398 if (status != FAIL)
3399 status = ga_grow(&end_ga, 1);
3400 cctx->ctx_lnum = save_lnum;
3401 if (status == FAIL)
3402 {
3403 ga_clear(&end_ga);
3404 return FAIL;
3405 }
3406
3407 if (jump_when != JUMP_NEVER)
3408 {
3409 if (cctx->ctx_skip != SKIP_YES)
3410 {
3411 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3412 ++end_ga.ga_len;
3413 }
3414 generate_JUMP(cctx, jump_when, 0);
3415 }
3416
3417 // eval the next expression
3418 SOURCING_LNUM = save_sourcing_lnum;
3419 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3420 {
3421 ga_clear(&end_ga);
3422 return FAIL;
3423 }
3424
3425 const_used = ppconst->pp_used;
3426 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3427 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3428 {
3429 ga_clear(&end_ga);
3430 return FAIL;
3431 }
3432
3433 // "0 || 1" results in true, "1 && 0" results in false.
3434 if (ppconst->pp_used == const_used + 1)
3435 {
3436 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3437
3438 if (tv->v_type == VAR_NUMBER
3439 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3440 {
3441 tv->vval.v_number = tv->vval.v_number == 1
3442 ? VVAL_TRUE : VVAL_FALSE;
3443 tv->v_type = VAR_BOOL;
3444 }
3445 }
3446
3447 p = may_peek_next_line(cctx, *arg, &next);
3448 }
3449
3450 if (check_ppconst_bool(ppconst) == FAIL)
3451 {
3452 ga_clear(&end_ga);
3453 return FAIL;
3454 }
3455
3456 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3457 // Every part must evaluate to a bool.
3458 if (bool_on_stack(cctx) == FAIL)
3459 {
3460 ga_clear(&end_ga);
3461 return FAIL;
3462 }
3463
3464 if (end_ga.ga_len > 0)
3465 {
3466 // Fill in the end label in all jumps.
3467 generate_ppconst(cctx, ppconst);
3468 while (end_ga.ga_len > 0)
3469 {
3470 isn_T *isn;
3471
3472 --end_ga.ga_len;
3473 isn = ((isn_T *)instr->ga_data)
3474 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3475 isn->isn_arg.jump.jump_where = instr->ga_len;
3476 }
3477 }
3478 ga_clear(&end_ga);
3479
3480 cctx->ctx_skip = save_skip;
3481 }
3482
3483 return OK;
3484}
3485
3486/*
3487 * expr4a && expr4a && expr4a logical AND
3488 *
3489 * Produces instructions:
3490 * EVAL expr4a Push result of "expr4a"
3491 * COND2BOOL convert to bool if needed
3492 * JUMP_IF_COND_FALSE end
3493 * EVAL expr4b Push result of "expr4b"
3494 * JUMP_IF_COND_FALSE end
3495 * EVAL expr4c Push result of "expr4c"
3496 * end:
3497 */
3498 static int
3499compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3500{
3501 int ppconst_used = ppconst->pp_used;
3502
3503 // get the first variable
3504 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3505 return FAIL;
3506
3507 // || and && work almost the same
3508 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3509}
3510
3511/*
3512 * expr3a || expr3b || expr3c logical OR
3513 *
3514 * Produces instructions:
3515 * EVAL expr3a Push result of "expr3a"
3516 * COND2BOOL convert to bool if needed
3517 * JUMP_IF_COND_TRUE end
3518 * EVAL expr3b Push result of "expr3b"
3519 * JUMP_IF_COND_TRUE end
3520 * EVAL expr3c Push result of "expr3c"
3521 * end:
3522 */
3523 static int
3524compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3525{
3526 int ppconst_used = ppconst->pp_used;
3527
3528 // eval the first expression
3529 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3530 return FAIL;
3531
3532 // || and && work almost the same
3533 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3534}
3535
3536/*
3537 * Toplevel expression: expr2 ? expr1a : expr1b
3538 * Produces instructions:
3539 * EVAL expr2 Push result of "expr2"
3540 * JUMP_IF_FALSE alt jump if false
3541 * EVAL expr1a
3542 * JUMP_ALWAYS end
3543 * alt: EVAL expr1b
3544 * end:
3545 *
3546 * Toplevel expression: expr2 ?? expr1
3547 * Produces instructions:
3548 * EVAL expr2 Push result of "expr2"
3549 * JUMP_AND_KEEP_IF_TRUE end jump if true
3550 * EVAL expr1
3551 * end:
3552 */
3553 int
3554compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3555{
3556 char_u *p;
3557 int ppconst_used = ppconst->pp_used;
3558 char_u *next;
3559
3560 // Ignore all kinds of errors when not producing code.
3561 if (cctx->ctx_skip == SKIP_YES)
3562 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003563 int prev_did_emsg = did_emsg;
3564
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003565 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003566 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003567 }
3568
3569 // Evaluate the first expression.
3570 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3571 return FAIL;
3572
3573 p = may_peek_next_line(cctx, *arg, &next);
3574 if (*p == '?')
3575 {
3576 int op_falsy = p[1] == '?';
3577 garray_T *instr = &cctx->ctx_instr;
3578 garray_T *stack = &cctx->ctx_type_stack;
3579 int alt_idx = instr->ga_len;
3580 int end_idx = 0;
3581 isn_T *isn;
3582 type_T *type1 = NULL;
3583 int has_const_expr = FALSE;
3584 int const_value = FALSE;
3585 int save_skip = cctx->ctx_skip;
3586
3587 if (next != NULL)
3588 {
3589 *arg = next_line_from_context(cctx, TRUE);
3590 p = skipwhite(*arg);
3591 }
3592
3593 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3594 {
3595 semsg(_(e_white_space_required_before_and_after_str_at_str),
3596 op_falsy ? "??" : "?", p);
3597 return FAIL;
3598 }
3599
3600 if (ppconst->pp_used == ppconst_used + 1)
3601 {
3602 // the condition is a constant, we know whether the ? or the :
3603 // expression is to be evaluated.
3604 has_const_expr = TRUE;
3605 if (op_falsy)
3606 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3607 else
3608 {
3609 int error = FALSE;
3610
3611 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3612 &error);
3613 if (error)
3614 return FAIL;
3615 }
3616 cctx->ctx_skip = save_skip == SKIP_YES ||
3617 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3618
3619 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3620 // "left ?? right" and "left" is truthy: produce "left"
3621 generate_ppconst(cctx, ppconst);
3622 else
3623 {
3624 clear_tv(&ppconst->pp_tv[ppconst_used]);
3625 --ppconst->pp_used;
3626 }
3627 }
3628 else
3629 {
3630 generate_ppconst(cctx, ppconst);
3631 if (op_falsy)
3632 end_idx = instr->ga_len;
3633 generate_JUMP(cctx, op_falsy
3634 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3635 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003636 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003637 }
3638
3639 // evaluate the second expression; any type is accepted
3640 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3641 return FAIL;
3642 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3643 return FAIL;
3644
3645 if (!has_const_expr)
3646 {
3647 generate_ppconst(cctx, ppconst);
3648
3649 if (!op_falsy)
3650 {
3651 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003652 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003653 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003654
3655 end_idx = instr->ga_len;
3656 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3657
3658 // jump here from JUMP_IF_FALSE
3659 isn = ((isn_T *)instr->ga_data) + alt_idx;
3660 isn->isn_arg.jump.jump_where = instr->ga_len;
3661 }
3662 }
3663
3664 if (!op_falsy)
3665 {
3666 // Check for the ":".
3667 p = may_peek_next_line(cctx, *arg, &next);
3668 if (*p != ':')
3669 {
3670 emsg(_(e_missing_colon_after_questionmark));
3671 return FAIL;
3672 }
3673 if (next != NULL)
3674 {
3675 *arg = next_line_from_context(cctx, TRUE);
3676 p = skipwhite(*arg);
3677 }
3678
3679 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3680 {
3681 semsg(_(e_white_space_required_before_and_after_str_at_str),
3682 ":", p);
3683 return FAIL;
3684 }
3685
3686 // evaluate the third expression
3687 if (has_const_expr)
3688 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3689 ? SKIP_YES : SKIP_NOT;
3690 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3691 return FAIL;
3692 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3693 return FAIL;
3694 }
3695
3696 if (!has_const_expr)
3697 {
3698 type_T **typep;
3699
3700 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003701 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003702
3703 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003704 typep = &((((type2_T *)stack->ga_data)
3705 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003706 common_type(type1, *typep, typep, cctx->ctx_type_list);
3707
3708 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3709 isn = ((isn_T *)instr->ga_data) + end_idx;
3710 isn->isn_arg.jump.jump_where = instr->ga_len;
3711 }
3712
3713 cctx->ctx_skip = save_skip;
3714 }
3715 return OK;
3716}
3717
3718/*
3719 * Toplevel expression.
3720 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3721 * Returns OK or FAIL.
3722 */
3723 int
3724compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3725{
3726 ppconst_T ppconst;
3727
3728 CLEAR_FIELD(ppconst);
3729 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3730 {
3731 clear_ppconst(&ppconst);
3732 return FAIL;
3733 }
3734 if (is_const != NULL)
3735 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3736 if (generate_ppconst(cctx, &ppconst) == FAIL)
3737 return FAIL;
3738 return OK;
3739}
3740
3741/*
3742 * Toplevel expression.
3743 */
3744 int
3745compile_expr0(char_u **arg, cctx_T *cctx)
3746{
3747 return compile_expr0_ext(arg, cctx, NULL);
3748}
3749
3750
3751#endif // defined(FEAT_EVAL)