blob: 76ce9c50b3e36c7c50ea619a64b1fc45c647826d [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000102 if (need_type(idxtype, &t_number, FALSE,
103 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000104 return FAIL;
105 if (is_slice)
106 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000107 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000108 if (need_type(idxtype, &t_number, FALSE,
109 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000110 return FAIL;
111 }
112 }
113
114 if (vartype == VAR_DICT)
115 {
116 if (is_slice)
117 {
118 emsg(_(e_cannot_slice_dictionary));
119 return FAIL;
120 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000122 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000123 typep->type_curr = typep->type_curr->tt_member;
124 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000125 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 typep->type_curr = &t_any;
127 if (typep->type_decl->tt_type == VAR_DICT)
128 {
129 typep->type_decl = typep->type_decl->tt_member;
130 if (typep->type_decl == &t_unknown)
131 // empty dict was used
132 typep->type_decl = &t_any;
133 }
134 else
135 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000136 }
137 else
138 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000139 if (need_type(typep->type_curr, &t_dict_any, FALSE,
140 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000141 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000142 typep->type_curr = &t_any;
143 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200145 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL
Bram Moolenaard0132f42022-05-12 11:05:40 +0100146 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147 return FAIL;
148 if (keeping_dict != NULL)
149 *keeping_dict = TRUE;
150 }
151 else if (vartype == VAR_STRING)
152 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000153 typep->type_curr = &t_string;
154 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000155 if ((is_slice
156 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
157 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
158 return FAIL;
159 }
160 else if (vartype == VAR_BLOB)
161 {
162 if (is_slice)
163 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 typep->type_curr = &t_blob;
165 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
167 return FAIL;
168 }
169 else
170 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000171 typep->type_curr = &t_number;
172 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
174 return FAIL;
175 }
176 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100177 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
178 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000179 {
180 if (is_slice)
181 {
182 if (generate_instr_drop(cctx,
183 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
184 2) == FAIL)
185 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000186 // a copy is made so the member type is no longer declared
187 if (typep->type_decl->tt_type == VAR_LIST)
188 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000189
190 // a copy is made, the composite is no longer "const"
191 if (typep->type_curr->tt_flags & TTFLAG_CONST)
192 {
193 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
194
195 if (type != typep->type_curr) // did get a copy
196 {
197 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
198 typep->type_curr = type;
199 }
200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201 }
202 else
203 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 typep->type_curr = typep->type_curr->tt_member;
207 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000209 typep->type_curr = &t_any;
210 if (typep->type_decl->tt_type == VAR_LIST)
211 {
212 typep->type_decl = typep->type_decl->tt_member;
213 if (typep->type_decl == &t_unknown)
214 // empty list was used
215 typep->type_decl = &t_any;
216 }
217 else
218 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 }
220 if (generate_instr_drop(cctx,
221 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
222 == FAIL)
223 return FAIL;
224 }
225 }
226 else
227 {
228 switch (vartype)
229 {
230 case VAR_FUNC:
231 case VAR_PARTIAL:
232 emsg(_(e_cannot_index_a_funcref));
233 break;
234 case VAR_BOOL:
235 case VAR_SPECIAL:
236 case VAR_JOB:
237 case VAR_CHANNEL:
238 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 case VAR_CLASS:
240 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200241 case VAR_TYPEALIAS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000242 case VAR_UNKNOWN:
243 case VAR_ANY:
244 case VAR_VOID:
245 emsg(_(e_cannot_index_special_variable));
246 break;
247 default:
248 emsg(_(e_string_list_dict_or_blob_required));
249 }
250 return FAIL;
251 }
252 return OK;
253}
254
255/*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200256 * Returns TRUE if the current function is inside the class "cl" or one of the
257 * parent classes.
258 */
259 static int
260inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
261{
262 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
263 {
264 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
265 {
266 class_T *clp = cctx->ctx_ufunc->uf_class;
267 while (clp != NULL)
268 {
269 if (clp == cl)
270 return TRUE;
271 clp = clp->class_extends;
272 }
273 }
274 }
275
276 return FALSE;
277}
278
279/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000280 * Compile ".member" coming after an object or class.
281 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000282 static int
283compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
284{
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200285 int m_idx;
286
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000287 if (VIM_ISWHITE((*arg)[1]))
288 {
289 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
290 return FAIL;
291 }
292
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000293 class_T *cl = type->tt_class;
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100294 int is_super = ((type->tt_flags & TTFLAG_SUPER) == TTFLAG_SUPER);
Bram Moolenaar58b40092023-01-11 15:59:05 +0000295 if (type == &t_super)
296 {
297 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000298 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200299 emsg(_(e_using_super_not_in_class_method));
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000300 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000301 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000302 is_super = TRUE;
303 cl = cctx->ctx_ufunc->uf_class;
304 // Remove &t_super from the stack.
305 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000306 }
307 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000308 {
309 garray_T *instr = &cctx->ctx_instr;
310 if (instr->ga_len > 0)
311 {
312 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
313 if (isn->isn_type == ISN_LOADSCRIPT)
314 {
315 // The class was recognized as a script item. We only need
316 // to know what class it is, drop the instruction.
317 --instr->ga_len;
318 vim_free(isn->isn_arg.script.scriptref);
319 }
320 }
321 }
322
Ernie Raelf77a7f72023-03-03 15:05:30 +0000323 if (cl == NULL)
324 {
325 // TODO: this should not give an error but be handled at runtime
326 emsg(_(e_incomplete_type));
327 return FAIL;
328 }
329
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000330 ++*arg;
331 char_u *name = *arg;
332 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
333 if (name_end == name)
334 return FAIL;
335 size_t len = name_end - name;
336
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000337 if (*name_end == '(')
338 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000339 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000340 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000341 ufunc_T **functions;
342
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000343 if (type->tt_type == VAR_CLASS)
344 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000345 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000346 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000347 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000348 }
349 else
350 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000351 // type->tt_type == VAR_OBJECT: method call
Ernie Rael58c95792024-08-13 23:27:22 +0200352 // When compiling Func and doing "super.SomeFunc()", must be in the
353 // class context that defines Func.
354 if (is_super)
355 cl = cctx->ctx_ufunc->uf_defclass;
356
Bram Moolenaar574950d2023-01-03 19:08:50 +0000357 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000358 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000360 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000361
362 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000363 int fi;
364 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000365 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000366 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000367 // Use a separate pointer to avoid that ASAN complains about
368 // uf_name[] only being 4 characters.
369 char_u *ufname = (char_u *)fp->uf_name;
370 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
371 {
372 ufunc = fp;
373 break;
374 }
375 }
Ernie Raelbce60c42025-01-19 10:03:00 +0100376
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200377 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000378 if (ufunc == NULL)
379 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200380 // could be a funcref in a member variable
381 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
382 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
383 {
384 method_not_found_msg(cl, type->tt_type, name, len);
385 return FAIL;
386 }
387 if (type->tt_type == VAR_CLASS)
388 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200389 // Remove the class type from the stack
390 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200391 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
392 return FAIL;
393 }
394 else
395 {
Yegappan Lakshmananc10342d2025-01-11 09:39:01 +0100396 int status;
397
398 if (IS_INTERFACE(cl))
399 status = generate_GET_ITF_MEMBER(cctx, cl, m_idx,
400 ocm->ocm_type);
401 else
402 status = generate_GET_OBJ_MEMBER(cctx, m_idx,
403 ocm->ocm_type);
404 if (status == FAIL)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200405 return FAIL;
406 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000407 }
408
Yegappan Lakshmanancb848b62025-01-20 21:38:09 +0100409 if (is_super && ufunc != NULL && IS_ABSTRACT_METHOD(ufunc))
Ernie Raelbce60c42025-01-19 10:03:00 +0100410 {
411 // Trying to invoke an abstract method in a super class is not
412 // allowed.
413 semsg(_(e_abstract_method_str_direct), ufunc->uf_name,
414 ufunc->uf_defclass->class_name);
415 return FAIL;
416 }
417
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200418 // A private object method can be used only inside the class where it
419 // is defined or in one of the child classes.
420 // A private class method can be used only in the class where it is
421 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200422 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200423 ((type->tt_type == VAR_OBJECT
424 && !inside_class_hierarchy(cctx, cl))
425 || (type->tt_type == VAR_CLASS
426 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200427 {
Ernie Rael03042a22023-11-11 08:53:32 +0100428 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200429 return FAIL;
430 }
431
Bram Moolenaar574950d2023-01-03 19:08:50 +0000432 // Compile the arguments and call the class function or object method.
433 // The object method will know that the object is on the stack, just
434 // before the arguments.
435 *arg = skipwhite(name_end + 1);
436 int argcount = 0;
437 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
438 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000439
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200440 if (ocm != NULL)
441 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000442 if (type->tt_type == VAR_OBJECT
443 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
Ernie Rael58c95792024-08-13 23:27:22 +0200444 return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super);
445 return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000446 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000447
448 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000449 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200450 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200451 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000452 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200453 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000454 {
Ernie Rael03042a22023-11-11 08:53:32 +0100455 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200456 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200457 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200458 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200459
460 *arg = name_end;
461 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200462 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
463 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200464 }
465
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200466 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200467 m_idx = object_method_idx(cl, name, len);
468 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000469 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200470 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100471 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200472 if (*name == '_' && !inside_class(cctx, cl))
473 {
Ernie Rael03042a22023-11-11 08:53:32 +0100474 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200475 return FAIL;
476 }
477 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200478 // Remove the object type from the stack
479 --cctx->ctx_type_stack.ga_len;
480 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000481 }
482
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200483 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000484 }
485 else
486 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000487 // load class member
488 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200489 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
490 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000491 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200492 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200493 // A private class variable can be accessed only in the class where
494 // it is defined.
495 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200496 {
Ernie Rael03042a22023-11-11 08:53:32 +0100497 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200498 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200499 return FAIL;
500 }
501
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000502 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200503 // Remove the class type from the stack
504 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000505 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
506 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200507
508 // Could be a class method reference: "class.Func".
509 m_idx = class_method_idx(cl, name, len);
510 if (m_idx >= 0)
511 {
512 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100513 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200514 if (*name == '_' && !inside_class(cctx, cl))
515 {
Ernie Rael03042a22023-11-11 08:53:32 +0100516 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200517 return FAIL;
518 }
519 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200520 // Remove the class type from the stack
521 --cctx->ctx_type_stack.ga_len;
522 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200523 }
524
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200525 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000526 }
527
528 return FAIL;
529}
530
531/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000532 * Generate an instruction to load script-local variable "name", without the
533 * leading "s:".
534 * Also finds imported variables.
535 */
536 int
537compile_load_scriptvar(
538 cctx_T *cctx,
539 char_u *name, // variable NUL terminated
540 char_u *start, // start of variable
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100541 char_u **end, // end of variable, may be NULL
542 imported_T *import) // found imported item, can be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543{
544 scriptitem_T *si;
545 int idx;
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100546 imported_T *imp;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547
548 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
549 return FAIL;
550 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000551 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000552 if (idx >= 0)
553 {
554 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
555
556 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
557 current_sctx.sc_sid, idx, sv->sv_type);
558 return OK;
559 }
560
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100561 if (end == NULL)
562 imp = NULL;
563 else if (import == NULL)
564 imp = find_imported(name, 0, FALSE);
565 else
566 imp = import;
567
568 if (imp != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000570 char_u *p = skipwhite(*end);
571 char_u *exp_name;
572 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100573 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000574 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000575 int done = FALSE;
576 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000577
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100578 check_script_symlink(imp->imp_sid);
579 import_check_sourced_sid(&imp->imp_sid);
Ernie Rael9a901792024-04-16 22:11:56 +0200580
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000581 // Need to lookup the member.
582 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000583 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000584 semsg(_(e_expected_dot_after_name_str), start);
585 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000586 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000587 ++p;
588 if (VIM_ISWHITE(*p))
589 {
590 emsg(_(e_no_white_space_allowed_after_dot));
591 return FAIL;
592 }
593
594 // isolate one name
595 exp_name = p;
596 while (eval_isnamec(*p))
597 ++p;
598 cc = *p;
599 *p = NUL;
600
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100601 si = SCRIPT_ITEM(imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100602 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
603 // "import autoload './dir/script.vim'" or
604 // "import autoload './autoload/script.vim'" - load script first
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100605 res = generate_SOURCE(cctx, imp->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100606
607 if (res == OK)
608 {
609 if (si->sn_autoload_prefix != NULL
610 && si->sn_state == SN_STATE_NOT_LOADED)
611 {
612 char_u *auto_name =
613 concat_str(si->sn_autoload_prefix, exp_name);
614
615 // autoload script must be loaded later, access by the autoload
616 // name. If a '(' follows it must be a function. Otherwise we
617 // don't know, it can be "script.Func".
618 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100619 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100620 else
621 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
622 vim_free(auto_name);
623 done = TRUE;
624 }
625 else if (si->sn_import_autoload
626 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100627 {
628 // If a '(' follows it must be a function. Otherwise we don't
629 // know, it can be "script.Func".
630 if (cc == '(' || paren_follows_after_expr)
631 {
632 char_u sid_name[MAX_FUNC_NAME_LEN];
633
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100634 func_name_with_sid(exp_name, imp->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100635 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100636 }
637 else
638 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100639 imp->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100640 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100641 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100642 else
643 {
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100644 idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100645 cctx, NULL, TRUE);
646 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100647 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100648
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000649 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000650 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000651 if (done)
652 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000653
654 if (idx < 0)
655 {
656 if (ufunc != NULL)
657 {
658 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100659 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000660 return OK;
661 }
662 return FAIL;
663 }
664
665 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100666 imp->imp_sid,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000667 idx,
668 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669 return OK;
670 }
671
Bram Moolenaard0132f42022-05-12 11:05:40 +0100672 // Can only get here if we know "name" is a script variable and not in a
673 // Vim9 script (variable is not in sn_var_vals): old style script.
674 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000675 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676}
677
678 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000679generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000680{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000681 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000682 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000683
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000684 // Reject a global non-autoload function found without the "g:" prefix.
685 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686 return FAIL;
687
688 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000689 compile_type = get_compile_type(ufunc);
690 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000691 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100693 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694}
695
696/*
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100697 * Returns TRUE if compiling a class method.
698 */
699 static int
700compiling_a_class_method(cctx_T *cctx)
701{
702 // For an object method, the FC_OBJECT flag will be set.
703 // For a constructor method, the FC_NEW flag will be set.
704 // Excluding these methods, the others are class methods.
705 // When compiling a closure function inside an object method,
706 // cctx->ctx_outer->ctx_func will point to the object method.
707 return cctx->ctx_ufunc != NULL
708 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0
709 && (cctx->ctx_outer == NULL
710 || cctx->ctx_outer->ctx_ufunc == NULL
711 || cctx->ctx_outer->ctx_ufunc->uf_class == NULL
712 || (cctx->ctx_outer->ctx_ufunc->uf_flags
713 & (FC_OBJECT|FC_NEW)) == 0);
714}
715
716/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717 * Compile a variable name into a load instruction.
718 * "end" points to just after the name.
719 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
720 * When "error" is FALSE do not give an error when not found.
721 */
722 int
723compile_load(
724 char_u **arg,
725 char_u *end_arg,
726 cctx_T *cctx,
727 int is_expr,
728 int error)
729{
730 type_T *type;
731 char_u *name = NULL;
732 char_u *end = end_arg;
733 int res = FAIL;
734 int prev_called_emsg = called_emsg;
735
736 if (*(*arg + 1) == ':')
737 {
738 if (end <= *arg + 2)
739 {
740 isntype_T isn_type;
741
742 // load dictionary of namespace
743 switch (**arg)
744 {
745 case 'g': isn_type = ISN_LOADGDICT; break;
746 case 'w': isn_type = ISN_LOADWDICT; break;
747 case 't': isn_type = ISN_LOADTDICT; break;
748 case 'b': isn_type = ISN_LOADBDICT; break;
749 default:
750 semsg(_(e_namespace_not_supported_str), *arg);
751 goto theend;
752 }
753 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
754 goto theend;
755 res = OK;
756 }
757 else
758 {
759 isntype_T isn_type = ISN_DROP;
760
761 // load namespaced variable
762 name = vim_strnsave(*arg + 2, end - (*arg + 2));
763 if (name == NULL)
764 return FAIL;
765
766 switch (**arg)
767 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100768 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000770 case 's': if (current_script_is_vim9())
771 {
772 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
773 *arg);
774 vim_free(name);
775 return FAIL;
776 }
Kota Kato948a3892022-08-16 16:09:59 +0100777 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000778 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 else
780 res = compile_load_scriptvar(cctx, name,
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100781 NULL, &end, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 break;
783 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
784 {
785 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000786 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000787 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 else
789 isn_type = ISN_LOADG;
790 }
791 else
792 {
793 isn_type = ISN_LOADAUTO;
794 vim_free(name);
795 name = vim_strnsave(*arg, end - *arg);
796 if (name == NULL)
797 return FAIL;
798 }
799 break;
800 case 'w': isn_type = ISN_LOADW; break;
801 case 't': isn_type = ISN_LOADT; break;
802 case 'b': isn_type = ISN_LOADB; break;
803 default: // cannot happen, just in case
804 semsg(_(e_namespace_not_supported_str), *arg);
805 goto theend;
806 }
807 if (isn_type != ISN_DROP)
808 {
809 // Global, Buffer-local, Window-local and Tabpage-local
810 // variables can be defined later, thus we don't check if it
811 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000812 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000813 }
814 }
815 }
816 else
817 {
818 size_t len = end - *arg;
819 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200820 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 int gen_load = FALSE;
822 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100823 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100824 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825
Hirohito Higashif7cb9f92025-02-04 16:37:19 +0100826 name = vim_strnsave(*arg, len);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 if (name == NULL)
828 return FAIL;
829
Yegappan Lakshmananb5f463c2025-02-16 16:25:24 +0100830 if (STRCMP(name, "super") == 0 && compiling_a_class_method(cctx))
Bram Moolenaar58b40092023-01-11 15:59:05 +0000831 {
832 // super.SomeFunc() in a class function: push &t_super type, this
833 // is recognized in compile_subscript().
834 res = push_type_stack(cctx, &t_super);
835 if (*end != '.')
836 emsg(_(e_super_must_be_followed_by_dot));
837 }
838 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000839 {
840 script_autoload(name, FALSE);
841 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
842 }
843 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
844 == OK)
845 {
846 if (gen_load_outer == 0)
847 gen_load = TRUE;
848 }
849 else
850 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000851 lvar_T lvar;
852 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853
854 if (lookup_local(*arg, len, &lvar, cctx) == OK)
855 {
856 type = lvar.lv_type;
857 idx = lvar.lv_idx;
858 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100859 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100861 outer_loop_depth = lvar.lv_loop_depth;
862 outer_loop_idx = lvar.lv_loop_idx;
863 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 else
865 gen_load = TRUE;
866 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200867 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
868 (((idx =
869 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
870 || ((method_idx =
871 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000872 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200873 // Referencing a class variable or method without the class
874 // name. A class variable or method can be referenced without
875 // the class name only in the class where the function is
876 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200877 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200878 {
879 if (idx >= 0)
880 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
881 else
882 {
883 ufunc_T *fp = cl->class_class_functions[method_idx];
884 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
885 NULL);
886 }
887 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200888 else
889 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200890 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200891 name, cl->class_name);
892 res = FAIL;
893 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000894 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 else
896 {
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100897 imported_T *imp = NULL;
898
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000899 // "var" can be script-local even without using "s:" if it
900 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000901 if (script_var_exists(*arg, len, cctx, NULL) == OK
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100902 || (imp = find_imported(name, 0, FALSE)) != NULL
903 || (imp = find_imported_from_extends(cctx, name, 0, FALSE))
904 != NULL)
Hirohito Higashi5887cce2025-02-16 16:34:30 +0100905 res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906
907 // When evaluating an expression and the name starts with an
908 // uppercase letter it can be a user defined function.
909 // generate_funcref() will fail if the function can't be found.
910 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000911 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000912 }
913 }
914 if (gen_load)
915 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
916 if (gen_load_outer > 0)
917 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100918 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
919 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920 cctx->ctx_outer_used = TRUE;
921 }
922 }
923
924 *arg = end;
925
926theend:
927 if (res == FAIL && error && called_emsg == prev_called_emsg)
928 semsg(_(e_variable_not_found_str), name);
929 vim_free(name);
930 return res;
931}
932
933/*
934 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100935 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 * Returns FAIL if compilation fails.
937 */
938 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100939compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000940{
LemonBoyf3b48952022-05-05 13:53:03 +0100941 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942 garray_T save_ga = cctx->ctx_instr;
943 int expr_res;
944 int trailing_error;
945 int instr_count;
946 isn_T *instr = NULL;
947
948 // Remove the string type from the stack.
949 --cctx->ctx_type_stack.ga_len;
950
951 // Temporarily reset the list of instructions so that the jump labels are
952 // correct.
953 cctx->ctx_instr.ga_len = 0;
954 cctx->ctx_instr.ga_maxlen = 0;
955 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000956
957 // avoid peeking a next line
958 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
959 cctx->ctx_ufunc->uf_lines.ga_len = 0;
960
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000962
963 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
964
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000965 s = skipwhite(s);
966 trailing_error = *s != NUL;
967
968 if (expr_res == FAIL || trailing_error
969 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
970 {
971 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000972 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000973 clear_instr_ga(&cctx->ctx_instr);
974 cctx->ctx_instr = save_ga;
975 ++cctx->ctx_type_stack.ga_len;
976 return FAIL;
977 }
978
979 // Move the generated instructions into the ISN_INSTR instruction, then
980 // restore the list of instructions.
981 instr_count = cctx->ctx_instr.ga_len;
982 instr = cctx->ctx_instr.ga_data;
983 instr[instr_count].isn_type = ISN_FINISH;
984
985 cctx->ctx_instr = save_ga;
986 vim_free(isn->isn_arg.string);
987 isn->isn_type = ISN_INSTR;
988 isn->isn_arg.instr = instr;
989 return OK;
990}
991
992/*
993 * Compile the argument expressions.
994 * "arg" points to just after the "(" and is advanced to after the ")"
995 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100996 int
LemonBoyf3b48952022-05-05 13:53:03 +0100997compile_arguments(
998 char_u **arg,
999 cctx_T *cctx,
1000 int *argcount,
1001 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001002{
1003 char_u *p = *arg;
1004 char_u *whitep = *arg;
1005 int must_end = FALSE;
1006 int instr_count;
1007
1008 for (;;)
1009 {
1010 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1011 goto failret;
1012 if (*p == ')')
1013 {
1014 *arg = p + 1;
1015 return OK;
1016 }
1017 if (must_end)
1018 {
1019 semsg(_(e_missing_comma_before_argument_str), p);
1020 return FAIL;
1021 }
1022
1023 instr_count = cctx->ctx_instr.ga_len;
1024 if (compile_expr0(&p, cctx) == FAIL)
1025 return FAIL;
1026 ++*argcount;
1027
LemonBoyf3b48952022-05-05 13:53:03 +01001028 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001029 && cctx->ctx_instr.ga_len == instr_count + 1)
1030 {
1031 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1032
1033 // {skip} argument of searchpair() can be compiled if not empty
1034 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +01001035 compile_string(isn, cctx, 0);
1036 }
1037 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
1038 && cctx->ctx_instr.ga_len == instr_count + 1)
1039 {
1040 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1041
1042 // {sub} argument of substitute() can be compiled if it starts
1043 // with \=
1044 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001045 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001046 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001047 }
1048
1049 if (*p != ',' && *skipwhite(p) == ',')
1050 {
1051 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1052 p = skipwhite(p);
1053 }
1054 if (*p == ',')
1055 {
1056 ++p;
1057 if (*p != NUL && !VIM_ISWHITE(*p))
1058 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1059 }
1060 else
1061 must_end = TRUE;
1062 whitep = p;
1063 p = skipwhite(p);
1064 }
1065failret:
1066 emsg(_(e_missing_closing_paren));
1067 return FAIL;
1068}
1069
1070/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001071 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1072 * etc.) if the class implements it.
1073 */
1074 static int
1075compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1076{
1077 type_T *type = get_decl_type_on_stack(cctx, 0);
1078 int res = FAIL;
1079
1080 // If the built in function is invoked on an object and the class
1081 // implements the corresponding built in method, then invoke the object
1082 // method.
1083 if (type->tt_type == VAR_OBJECT)
1084 {
1085 int method_idx;
1086 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1087 &method_idx);
1088 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001089 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001090 }
1091
1092 return res;
1093}
1094
1095
1096/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001097 * Compile a function call: name(arg1, arg2)
1098 * "arg" points to "name", "arg + varlen" to the "(".
1099 * "argcount_init" is 1 for "value->method()"
1100 * Instructions:
1101 * EVAL arg1
1102 * EVAL arg2
1103 * BCALL / DCALL / UCALL
1104 */
1105 static int
1106compile_call(
1107 char_u **arg,
1108 size_t varlen,
1109 cctx_T *cctx,
1110 ppconst_T *ppconst,
1111 int argcount_init)
1112{
1113 char_u *name = *arg;
1114 char_u *p;
1115 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001116 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 char_u fname_buf[FLEN_FIXED + 1];
1118 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001119 ufunc_T *ufunc = NULL;
1120 int res = FAIL;
1121 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001122 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001123 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001124 imported_T *import;
1125
1126 if (varlen >= sizeof(namebuf))
1127 {
1128 semsg(_(e_name_too_long_str), name);
1129 return FAIL;
1130 }
1131 vim_strncpy(namebuf, *arg, varlen);
1132
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001133 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001134 if (import != NULL)
1135 {
1136 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1137 return FAIL;
1138 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139
1140 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001141 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001142 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001143 if ((varlen == 3
1144 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1146 {
1147 char_u *s = skipwhite(*arg + varlen + 1);
1148 typval_T argvars[2];
1149 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001150 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001151
1152 argvars[0].v_type = VAR_UNKNOWN;
1153 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001154 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001155 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001156 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157 s = skipwhite(s);
1158 if (*s == ')' && argvars[0].v_type == VAR_STRING
1159 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1160 || !is_has))
1161 {
1162 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1163
1164 *arg = s + 1;
1165 argvars[1].v_type = VAR_UNKNOWN;
1166 tv->v_type = VAR_NUMBER;
1167 tv->vval.v_number = 0;
1168 if (is_has)
1169 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001170 else if (is_len)
1171 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001172 else
1173 f_exists(argvars, tv);
1174 clear_tv(&argvars[0]);
1175 ++ppconst->pp_used;
1176 return OK;
1177 }
1178 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001179 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180 {
1181 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1182 return FAIL;
1183 }
1184 }
1185
1186 if (generate_ppconst(cctx, ppconst) == FAIL)
1187 return FAIL;
1188
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001189 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1191
1192 // We handle the "skip" argument of searchpair() and searchpairpos()
1193 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001194 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1195 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1196 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1197 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1198 special_fn = CA_SEARCHPAIR;
1199 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1200 special_fn = CA_SUBSTITUTE;
1201 else
1202 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203
1204 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001205 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001206 goto theend;
1207
1208 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1209 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1210 {
1211 int idx;
1212
1213 // builtin function
1214 idx = find_internal_func(name);
1215 if (idx >= 0)
1216 {
1217 if (STRCMP(name, "flatten") == 0)
1218 {
1219 emsg(_(e_cannot_use_flatten_in_vim9_script));
1220 goto theend;
1221 }
1222
1223 if (STRCMP(name, "add") == 0 && argcount == 2)
1224 {
h-eastb895b0f2023-09-24 15:46:31 +02001225 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001226 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1227 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001228
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229 // add() can be compiled to instructions if we know the type
1230 if (type->tt_type == VAR_LIST)
1231 {
1232 // inline "add(list, item)" so that the type can be checked
1233 res = generate_LISTAPPEND(cctx);
1234 idx = -1;
1235 }
1236 else if (type->tt_type == VAR_BLOB)
1237 {
1238 // inline "add(blob, nr)" so that the type can be checked
1239 res = generate_BLOBAPPEND(cctx);
1240 idx = -1;
1241 }
1242 }
1243
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001244 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1245 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001246 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001247 // May have the "D" or "R" flag, reserve a variable for a
1248 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001249 if (get_defer_var_idx(cctx) == 0)
1250 idx = -1;
1251 }
1252
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001253 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1254 if (STRCMP(name, "string") == 0)
1255 builtin_method = CLASS_BUILTIN_STRING;
1256 else if (STRCMP(name, "empty") == 0)
1257 builtin_method = CLASS_BUILTIN_EMPTY;
1258 else if (STRCMP(name, "len") == 0)
1259 builtin_method = CLASS_BUILTIN_LEN;
1260 if (builtin_method != CLASS_BUILTIN_INVALID)
1261 {
1262 res = compile_builtin_method_call(cctx, builtin_method);
1263 if (res == OK)
1264 idx = -1;
1265 }
1266
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267 if (idx >= 0)
1268 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1269 }
1270 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001271 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001272 goto theend;
1273 }
1274
Bram Moolenaar62aec932022-01-29 21:45:34 +00001275 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1276
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277 // An argument or local variable can be a function reference, this
1278 // overrules a function name.
1279 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1280 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1281 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001282 class_T *cl = NULL;
1283 int mi = 0;
1284
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001285 // If we can find the function by name generate the right call.
1286 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001287 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001288 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001289 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001290 if (!func_is_global(ufunc))
1291 {
Ernie Rael58c95792024-08-13 23:27:22 +02001292 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001293 goto theend;
1294 }
1295 if (!has_g_namespace
1296 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1297 {
1298 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001299 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001300 goto theend;
1301 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001302 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001303 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001304 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001305 // Class method invocation without the class name.
1306 // A class method can be referenced without the class name only in
1307 // the class where the function is defined.
1308 if (cctx->ctx_ufunc->uf_defclass == cl)
1309 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001310 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001311 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001312 }
1313 else
1314 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001315 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001316 name, cl->class_name);
1317 res = FAIL;
1318 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001319 goto theend;
1320 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321 }
1322
1323 // If the name is a variable, load it and use PCALL.
1324 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001325 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001327 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1329 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001330 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331
Christian Brabandtd5475e82023-08-17 23:34:09 +02001332 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333 goto theend;
1334 }
1335
1336 // If we can find a global function by name generate the right call.
1337 if (ufunc != NULL)
1338 {
Ernie Rael58c95792024-08-13 23:27:22 +02001339 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340 goto theend;
1341 }
1342
1343 // A global function may be defined only later. Need to figure out at
1344 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001345 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001346 res = generate_UCALL(cctx, name, argcount);
1347 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001348 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001349
1350theend:
1351 vim_free(tofree);
1352 return res;
1353}
1354
1355// like NAMESPACE_CHAR but with 'a' and 'l'.
1356#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1357
1358/*
1359 * Find the end of a variable or function name. Unlike find_name_end() this
1360 * does not recognize magic braces.
1361 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1362 * Return a pointer to just after the name. Equal to "arg" if there is no
1363 * valid name.
1364 */
1365 char_u *
1366to_name_end(char_u *arg, int use_namespace)
1367{
1368 char_u *p;
1369
1370 // Quick check for valid starting character.
1371 if (!eval_isnamec1(*arg))
1372 return arg;
1373
1374 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1375 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1376 // and can be used in slice "[n:]".
1377 if (*p == ':' && (p != arg + 1
1378 || !use_namespace
1379 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1380 break;
1381 return p;
1382}
1383
1384/*
1385 * Like to_name_end() but also skip over a list or dict constant.
1386 * Also accept "<SNR>123_Func".
1387 * This intentionally does not handle line continuation.
1388 */
1389 char_u *
1390to_name_const_end(char_u *arg)
1391{
1392 char_u *p = arg;
1393 typval_T rettv;
1394
1395 if (STRNCMP(p, "<SNR>", 5) == 0)
1396 p = skipdigits(p + 5);
1397 p = to_name_end(p, TRUE);
1398 if (p == arg && *arg == '[')
1399 {
1400
1401 // Can be "[1, 2, 3]->Func()".
1402 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1403 p = arg;
1404 }
1405 return p;
1406}
1407
1408/*
1409 * parse a list: [expr, expr]
1410 * "*arg" points to the '['.
1411 * ppconst->pp_is_const is set if all items are a constant.
1412 */
1413 static int
1414compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1415{
1416 char_u *p = skipwhite(*arg + 1);
1417 char_u *whitep = *arg + 1;
1418 int count = 0;
1419 int is_const;
1420 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001421 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422
1423 for (;;)
1424 {
1425 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1426 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001427 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001428 return FAIL;
1429 }
1430 if (*p == ',')
1431 {
1432 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1433 return FAIL;
1434 }
1435 if (*p == ']')
1436 {
1437 ++p;
1438 break;
1439 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001440 if (must_end)
1441 {
1442 semsg(_(e_missing_comma_in_list_str), p);
1443 return FAIL;
1444 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001445 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1446 return FAIL;
1447 if (!is_const)
1448 is_all_const = FALSE;
1449 ++count;
1450 if (*p == ',')
1451 {
1452 ++p;
1453 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1454 {
1455 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1456 return FAIL;
1457 }
1458 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001459 else
1460 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001461 whitep = p;
1462 p = skipwhite(p);
1463 }
1464 *arg = p;
1465
1466 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001467 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001468}
1469
1470/*
1471 * Parse a lambda: "(arg, arg) => expr"
1472 * "*arg" points to the '('.
1473 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1474 */
1475 static int
1476compile_lambda(char_u **arg, cctx_T *cctx)
1477{
1478 int r;
1479 typval_T rettv;
1480 ufunc_T *ufunc;
1481 evalarg_T evalarg;
1482
1483 init_evalarg(&evalarg);
1484 evalarg.eval_flags = EVAL_EVALUATE;
1485 evalarg.eval_cctx = cctx;
1486
1487 // Get the funcref in "rettv".
1488 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1489 if (r != OK)
1490 {
1491 clear_evalarg(&evalarg, NULL);
1492 return r;
1493 }
1494
1495 // "rettv" will now be a partial referencing the function.
1496 ufunc = rettv.vval.v_partial->pt_func;
1497 ++ufunc->uf_refcount;
1498 clear_tv(&rettv);
1499
1500 // Compile it here to get the return type. The return type is optional,
1501 // when it's missing use t_unknown. This is recognized in
1502 // compile_return().
1503 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1504 ufunc->uf_ret_type = &t_unknown;
1505 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1506
1507 // When the outer function is compiled for profiling or debugging, the
1508 // lambda may be called without profiling or debugging. Compile it here in
1509 // the right context.
1510 if (cctx->ctx_compile_type == CT_DEBUG
1511#ifdef FEAT_PROFILE
1512 || cctx->ctx_compile_type == CT_PROFILE
1513#endif
1514 )
1515 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1516
Bram Moolenaar139575d2022-03-15 19:29:30 +00001517 // if the outer function is not compiled for debugging or profiling, this
1518 // one might be
1519 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001520 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001521 compiletype_T compile_type = get_compile_type(ufunc);
1522
1523 if (compile_type != CT_NONE)
1524 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001525 }
1526
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001527 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1528 // "*arg" may point into it. Point into the original line to avoid a
1529 // dangling pointer.
1530 if (evalarg.eval_using_cmdline)
1531 {
1532 garray_T *gap = &evalarg.eval_tofree_ga;
1533 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1534
1535 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1536 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001537 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001538 }
1539
1540 clear_evalarg(&evalarg, NULL);
1541
1542 if (ufunc->uf_def_status == UF_COMPILED)
1543 {
1544 // The return type will now be known.
1545 set_function_type(ufunc);
1546
1547 // The function reference count will be 1. When the ISN_FUNCREF
1548 // instruction is deleted the reference count is decremented and the
1549 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001550 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001551 }
1552
1553 func_ptr_unref(ufunc);
1554 return FAIL;
1555}
1556
1557/*
1558 * Get a lambda and compile it. Uses Vim9 syntax.
1559 */
1560 int
1561get_lambda_tv_and_compile(
1562 char_u **arg,
1563 typval_T *rettv,
1564 int types_optional,
1565 evalarg_T *evalarg)
1566{
1567 int r;
1568 ufunc_T *ufunc;
1569 int save_sc_version = current_sctx.sc_version;
1570
1571 // Get the funcref in "rettv".
1572 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1573 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1574 current_sctx.sc_version = save_sc_version;
1575 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001576 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001577
1578 // "rettv" will now be a partial referencing the function.
1579 ufunc = rettv->vval.v_partial->pt_func;
1580
1581 // Compile it here to get the return type. The return type is optional,
1582 // when it's missing use t_unknown. This is recognized in
1583 // compile_return().
1584 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1585 ufunc->uf_ret_type = &t_unknown;
1586 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1587
1588 if (ufunc->uf_def_status == UF_COMPILED)
1589 {
1590 // The return type will now be known.
1591 set_function_type(ufunc);
1592 return OK;
1593 }
1594 clear_tv(rettv);
1595 return FAIL;
1596}
1597
1598/*
1599 * parse a dict: {key: val, [key]: val}
1600 * "*arg" points to the '{'.
1601 * ppconst->pp_is_const is set if all item values are a constant.
1602 */
1603 static int
1604compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1605{
1606 garray_T *instr = &cctx->ctx_instr;
1607 int count = 0;
1608 dict_T *d = dict_alloc();
1609 dictitem_T *item;
1610 char_u *whitep = *arg + 1;
1611 char_u *p;
1612 int is_const;
1613 int is_all_const = TRUE; // reset when non-const encountered
1614
1615 if (d == NULL)
1616 return FAIL;
1617 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001618 {
1619 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001620 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001621 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001622 for (;;)
1623 {
1624 char_u *key = NULL;
1625
1626 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1627 {
1628 *arg = NULL;
1629 goto failret;
1630 }
1631
1632 if (**arg == '}')
1633 break;
1634
1635 if (**arg == '[')
1636 {
1637 isn_T *isn;
1638
1639 // {[expr]: value} uses an evaluated key.
1640 *arg = skipwhite(*arg + 1);
1641 if (compile_expr0(arg, cctx) == FAIL)
1642 return FAIL;
1643 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1644 if (isn->isn_type == ISN_PUSHNR)
1645 {
1646 char buf[NUMBUFLEN];
1647
1648 // Convert to string at compile time.
1649 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1650 isn->isn_type = ISN_PUSHS;
1651 isn->isn_arg.string = vim_strsave((char_u *)buf);
1652 }
1653 if (isn->isn_type == ISN_PUSHS)
1654 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001655 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001656 return FAIL;
1657 *arg = skipwhite(*arg);
1658 if (**arg != ']')
1659 {
1660 emsg(_(e_missing_matching_bracket_after_dict_key));
1661 return FAIL;
1662 }
1663 ++*arg;
1664 }
1665 else
1666 {
1667 // {"name": value},
1668 // {'name': value},
1669 // {name: value} use "name" as a literal key
1670 key = get_literal_key(arg);
1671 if (key == NULL)
1672 return FAIL;
1673 if (generate_PUSHS(cctx, &key) == FAIL)
1674 return FAIL;
1675 }
1676
1677 // Check for duplicate keys, if using string keys.
1678 if (key != NULL)
1679 {
1680 item = dict_find(d, key, -1);
1681 if (item != NULL)
1682 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001683 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684 goto failret;
1685 }
1686 item = dictitem_alloc(key);
1687 if (item != NULL)
1688 {
1689 item->di_tv.v_type = VAR_UNKNOWN;
1690 item->di_tv.v_lock = 0;
1691 if (dict_add(d, item) == FAIL)
1692 dictitem_free(item);
1693 }
1694 }
1695
1696 if (**arg != ':')
1697 {
1698 if (*skipwhite(*arg) == ':')
1699 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1700 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001701 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 return FAIL;
1703 }
1704 whitep = *arg + 1;
1705 if (!IS_WHITE_OR_NUL(*whitep))
1706 {
1707 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1708 return FAIL;
1709 }
1710
1711 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1712 {
1713 *arg = NULL;
1714 goto failret;
1715 }
1716
1717 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1718 return FAIL;
1719 if (!is_const)
1720 is_all_const = FALSE;
1721 ++count;
1722
1723 whitep = *arg;
1724 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1725 {
1726 *arg = NULL;
1727 goto failret;
1728 }
1729 if (**arg == '}')
1730 break;
1731 if (**arg != ',')
1732 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001733 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001734 goto failret;
1735 }
1736 if (IS_WHITE_OR_NUL(*whitep))
1737 {
1738 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1739 return FAIL;
1740 }
1741 whitep = *arg + 1;
1742 if (!IS_WHITE_OR_NUL(*whitep))
1743 {
1744 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1745 return FAIL;
1746 }
1747 *arg = skipwhite(whitep);
1748 }
1749
1750 *arg = *arg + 1;
1751
1752 // Allow for following comment, after at least one space.
1753 p = skipwhite(*arg);
1754 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1755 *arg += STRLEN(*arg);
1756
1757 dict_unref(d);
1758 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001759 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001760
1761failret:
1762 if (*arg == NULL)
1763 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001764 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 *arg = (char_u *)"";
1766 }
1767 dict_unref(d);
1768 return FAIL;
1769}
1770
1771/*
1772 * Compile "&option".
1773 */
1774 static int
1775compile_get_option(char_u **arg, cctx_T *cctx)
1776{
1777 typval_T rettv;
1778 char_u *start = *arg;
1779 int ret;
1780
1781 // parse the option and get the current value to get the type.
1782 rettv.v_type = VAR_UNKNOWN;
1783 ret = eval_option(arg, &rettv, TRUE);
1784 if (ret == OK)
1785 {
1786 // include the '&' in the name, eval_option() expects it.
1787 char_u *name = vim_strnsave(start, *arg - start);
1788 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1789 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1790
1791 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1792 vim_free(name);
1793 }
1794 clear_tv(&rettv);
1795
1796 return ret;
1797}
1798
1799/*
1800 * Compile "$VAR".
1801 */
1802 static int
1803compile_get_env(char_u **arg, cctx_T *cctx)
1804{
1805 char_u *start = *arg;
1806 int len;
1807 int ret;
1808 char_u *name;
1809
1810 ++*arg;
1811 len = get_env_len(arg);
1812 if (len == 0)
1813 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001814 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001815 return FAIL;
1816 }
1817
1818 // include the '$' in the name, eval_env_var() expects it.
1819 name = vim_strnsave(start, len + 1);
1820 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1821 vim_free(name);
1822 return ret;
1823}
1824
1825/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001826 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001827 */
1828 static int
1829compile_interp_string(char_u **arg, cctx_T *cctx)
1830{
1831 typval_T tv;
1832 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001833 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001834 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001835 int count = 0;
1836 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001837
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001838 // *arg is on the '$' character, move it to the first string character.
1839 ++*arg;
1840 quote = **arg;
1841 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001842
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001843 for (;;)
1844 {
1845 // Get the string up to the matching quote or to a single '{'.
1846 // "arg" is advanced to either the quote or the '{'.
1847 if (quote == '"')
1848 ret = eval_string(arg, &tv, evaluate, TRUE);
1849 else
1850 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1851 if (ret == FAIL)
1852 break;
1853 if (evaluate)
1854 {
1855 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1856 || (**arg != '{' && count == 0))
1857 {
1858 // generate non-empty string or empty string if it's the only
1859 // one
1860 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1861 return FAIL;
1862 tv.vval.v_string = NULL; // don't free it now
1863 ++count;
1864 }
1865 clear_tv(&tv);
1866 }
1867
1868 if (**arg != '{')
1869 {
1870 // found terminating quote
1871 ++*arg;
1872 break;
1873 }
1874
1875 p = compile_one_expr_in_str(*arg, cctx);
1876 if (p == NULL)
1877 {
1878 ret = FAIL;
1879 break;
1880 }
1881 ++count;
1882 *arg = p;
1883 }
LemonBoy2eaef102022-05-06 13:14:50 +01001884
1885 if (ret == FAIL || !evaluate)
1886 return ret;
1887
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001888 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1889 if (count > 1)
1890 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001891
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001892 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001893}
1894
1895/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001896 * Compile "@r".
1897 */
1898 static int
1899compile_get_register(char_u **arg, cctx_T *cctx)
1900{
1901 int ret;
1902
1903 ++*arg;
1904 if (**arg == NUL)
1905 {
1906 semsg(_(e_syntax_error_at_str), *arg - 1);
1907 return FAIL;
1908 }
1909 if (!valid_yank_reg(**arg, FALSE))
1910 {
1911 emsg_invreg(**arg);
1912 return FAIL;
1913 }
1914 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1915 ++*arg;
1916 return ret;
1917}
1918
1919/*
1920 * Apply leading '!', '-' and '+' to constant "rettv".
1921 * When "numeric_only" is TRUE do not apply '!'.
1922 */
1923 static int
1924apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1925{
1926 char_u *p = *end;
1927
1928 // this works from end to start
1929 while (p > start)
1930 {
1931 --p;
1932 if (*p == '-' || *p == '+')
1933 {
1934 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001935 if (rettv->v_type == VAR_FLOAT)
1936 {
1937 if (*p == '-')
1938 rettv->vval.v_float = -rettv->vval.v_float;
1939 }
1940 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 {
1942 varnumber_T val;
1943 int error = FALSE;
1944
1945 // tv_get_number_chk() accepts a string, but we don't want that
1946 // here
1947 if (check_not_string(rettv) == FAIL)
1948 return FAIL;
1949 val = tv_get_number_chk(rettv, &error);
1950 clear_tv(rettv);
1951 if (error)
1952 return FAIL;
1953 if (*p == '-')
1954 val = -val;
1955 rettv->v_type = VAR_NUMBER;
1956 rettv->vval.v_number = val;
1957 }
1958 }
1959 else if (numeric_only)
1960 {
1961 ++p;
1962 break;
1963 }
1964 else if (*p == '!')
1965 {
1966 int v = tv2bool(rettv);
1967
1968 // '!' is permissive in the type.
1969 clear_tv(rettv);
1970 rettv->v_type = VAR_BOOL;
1971 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1972 }
1973 }
1974 *end = p;
1975 return OK;
1976}
1977
1978/*
1979 * Recognize v: variables that are constants and set "rettv".
1980 */
1981 static void
1982get_vim_constant(char_u **arg, typval_T *rettv)
1983{
1984 if (STRNCMP(*arg, "v:true", 6) == 0)
1985 {
1986 rettv->v_type = VAR_BOOL;
1987 rettv->vval.v_number = VVAL_TRUE;
1988 *arg += 6;
1989 }
1990 else if (STRNCMP(*arg, "v:false", 7) == 0)
1991 {
1992 rettv->v_type = VAR_BOOL;
1993 rettv->vval.v_number = VVAL_FALSE;
1994 *arg += 7;
1995 }
1996 else if (STRNCMP(*arg, "v:null", 6) == 0)
1997 {
1998 rettv->v_type = VAR_SPECIAL;
1999 rettv->vval.v_number = VVAL_NULL;
2000 *arg += 6;
2001 }
2002 else if (STRNCMP(*arg, "v:none", 6) == 0)
2003 {
2004 rettv->v_type = VAR_SPECIAL;
2005 rettv->vval.v_number = VVAL_NONE;
2006 *arg += 6;
2007 }
2008}
2009
2010 exprtype_T
2011get_compare_type(char_u *p, int *len, int *type_is)
2012{
2013 exprtype_T type = EXPR_UNKNOWN;
2014 int i;
2015
2016 switch (p[0])
2017 {
2018 case '=': if (p[1] == '=')
2019 type = EXPR_EQUAL;
2020 else if (p[1] == '~')
2021 type = EXPR_MATCH;
2022 break;
2023 case '!': if (p[1] == '=')
2024 type = EXPR_NEQUAL;
2025 else if (p[1] == '~')
2026 type = EXPR_NOMATCH;
2027 break;
2028 case '>': if (p[1] != '=')
2029 {
2030 type = EXPR_GREATER;
2031 *len = 1;
2032 }
2033 else
2034 type = EXPR_GEQUAL;
2035 break;
2036 case '<': if (p[1] != '=')
2037 {
2038 type = EXPR_SMALLER;
2039 *len = 1;
2040 }
2041 else
2042 type = EXPR_SEQUAL;
2043 break;
2044 case 'i': if (p[1] == 's')
2045 {
2046 // "is" and "isnot"; but not a prefix of a name
2047 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2048 *len = 5;
2049 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002050 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002051 {
2052 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2053 *type_is = TRUE;
2054 }
2055 }
2056 break;
2057 }
2058 return type;
2059}
2060
2061/*
2062 * Skip over an expression, ignoring most errors.
2063 */
2064 void
2065skip_expr_cctx(char_u **arg, cctx_T *cctx)
2066{
2067 evalarg_T evalarg;
2068
2069 init_evalarg(&evalarg);
2070 evalarg.eval_cctx = cctx;
2071 skip_expr(arg, &evalarg);
2072 clear_evalarg(&evalarg, NULL);
2073}
2074
2075/*
2076 * Check that the top of the type stack has a type that can be used as a
2077 * condition. Give an error and return FAIL if not.
2078 */
2079 int
2080bool_on_stack(cctx_T *cctx)
2081{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082 type_T *type;
2083
Bram Moolenaar078a4612022-01-04 15:17:03 +00002084 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085 if (type == &t_bool)
2086 return OK;
2087
Bram Moolenaar4913d422022-10-17 13:13:32 +01002088 if (type->tt_type == VAR_ANY
2089 || type->tt_type == VAR_UNKNOWN
2090 || type->tt_type == VAR_NUMBER
2091 || type == &t_number_bool
2092 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002093 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2094 // This requires a runtime type check.
2095 return generate_COND2BOOL(cctx);
2096
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002097 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098}
2099
2100/*
2101 * Give the "white on both sides" error, taking the operator from "p[len]".
2102 */
2103 void
2104error_white_both(char_u *op, int len)
2105{
2106 char_u buf[10];
2107
2108 vim_strncpy(buf, op, len);
2109 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2110}
2111
2112/*
2113 * Compile code to apply '-', '+' and '!'.
2114 * When "numeric_only" is TRUE do not apply '!'.
2115 */
2116 static int
2117compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2118{
2119 char_u *p = *end;
2120
2121 // this works from end to start
2122 while (p > start)
2123 {
2124 --p;
2125 while (VIM_ISWHITE(*p))
2126 --p;
2127 if (*p == '-' || *p == '+')
2128 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002129 type_T *type = get_type_on_stack(cctx, 0);
2130 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002131 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132 return FAIL;
2133
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002134 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002135 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2136 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 }
2138 else if (numeric_only)
2139 {
2140 ++p;
2141 break;
2142 }
2143 else
2144 {
2145 int invert = *p == '!';
2146
2147 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2148 {
2149 if (p[-1] == '!')
2150 invert = !invert;
2151 --p;
2152 }
2153 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2154 return FAIL;
2155 }
2156 }
2157 *end = p;
2158 return OK;
2159}
2160
2161/*
2162 * Compile "(expression)": recursive!
2163 * Return FAIL/OK.
2164 */
2165 static int
2166compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2167{
2168 int ret;
2169 char_u *p = *arg + 1;
2170
2171 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2172 return FAIL;
2173 if (ppconst->pp_used <= PPSIZE - 10)
2174 {
2175 ret = compile_expr1(arg, cctx, ppconst);
2176 }
2177 else
2178 {
2179 // Not enough space in ppconst, flush constants.
2180 if (generate_ppconst(cctx, ppconst) == FAIL)
2181 return FAIL;
2182 ret = compile_expr0(arg, cctx);
2183 }
2184 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2185 return FAIL;
2186 if (**arg == ')')
2187 ++*arg;
2188 else if (ret == OK)
2189 {
2190 emsg(_(e_missing_closing_paren));
2191 ret = FAIL;
2192 }
2193 return ret;
2194}
2195
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002196static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002197
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002198/*
2199 * Compile whatever comes after "name" or "name()".
2200 * Advances "*arg" only when something was recognized.
2201 */
2202 static int
2203compile_subscript(
2204 char_u **arg,
2205 cctx_T *cctx,
2206 char_u *start_leader,
2207 char_u **end_leader,
2208 ppconst_T *ppconst)
2209{
2210 char_u *name_start = *end_leader;
2211 int keeping_dict = FALSE;
2212
2213 for (;;)
2214 {
2215 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002216 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002217
2218 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2219 {
2220 char_u *next = peek_next_line_from_context(cctx);
2221
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002222 // If a following line starts with "->{", "->(" or "->X" advance to
2223 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 // Also if a following line starts with ".x".
2225 if (next != NULL &&
2226 ((next[0] == '-' && next[1] == '>'
2227 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002228 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 || ASCII_ISALPHA(*skipwhite(next + 2))))
2230 || (next[0] == '.' && eval_isdictc(next[1]))))
2231 {
2232 next = next_line_from_context(cctx, TRUE);
2233 if (next == NULL)
2234 return FAIL;
2235 *arg = next;
2236 p = skipwhite(*arg);
2237 }
2238 }
2239
2240 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2241 // is not a function call.
2242 if (**arg == '(')
2243 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002244 int argcount = 0;
2245
2246 if (generate_ppconst(cctx, ppconst) == FAIL)
2247 return FAIL;
2248 ppconst->pp_is_const = FALSE;
2249
2250 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002251 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252
2253 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002254 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002255 return FAIL;
2256 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2257 return FAIL;
2258 if (keeping_dict)
2259 {
2260 keeping_dict = FALSE;
2261 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2262 return FAIL;
2263 }
2264 }
2265 else if (*p == '-' && p[1] == '>')
2266 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002267 char_u *pstart = p;
2268 int alt;
2269 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270
Bram Moolenaarc7349932022-01-16 20:59:39 +00002271 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002272 if (generate_ppconst(cctx, ppconst) == FAIL)
2273 return FAIL;
2274 ppconst->pp_is_const = FALSE;
2275
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276 // Apply the '!', '-' and '+' first:
2277 // -1.0->func() works like (-1.0)->func()
2278 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2279 return FAIL;
2280
2281 p += 2;
2282 *arg = skipwhite(p);
2283 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002284
2285 // Three alternatives handled here:
2286 // 1. "base->name(" only a name, use compile_call()
2287 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2288 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002290 alt = 2;
2291 else
2292 {
2293 // alternative 1 or 3
2294 p = *arg;
2295 if (!eval_isnamec1(*p))
2296 {
2297 semsg(_(e_trailing_characters_str), pstart);
2298 return FAIL;
2299 }
2300 if (ASCII_ISALPHA(*p) && p[1] == ':')
2301 p += 2;
2302 for ( ; eval_isnamec(*p); ++p)
2303 ;
2304 if (*p == '(')
2305 {
2306 // alternative 1
2307 alt = 1;
2308 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2309 return FAIL;
2310 }
2311 else
2312 {
2313 // Must be alternative 3, find the "(". Only works within
2314 // one line.
2315 alt = 3;
2316 paren = vim_strchr(p, '(');
2317 if (paren == NULL)
2318 {
2319 semsg(_(e_missing_parenthesis_str), *arg);
2320 return FAIL;
2321 }
2322 }
2323 }
2324
2325 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 {
2327 int argcount = 1;
2328 garray_T *stack = &cctx->ctx_type_stack;
2329 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002330 int expr_isn_start = cctx->ctx_instr.ga_len;
2331 int expr_isn_end;
2332 int arg_isn_count;
2333
Bram Moolenaarc7349932022-01-16 20:59:39 +00002334 if (alt == 2)
2335 {
2336 // Funcref call: list->(Refs[2])(arg)
2337 // or lambda: list->((arg) => expr)(arg)
2338 //
2339 // Fist compile the function expression.
2340 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2341 return FAIL;
2342 }
2343 else
2344 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002345 int fail;
2346 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002347 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002348
Bram Moolenaarc7349932022-01-16 20:59:39 +00002349 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002350
2351 // instead of using LOADG for "import.Func" use PUSHFUNC
2352 ++paren_follows_after_expr;
2353
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002354 // do not look in the next line
2355 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002356
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002357 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002358 || *skipwhite(*arg) != NUL;
2359 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002360 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002361 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002362
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002363 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002364 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002365 if (did_emsg == prev_did_emsg)
2366 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002367 return FAIL;
2368 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002369 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371 // Compile the arguments.
2372 if (**arg != '(')
2373 {
2374 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002375 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002376 else
2377 semsg(_(e_missing_parenthesis_str), *arg);
2378 return FAIL;
2379 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002380
2381 // Remember the next instruction index, where the instructions
2382 // for arguments are being written.
2383 expr_isn_end = cctx->ctx_instr.ga_len;
2384
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002385 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002386 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2387 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002388 return FAIL;
2389
2390 // Move the instructions for the arguments to before the
2391 // instructions of the expression and move the type of the
2392 // expression after the argument types. This is what ISN_PCALL
2393 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2395 if (arg_isn_count > 0)
2396 {
2397 int expr_isn_count = expr_isn_end - expr_isn_start;
2398 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002399 type_T *decl_type;
2400 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002401
2402 if (isn == NULL)
2403 return FAIL;
2404 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2405 + expr_isn_start,
2406 sizeof(isn_T) * expr_isn_count);
2407 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2408 + expr_isn_start,
2409 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2410 sizeof(isn_T) * arg_isn_count);
2411 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2412 + expr_isn_start + arg_isn_count,
2413 isn, sizeof(isn_T) * expr_isn_count);
2414 vim_free(isn);
2415
Bram Moolenaar078a4612022-01-04 15:17:03 +00002416 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2417 type = typep->type_curr;
2418 decl_type = typep->type_decl;
2419 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2420 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2421 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002423 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2424 typep->type_curr = type;
2425 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426 }
2427
Bram Moolenaar078a4612022-01-04 15:17:03 +00002428 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2430 return FAIL;
2431 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002432
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433 if (keeping_dict)
2434 {
2435 keeping_dict = FALSE;
2436 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2437 return FAIL;
2438 }
2439 }
2440 else if (**arg == '[')
2441 {
2442 int is_slice = FALSE;
2443
2444 // list index: list[123]
2445 // dict member: dict[key]
2446 // string index: text[123]
2447 // blob index: blob[123]
2448 if (generate_ppconst(cctx, ppconst) == FAIL)
2449 return FAIL;
2450 ppconst->pp_is_const = FALSE;
2451
2452 ++p;
2453 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2454 return FAIL;
2455 if (**arg == ':')
2456 {
2457 // missing first index is equal to zero
2458 generate_PUSHNR(cctx, 0);
2459 }
2460 else
2461 {
2462 if (compile_expr0(arg, cctx) == FAIL)
2463 return FAIL;
2464 if (**arg == ':')
2465 {
2466 semsg(_(e_white_space_required_before_and_after_str_at_str),
2467 ":", *arg);
2468 return FAIL;
2469 }
2470 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2471 return FAIL;
2472 *arg = skipwhite(*arg);
2473 }
2474 if (**arg == ':')
2475 {
2476 is_slice = TRUE;
2477 ++*arg;
2478 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2479 {
2480 semsg(_(e_white_space_required_before_and_after_str_at_str),
2481 ":", *arg);
2482 return FAIL;
2483 }
2484 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2485 return FAIL;
2486 if (**arg == ']')
2487 // missing second index is equal to end of string
2488 generate_PUSHNR(cctx, -1);
2489 else
2490 {
2491 if (compile_expr0(arg, cctx) == FAIL)
2492 return FAIL;
2493 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2494 return FAIL;
2495 *arg = skipwhite(*arg);
2496 }
2497 }
2498
2499 if (**arg != ']')
2500 {
2501 emsg(_(e_missing_closing_square_brace));
2502 return FAIL;
2503 }
2504 *arg = *arg + 1;
2505
2506 if (keeping_dict)
2507 {
2508 keeping_dict = FALSE;
2509 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2510 return FAIL;
2511 }
2512 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2513 return FAIL;
2514 }
2515 else if (*p == '.' && p[1] != '.')
2516 {
2517 // dictionary member: dict.name
2518 if (generate_ppconst(cctx, ppconst) == FAIL)
2519 return FAIL;
2520 ppconst->pp_is_const = FALSE;
2521
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002522 type = get_type_on_stack(cctx, 0);
2523 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002524 && (type->tt_type == VAR_CLASS
2525 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002526 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002527 // class member: SomeClass.varname
2528 // class method: SomeClass.SomeMethod()
2529 // class constructor: SomeClass.new()
2530 // object member: someObject.varname, this.varname
2531 // object method: someObject.SomeMethod(), this.SomeMethod()
2532 *arg = p;
2533 if (compile_class_object_index(cctx, arg, type) == FAIL)
2534 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002536 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002537 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002538 *arg = p + 1;
2539 if (IS_WHITE_OR_NUL(**arg))
2540 {
2541 emsg(_(e_missing_name_after_dot));
2542 return FAIL;
2543 }
2544 p = *arg;
2545 if (eval_isdictc(*p))
2546 while (eval_isnamec(*p))
2547 MB_PTR_ADV(p);
2548 if (p == *arg)
2549 {
2550 semsg(_(e_syntax_error_at_str), *arg);
2551 return FAIL;
2552 }
2553 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2554 return FAIL;
2555 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2556 return FAIL;
2557 keeping_dict = TRUE;
2558 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002560 }
2561 else
2562 break;
2563 }
2564
2565 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2566 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002567 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2568 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002569 return FAIL;
2570
2571 return OK;
2572}
2573
2574/*
2575 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2576 * "arg" is advanced until after the expression, skipping white space.
2577 *
2578 * If the value is a constant "ppconst->pp_used" will be non-zero.
2579 * Before instructions are generated, any values in "ppconst" will generated.
2580 *
2581 * This is the compiling equivalent of eval1(), eval2(), etc.
2582 */
2583
2584/*
2585 * number number constant
2586 * 0zFFFFFFFF Blob constant
2587 * "string" string constant
2588 * 'string' literal string constant
2589 * &option-name option value
2590 * @r register contents
2591 * identifier variable value
2592 * function() function call
2593 * $VAR environment variable
2594 * (expression) nested expression
2595 * [expr, expr] List
2596 * {key: val, [key]: val} Dictionary
2597 *
2598 * Also handle:
2599 * ! in front logical NOT
2600 * - in front unary minus
2601 * + in front unary plus (ignored)
2602 * trailing (arg) funcref/partial call
2603 * trailing [] subscript in String or List
2604 * trailing .name entry in Dictionary
2605 * trailing ->name() method call
2606 */
2607 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002608compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002609 char_u **arg,
2610 cctx_T *cctx,
2611 ppconst_T *ppconst)
2612{
2613 char_u *start_leader, *end_leader;
2614 int ret = OK;
2615 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2616 int used_before = ppconst->pp_used;
2617
2618 ppconst->pp_is_const = FALSE;
2619
2620 /*
2621 * Skip '!', '-' and '+' characters. They are handled later.
2622 */
2623 start_leader = *arg;
2624 if (eval_leader(arg, TRUE) == FAIL)
2625 return FAIL;
2626 end_leader = *arg;
2627
2628 rettv->v_type = VAR_UNKNOWN;
2629 switch (**arg)
2630 {
2631 /*
2632 * Number constant.
2633 */
2634 case '0': // also for blob starting with 0z
2635 case '1':
2636 case '2':
2637 case '3':
2638 case '4':
2639 case '5':
2640 case '6':
2641 case '7':
2642 case '8':
2643 case '9':
2644 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2645 return FAIL;
2646 // Apply "-" and "+" just before the number now, right to
2647 // left. Matters especially when "->" follows. Stops at
2648 // '!'.
2649 if (apply_leader(rettv, TRUE,
2650 start_leader, &end_leader) == FAIL)
2651 {
2652 clear_tv(rettv);
2653 return FAIL;
2654 }
2655 break;
2656
2657 /*
2658 * String constant: "string".
2659 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002660 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002661 return FAIL;
2662 break;
2663
2664 /*
2665 * Literal string constant: 'str''ing'.
2666 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002667 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002668 return FAIL;
2669 break;
2670
2671 /*
2672 * Constant Vim variable.
2673 */
2674 case 'v': get_vim_constant(arg, rettv);
2675 ret = NOTDONE;
2676 break;
2677
2678 /*
2679 * "true" constant
2680 */
2681 case 't': if (STRNCMP(*arg, "true", 4) == 0
2682 && !eval_isnamec((*arg)[4]))
2683 {
2684 *arg += 4;
2685 rettv->v_type = VAR_BOOL;
2686 rettv->vval.v_number = VVAL_TRUE;
2687 }
2688 else
2689 ret = NOTDONE;
2690 break;
2691
2692 /*
2693 * "false" constant
2694 */
2695 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2696 && !eval_isnamec((*arg)[5]))
2697 {
2698 *arg += 5;
2699 rettv->v_type = VAR_BOOL;
2700 rettv->vval.v_number = VVAL_FALSE;
2701 }
2702 else
2703 ret = NOTDONE;
2704 break;
2705
2706 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002707 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002709 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002710 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002711 char_u *p = *arg + 4;
2712 int len;
2713
2714 for (len = 0; eval_isnamec(p[len]); ++len)
2715 ;
2716 ret = handle_predefined(*arg, len + 4, rettv);
2717 if (ret == FAIL)
2718 ret = NOTDONE;
2719 else
2720 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002721 }
2722 else
2723 ret = NOTDONE;
2724 break;
2725
2726 /*
2727 * List: [expr, expr]
2728 */
2729 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2730 return FAIL;
2731 ret = compile_list(arg, cctx, ppconst);
2732 break;
2733
2734 /*
2735 * Dictionary: {'key': val, 'key': val}
2736 */
2737 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2738 return FAIL;
2739 ret = compile_dict(arg, cctx, ppconst);
2740 break;
2741
2742 /*
2743 * Option value: &name
2744 */
2745 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2746 return FAIL;
2747 ret = compile_get_option(arg, cctx);
2748 break;
2749
2750 /*
2751 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002752 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002753 */
2754 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2755 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002756 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2757 ret = compile_interp_string(arg, cctx);
2758 else
2759 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002760 break;
2761
2762 /*
2763 * Register contents: @r.
2764 */
2765 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2766 return FAIL;
2767 ret = compile_get_register(arg, cctx);
2768 break;
2769 /*
2770 * nested expression: (expression).
2771 * lambda: (arg, arg) => expr
2772 * funcref: (arg, arg) => { statement }
2773 */
2774 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2775 ret = compile_lambda(arg, cctx);
2776 if (ret == NOTDONE)
2777 ret = compile_parenthesis(arg, cctx, ppconst);
2778 break;
2779
2780 default: ret = NOTDONE;
2781 break;
2782 }
2783 if (ret == FAIL)
2784 return FAIL;
2785
2786 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2787 {
2788 if (cctx->ctx_skip == SKIP_YES)
2789 clear_tv(rettv);
2790 else
2791 // A constant expression can possibly be handled compile time,
2792 // return the value instead of generating code.
2793 ++ppconst->pp_used;
2794 }
2795 else if (ret == NOTDONE)
2796 {
2797 char_u *p;
2798 int r;
2799
2800 if (!eval_isnamec1(**arg))
2801 {
2802 if (!vim9_bad_comment(*arg))
2803 {
2804 if (ends_excmd(*skipwhite(*arg)))
2805 semsg(_(e_empty_expression_str), *arg);
2806 else
2807 semsg(_(e_name_expected_str), *arg);
2808 }
2809 return FAIL;
2810 }
2811
2812 // "name" or "name()"
2813 p = to_name_end(*arg, TRUE);
2814 if (p - *arg == (size_t)1 && **arg == '_')
2815 {
2816 emsg(_(e_cannot_use_underscore_here));
2817 return FAIL;
2818 }
2819
2820 if (*p == '(')
2821 {
2822 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2823 }
2824 else
2825 {
2826 if (cctx->ctx_skip != SKIP_YES
2827 && generate_ppconst(cctx, ppconst) == FAIL)
2828 return FAIL;
2829 r = compile_load(arg, p, cctx, TRUE, TRUE);
2830 }
2831 if (r == FAIL)
2832 return FAIL;
2833 }
2834
2835 // Handle following "[]", ".member", etc.
2836 // Then deal with prefixed '-', '+' and '!', if not done already.
2837 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2838 ppconst) == FAIL)
2839 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002840 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002841 {
2842 // apply the '!', '-' and '+' before the constant
2843 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2844 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2845 return FAIL;
2846 return OK;
2847 }
2848 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2849 return FAIL;
2850 return OK;
2851}
2852
2853/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002854 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002855 */
2856 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002857compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002858{
2859 type_T *want_type = NULL;
2860
2861 // Recognize <type>
2862 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2863 {
2864 ++*arg;
2865 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2866 if (want_type == NULL)
2867 return FAIL;
2868
2869 if (**arg != '>')
2870 {
2871 if (*skipwhite(*arg) == '>')
2872 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2873 else
2874 emsg(_(e_missing_gt));
2875 return FAIL;
2876 }
2877 ++*arg;
2878 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2879 return FAIL;
2880 }
2881
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002882 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002883 return FAIL;
2884
2885 if (want_type != NULL)
2886 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002887 type_T *actual;
2888 where_T where = WHERE_INIT;
2889
LemonBoy50d48542024-07-04 17:03:17 +02002890 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002891 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002892 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002893 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002894 {
LemonBoy50d48542024-07-04 17:03:17 +02002895 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2896 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002897 return FAIL;
2898 }
2899 }
2900
2901 return OK;
2902}
2903
2904/*
2905 * * number multiplication
2906 * / number division
2907 * % number modulo
2908 */
2909 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002910compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002911{
2912 char_u *op;
2913 char_u *next;
2914 int ppconst_used = ppconst->pp_used;
2915
2916 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002917 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002918 return FAIL;
2919
2920 /*
2921 * Repeat computing, until no "*", "/" or "%" is following.
2922 */
2923 for (;;)
2924 {
2925 op = may_peek_next_line(cctx, *arg, &next);
2926 if (*op != '*' && *op != '/' && *op != '%')
2927 break;
2928 if (next != NULL)
2929 {
2930 *arg = next_line_from_context(cctx, TRUE);
2931 op = skipwhite(*arg);
2932 }
2933
2934 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2935 {
2936 error_white_both(op, 1);
2937 return FAIL;
2938 }
2939 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2940 return FAIL;
2941
2942 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002943 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002944 return FAIL;
2945
2946 if (ppconst->pp_used == ppconst_used + 2
2947 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2948 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2949 {
2950 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2951 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2952 varnumber_T res = 0;
2953 int failed = FALSE;
2954
2955 // both are numbers: compute the result
2956 switch (*op)
2957 {
2958 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2959 break;
2960 case '/': res = num_divide(tv1->vval.v_number,
2961 tv2->vval.v_number, &failed);
2962 break;
2963 case '%': res = num_modulus(tv1->vval.v_number,
2964 tv2->vval.v_number, &failed);
2965 break;
2966 }
2967 if (failed)
2968 return FAIL;
2969 tv1->vval.v_number = res;
2970 --ppconst->pp_used;
2971 }
2972 else
2973 {
2974 generate_ppconst(cctx, ppconst);
2975 generate_two_op(cctx, op);
2976 }
2977 }
2978
2979 return OK;
2980}
2981
2982/*
2983 * + number addition or list/blobl concatenation
2984 * - number subtraction
2985 * .. string concatenation
2986 */
2987 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002988compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002989{
2990 char_u *op;
2991 char_u *next;
2992 int oplen;
2993 int ppconst_used = ppconst->pp_used;
2994
2995 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002996 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002997 return FAIL;
2998
2999 /*
3000 * Repeat computing, until no "+", "-" or ".." is following.
3001 */
3002 for (;;)
3003 {
3004 op = may_peek_next_line(cctx, *arg, &next);
3005 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
3006 break;
3007 if (op[0] == op[1] && *op != '.' && next)
3008 // Finding "++" or "--" on the next line is a separate command.
3009 // But ".." is concatenation.
3010 break;
3011 oplen = (*op == '.' ? 2 : 1);
3012 if (next != NULL)
3013 {
3014 *arg = next_line_from_context(cctx, TRUE);
3015 op = skipwhite(*arg);
3016 }
3017
3018 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3019 {
3020 error_white_both(op, oplen);
3021 return FAIL;
3022 }
3023
3024 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3025 return FAIL;
3026
3027 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003028 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003029 return FAIL;
3030
3031 if (ppconst->pp_used == ppconst_used + 2
3032 && (*op == '.'
3033 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3034 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3035 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3036 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3037 {
3038 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3039 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3040
3041 // concat/subtract/add constant numbers
3042 if (*op == '+')
3043 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3044 else if (*op == '-')
3045 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3046 else
3047 {
3048 // concatenate constant strings
3049 char_u *s1 = tv1->vval.v_string;
3050 char_u *s2 = tv2->vval.v_string;
3051 size_t len1 = STRLEN(s1);
3052
3053 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3054 if (tv1->vval.v_string == NULL)
3055 {
3056 clear_ppconst(ppconst);
3057 return FAIL;
3058 }
3059 mch_memmove(tv1->vval.v_string, s1, len1);
3060 STRCPY(tv1->vval.v_string + len1, s2);
3061 vim_free(s1);
3062 vim_free(s2);
3063 }
3064 --ppconst->pp_used;
3065 }
3066 else
3067 {
3068 generate_ppconst(cctx, ppconst);
3069 ppconst->pp_is_const = FALSE;
3070 if (*op == '.')
3071 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003072 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3073 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003074 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003075 if (generate_CONCAT(cctx, 2) == FAIL)
3076 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003077 }
3078 else
3079 generate_two_op(cctx, op);
3080 }
3081 }
3082
3083 return OK;
3084}
3085
3086/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003087 * expr6a >> expr6b
3088 * expr6a << expr6b
3089 *
3090 * Produces instructions:
3091 * OPNR bitwise left or right shift
3092 */
3093 static int
3094compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3095{
3096 exprtype_T type = EXPR_UNKNOWN;
3097 char_u *p;
3098 char_u *next;
3099 int len = 2;
3100 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003101 isn_T *isn;
3102
3103 // get the first variable
3104 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3105 return FAIL;
3106
3107 /*
3108 * Repeat computing, until no "+", "-" or ".." is following.
3109 */
3110 for (;;)
3111 {
3112 type = EXPR_UNKNOWN;
3113
3114 p = may_peek_next_line(cctx, *arg, &next);
3115 if (p[0] == '<' && p[1] == '<')
3116 type = EXPR_LSHIFT;
3117 else if (p[0] == '>' && p[1] == '>')
3118 type = EXPR_RSHIFT;
3119
3120 if (type == EXPR_UNKNOWN)
3121 return OK;
3122
3123 // Handle a bitwise left or right shift operator
3124 if (ppconst->pp_used == ppconst_used + 1)
3125 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003126 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003127 {
3128 // left operand should be a number
3129 emsg(_(e_bitshift_ops_must_be_number));
3130 return FAIL;
3131 }
3132 }
3133 else
3134 {
3135 type_T *t = get_type_on_stack(cctx, 0);
3136
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003137 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003138 {
3139 emsg(_(e_bitshift_ops_must_be_number));
3140 return FAIL;
3141 }
3142 }
3143
3144 if (next != NULL)
3145 {
3146 *arg = next_line_from_context(cctx, TRUE);
3147 p = skipwhite(*arg);
3148 }
3149
3150 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3151 {
3152 error_white_both(p, len);
3153 return FAIL;
3154 }
3155
3156 // get the second variable
3157 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3158 return FAIL;
3159
3160 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3161 return FAIL;
3162
3163 if (ppconst->pp_used == ppconst_used + 2)
3164 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003165 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3166 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3167
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003168 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003169 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3170 {
3171 // right operand should be a positive number
3172 if (tv2->v_type != VAR_NUMBER)
3173 emsg(_(e_bitshift_ops_must_be_number));
3174 else
dundargocc57b5bc2022-11-02 13:30:51 +00003175 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003176 return FAIL;
3177 }
3178
3179 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3180 tv1->vval.v_number = 0;
3181 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003182 tv1->vval.v_number =
3183 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003184 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003185 tv1->vval.v_number =
3186 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003187 clear_tv(tv2);
3188 --ppconst->pp_used;
3189 }
3190 else
3191 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003192 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3193 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003194 {
3195 emsg(_(e_bitshift_ops_must_be_number));
3196 return FAIL;
3197 }
3198
3199 generate_ppconst(cctx, ppconst);
3200
3201 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3202 if (isn == NULL)
3203 return FAIL;
3204
3205 if (isn != NULL)
3206 isn->isn_arg.op.op_type = type;
3207 }
3208 }
3209
3210 return OK;
3211}
3212
3213/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003214 * expr5a == expr5b
3215 * expr5a =~ expr5b
3216 * expr5a != expr5b
3217 * expr5a !~ expr5b
3218 * expr5a > expr5b
3219 * expr5a >= expr5b
3220 * expr5a < expr5b
3221 * expr5a <= expr5b
3222 * expr5a is expr5b
3223 * expr5a isnot expr5b
3224 *
3225 * Produces instructions:
3226 * EVAL expr5a Push result of "expr5a"
3227 * EVAL expr5b Push result of "expr5b"
3228 * COMPARE one of the compare instructions
3229 */
3230 static int
3231compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3232{
3233 exprtype_T type = EXPR_UNKNOWN;
3234 char_u *p;
3235 char_u *next;
3236 int len = 2;
3237 int type_is = FALSE;
3238 int ppconst_used = ppconst->pp_used;
3239
3240 // get the first variable
3241 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3242 return FAIL;
3243
3244 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003245
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003246 type = get_compare_type(p, &len, &type_is);
3247
3248 /*
3249 * If there is a comparative operator, use it.
3250 */
3251 if (type != EXPR_UNKNOWN)
3252 {
3253 int ic = FALSE; // Default: do not ignore case
3254
3255 if (next != NULL)
3256 {
3257 *arg = next_line_from_context(cctx, TRUE);
3258 p = skipwhite(*arg);
3259 }
3260 if (type_is && (p[len] == '?' || p[len] == '#'))
3261 {
3262 semsg(_(e_invalid_expression_str), *arg);
3263 return FAIL;
3264 }
3265 // extra question mark appended: ignore case
3266 if (p[len] == '?')
3267 {
3268 ic = TRUE;
3269 ++len;
3270 }
3271 // extra '#' appended: match case (ignored)
3272 else if (p[len] == '#')
3273 ++len;
3274 // nothing appended: match case
3275
3276 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3277 {
3278 error_white_both(p, len);
3279 return FAIL;
3280 }
3281
3282 // get the second variable
3283 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3284 return FAIL;
3285
3286 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3287 return FAIL;
3288
3289 if (ppconst->pp_used == ppconst_used + 2)
3290 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003291 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003292 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3293 int ret;
3294
3295 // Both sides are a constant, compute the result now.
3296 // First check for a valid combination of types, this is more
3297 // strict than typval_compare().
3298 if (check_compare_types(type, tv1, tv2) == FAIL)
3299 ret = FAIL;
3300 else
3301 {
3302 ret = typval_compare(tv1, tv2, type, ic);
3303 tv1->v_type = VAR_BOOL;
3304 tv1->vval.v_number = tv1->vval.v_number
3305 ? VVAL_TRUE : VVAL_FALSE;
3306 clear_tv(tv2);
3307 --ppconst->pp_used;
3308 }
3309 return ret;
3310 }
3311
3312 generate_ppconst(cctx, ppconst);
3313 return generate_COMPARE(cctx, type, ic);
3314 }
3315
3316 return OK;
3317}
3318
3319static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3320
3321/*
3322 * Compile || or &&.
3323 */
3324 static int
3325compile_and_or(
3326 char_u **arg,
3327 cctx_T *cctx,
3328 char *op,
3329 ppconst_T *ppconst,
3330 int ppconst_used UNUSED)
3331{
3332 char_u *next;
3333 char_u *p = may_peek_next_line(cctx, *arg, &next);
3334 int opchar = *op;
3335
3336 if (p[0] == opchar && p[1] == opchar)
3337 {
3338 garray_T *instr = &cctx->ctx_instr;
3339 garray_T end_ga;
3340 int save_skip = cctx->ctx_skip;
3341
3342 /*
3343 * Repeat until there is no following "||" or "&&"
3344 */
3345 ga_init2(&end_ga, sizeof(int), 10);
3346 while (p[0] == opchar && p[1] == opchar)
3347 {
3348 long start_lnum = SOURCING_LNUM;
3349 long save_sourcing_lnum;
3350 int start_ctx_lnum = cctx->ctx_lnum;
3351 int save_lnum;
3352 int const_used;
3353 int status;
3354 jumpwhen_T jump_when = opchar == '|'
3355 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3356
3357 if (next != NULL)
3358 {
3359 *arg = next_line_from_context(cctx, TRUE);
3360 p = skipwhite(*arg);
3361 }
3362
3363 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3364 {
3365 semsg(_(e_white_space_required_before_and_after_str_at_str),
3366 op, p);
3367 ga_clear(&end_ga);
3368 return FAIL;
3369 }
3370
3371 save_sourcing_lnum = SOURCING_LNUM;
3372 SOURCING_LNUM = start_lnum;
3373 save_lnum = cctx->ctx_lnum;
3374 cctx->ctx_lnum = start_ctx_lnum;
3375
3376 status = check_ppconst_bool(ppconst);
3377 if (status != FAIL)
3378 {
3379 // Use the last ppconst if possible.
3380 if (ppconst->pp_used > 0)
3381 {
3382 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3383 int is_true = tv2bool(tv);
3384
3385 if ((is_true && opchar == '|')
3386 || (!is_true && opchar == '&'))
3387 {
3388 // For "false && expr" and "true || expr" the "expr"
3389 // does not need to be evaluated.
3390 cctx->ctx_skip = SKIP_YES;
3391 clear_tv(tv);
3392 tv->v_type = VAR_BOOL;
3393 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3394 }
3395 else
3396 {
3397 // For "true && expr" and "false || expr" only "expr"
3398 // needs to be evaluated.
3399 --ppconst->pp_used;
3400 jump_when = JUMP_NEVER;
3401 }
3402 }
3403 else
3404 {
3405 // Every part must evaluate to a bool.
3406 status = bool_on_stack(cctx);
3407 }
3408 }
3409 if (status != FAIL)
3410 status = ga_grow(&end_ga, 1);
3411 cctx->ctx_lnum = save_lnum;
3412 if (status == FAIL)
3413 {
3414 ga_clear(&end_ga);
3415 return FAIL;
3416 }
3417
3418 if (jump_when != JUMP_NEVER)
3419 {
3420 if (cctx->ctx_skip != SKIP_YES)
3421 {
3422 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3423 ++end_ga.ga_len;
3424 }
3425 generate_JUMP(cctx, jump_when, 0);
3426 }
3427
3428 // eval the next expression
3429 SOURCING_LNUM = save_sourcing_lnum;
3430 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3431 {
3432 ga_clear(&end_ga);
3433 return FAIL;
3434 }
3435
3436 const_used = ppconst->pp_used;
3437 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3438 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3439 {
3440 ga_clear(&end_ga);
3441 return FAIL;
3442 }
3443
3444 // "0 || 1" results in true, "1 && 0" results in false.
3445 if (ppconst->pp_used == const_used + 1)
3446 {
3447 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3448
3449 if (tv->v_type == VAR_NUMBER
3450 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3451 {
3452 tv->vval.v_number = tv->vval.v_number == 1
3453 ? VVAL_TRUE : VVAL_FALSE;
3454 tv->v_type = VAR_BOOL;
3455 }
3456 }
3457
3458 p = may_peek_next_line(cctx, *arg, &next);
3459 }
3460
3461 if (check_ppconst_bool(ppconst) == FAIL)
3462 {
3463 ga_clear(&end_ga);
3464 return FAIL;
3465 }
3466
3467 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3468 // Every part must evaluate to a bool.
3469 if (bool_on_stack(cctx) == FAIL)
3470 {
3471 ga_clear(&end_ga);
3472 return FAIL;
3473 }
3474
3475 if (end_ga.ga_len > 0)
3476 {
3477 // Fill in the end label in all jumps.
3478 generate_ppconst(cctx, ppconst);
3479 while (end_ga.ga_len > 0)
3480 {
3481 isn_T *isn;
3482
3483 --end_ga.ga_len;
3484 isn = ((isn_T *)instr->ga_data)
3485 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3486 isn->isn_arg.jump.jump_where = instr->ga_len;
3487 }
3488 }
3489 ga_clear(&end_ga);
3490
3491 cctx->ctx_skip = save_skip;
3492 }
3493
3494 return OK;
3495}
3496
3497/*
3498 * expr4a && expr4a && expr4a logical AND
3499 *
3500 * Produces instructions:
3501 * EVAL expr4a Push result of "expr4a"
3502 * COND2BOOL convert to bool if needed
3503 * JUMP_IF_COND_FALSE end
3504 * EVAL expr4b Push result of "expr4b"
3505 * JUMP_IF_COND_FALSE end
3506 * EVAL expr4c Push result of "expr4c"
3507 * end:
3508 */
3509 static int
3510compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3511{
3512 int ppconst_used = ppconst->pp_used;
3513
3514 // get the first variable
3515 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3516 return FAIL;
3517
3518 // || and && work almost the same
3519 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3520}
3521
3522/*
3523 * expr3a || expr3b || expr3c logical OR
3524 *
3525 * Produces instructions:
3526 * EVAL expr3a Push result of "expr3a"
3527 * COND2BOOL convert to bool if needed
3528 * JUMP_IF_COND_TRUE end
3529 * EVAL expr3b Push result of "expr3b"
3530 * JUMP_IF_COND_TRUE end
3531 * EVAL expr3c Push result of "expr3c"
3532 * end:
3533 */
3534 static int
3535compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3536{
3537 int ppconst_used = ppconst->pp_used;
3538
3539 // eval the first expression
3540 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3541 return FAIL;
3542
3543 // || and && work almost the same
3544 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3545}
3546
3547/*
3548 * Toplevel expression: expr2 ? expr1a : expr1b
3549 * Produces instructions:
3550 * EVAL expr2 Push result of "expr2"
3551 * JUMP_IF_FALSE alt jump if false
3552 * EVAL expr1a
3553 * JUMP_ALWAYS end
3554 * alt: EVAL expr1b
3555 * end:
3556 *
3557 * Toplevel expression: expr2 ?? expr1
3558 * Produces instructions:
3559 * EVAL expr2 Push result of "expr2"
3560 * JUMP_AND_KEEP_IF_TRUE end jump if true
3561 * EVAL expr1
3562 * end:
3563 */
3564 int
3565compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3566{
3567 char_u *p;
3568 int ppconst_used = ppconst->pp_used;
3569 char_u *next;
3570
3571 // Ignore all kinds of errors when not producing code.
3572 if (cctx->ctx_skip == SKIP_YES)
3573 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003574 int prev_did_emsg = did_emsg;
3575
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003576 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003577 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003578 }
3579
3580 // Evaluate the first expression.
3581 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3582 return FAIL;
3583
3584 p = may_peek_next_line(cctx, *arg, &next);
3585 if (*p == '?')
3586 {
3587 int op_falsy = p[1] == '?';
3588 garray_T *instr = &cctx->ctx_instr;
3589 garray_T *stack = &cctx->ctx_type_stack;
3590 int alt_idx = instr->ga_len;
3591 int end_idx = 0;
3592 isn_T *isn;
3593 type_T *type1 = NULL;
3594 int has_const_expr = FALSE;
3595 int const_value = FALSE;
3596 int save_skip = cctx->ctx_skip;
3597
3598 if (next != NULL)
3599 {
3600 *arg = next_line_from_context(cctx, TRUE);
3601 p = skipwhite(*arg);
3602 }
3603
3604 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3605 {
3606 semsg(_(e_white_space_required_before_and_after_str_at_str),
3607 op_falsy ? "??" : "?", p);
3608 return FAIL;
3609 }
3610
3611 if (ppconst->pp_used == ppconst_used + 1)
3612 {
3613 // the condition is a constant, we know whether the ? or the :
3614 // expression is to be evaluated.
3615 has_const_expr = TRUE;
3616 if (op_falsy)
3617 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3618 else
3619 {
3620 int error = FALSE;
3621
3622 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3623 &error);
3624 if (error)
3625 return FAIL;
3626 }
3627 cctx->ctx_skip = save_skip == SKIP_YES ||
3628 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3629
3630 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3631 // "left ?? right" and "left" is truthy: produce "left"
3632 generate_ppconst(cctx, ppconst);
3633 else
3634 {
3635 clear_tv(&ppconst->pp_tv[ppconst_used]);
3636 --ppconst->pp_used;
3637 }
3638 }
3639 else
3640 {
3641 generate_ppconst(cctx, ppconst);
3642 if (op_falsy)
3643 end_idx = instr->ga_len;
3644 generate_JUMP(cctx, op_falsy
3645 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3646 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003647 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003648 }
3649
3650 // evaluate the second expression; any type is accepted
3651 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3652 return FAIL;
3653 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3654 return FAIL;
3655
3656 if (!has_const_expr)
3657 {
3658 generate_ppconst(cctx, ppconst);
3659
3660 if (!op_falsy)
3661 {
3662 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003663 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003664 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003665
3666 end_idx = instr->ga_len;
3667 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3668
3669 // jump here from JUMP_IF_FALSE
3670 isn = ((isn_T *)instr->ga_data) + alt_idx;
3671 isn->isn_arg.jump.jump_where = instr->ga_len;
3672 }
3673 }
3674
3675 if (!op_falsy)
3676 {
3677 // Check for the ":".
3678 p = may_peek_next_line(cctx, *arg, &next);
3679 if (*p != ':')
3680 {
3681 emsg(_(e_missing_colon_after_questionmark));
3682 return FAIL;
3683 }
3684 if (next != NULL)
3685 {
3686 *arg = next_line_from_context(cctx, TRUE);
3687 p = skipwhite(*arg);
3688 }
3689
3690 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3691 {
3692 semsg(_(e_white_space_required_before_and_after_str_at_str),
3693 ":", p);
3694 return FAIL;
3695 }
3696
3697 // evaluate the third expression
3698 if (has_const_expr)
3699 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3700 ? SKIP_YES : SKIP_NOT;
3701 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3702 return FAIL;
3703 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3704 return FAIL;
3705 }
3706
3707 if (!has_const_expr)
3708 {
3709 type_T **typep;
3710
3711 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003712 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003713
3714 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003715 typep = &((((type2_T *)stack->ga_data)
3716 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003717 common_type(type1, *typep, typep, cctx->ctx_type_list);
3718
3719 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3720 isn = ((isn_T *)instr->ga_data) + end_idx;
3721 isn->isn_arg.jump.jump_where = instr->ga_len;
3722 }
3723
3724 cctx->ctx_skip = save_skip;
3725 }
3726 return OK;
3727}
3728
3729/*
3730 * Toplevel expression.
3731 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3732 * Returns OK or FAIL.
3733 */
3734 int
3735compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3736{
3737 ppconst_T ppconst;
3738
3739 CLEAR_FIELD(ppconst);
3740 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3741 {
3742 clear_ppconst(&ppconst);
3743 return FAIL;
3744 }
3745 if (is_const != NULL)
3746 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3747 if (generate_ppconst(cctx, &ppconst) == FAIL)
3748 return FAIL;
3749 return OK;
3750}
3751
3752/*
3753 * Toplevel expression.
3754 */
3755 int
3756compile_expr0(char_u **arg, cctx_T *cctx)
3757{
3758 return compile_expr0_ext(arg, cctx, NULL);
3759}
3760
3761
3762#endif // defined(FEAT_EVAL)