blob: fe2be410bad73c972a84c3a07800585d489b3c66 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000102 if (need_type(idxtype, &t_number, FALSE,
103 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000104 return FAIL;
105 if (is_slice)
106 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000107 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000108 if (need_type(idxtype, &t_number, FALSE,
109 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000110 return FAIL;
111 }
112 }
113
114 if (vartype == VAR_DICT)
115 {
116 if (is_slice)
117 {
118 emsg(_(e_cannot_slice_dictionary));
119 return FAIL;
120 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000122 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000123 typep->type_curr = typep->type_curr->tt_member;
124 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000125 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 typep->type_curr = &t_any;
127 if (typep->type_decl->tt_type == VAR_DICT)
128 {
129 typep->type_decl = typep->type_decl->tt_member;
130 if (typep->type_decl == &t_unknown)
131 // empty dict was used
132 typep->type_decl = &t_any;
133 }
134 else
135 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000136 }
137 else
138 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000139 if (need_type(typep->type_curr, &t_dict_any, FALSE,
140 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000141 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000142 typep->type_curr = &t_any;
143 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200145 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL
Bram Moolenaard0132f42022-05-12 11:05:40 +0100146 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147 return FAIL;
148 if (keeping_dict != NULL)
149 *keeping_dict = TRUE;
150 }
151 else if (vartype == VAR_STRING)
152 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000153 typep->type_curr = &t_string;
154 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000155 if ((is_slice
156 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
157 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
158 return FAIL;
159 }
160 else if (vartype == VAR_BLOB)
161 {
162 if (is_slice)
163 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 typep->type_curr = &t_blob;
165 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
167 return FAIL;
168 }
169 else
170 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000171 typep->type_curr = &t_number;
172 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
174 return FAIL;
175 }
176 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100177 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
178 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000179 {
180 if (is_slice)
181 {
182 if (generate_instr_drop(cctx,
183 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
184 2) == FAIL)
185 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000186 // a copy is made so the member type is no longer declared
187 if (typep->type_decl->tt_type == VAR_LIST)
188 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000189
190 // a copy is made, the composite is no longer "const"
191 if (typep->type_curr->tt_flags & TTFLAG_CONST)
192 {
193 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
194
195 if (type != typep->type_curr) // did get a copy
196 {
197 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
198 typep->type_curr = type;
199 }
200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201 }
202 else
203 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 typep->type_curr = typep->type_curr->tt_member;
207 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000209 typep->type_curr = &t_any;
210 if (typep->type_decl->tt_type == VAR_LIST)
211 {
212 typep->type_decl = typep->type_decl->tt_member;
213 if (typep->type_decl == &t_unknown)
214 // empty list was used
215 typep->type_decl = &t_any;
216 }
217 else
218 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 }
220 if (generate_instr_drop(cctx,
221 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
222 == FAIL)
223 return FAIL;
224 }
225 }
226 else
227 {
228 switch (vartype)
229 {
230 case VAR_FUNC:
231 case VAR_PARTIAL:
232 emsg(_(e_cannot_index_a_funcref));
233 break;
234 case VAR_BOOL:
235 case VAR_SPECIAL:
236 case VAR_JOB:
237 case VAR_CHANNEL:
238 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 case VAR_CLASS:
240 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200241 case VAR_TYPEALIAS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000242 case VAR_UNKNOWN:
243 case VAR_ANY:
244 case VAR_VOID:
245 emsg(_(e_cannot_index_special_variable));
246 break;
247 default:
248 emsg(_(e_string_list_dict_or_blob_required));
249 }
250 return FAIL;
251 }
252 return OK;
253}
254
255/*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200256 * Returns TRUE if the current function is inside the class "cl" or one of the
257 * parent classes.
258 */
259 static int
260inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
261{
262 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
263 {
264 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
265 {
266 class_T *clp = cctx->ctx_ufunc->uf_class;
267 while (clp != NULL)
268 {
269 if (clp == cl)
270 return TRUE;
271 clp = clp->class_extends;
272 }
273 }
274 }
275
276 return FALSE;
277}
278
279/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000280 * Compile ".member" coming after an object or class.
281 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000282 static int
283compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
284{
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200285 int m_idx;
286
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000287 if (VIM_ISWHITE((*arg)[1]))
288 {
289 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
290 return FAIL;
291 }
292
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000293 class_T *cl = type->tt_class;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000294 int is_super = type->tt_flags & TTFLAG_SUPER;
295 if (type == &t_super)
296 {
297 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000298 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200299 emsg(_(e_using_super_not_in_class_method));
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000300 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000301 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000302 is_super = TRUE;
303 cl = cctx->ctx_ufunc->uf_class;
304 // Remove &t_super from the stack.
305 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000306 }
307 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000308 {
309 garray_T *instr = &cctx->ctx_instr;
310 if (instr->ga_len > 0)
311 {
312 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
313 if (isn->isn_type == ISN_LOADSCRIPT)
314 {
315 // The class was recognized as a script item. We only need
316 // to know what class it is, drop the instruction.
317 --instr->ga_len;
318 vim_free(isn->isn_arg.script.scriptref);
319 }
320 }
321 }
322
Ernie Raelf77a7f72023-03-03 15:05:30 +0000323 if (cl == NULL)
324 {
325 // TODO: this should not give an error but be handled at runtime
326 emsg(_(e_incomplete_type));
327 return FAIL;
328 }
329
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000330 ++*arg;
331 char_u *name = *arg;
332 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
333 if (name_end == name)
334 return FAIL;
335 size_t len = name_end - name;
336
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000337 if (*name_end == '(')
338 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000339 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000340 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000341 ufunc_T **functions;
342
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000343 if (type->tt_type == VAR_CLASS)
344 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000345 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000346 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000347 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000348 }
349 else
350 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000351 // type->tt_type == VAR_OBJECT: method call
Ernie Rael58c95792024-08-13 23:27:22 +0200352 // When compiling Func and doing "super.SomeFunc()", must be in the
353 // class context that defines Func.
354 if (is_super)
355 cl = cctx->ctx_ufunc->uf_defclass;
356
Bram Moolenaar574950d2023-01-03 19:08:50 +0000357 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000358 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000360 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000361
362 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000363 int fi;
364 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000365 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000366 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000367 // Use a separate pointer to avoid that ASAN complains about
368 // uf_name[] only being 4 characters.
369 char_u *ufname = (char_u *)fp->uf_name;
370 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
371 {
372 ufunc = fp;
373 break;
374 }
375 }
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/*
697 * Compile a variable name into a load instruction.
698 * "end" points to just after the name.
699 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
700 * When "error" is FALSE do not give an error when not found.
701 */
702 int
703compile_load(
704 char_u **arg,
705 char_u *end_arg,
706 cctx_T *cctx,
707 int is_expr,
708 int error)
709{
710 type_T *type;
711 char_u *name = NULL;
712 char_u *end = end_arg;
713 int res = FAIL;
714 int prev_called_emsg = called_emsg;
715
716 if (*(*arg + 1) == ':')
717 {
718 if (end <= *arg + 2)
719 {
720 isntype_T isn_type;
721
722 // load dictionary of namespace
723 switch (**arg)
724 {
725 case 'g': isn_type = ISN_LOADGDICT; break;
726 case 'w': isn_type = ISN_LOADWDICT; break;
727 case 't': isn_type = ISN_LOADTDICT; break;
728 case 'b': isn_type = ISN_LOADBDICT; break;
729 default:
730 semsg(_(e_namespace_not_supported_str), *arg);
731 goto theend;
732 }
733 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
734 goto theend;
735 res = OK;
736 }
737 else
738 {
739 isntype_T isn_type = ISN_DROP;
740
741 // load namespaced variable
742 name = vim_strnsave(*arg + 2, end - (*arg + 2));
743 if (name == NULL)
744 return FAIL;
745
746 switch (**arg)
747 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100748 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000750 case 's': if (current_script_is_vim9())
751 {
752 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
753 *arg);
754 vim_free(name);
755 return FAIL;
756 }
Kota Kato948a3892022-08-16 16:09:59 +0100757 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000758 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 else
760 res = compile_load_scriptvar(cctx, name,
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100761 NULL, &end, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 break;
763 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
764 {
765 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000766 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000767 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768 else
769 isn_type = ISN_LOADG;
770 }
771 else
772 {
773 isn_type = ISN_LOADAUTO;
774 vim_free(name);
775 name = vim_strnsave(*arg, end - *arg);
776 if (name == NULL)
777 return FAIL;
778 }
779 break;
780 case 'w': isn_type = ISN_LOADW; break;
781 case 't': isn_type = ISN_LOADT; break;
782 case 'b': isn_type = ISN_LOADB; break;
783 default: // cannot happen, just in case
784 semsg(_(e_namespace_not_supported_str), *arg);
785 goto theend;
786 }
787 if (isn_type != ISN_DROP)
788 {
789 // Global, Buffer-local, Window-local and Tabpage-local
790 // variables can be defined later, thus we don't check if it
791 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000792 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 }
794 }
795 }
796 else
797 {
798 size_t len = end - *arg;
799 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200800 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 int gen_load = FALSE;
802 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100803 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100804 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000805
Hirohito Higashif7cb9f92025-02-04 16:37:19 +0100806 name = vim_strnsave(*arg, len);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807 if (name == NULL)
808 return FAIL;
809
Bram Moolenaar58b40092023-01-11 15:59:05 +0000810 if (STRCMP(name, "super") == 0
811 && cctx->ctx_ufunc != NULL
812 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
813 {
814 // super.SomeFunc() in a class function: push &t_super type, this
815 // is recognized in compile_subscript().
816 res = push_type_stack(cctx, &t_super);
817 if (*end != '.')
818 emsg(_(e_super_must_be_followed_by_dot));
819 }
820 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 {
822 script_autoload(name, FALSE);
823 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
824 }
825 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
826 == OK)
827 {
828 if (gen_load_outer == 0)
829 gen_load = TRUE;
830 }
831 else
832 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000833 lvar_T lvar;
834 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835
836 if (lookup_local(*arg, len, &lvar, cctx) == OK)
837 {
838 type = lvar.lv_type;
839 idx = lvar.lv_idx;
840 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100841 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100843 outer_loop_depth = lvar.lv_loop_depth;
844 outer_loop_idx = lvar.lv_loop_idx;
845 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 else
847 gen_load = TRUE;
848 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200849 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
850 (((idx =
851 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
852 || ((method_idx =
853 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000854 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200855 // Referencing a class variable or method without the class
856 // name. A class variable or method can be referenced without
857 // the class name only in the class where the function is
858 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200859 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200860 {
861 if (idx >= 0)
862 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
863 else
864 {
865 ufunc_T *fp = cl->class_class_functions[method_idx];
866 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
867 NULL);
868 }
869 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200870 else
871 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200872 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200873 name, cl->class_name);
874 res = FAIL;
875 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000876 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 else
878 {
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100879 imported_T *imp = NULL;
880
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 // "var" can be script-local even without using "s:" if it
882 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000883 if (script_var_exists(*arg, len, cctx, NULL) == OK
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100884 || (imp = find_imported(name, 0, FALSE)) != NULL
885 || (imp = find_imported_from_extends(cctx, name, 0, FALSE))
886 != NULL)
887 res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888
889 // When evaluating an expression and the name starts with an
890 // uppercase letter it can be a user defined function.
891 // generate_funcref() will fail if the function can't be found.
892 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000893 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000894 }
895 }
896 if (gen_load)
897 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
898 if (gen_load_outer > 0)
899 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100900 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
901 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000902 cctx->ctx_outer_used = TRUE;
903 }
904 }
905
906 *arg = end;
907
908theend:
909 if (res == FAIL && error && called_emsg == prev_called_emsg)
910 semsg(_(e_variable_not_found_str), name);
911 vim_free(name);
912 return res;
913}
914
915/*
916 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100917 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 * Returns FAIL if compilation fails.
919 */
920 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100921compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922{
LemonBoyf3b48952022-05-05 13:53:03 +0100923 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000924 garray_T save_ga = cctx->ctx_instr;
925 int expr_res;
926 int trailing_error;
927 int instr_count;
928 isn_T *instr = NULL;
929
930 // Remove the string type from the stack.
931 --cctx->ctx_type_stack.ga_len;
932
933 // Temporarily reset the list of instructions so that the jump labels are
934 // correct.
935 cctx->ctx_instr.ga_len = 0;
936 cctx->ctx_instr.ga_maxlen = 0;
937 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000938
939 // avoid peeking a next line
940 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
941 cctx->ctx_ufunc->uf_lines.ga_len = 0;
942
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000943 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000944
945 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
946
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000947 s = skipwhite(s);
948 trailing_error = *s != NUL;
949
950 if (expr_res == FAIL || trailing_error
951 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
952 {
953 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000954 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000955 clear_instr_ga(&cctx->ctx_instr);
956 cctx->ctx_instr = save_ga;
957 ++cctx->ctx_type_stack.ga_len;
958 return FAIL;
959 }
960
961 // Move the generated instructions into the ISN_INSTR instruction, then
962 // restore the list of instructions.
963 instr_count = cctx->ctx_instr.ga_len;
964 instr = cctx->ctx_instr.ga_data;
965 instr[instr_count].isn_type = ISN_FINISH;
966
967 cctx->ctx_instr = save_ga;
968 vim_free(isn->isn_arg.string);
969 isn->isn_type = ISN_INSTR;
970 isn->isn_arg.instr = instr;
971 return OK;
972}
973
974/*
975 * Compile the argument expressions.
976 * "arg" points to just after the "(" and is advanced to after the ")"
977 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100978 int
LemonBoyf3b48952022-05-05 13:53:03 +0100979compile_arguments(
980 char_u **arg,
981 cctx_T *cctx,
982 int *argcount,
983 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984{
985 char_u *p = *arg;
986 char_u *whitep = *arg;
987 int must_end = FALSE;
988 int instr_count;
989
990 for (;;)
991 {
992 if (may_get_next_line(whitep, &p, cctx) == FAIL)
993 goto failret;
994 if (*p == ')')
995 {
996 *arg = p + 1;
997 return OK;
998 }
999 if (must_end)
1000 {
1001 semsg(_(e_missing_comma_before_argument_str), p);
1002 return FAIL;
1003 }
1004
1005 instr_count = cctx->ctx_instr.ga_len;
1006 if (compile_expr0(&p, cctx) == FAIL)
1007 return FAIL;
1008 ++*argcount;
1009
LemonBoyf3b48952022-05-05 13:53:03 +01001010 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011 && cctx->ctx_instr.ga_len == instr_count + 1)
1012 {
1013 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1014
1015 // {skip} argument of searchpair() can be compiled if not empty
1016 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +01001017 compile_string(isn, cctx, 0);
1018 }
1019 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
1020 && cctx->ctx_instr.ga_len == instr_count + 1)
1021 {
1022 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
1023
1024 // {sub} argument of substitute() can be compiled if it starts
1025 // with \=
1026 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +01001027 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +01001028 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001029 }
1030
1031 if (*p != ',' && *skipwhite(p) == ',')
1032 {
1033 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1034 p = skipwhite(p);
1035 }
1036 if (*p == ',')
1037 {
1038 ++p;
1039 if (*p != NUL && !VIM_ISWHITE(*p))
1040 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1041 }
1042 else
1043 must_end = TRUE;
1044 whitep = p;
1045 p = skipwhite(p);
1046 }
1047failret:
1048 emsg(_(e_missing_closing_paren));
1049 return FAIL;
1050}
1051
1052/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001053 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1054 * etc.) if the class implements it.
1055 */
1056 static int
1057compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1058{
1059 type_T *type = get_decl_type_on_stack(cctx, 0);
1060 int res = FAIL;
1061
1062 // If the built in function is invoked on an object and the class
1063 // implements the corresponding built in method, then invoke the object
1064 // method.
1065 if (type->tt_type == VAR_OBJECT)
1066 {
1067 int method_idx;
1068 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1069 &method_idx);
1070 if (uf != NULL)
Ernie Rael58c95792024-08-13 23:27:22 +02001071 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE);
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001072 }
1073
1074 return res;
1075}
1076
1077
1078/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001079 * Compile a function call: name(arg1, arg2)
1080 * "arg" points to "name", "arg + varlen" to the "(".
1081 * "argcount_init" is 1 for "value->method()"
1082 * Instructions:
1083 * EVAL arg1
1084 * EVAL arg2
1085 * BCALL / DCALL / UCALL
1086 */
1087 static int
1088compile_call(
1089 char_u **arg,
1090 size_t varlen,
1091 cctx_T *cctx,
1092 ppconst_T *ppconst,
1093 int argcount_init)
1094{
1095 char_u *name = *arg;
1096 char_u *p;
1097 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001098 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 char_u fname_buf[FLEN_FIXED + 1];
1100 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101 ufunc_T *ufunc = NULL;
1102 int res = FAIL;
1103 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001104 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001105 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001106 imported_T *import;
1107
1108 if (varlen >= sizeof(namebuf))
1109 {
1110 semsg(_(e_name_too_long_str), name);
1111 return FAIL;
1112 }
1113 vim_strncpy(namebuf, *arg, varlen);
1114
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001115 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001116 if (import != NULL)
1117 {
1118 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1119 return FAIL;
1120 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001121
1122 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001123 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001125 if ((varlen == 3
1126 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001127 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1128 {
1129 char_u *s = skipwhite(*arg + varlen + 1);
1130 typval_T argvars[2];
1131 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001132 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001133
1134 argvars[0].v_type = VAR_UNKNOWN;
1135 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001136 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001137 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001138 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139 s = skipwhite(s);
1140 if (*s == ')' && argvars[0].v_type == VAR_STRING
1141 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1142 || !is_has))
1143 {
1144 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1145
1146 *arg = s + 1;
1147 argvars[1].v_type = VAR_UNKNOWN;
1148 tv->v_type = VAR_NUMBER;
1149 tv->vval.v_number = 0;
1150 if (is_has)
1151 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001152 else if (is_len)
1153 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154 else
1155 f_exists(argvars, tv);
1156 clear_tv(&argvars[0]);
1157 ++ppconst->pp_used;
1158 return OK;
1159 }
1160 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001161 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162 {
1163 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1164 return FAIL;
1165 }
1166 }
1167
1168 if (generate_ppconst(cctx, ppconst) == FAIL)
1169 return FAIL;
1170
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001171 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001172 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1173
1174 // We handle the "skip" argument of searchpair() and searchpairpos()
1175 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001176 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1177 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1178 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1179 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1180 special_fn = CA_SEARCHPAIR;
1181 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1182 special_fn = CA_SUBSTITUTE;
1183 else
1184 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185
1186 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001187 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 goto theend;
1189
1190 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1191 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1192 {
1193 int idx;
1194
1195 // builtin function
1196 idx = find_internal_func(name);
1197 if (idx >= 0)
1198 {
1199 if (STRCMP(name, "flatten") == 0)
1200 {
1201 emsg(_(e_cannot_use_flatten_in_vim9_script));
1202 goto theend;
1203 }
1204
1205 if (STRCMP(name, "add") == 0 && argcount == 2)
1206 {
h-eastb895b0f2023-09-24 15:46:31 +02001207 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001208 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1209 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001210
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211 // add() can be compiled to instructions if we know the type
1212 if (type->tt_type == VAR_LIST)
1213 {
1214 // inline "add(list, item)" so that the type can be checked
1215 res = generate_LISTAPPEND(cctx);
1216 idx = -1;
1217 }
1218 else if (type->tt_type == VAR_BLOB)
1219 {
1220 // inline "add(blob, nr)" so that the type can be checked
1221 res = generate_BLOBAPPEND(cctx);
1222 idx = -1;
1223 }
1224 }
1225
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001226 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1227 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001228 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001229 // May have the "D" or "R" flag, reserve a variable for a
1230 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001231 if (get_defer_var_idx(cctx) == 0)
1232 idx = -1;
1233 }
1234
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001235 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1236 if (STRCMP(name, "string") == 0)
1237 builtin_method = CLASS_BUILTIN_STRING;
1238 else if (STRCMP(name, "empty") == 0)
1239 builtin_method = CLASS_BUILTIN_EMPTY;
1240 else if (STRCMP(name, "len") == 0)
1241 builtin_method = CLASS_BUILTIN_LEN;
1242 if (builtin_method != CLASS_BUILTIN_INVALID)
1243 {
1244 res = compile_builtin_method_call(cctx, builtin_method);
1245 if (res == OK)
1246 idx = -1;
1247 }
1248
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 if (idx >= 0)
1250 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1251 }
1252 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001253 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001254 goto theend;
1255 }
1256
Bram Moolenaar62aec932022-01-29 21:45:34 +00001257 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1258
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001259 // An argument or local variable can be a function reference, this
1260 // overrules a function name.
1261 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1262 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1263 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001264 class_T *cl = NULL;
1265 int mi = 0;
1266
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267 // If we can find the function by name generate the right call.
1268 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001269 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001270 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001271 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001272 if (!func_is_global(ufunc))
1273 {
Ernie Rael58c95792024-08-13 23:27:22 +02001274 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001275 goto theend;
1276 }
1277 if (!has_g_namespace
1278 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1279 {
1280 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001281 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001282 goto theend;
1283 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001284 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001285 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001286 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001287 // Class method invocation without the class name.
1288 // A class method can be referenced without the class name only in
1289 // the class where the function is defined.
1290 if (cctx->ctx_ufunc->uf_defclass == cl)
1291 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001292 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
Ernie Rael58c95792024-08-13 23:27:22 +02001293 0, argcount, FALSE);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001294 }
1295 else
1296 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001297 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001298 name, cl->class_name);
1299 res = FAIL;
1300 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001301 goto theend;
1302 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001303 }
1304
1305 // If the name is a variable, load it and use PCALL.
1306 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001307 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001308 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001309 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1311 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001312 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001313
Christian Brabandtd5475e82023-08-17 23:34:09 +02001314 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315 goto theend;
1316 }
1317
1318 // If we can find a global function by name generate the right call.
1319 if (ufunc != NULL)
1320 {
Ernie Rael58c95792024-08-13 23:27:22 +02001321 res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322 goto theend;
1323 }
1324
1325 // A global function may be defined only later. Need to figure out at
1326 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001327 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328 res = generate_UCALL(cctx, name, argcount);
1329 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001330 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331
1332theend:
1333 vim_free(tofree);
1334 return res;
1335}
1336
1337// like NAMESPACE_CHAR but with 'a' and 'l'.
1338#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1339
1340/*
1341 * Find the end of a variable or function name. Unlike find_name_end() this
1342 * does not recognize magic braces.
1343 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1344 * Return a pointer to just after the name. Equal to "arg" if there is no
1345 * valid name.
1346 */
1347 char_u *
1348to_name_end(char_u *arg, int use_namespace)
1349{
1350 char_u *p;
1351
1352 // Quick check for valid starting character.
1353 if (!eval_isnamec1(*arg))
1354 return arg;
1355
1356 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1357 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1358 // and can be used in slice "[n:]".
1359 if (*p == ':' && (p != arg + 1
1360 || !use_namespace
1361 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1362 break;
1363 return p;
1364}
1365
1366/*
1367 * Like to_name_end() but also skip over a list or dict constant.
1368 * Also accept "<SNR>123_Func".
1369 * This intentionally does not handle line continuation.
1370 */
1371 char_u *
1372to_name_const_end(char_u *arg)
1373{
1374 char_u *p = arg;
1375 typval_T rettv;
1376
1377 if (STRNCMP(p, "<SNR>", 5) == 0)
1378 p = skipdigits(p + 5);
1379 p = to_name_end(p, TRUE);
1380 if (p == arg && *arg == '[')
1381 {
1382
1383 // Can be "[1, 2, 3]->Func()".
1384 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1385 p = arg;
1386 }
1387 return p;
1388}
1389
1390/*
1391 * parse a list: [expr, expr]
1392 * "*arg" points to the '['.
1393 * ppconst->pp_is_const is set if all items are a constant.
1394 */
1395 static int
1396compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1397{
1398 char_u *p = skipwhite(*arg + 1);
1399 char_u *whitep = *arg + 1;
1400 int count = 0;
1401 int is_const;
1402 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001403 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001404
1405 for (;;)
1406 {
1407 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1408 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001409 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410 return FAIL;
1411 }
1412 if (*p == ',')
1413 {
1414 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1415 return FAIL;
1416 }
1417 if (*p == ']')
1418 {
1419 ++p;
1420 break;
1421 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001422 if (must_end)
1423 {
1424 semsg(_(e_missing_comma_in_list_str), p);
1425 return FAIL;
1426 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001427 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1428 return FAIL;
1429 if (!is_const)
1430 is_all_const = FALSE;
1431 ++count;
1432 if (*p == ',')
1433 {
1434 ++p;
1435 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1436 {
1437 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1438 return FAIL;
1439 }
1440 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001441 else
1442 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001443 whitep = p;
1444 p = skipwhite(p);
1445 }
1446 *arg = p;
1447
1448 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001449 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450}
1451
1452/*
1453 * Parse a lambda: "(arg, arg) => expr"
1454 * "*arg" points to the '('.
1455 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1456 */
1457 static int
1458compile_lambda(char_u **arg, cctx_T *cctx)
1459{
1460 int r;
1461 typval_T rettv;
1462 ufunc_T *ufunc;
1463 evalarg_T evalarg;
1464
1465 init_evalarg(&evalarg);
1466 evalarg.eval_flags = EVAL_EVALUATE;
1467 evalarg.eval_cctx = cctx;
1468
1469 // Get the funcref in "rettv".
1470 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1471 if (r != OK)
1472 {
1473 clear_evalarg(&evalarg, NULL);
1474 return r;
1475 }
1476
1477 // "rettv" will now be a partial referencing the function.
1478 ufunc = rettv.vval.v_partial->pt_func;
1479 ++ufunc->uf_refcount;
1480 clear_tv(&rettv);
1481
1482 // Compile it here to get the return type. The return type is optional,
1483 // when it's missing use t_unknown. This is recognized in
1484 // compile_return().
1485 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1486 ufunc->uf_ret_type = &t_unknown;
1487 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1488
1489 // When the outer function is compiled for profiling or debugging, the
1490 // lambda may be called without profiling or debugging. Compile it here in
1491 // the right context.
1492 if (cctx->ctx_compile_type == CT_DEBUG
1493#ifdef FEAT_PROFILE
1494 || cctx->ctx_compile_type == CT_PROFILE
1495#endif
1496 )
1497 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1498
Bram Moolenaar139575d2022-03-15 19:29:30 +00001499 // if the outer function is not compiled for debugging or profiling, this
1500 // one might be
1501 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001502 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001503 compiletype_T compile_type = get_compile_type(ufunc);
1504
1505 if (compile_type != CT_NONE)
1506 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001507 }
1508
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1510 // "*arg" may point into it. Point into the original line to avoid a
1511 // dangling pointer.
1512 if (evalarg.eval_using_cmdline)
1513 {
1514 garray_T *gap = &evalarg.eval_tofree_ga;
1515 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1516
1517 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1518 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001519 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001520 }
1521
1522 clear_evalarg(&evalarg, NULL);
1523
1524 if (ufunc->uf_def_status == UF_COMPILED)
1525 {
1526 // The return type will now be known.
1527 set_function_type(ufunc);
1528
1529 // The function reference count will be 1. When the ISN_FUNCREF
1530 // instruction is deleted the reference count is decremented and the
1531 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001532 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001533 }
1534
1535 func_ptr_unref(ufunc);
1536 return FAIL;
1537}
1538
1539/*
1540 * Get a lambda and compile it. Uses Vim9 syntax.
1541 */
1542 int
1543get_lambda_tv_and_compile(
1544 char_u **arg,
1545 typval_T *rettv,
1546 int types_optional,
1547 evalarg_T *evalarg)
1548{
1549 int r;
1550 ufunc_T *ufunc;
1551 int save_sc_version = current_sctx.sc_version;
1552
1553 // Get the funcref in "rettv".
1554 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1555 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1556 current_sctx.sc_version = save_sc_version;
1557 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001558 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001559
1560 // "rettv" will now be a partial referencing the function.
1561 ufunc = rettv->vval.v_partial->pt_func;
1562
1563 // Compile it here to get the return type. The return type is optional,
1564 // when it's missing use t_unknown. This is recognized in
1565 // compile_return().
1566 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1567 ufunc->uf_ret_type = &t_unknown;
1568 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1569
1570 if (ufunc->uf_def_status == UF_COMPILED)
1571 {
1572 // The return type will now be known.
1573 set_function_type(ufunc);
1574 return OK;
1575 }
1576 clear_tv(rettv);
1577 return FAIL;
1578}
1579
1580/*
1581 * parse a dict: {key: val, [key]: val}
1582 * "*arg" points to the '{'.
1583 * ppconst->pp_is_const is set if all item values are a constant.
1584 */
1585 static int
1586compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1587{
1588 garray_T *instr = &cctx->ctx_instr;
1589 int count = 0;
1590 dict_T *d = dict_alloc();
1591 dictitem_T *item;
1592 char_u *whitep = *arg + 1;
1593 char_u *p;
1594 int is_const;
1595 int is_all_const = TRUE; // reset when non-const encountered
1596
1597 if (d == NULL)
1598 return FAIL;
1599 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001600 {
1601 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001602 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001603 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001604 for (;;)
1605 {
1606 char_u *key = NULL;
1607
1608 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1609 {
1610 *arg = NULL;
1611 goto failret;
1612 }
1613
1614 if (**arg == '}')
1615 break;
1616
1617 if (**arg == '[')
1618 {
1619 isn_T *isn;
1620
1621 // {[expr]: value} uses an evaluated key.
1622 *arg = skipwhite(*arg + 1);
1623 if (compile_expr0(arg, cctx) == FAIL)
1624 return FAIL;
1625 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1626 if (isn->isn_type == ISN_PUSHNR)
1627 {
1628 char buf[NUMBUFLEN];
1629
1630 // Convert to string at compile time.
1631 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1632 isn->isn_type = ISN_PUSHS;
1633 isn->isn_arg.string = vim_strsave((char_u *)buf);
1634 }
1635 if (isn->isn_type == ISN_PUSHS)
1636 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001637 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001638 return FAIL;
1639 *arg = skipwhite(*arg);
1640 if (**arg != ']')
1641 {
1642 emsg(_(e_missing_matching_bracket_after_dict_key));
1643 return FAIL;
1644 }
1645 ++*arg;
1646 }
1647 else
1648 {
1649 // {"name": value},
1650 // {'name': value},
1651 // {name: value} use "name" as a literal key
1652 key = get_literal_key(arg);
1653 if (key == NULL)
1654 return FAIL;
1655 if (generate_PUSHS(cctx, &key) == FAIL)
1656 return FAIL;
1657 }
1658
1659 // Check for duplicate keys, if using string keys.
1660 if (key != NULL)
1661 {
1662 item = dict_find(d, key, -1);
1663 if (item != NULL)
1664 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001665 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001666 goto failret;
1667 }
1668 item = dictitem_alloc(key);
1669 if (item != NULL)
1670 {
1671 item->di_tv.v_type = VAR_UNKNOWN;
1672 item->di_tv.v_lock = 0;
1673 if (dict_add(d, item) == FAIL)
1674 dictitem_free(item);
1675 }
1676 }
1677
1678 if (**arg != ':')
1679 {
1680 if (*skipwhite(*arg) == ':')
1681 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1682 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001683 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684 return FAIL;
1685 }
1686 whitep = *arg + 1;
1687 if (!IS_WHITE_OR_NUL(*whitep))
1688 {
1689 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1690 return FAIL;
1691 }
1692
1693 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1694 {
1695 *arg = NULL;
1696 goto failret;
1697 }
1698
1699 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1700 return FAIL;
1701 if (!is_const)
1702 is_all_const = FALSE;
1703 ++count;
1704
1705 whitep = *arg;
1706 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1707 {
1708 *arg = NULL;
1709 goto failret;
1710 }
1711 if (**arg == '}')
1712 break;
1713 if (**arg != ',')
1714 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001715 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716 goto failret;
1717 }
1718 if (IS_WHITE_OR_NUL(*whitep))
1719 {
1720 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1721 return FAIL;
1722 }
1723 whitep = *arg + 1;
1724 if (!IS_WHITE_OR_NUL(*whitep))
1725 {
1726 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1727 return FAIL;
1728 }
1729 *arg = skipwhite(whitep);
1730 }
1731
1732 *arg = *arg + 1;
1733
1734 // Allow for following comment, after at least one space.
1735 p = skipwhite(*arg);
1736 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1737 *arg += STRLEN(*arg);
1738
1739 dict_unref(d);
1740 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001741 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742
1743failret:
1744 if (*arg == NULL)
1745 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001746 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001747 *arg = (char_u *)"";
1748 }
1749 dict_unref(d);
1750 return FAIL;
1751}
1752
1753/*
1754 * Compile "&option".
1755 */
1756 static int
1757compile_get_option(char_u **arg, cctx_T *cctx)
1758{
1759 typval_T rettv;
1760 char_u *start = *arg;
1761 int ret;
1762
1763 // parse the option and get the current value to get the type.
1764 rettv.v_type = VAR_UNKNOWN;
1765 ret = eval_option(arg, &rettv, TRUE);
1766 if (ret == OK)
1767 {
1768 // include the '&' in the name, eval_option() expects it.
1769 char_u *name = vim_strnsave(start, *arg - start);
1770 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1771 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1772
1773 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1774 vim_free(name);
1775 }
1776 clear_tv(&rettv);
1777
1778 return ret;
1779}
1780
1781/*
1782 * Compile "$VAR".
1783 */
1784 static int
1785compile_get_env(char_u **arg, cctx_T *cctx)
1786{
1787 char_u *start = *arg;
1788 int len;
1789 int ret;
1790 char_u *name;
1791
1792 ++*arg;
1793 len = get_env_len(arg);
1794 if (len == 0)
1795 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001796 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001797 return FAIL;
1798 }
1799
1800 // include the '$' in the name, eval_env_var() expects it.
1801 name = vim_strnsave(start, len + 1);
1802 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1803 vim_free(name);
1804 return ret;
1805}
1806
1807/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001808 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001809 */
1810 static int
1811compile_interp_string(char_u **arg, cctx_T *cctx)
1812{
1813 typval_T tv;
1814 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001815 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001816 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001817 int count = 0;
1818 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001819
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001820 // *arg is on the '$' character, move it to the first string character.
1821 ++*arg;
1822 quote = **arg;
1823 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001824
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001825 for (;;)
1826 {
1827 // Get the string up to the matching quote or to a single '{'.
1828 // "arg" is advanced to either the quote or the '{'.
1829 if (quote == '"')
1830 ret = eval_string(arg, &tv, evaluate, TRUE);
1831 else
1832 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1833 if (ret == FAIL)
1834 break;
1835 if (evaluate)
1836 {
1837 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1838 || (**arg != '{' && count == 0))
1839 {
1840 // generate non-empty string or empty string if it's the only
1841 // one
1842 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1843 return FAIL;
1844 tv.vval.v_string = NULL; // don't free it now
1845 ++count;
1846 }
1847 clear_tv(&tv);
1848 }
1849
1850 if (**arg != '{')
1851 {
1852 // found terminating quote
1853 ++*arg;
1854 break;
1855 }
1856
1857 p = compile_one_expr_in_str(*arg, cctx);
1858 if (p == NULL)
1859 {
1860 ret = FAIL;
1861 break;
1862 }
1863 ++count;
1864 *arg = p;
1865 }
LemonBoy2eaef102022-05-06 13:14:50 +01001866
1867 if (ret == FAIL || !evaluate)
1868 return ret;
1869
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001870 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1871 if (count > 1)
1872 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001873
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001874 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001875}
1876
1877/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001878 * Compile "@r".
1879 */
1880 static int
1881compile_get_register(char_u **arg, cctx_T *cctx)
1882{
1883 int ret;
1884
1885 ++*arg;
1886 if (**arg == NUL)
1887 {
1888 semsg(_(e_syntax_error_at_str), *arg - 1);
1889 return FAIL;
1890 }
1891 if (!valid_yank_reg(**arg, FALSE))
1892 {
1893 emsg_invreg(**arg);
1894 return FAIL;
1895 }
1896 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1897 ++*arg;
1898 return ret;
1899}
1900
1901/*
1902 * Apply leading '!', '-' and '+' to constant "rettv".
1903 * When "numeric_only" is TRUE do not apply '!'.
1904 */
1905 static int
1906apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1907{
1908 char_u *p = *end;
1909
1910 // this works from end to start
1911 while (p > start)
1912 {
1913 --p;
1914 if (*p == '-' || *p == '+')
1915 {
1916 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001917 if (rettv->v_type == VAR_FLOAT)
1918 {
1919 if (*p == '-')
1920 rettv->vval.v_float = -rettv->vval.v_float;
1921 }
1922 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923 {
1924 varnumber_T val;
1925 int error = FALSE;
1926
1927 // tv_get_number_chk() accepts a string, but we don't want that
1928 // here
1929 if (check_not_string(rettv) == FAIL)
1930 return FAIL;
1931 val = tv_get_number_chk(rettv, &error);
1932 clear_tv(rettv);
1933 if (error)
1934 return FAIL;
1935 if (*p == '-')
1936 val = -val;
1937 rettv->v_type = VAR_NUMBER;
1938 rettv->vval.v_number = val;
1939 }
1940 }
1941 else if (numeric_only)
1942 {
1943 ++p;
1944 break;
1945 }
1946 else if (*p == '!')
1947 {
1948 int v = tv2bool(rettv);
1949
1950 // '!' is permissive in the type.
1951 clear_tv(rettv);
1952 rettv->v_type = VAR_BOOL;
1953 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1954 }
1955 }
1956 *end = p;
1957 return OK;
1958}
1959
1960/*
1961 * Recognize v: variables that are constants and set "rettv".
1962 */
1963 static void
1964get_vim_constant(char_u **arg, typval_T *rettv)
1965{
1966 if (STRNCMP(*arg, "v:true", 6) == 0)
1967 {
1968 rettv->v_type = VAR_BOOL;
1969 rettv->vval.v_number = VVAL_TRUE;
1970 *arg += 6;
1971 }
1972 else if (STRNCMP(*arg, "v:false", 7) == 0)
1973 {
1974 rettv->v_type = VAR_BOOL;
1975 rettv->vval.v_number = VVAL_FALSE;
1976 *arg += 7;
1977 }
1978 else if (STRNCMP(*arg, "v:null", 6) == 0)
1979 {
1980 rettv->v_type = VAR_SPECIAL;
1981 rettv->vval.v_number = VVAL_NULL;
1982 *arg += 6;
1983 }
1984 else if (STRNCMP(*arg, "v:none", 6) == 0)
1985 {
1986 rettv->v_type = VAR_SPECIAL;
1987 rettv->vval.v_number = VVAL_NONE;
1988 *arg += 6;
1989 }
1990}
1991
1992 exprtype_T
1993get_compare_type(char_u *p, int *len, int *type_is)
1994{
1995 exprtype_T type = EXPR_UNKNOWN;
1996 int i;
1997
1998 switch (p[0])
1999 {
2000 case '=': if (p[1] == '=')
2001 type = EXPR_EQUAL;
2002 else if (p[1] == '~')
2003 type = EXPR_MATCH;
2004 break;
2005 case '!': if (p[1] == '=')
2006 type = EXPR_NEQUAL;
2007 else if (p[1] == '~')
2008 type = EXPR_NOMATCH;
2009 break;
2010 case '>': if (p[1] != '=')
2011 {
2012 type = EXPR_GREATER;
2013 *len = 1;
2014 }
2015 else
2016 type = EXPR_GEQUAL;
2017 break;
2018 case '<': if (p[1] != '=')
2019 {
2020 type = EXPR_SMALLER;
2021 *len = 1;
2022 }
2023 else
2024 type = EXPR_SEQUAL;
2025 break;
2026 case 'i': if (p[1] == 's')
2027 {
2028 // "is" and "isnot"; but not a prefix of a name
2029 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2030 *len = 5;
2031 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01002032 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033 {
2034 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2035 *type_is = TRUE;
2036 }
2037 }
2038 break;
2039 }
2040 return type;
2041}
2042
2043/*
2044 * Skip over an expression, ignoring most errors.
2045 */
2046 void
2047skip_expr_cctx(char_u **arg, cctx_T *cctx)
2048{
2049 evalarg_T evalarg;
2050
2051 init_evalarg(&evalarg);
2052 evalarg.eval_cctx = cctx;
2053 skip_expr(arg, &evalarg);
2054 clear_evalarg(&evalarg, NULL);
2055}
2056
2057/*
2058 * Check that the top of the type stack has a type that can be used as a
2059 * condition. Give an error and return FAIL if not.
2060 */
2061 int
2062bool_on_stack(cctx_T *cctx)
2063{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 type_T *type;
2065
Bram Moolenaar078a4612022-01-04 15:17:03 +00002066 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002067 if (type == &t_bool)
2068 return OK;
2069
Bram Moolenaar4913d422022-10-17 13:13:32 +01002070 if (type->tt_type == VAR_ANY
2071 || type->tt_type == VAR_UNKNOWN
2072 || type->tt_type == VAR_NUMBER
2073 || type == &t_number_bool
2074 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002075 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2076 // This requires a runtime type check.
2077 return generate_COND2BOOL(cctx);
2078
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002079 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002080}
2081
2082/*
2083 * Give the "white on both sides" error, taking the operator from "p[len]".
2084 */
2085 void
2086error_white_both(char_u *op, int len)
2087{
2088 char_u buf[10];
2089
2090 vim_strncpy(buf, op, len);
2091 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2092}
2093
2094/*
2095 * Compile code to apply '-', '+' and '!'.
2096 * When "numeric_only" is TRUE do not apply '!'.
2097 */
2098 static int
2099compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2100{
2101 char_u *p = *end;
2102
2103 // this works from end to start
2104 while (p > start)
2105 {
2106 --p;
2107 while (VIM_ISWHITE(*p))
2108 --p;
2109 if (*p == '-' || *p == '+')
2110 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002111 type_T *type = get_type_on_stack(cctx, 0);
2112 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002113 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002114 return FAIL;
2115
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002116 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002117 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2118 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002119 }
2120 else if (numeric_only)
2121 {
2122 ++p;
2123 break;
2124 }
2125 else
2126 {
2127 int invert = *p == '!';
2128
2129 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2130 {
2131 if (p[-1] == '!')
2132 invert = !invert;
2133 --p;
2134 }
2135 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2136 return FAIL;
2137 }
2138 }
2139 *end = p;
2140 return OK;
2141}
2142
2143/*
2144 * Compile "(expression)": recursive!
2145 * Return FAIL/OK.
2146 */
2147 static int
2148compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2149{
2150 int ret;
2151 char_u *p = *arg + 1;
2152
2153 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2154 return FAIL;
2155 if (ppconst->pp_used <= PPSIZE - 10)
2156 {
2157 ret = compile_expr1(arg, cctx, ppconst);
2158 }
2159 else
2160 {
2161 // Not enough space in ppconst, flush constants.
2162 if (generate_ppconst(cctx, ppconst) == FAIL)
2163 return FAIL;
2164 ret = compile_expr0(arg, cctx);
2165 }
2166 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2167 return FAIL;
2168 if (**arg == ')')
2169 ++*arg;
2170 else if (ret == OK)
2171 {
2172 emsg(_(e_missing_closing_paren));
2173 ret = FAIL;
2174 }
2175 return ret;
2176}
2177
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002178static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002179
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002180/*
2181 * Compile whatever comes after "name" or "name()".
2182 * Advances "*arg" only when something was recognized.
2183 */
2184 static int
2185compile_subscript(
2186 char_u **arg,
2187 cctx_T *cctx,
2188 char_u *start_leader,
2189 char_u **end_leader,
2190 ppconst_T *ppconst)
2191{
2192 char_u *name_start = *end_leader;
2193 int keeping_dict = FALSE;
2194
2195 for (;;)
2196 {
2197 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002198 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002199
2200 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2201 {
2202 char_u *next = peek_next_line_from_context(cctx);
2203
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002204 // If a following line starts with "->{", "->(" or "->X" advance to
2205 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 // Also if a following line starts with ".x".
2207 if (next != NULL &&
2208 ((next[0] == '-' && next[1] == '>'
2209 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002210 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002211 || ASCII_ISALPHA(*skipwhite(next + 2))))
2212 || (next[0] == '.' && eval_isdictc(next[1]))))
2213 {
2214 next = next_line_from_context(cctx, TRUE);
2215 if (next == NULL)
2216 return FAIL;
2217 *arg = next;
2218 p = skipwhite(*arg);
2219 }
2220 }
2221
2222 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2223 // is not a function call.
2224 if (**arg == '(')
2225 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 int argcount = 0;
2227
2228 if (generate_ppconst(cctx, ppconst) == FAIL)
2229 return FAIL;
2230 ppconst->pp_is_const = FALSE;
2231
2232 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002233 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234
2235 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002236 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237 return FAIL;
2238 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2239 return FAIL;
2240 if (keeping_dict)
2241 {
2242 keeping_dict = FALSE;
2243 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2244 return FAIL;
2245 }
2246 }
2247 else if (*p == '-' && p[1] == '>')
2248 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002249 char_u *pstart = p;
2250 int alt;
2251 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252
Bram Moolenaarc7349932022-01-16 20:59:39 +00002253 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002254 if (generate_ppconst(cctx, ppconst) == FAIL)
2255 return FAIL;
2256 ppconst->pp_is_const = FALSE;
2257
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002258 // Apply the '!', '-' and '+' first:
2259 // -1.0->func() works like (-1.0)->func()
2260 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2261 return FAIL;
2262
2263 p += 2;
2264 *arg = skipwhite(p);
2265 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002266
2267 // Three alternatives handled here:
2268 // 1. "base->name(" only a name, use compile_call()
2269 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2270 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002271 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002272 alt = 2;
2273 else
2274 {
2275 // alternative 1 or 3
2276 p = *arg;
2277 if (!eval_isnamec1(*p))
2278 {
2279 semsg(_(e_trailing_characters_str), pstart);
2280 return FAIL;
2281 }
2282 if (ASCII_ISALPHA(*p) && p[1] == ':')
2283 p += 2;
2284 for ( ; eval_isnamec(*p); ++p)
2285 ;
2286 if (*p == '(')
2287 {
2288 // alternative 1
2289 alt = 1;
2290 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2291 return FAIL;
2292 }
2293 else
2294 {
2295 // Must be alternative 3, find the "(". Only works within
2296 // one line.
2297 alt = 3;
2298 paren = vim_strchr(p, '(');
2299 if (paren == NULL)
2300 {
2301 semsg(_(e_missing_parenthesis_str), *arg);
2302 return FAIL;
2303 }
2304 }
2305 }
2306
2307 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002308 {
2309 int argcount = 1;
2310 garray_T *stack = &cctx->ctx_type_stack;
2311 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312 int expr_isn_start = cctx->ctx_instr.ga_len;
2313 int expr_isn_end;
2314 int arg_isn_count;
2315
Bram Moolenaarc7349932022-01-16 20:59:39 +00002316 if (alt == 2)
2317 {
2318 // Funcref call: list->(Refs[2])(arg)
2319 // or lambda: list->((arg) => expr)(arg)
2320 //
2321 // Fist compile the function expression.
2322 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2323 return FAIL;
2324 }
2325 else
2326 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002327 int fail;
2328 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002329 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002330
Bram Moolenaarc7349932022-01-16 20:59:39 +00002331 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002332
2333 // instead of using LOADG for "import.Func" use PUSHFUNC
2334 ++paren_follows_after_expr;
2335
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002336 // do not look in the next line
2337 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002338
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002339 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002340 || *skipwhite(*arg) != NUL;
2341 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002342 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002343 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002344
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002345 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002346 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002347 if (did_emsg == prev_did_emsg)
2348 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002349 return FAIL;
2350 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002351 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002352
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002353 // Compile the arguments.
2354 if (**arg != '(')
2355 {
2356 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002357 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002358 else
2359 semsg(_(e_missing_parenthesis_str), *arg);
2360 return FAIL;
2361 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002362
2363 // Remember the next instruction index, where the instructions
2364 // for arguments are being written.
2365 expr_isn_end = cctx->ctx_instr.ga_len;
2366
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002367 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002368 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2369 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370 return FAIL;
2371
2372 // Move the instructions for the arguments to before the
2373 // instructions of the expression and move the type of the
2374 // expression after the argument types. This is what ISN_PCALL
2375 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002376 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2377 if (arg_isn_count > 0)
2378 {
2379 int expr_isn_count = expr_isn_end - expr_isn_start;
2380 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002381 type_T *decl_type;
2382 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383
2384 if (isn == NULL)
2385 return FAIL;
2386 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2387 + expr_isn_start,
2388 sizeof(isn_T) * expr_isn_count);
2389 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2390 + expr_isn_start,
2391 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2392 sizeof(isn_T) * arg_isn_count);
2393 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2394 + expr_isn_start + arg_isn_count,
2395 isn, sizeof(isn_T) * expr_isn_count);
2396 vim_free(isn);
2397
Bram Moolenaar078a4612022-01-04 15:17:03 +00002398 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2399 type = typep->type_curr;
2400 decl_type = typep->type_decl;
2401 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2402 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2403 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002404 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002405 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2406 typep->type_curr = type;
2407 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002408 }
2409
Bram Moolenaar078a4612022-01-04 15:17:03 +00002410 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002411 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2412 return FAIL;
2413 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002414
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002415 if (keeping_dict)
2416 {
2417 keeping_dict = FALSE;
2418 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2419 return FAIL;
2420 }
2421 }
2422 else if (**arg == '[')
2423 {
2424 int is_slice = FALSE;
2425
2426 // list index: list[123]
2427 // dict member: dict[key]
2428 // string index: text[123]
2429 // blob index: blob[123]
2430 if (generate_ppconst(cctx, ppconst) == FAIL)
2431 return FAIL;
2432 ppconst->pp_is_const = FALSE;
2433
2434 ++p;
2435 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2436 return FAIL;
2437 if (**arg == ':')
2438 {
2439 // missing first index is equal to zero
2440 generate_PUSHNR(cctx, 0);
2441 }
2442 else
2443 {
2444 if (compile_expr0(arg, cctx) == FAIL)
2445 return FAIL;
2446 if (**arg == ':')
2447 {
2448 semsg(_(e_white_space_required_before_and_after_str_at_str),
2449 ":", *arg);
2450 return FAIL;
2451 }
2452 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2453 return FAIL;
2454 *arg = skipwhite(*arg);
2455 }
2456 if (**arg == ':')
2457 {
2458 is_slice = TRUE;
2459 ++*arg;
2460 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2461 {
2462 semsg(_(e_white_space_required_before_and_after_str_at_str),
2463 ":", *arg);
2464 return FAIL;
2465 }
2466 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2467 return FAIL;
2468 if (**arg == ']')
2469 // missing second index is equal to end of string
2470 generate_PUSHNR(cctx, -1);
2471 else
2472 {
2473 if (compile_expr0(arg, cctx) == FAIL)
2474 return FAIL;
2475 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2476 return FAIL;
2477 *arg = skipwhite(*arg);
2478 }
2479 }
2480
2481 if (**arg != ']')
2482 {
2483 emsg(_(e_missing_closing_square_brace));
2484 return FAIL;
2485 }
2486 *arg = *arg + 1;
2487
2488 if (keeping_dict)
2489 {
2490 keeping_dict = FALSE;
2491 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2492 return FAIL;
2493 }
2494 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2495 return FAIL;
2496 }
2497 else if (*p == '.' && p[1] != '.')
2498 {
2499 // dictionary member: dict.name
2500 if (generate_ppconst(cctx, ppconst) == FAIL)
2501 return FAIL;
2502 ppconst->pp_is_const = FALSE;
2503
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002504 type = get_type_on_stack(cctx, 0);
2505 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002506 && (type->tt_type == VAR_CLASS
2507 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002508 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002509 // class member: SomeClass.varname
2510 // class method: SomeClass.SomeMethod()
2511 // class constructor: SomeClass.new()
2512 // object member: someObject.varname, this.varname
2513 // object method: someObject.SomeMethod(), this.SomeMethod()
2514 *arg = p;
2515 if (compile_class_object_index(cctx, arg, type) == FAIL)
2516 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002517 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002518 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002520 *arg = p + 1;
2521 if (IS_WHITE_OR_NUL(**arg))
2522 {
2523 emsg(_(e_missing_name_after_dot));
2524 return FAIL;
2525 }
2526 p = *arg;
2527 if (eval_isdictc(*p))
2528 while (eval_isnamec(*p))
2529 MB_PTR_ADV(p);
2530 if (p == *arg)
2531 {
2532 semsg(_(e_syntax_error_at_str), *arg);
2533 return FAIL;
2534 }
2535 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2536 return FAIL;
2537 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2538 return FAIL;
2539 keeping_dict = TRUE;
2540 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002541 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542 }
2543 else
2544 break;
2545 }
2546
2547 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2548 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002549 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2550 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002551 return FAIL;
2552
2553 return OK;
2554}
2555
2556/*
2557 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2558 * "arg" is advanced until after the expression, skipping white space.
2559 *
2560 * If the value is a constant "ppconst->pp_used" will be non-zero.
2561 * Before instructions are generated, any values in "ppconst" will generated.
2562 *
2563 * This is the compiling equivalent of eval1(), eval2(), etc.
2564 */
2565
2566/*
2567 * number number constant
2568 * 0zFFFFFFFF Blob constant
2569 * "string" string constant
2570 * 'string' literal string constant
2571 * &option-name option value
2572 * @r register contents
2573 * identifier variable value
2574 * function() function call
2575 * $VAR environment variable
2576 * (expression) nested expression
2577 * [expr, expr] List
2578 * {key: val, [key]: val} Dictionary
2579 *
2580 * Also handle:
2581 * ! in front logical NOT
2582 * - in front unary minus
2583 * + in front unary plus (ignored)
2584 * trailing (arg) funcref/partial call
2585 * trailing [] subscript in String or List
2586 * trailing .name entry in Dictionary
2587 * trailing ->name() method call
2588 */
2589 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002590compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591 char_u **arg,
2592 cctx_T *cctx,
2593 ppconst_T *ppconst)
2594{
2595 char_u *start_leader, *end_leader;
2596 int ret = OK;
2597 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2598 int used_before = ppconst->pp_used;
2599
2600 ppconst->pp_is_const = FALSE;
2601
2602 /*
2603 * Skip '!', '-' and '+' characters. They are handled later.
2604 */
2605 start_leader = *arg;
2606 if (eval_leader(arg, TRUE) == FAIL)
2607 return FAIL;
2608 end_leader = *arg;
2609
2610 rettv->v_type = VAR_UNKNOWN;
2611 switch (**arg)
2612 {
2613 /*
2614 * Number constant.
2615 */
2616 case '0': // also for blob starting with 0z
2617 case '1':
2618 case '2':
2619 case '3':
2620 case '4':
2621 case '5':
2622 case '6':
2623 case '7':
2624 case '8':
2625 case '9':
2626 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2627 return FAIL;
2628 // Apply "-" and "+" just before the number now, right to
2629 // left. Matters especially when "->" follows. Stops at
2630 // '!'.
2631 if (apply_leader(rettv, TRUE,
2632 start_leader, &end_leader) == FAIL)
2633 {
2634 clear_tv(rettv);
2635 return FAIL;
2636 }
2637 break;
2638
2639 /*
2640 * String constant: "string".
2641 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002642 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002643 return FAIL;
2644 break;
2645
2646 /*
2647 * Literal string constant: 'str''ing'.
2648 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002649 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650 return FAIL;
2651 break;
2652
2653 /*
2654 * Constant Vim variable.
2655 */
2656 case 'v': get_vim_constant(arg, rettv);
2657 ret = NOTDONE;
2658 break;
2659
2660 /*
2661 * "true" constant
2662 */
2663 case 't': if (STRNCMP(*arg, "true", 4) == 0
2664 && !eval_isnamec((*arg)[4]))
2665 {
2666 *arg += 4;
2667 rettv->v_type = VAR_BOOL;
2668 rettv->vval.v_number = VVAL_TRUE;
2669 }
2670 else
2671 ret = NOTDONE;
2672 break;
2673
2674 /*
2675 * "false" constant
2676 */
2677 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2678 && !eval_isnamec((*arg)[5]))
2679 {
2680 *arg += 5;
2681 rettv->v_type = VAR_BOOL;
2682 rettv->vval.v_number = VVAL_FALSE;
2683 }
2684 else
2685 ret = NOTDONE;
2686 break;
2687
2688 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002689 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002690 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002691 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002692 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002693 char_u *p = *arg + 4;
2694 int len;
2695
2696 for (len = 0; eval_isnamec(p[len]); ++len)
2697 ;
2698 ret = handle_predefined(*arg, len + 4, rettv);
2699 if (ret == FAIL)
2700 ret = NOTDONE;
2701 else
2702 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002703 }
2704 else
2705 ret = NOTDONE;
2706 break;
2707
2708 /*
2709 * List: [expr, expr]
2710 */
2711 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2712 return FAIL;
2713 ret = compile_list(arg, cctx, ppconst);
2714 break;
2715
2716 /*
2717 * Dictionary: {'key': val, 'key': val}
2718 */
2719 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2720 return FAIL;
2721 ret = compile_dict(arg, cctx, ppconst);
2722 break;
2723
2724 /*
2725 * Option value: &name
2726 */
2727 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2728 return FAIL;
2729 ret = compile_get_option(arg, cctx);
2730 break;
2731
2732 /*
2733 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002734 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002735 */
2736 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2737 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002738 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2739 ret = compile_interp_string(arg, cctx);
2740 else
2741 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002742 break;
2743
2744 /*
2745 * Register contents: @r.
2746 */
2747 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2748 return FAIL;
2749 ret = compile_get_register(arg, cctx);
2750 break;
2751 /*
2752 * nested expression: (expression).
2753 * lambda: (arg, arg) => expr
2754 * funcref: (arg, arg) => { statement }
2755 */
2756 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2757 ret = compile_lambda(arg, cctx);
2758 if (ret == NOTDONE)
2759 ret = compile_parenthesis(arg, cctx, ppconst);
2760 break;
2761
2762 default: ret = NOTDONE;
2763 break;
2764 }
2765 if (ret == FAIL)
2766 return FAIL;
2767
2768 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2769 {
2770 if (cctx->ctx_skip == SKIP_YES)
2771 clear_tv(rettv);
2772 else
2773 // A constant expression can possibly be handled compile time,
2774 // return the value instead of generating code.
2775 ++ppconst->pp_used;
2776 }
2777 else if (ret == NOTDONE)
2778 {
2779 char_u *p;
2780 int r;
2781
2782 if (!eval_isnamec1(**arg))
2783 {
2784 if (!vim9_bad_comment(*arg))
2785 {
2786 if (ends_excmd(*skipwhite(*arg)))
2787 semsg(_(e_empty_expression_str), *arg);
2788 else
2789 semsg(_(e_name_expected_str), *arg);
2790 }
2791 return FAIL;
2792 }
2793
2794 // "name" or "name()"
2795 p = to_name_end(*arg, TRUE);
2796 if (p - *arg == (size_t)1 && **arg == '_')
2797 {
2798 emsg(_(e_cannot_use_underscore_here));
2799 return FAIL;
2800 }
2801
2802 if (*p == '(')
2803 {
2804 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2805 }
2806 else
2807 {
2808 if (cctx->ctx_skip != SKIP_YES
2809 && generate_ppconst(cctx, ppconst) == FAIL)
2810 return FAIL;
2811 r = compile_load(arg, p, cctx, TRUE, TRUE);
2812 }
2813 if (r == FAIL)
2814 return FAIL;
2815 }
2816
2817 // Handle following "[]", ".member", etc.
2818 // Then deal with prefixed '-', '+' and '!', if not done already.
2819 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2820 ppconst) == FAIL)
2821 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002822 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002823 {
2824 // apply the '!', '-' and '+' before the constant
2825 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2826 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2827 return FAIL;
2828 return OK;
2829 }
2830 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2831 return FAIL;
2832 return OK;
2833}
2834
2835/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002836 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002837 */
2838 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002839compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002840{
2841 type_T *want_type = NULL;
2842
2843 // Recognize <type>
2844 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2845 {
2846 ++*arg;
2847 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2848 if (want_type == NULL)
2849 return FAIL;
2850
2851 if (**arg != '>')
2852 {
2853 if (*skipwhite(*arg) == '>')
2854 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2855 else
2856 emsg(_(e_missing_gt));
2857 return FAIL;
2858 }
2859 ++*arg;
2860 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2861 return FAIL;
2862 }
2863
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002864 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002865 return FAIL;
2866
2867 if (want_type != NULL)
2868 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002869 type_T *actual;
2870 where_T where = WHERE_INIT;
2871
LemonBoy50d48542024-07-04 17:03:17 +02002872 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002873 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002874 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002875 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002876 {
LemonBoy50d48542024-07-04 17:03:17 +02002877 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2878 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002879 return FAIL;
2880 }
2881 }
2882
2883 return OK;
2884}
2885
2886/*
2887 * * number multiplication
2888 * / number division
2889 * % number modulo
2890 */
2891 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002892compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002893{
2894 char_u *op;
2895 char_u *next;
2896 int ppconst_used = ppconst->pp_used;
2897
2898 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002899 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002900 return FAIL;
2901
2902 /*
2903 * Repeat computing, until no "*", "/" or "%" is following.
2904 */
2905 for (;;)
2906 {
2907 op = may_peek_next_line(cctx, *arg, &next);
2908 if (*op != '*' && *op != '/' && *op != '%')
2909 break;
2910 if (next != NULL)
2911 {
2912 *arg = next_line_from_context(cctx, TRUE);
2913 op = skipwhite(*arg);
2914 }
2915
2916 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2917 {
2918 error_white_both(op, 1);
2919 return FAIL;
2920 }
2921 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2922 return FAIL;
2923
2924 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002925 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002926 return FAIL;
2927
2928 if (ppconst->pp_used == ppconst_used + 2
2929 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2930 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2931 {
2932 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2933 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2934 varnumber_T res = 0;
2935 int failed = FALSE;
2936
2937 // both are numbers: compute the result
2938 switch (*op)
2939 {
2940 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2941 break;
2942 case '/': res = num_divide(tv1->vval.v_number,
2943 tv2->vval.v_number, &failed);
2944 break;
2945 case '%': res = num_modulus(tv1->vval.v_number,
2946 tv2->vval.v_number, &failed);
2947 break;
2948 }
2949 if (failed)
2950 return FAIL;
2951 tv1->vval.v_number = res;
2952 --ppconst->pp_used;
2953 }
2954 else
2955 {
2956 generate_ppconst(cctx, ppconst);
2957 generate_two_op(cctx, op);
2958 }
2959 }
2960
2961 return OK;
2962}
2963
2964/*
2965 * + number addition or list/blobl concatenation
2966 * - number subtraction
2967 * .. string concatenation
2968 */
2969 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002970compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002971{
2972 char_u *op;
2973 char_u *next;
2974 int oplen;
2975 int ppconst_used = ppconst->pp_used;
2976
2977 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002978 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002979 return FAIL;
2980
2981 /*
2982 * Repeat computing, until no "+", "-" or ".." is following.
2983 */
2984 for (;;)
2985 {
2986 op = may_peek_next_line(cctx, *arg, &next);
2987 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2988 break;
2989 if (op[0] == op[1] && *op != '.' && next)
2990 // Finding "++" or "--" on the next line is a separate command.
2991 // But ".." is concatenation.
2992 break;
2993 oplen = (*op == '.' ? 2 : 1);
2994 if (next != NULL)
2995 {
2996 *arg = next_line_from_context(cctx, TRUE);
2997 op = skipwhite(*arg);
2998 }
2999
3000 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
3001 {
3002 error_white_both(op, oplen);
3003 return FAIL;
3004 }
3005
3006 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
3007 return FAIL;
3008
3009 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003010 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003011 return FAIL;
3012
3013 if (ppconst->pp_used == ppconst_used + 2
3014 && (*op == '.'
3015 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3016 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3017 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3018 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
3019 {
3020 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3021 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
3022
3023 // concat/subtract/add constant numbers
3024 if (*op == '+')
3025 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3026 else if (*op == '-')
3027 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3028 else
3029 {
3030 // concatenate constant strings
3031 char_u *s1 = tv1->vval.v_string;
3032 char_u *s2 = tv2->vval.v_string;
3033 size_t len1 = STRLEN(s1);
3034
3035 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3036 if (tv1->vval.v_string == NULL)
3037 {
3038 clear_ppconst(ppconst);
3039 return FAIL;
3040 }
3041 mch_memmove(tv1->vval.v_string, s1, len1);
3042 STRCPY(tv1->vval.v_string + len1, s2);
3043 vim_free(s1);
3044 vim_free(s2);
3045 }
3046 --ppconst->pp_used;
3047 }
3048 else
3049 {
3050 generate_ppconst(cctx, ppconst);
3051 ppconst->pp_is_const = FALSE;
3052 if (*op == '.')
3053 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003054 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3055 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003056 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003057 if (generate_CONCAT(cctx, 2) == FAIL)
3058 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003059 }
3060 else
3061 generate_two_op(cctx, op);
3062 }
3063 }
3064
3065 return OK;
3066}
3067
3068/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003069 * expr6a >> expr6b
3070 * expr6a << expr6b
3071 *
3072 * Produces instructions:
3073 * OPNR bitwise left or right shift
3074 */
3075 static int
3076compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3077{
3078 exprtype_T type = EXPR_UNKNOWN;
3079 char_u *p;
3080 char_u *next;
3081 int len = 2;
3082 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003083 isn_T *isn;
3084
3085 // get the first variable
3086 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3087 return FAIL;
3088
3089 /*
3090 * Repeat computing, until no "+", "-" or ".." is following.
3091 */
3092 for (;;)
3093 {
3094 type = EXPR_UNKNOWN;
3095
3096 p = may_peek_next_line(cctx, *arg, &next);
3097 if (p[0] == '<' && p[1] == '<')
3098 type = EXPR_LSHIFT;
3099 else if (p[0] == '>' && p[1] == '>')
3100 type = EXPR_RSHIFT;
3101
3102 if (type == EXPR_UNKNOWN)
3103 return OK;
3104
3105 // Handle a bitwise left or right shift operator
3106 if (ppconst->pp_used == ppconst_used + 1)
3107 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003108 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003109 {
3110 // left operand should be a number
3111 emsg(_(e_bitshift_ops_must_be_number));
3112 return FAIL;
3113 }
3114 }
3115 else
3116 {
3117 type_T *t = get_type_on_stack(cctx, 0);
3118
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003119 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003120 {
3121 emsg(_(e_bitshift_ops_must_be_number));
3122 return FAIL;
3123 }
3124 }
3125
3126 if (next != NULL)
3127 {
3128 *arg = next_line_from_context(cctx, TRUE);
3129 p = skipwhite(*arg);
3130 }
3131
3132 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3133 {
3134 error_white_both(p, len);
3135 return FAIL;
3136 }
3137
3138 // get the second variable
3139 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3140 return FAIL;
3141
3142 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3143 return FAIL;
3144
3145 if (ppconst->pp_used == ppconst_used + 2)
3146 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003147 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3148 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3149
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003150 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003151 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3152 {
3153 // right operand should be a positive number
3154 if (tv2->v_type != VAR_NUMBER)
3155 emsg(_(e_bitshift_ops_must_be_number));
3156 else
dundargocc57b5bc2022-11-02 13:30:51 +00003157 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003158 return FAIL;
3159 }
3160
3161 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3162 tv1->vval.v_number = 0;
3163 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003164 tv1->vval.v_number =
3165 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003166 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003167 tv1->vval.v_number =
3168 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003169 clear_tv(tv2);
3170 --ppconst->pp_used;
3171 }
3172 else
3173 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003174 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3175 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003176 {
3177 emsg(_(e_bitshift_ops_must_be_number));
3178 return FAIL;
3179 }
3180
3181 generate_ppconst(cctx, ppconst);
3182
3183 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3184 if (isn == NULL)
3185 return FAIL;
3186
3187 if (isn != NULL)
3188 isn->isn_arg.op.op_type = type;
3189 }
3190 }
3191
3192 return OK;
3193}
3194
3195/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003196 * expr5a == expr5b
3197 * expr5a =~ expr5b
3198 * expr5a != expr5b
3199 * expr5a !~ expr5b
3200 * expr5a > expr5b
3201 * expr5a >= expr5b
3202 * expr5a < expr5b
3203 * expr5a <= expr5b
3204 * expr5a is expr5b
3205 * expr5a isnot expr5b
3206 *
3207 * Produces instructions:
3208 * EVAL expr5a Push result of "expr5a"
3209 * EVAL expr5b Push result of "expr5b"
3210 * COMPARE one of the compare instructions
3211 */
3212 static int
3213compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3214{
3215 exprtype_T type = EXPR_UNKNOWN;
3216 char_u *p;
3217 char_u *next;
3218 int len = 2;
3219 int type_is = FALSE;
3220 int ppconst_used = ppconst->pp_used;
3221
3222 // get the first variable
3223 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3224 return FAIL;
3225
3226 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003227
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003228 type = get_compare_type(p, &len, &type_is);
3229
3230 /*
3231 * If there is a comparative operator, use it.
3232 */
3233 if (type != EXPR_UNKNOWN)
3234 {
3235 int ic = FALSE; // Default: do not ignore case
3236
3237 if (next != NULL)
3238 {
3239 *arg = next_line_from_context(cctx, TRUE);
3240 p = skipwhite(*arg);
3241 }
3242 if (type_is && (p[len] == '?' || p[len] == '#'))
3243 {
3244 semsg(_(e_invalid_expression_str), *arg);
3245 return FAIL;
3246 }
3247 // extra question mark appended: ignore case
3248 if (p[len] == '?')
3249 {
3250 ic = TRUE;
3251 ++len;
3252 }
3253 // extra '#' appended: match case (ignored)
3254 else if (p[len] == '#')
3255 ++len;
3256 // nothing appended: match case
3257
3258 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3259 {
3260 error_white_both(p, len);
3261 return FAIL;
3262 }
3263
3264 // get the second variable
3265 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3266 return FAIL;
3267
3268 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3269 return FAIL;
3270
3271 if (ppconst->pp_used == ppconst_used + 2)
3272 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003273 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003274 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3275 int ret;
3276
3277 // Both sides are a constant, compute the result now.
3278 // First check for a valid combination of types, this is more
3279 // strict than typval_compare().
3280 if (check_compare_types(type, tv1, tv2) == FAIL)
3281 ret = FAIL;
3282 else
3283 {
3284 ret = typval_compare(tv1, tv2, type, ic);
3285 tv1->v_type = VAR_BOOL;
3286 tv1->vval.v_number = tv1->vval.v_number
3287 ? VVAL_TRUE : VVAL_FALSE;
3288 clear_tv(tv2);
3289 --ppconst->pp_used;
3290 }
3291 return ret;
3292 }
3293
3294 generate_ppconst(cctx, ppconst);
3295 return generate_COMPARE(cctx, type, ic);
3296 }
3297
3298 return OK;
3299}
3300
3301static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3302
3303/*
3304 * Compile || or &&.
3305 */
3306 static int
3307compile_and_or(
3308 char_u **arg,
3309 cctx_T *cctx,
3310 char *op,
3311 ppconst_T *ppconst,
3312 int ppconst_used UNUSED)
3313{
3314 char_u *next;
3315 char_u *p = may_peek_next_line(cctx, *arg, &next);
3316 int opchar = *op;
3317
3318 if (p[0] == opchar && p[1] == opchar)
3319 {
3320 garray_T *instr = &cctx->ctx_instr;
3321 garray_T end_ga;
3322 int save_skip = cctx->ctx_skip;
3323
3324 /*
3325 * Repeat until there is no following "||" or "&&"
3326 */
3327 ga_init2(&end_ga, sizeof(int), 10);
3328 while (p[0] == opchar && p[1] == opchar)
3329 {
3330 long start_lnum = SOURCING_LNUM;
3331 long save_sourcing_lnum;
3332 int start_ctx_lnum = cctx->ctx_lnum;
3333 int save_lnum;
3334 int const_used;
3335 int status;
3336 jumpwhen_T jump_when = opchar == '|'
3337 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3338
3339 if (next != NULL)
3340 {
3341 *arg = next_line_from_context(cctx, TRUE);
3342 p = skipwhite(*arg);
3343 }
3344
3345 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3346 {
3347 semsg(_(e_white_space_required_before_and_after_str_at_str),
3348 op, p);
3349 ga_clear(&end_ga);
3350 return FAIL;
3351 }
3352
3353 save_sourcing_lnum = SOURCING_LNUM;
3354 SOURCING_LNUM = start_lnum;
3355 save_lnum = cctx->ctx_lnum;
3356 cctx->ctx_lnum = start_ctx_lnum;
3357
3358 status = check_ppconst_bool(ppconst);
3359 if (status != FAIL)
3360 {
3361 // Use the last ppconst if possible.
3362 if (ppconst->pp_used > 0)
3363 {
3364 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3365 int is_true = tv2bool(tv);
3366
3367 if ((is_true && opchar == '|')
3368 || (!is_true && opchar == '&'))
3369 {
3370 // For "false && expr" and "true || expr" the "expr"
3371 // does not need to be evaluated.
3372 cctx->ctx_skip = SKIP_YES;
3373 clear_tv(tv);
3374 tv->v_type = VAR_BOOL;
3375 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3376 }
3377 else
3378 {
3379 // For "true && expr" and "false || expr" only "expr"
3380 // needs to be evaluated.
3381 --ppconst->pp_used;
3382 jump_when = JUMP_NEVER;
3383 }
3384 }
3385 else
3386 {
3387 // Every part must evaluate to a bool.
3388 status = bool_on_stack(cctx);
3389 }
3390 }
3391 if (status != FAIL)
3392 status = ga_grow(&end_ga, 1);
3393 cctx->ctx_lnum = save_lnum;
3394 if (status == FAIL)
3395 {
3396 ga_clear(&end_ga);
3397 return FAIL;
3398 }
3399
3400 if (jump_when != JUMP_NEVER)
3401 {
3402 if (cctx->ctx_skip != SKIP_YES)
3403 {
3404 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3405 ++end_ga.ga_len;
3406 }
3407 generate_JUMP(cctx, jump_when, 0);
3408 }
3409
3410 // eval the next expression
3411 SOURCING_LNUM = save_sourcing_lnum;
3412 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3413 {
3414 ga_clear(&end_ga);
3415 return FAIL;
3416 }
3417
3418 const_used = ppconst->pp_used;
3419 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3420 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3421 {
3422 ga_clear(&end_ga);
3423 return FAIL;
3424 }
3425
3426 // "0 || 1" results in true, "1 && 0" results in false.
3427 if (ppconst->pp_used == const_used + 1)
3428 {
3429 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3430
3431 if (tv->v_type == VAR_NUMBER
3432 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3433 {
3434 tv->vval.v_number = tv->vval.v_number == 1
3435 ? VVAL_TRUE : VVAL_FALSE;
3436 tv->v_type = VAR_BOOL;
3437 }
3438 }
3439
3440 p = may_peek_next_line(cctx, *arg, &next);
3441 }
3442
3443 if (check_ppconst_bool(ppconst) == FAIL)
3444 {
3445 ga_clear(&end_ga);
3446 return FAIL;
3447 }
3448
3449 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3450 // Every part must evaluate to a bool.
3451 if (bool_on_stack(cctx) == FAIL)
3452 {
3453 ga_clear(&end_ga);
3454 return FAIL;
3455 }
3456
3457 if (end_ga.ga_len > 0)
3458 {
3459 // Fill in the end label in all jumps.
3460 generate_ppconst(cctx, ppconst);
3461 while (end_ga.ga_len > 0)
3462 {
3463 isn_T *isn;
3464
3465 --end_ga.ga_len;
3466 isn = ((isn_T *)instr->ga_data)
3467 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3468 isn->isn_arg.jump.jump_where = instr->ga_len;
3469 }
3470 }
3471 ga_clear(&end_ga);
3472
3473 cctx->ctx_skip = save_skip;
3474 }
3475
3476 return OK;
3477}
3478
3479/*
3480 * expr4a && expr4a && expr4a logical AND
3481 *
3482 * Produces instructions:
3483 * EVAL expr4a Push result of "expr4a"
3484 * COND2BOOL convert to bool if needed
3485 * JUMP_IF_COND_FALSE end
3486 * EVAL expr4b Push result of "expr4b"
3487 * JUMP_IF_COND_FALSE end
3488 * EVAL expr4c Push result of "expr4c"
3489 * end:
3490 */
3491 static int
3492compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3493{
3494 int ppconst_used = ppconst->pp_used;
3495
3496 // get the first variable
3497 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3498 return FAIL;
3499
3500 // || and && work almost the same
3501 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3502}
3503
3504/*
3505 * expr3a || expr3b || expr3c logical OR
3506 *
3507 * Produces instructions:
3508 * EVAL expr3a Push result of "expr3a"
3509 * COND2BOOL convert to bool if needed
3510 * JUMP_IF_COND_TRUE end
3511 * EVAL expr3b Push result of "expr3b"
3512 * JUMP_IF_COND_TRUE end
3513 * EVAL expr3c Push result of "expr3c"
3514 * end:
3515 */
3516 static int
3517compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3518{
3519 int ppconst_used = ppconst->pp_used;
3520
3521 // eval the first expression
3522 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3523 return FAIL;
3524
3525 // || and && work almost the same
3526 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3527}
3528
3529/*
3530 * Toplevel expression: expr2 ? expr1a : expr1b
3531 * Produces instructions:
3532 * EVAL expr2 Push result of "expr2"
3533 * JUMP_IF_FALSE alt jump if false
3534 * EVAL expr1a
3535 * JUMP_ALWAYS end
3536 * alt: EVAL expr1b
3537 * end:
3538 *
3539 * Toplevel expression: expr2 ?? expr1
3540 * Produces instructions:
3541 * EVAL expr2 Push result of "expr2"
3542 * JUMP_AND_KEEP_IF_TRUE end jump if true
3543 * EVAL expr1
3544 * end:
3545 */
3546 int
3547compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3548{
3549 char_u *p;
3550 int ppconst_used = ppconst->pp_used;
3551 char_u *next;
3552
3553 // Ignore all kinds of errors when not producing code.
3554 if (cctx->ctx_skip == SKIP_YES)
3555 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003556 int prev_did_emsg = did_emsg;
3557
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003558 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003559 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003560 }
3561
3562 // Evaluate the first expression.
3563 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3564 return FAIL;
3565
3566 p = may_peek_next_line(cctx, *arg, &next);
3567 if (*p == '?')
3568 {
3569 int op_falsy = p[1] == '?';
3570 garray_T *instr = &cctx->ctx_instr;
3571 garray_T *stack = &cctx->ctx_type_stack;
3572 int alt_idx = instr->ga_len;
3573 int end_idx = 0;
3574 isn_T *isn;
3575 type_T *type1 = NULL;
3576 int has_const_expr = FALSE;
3577 int const_value = FALSE;
3578 int save_skip = cctx->ctx_skip;
3579
3580 if (next != NULL)
3581 {
3582 *arg = next_line_from_context(cctx, TRUE);
3583 p = skipwhite(*arg);
3584 }
3585
3586 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3587 {
3588 semsg(_(e_white_space_required_before_and_after_str_at_str),
3589 op_falsy ? "??" : "?", p);
3590 return FAIL;
3591 }
3592
3593 if (ppconst->pp_used == ppconst_used + 1)
3594 {
3595 // the condition is a constant, we know whether the ? or the :
3596 // expression is to be evaluated.
3597 has_const_expr = TRUE;
3598 if (op_falsy)
3599 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3600 else
3601 {
3602 int error = FALSE;
3603
3604 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3605 &error);
3606 if (error)
3607 return FAIL;
3608 }
3609 cctx->ctx_skip = save_skip == SKIP_YES ||
3610 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3611
3612 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3613 // "left ?? right" and "left" is truthy: produce "left"
3614 generate_ppconst(cctx, ppconst);
3615 else
3616 {
3617 clear_tv(&ppconst->pp_tv[ppconst_used]);
3618 --ppconst->pp_used;
3619 }
3620 }
3621 else
3622 {
3623 generate_ppconst(cctx, ppconst);
3624 if (op_falsy)
3625 end_idx = instr->ga_len;
3626 generate_JUMP(cctx, op_falsy
3627 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3628 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003629 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003630 }
3631
3632 // evaluate the second expression; any type is accepted
3633 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3634 return FAIL;
3635 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3636 return FAIL;
3637
3638 if (!has_const_expr)
3639 {
3640 generate_ppconst(cctx, ppconst);
3641
3642 if (!op_falsy)
3643 {
3644 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003645 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003646 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003647
3648 end_idx = instr->ga_len;
3649 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3650
3651 // jump here from JUMP_IF_FALSE
3652 isn = ((isn_T *)instr->ga_data) + alt_idx;
3653 isn->isn_arg.jump.jump_where = instr->ga_len;
3654 }
3655 }
3656
3657 if (!op_falsy)
3658 {
3659 // Check for the ":".
3660 p = may_peek_next_line(cctx, *arg, &next);
3661 if (*p != ':')
3662 {
3663 emsg(_(e_missing_colon_after_questionmark));
3664 return FAIL;
3665 }
3666 if (next != NULL)
3667 {
3668 *arg = next_line_from_context(cctx, TRUE);
3669 p = skipwhite(*arg);
3670 }
3671
3672 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3673 {
3674 semsg(_(e_white_space_required_before_and_after_str_at_str),
3675 ":", p);
3676 return FAIL;
3677 }
3678
3679 // evaluate the third expression
3680 if (has_const_expr)
3681 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3682 ? SKIP_YES : SKIP_NOT;
3683 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3684 return FAIL;
3685 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3686 return FAIL;
3687 }
3688
3689 if (!has_const_expr)
3690 {
3691 type_T **typep;
3692
3693 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003694 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003695
3696 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003697 typep = &((((type2_T *)stack->ga_data)
3698 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003699 common_type(type1, *typep, typep, cctx->ctx_type_list);
3700
3701 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3702 isn = ((isn_T *)instr->ga_data) + end_idx;
3703 isn->isn_arg.jump.jump_where = instr->ga_len;
3704 }
3705
3706 cctx->ctx_skip = save_skip;
3707 }
3708 return OK;
3709}
3710
3711/*
3712 * Toplevel expression.
3713 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3714 * Returns OK or FAIL.
3715 */
3716 int
3717compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3718{
3719 ppconst_T ppconst;
3720
3721 CLEAR_FIELD(ppconst);
3722 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3723 {
3724 clear_ppconst(&ppconst);
3725 return FAIL;
3726 }
3727 if (is_const != NULL)
3728 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3729 if (generate_ppconst(cctx, &ppconst) == FAIL)
3730 return FAIL;
3731 return OK;
3732}
3733
3734/*
3735 * Toplevel expression.
3736 */
3737 int
3738compile_expr0(char_u **arg, cctx_T *cctx)
3739{
3740 return compile_expr0_ext(arg, cctx, NULL);
3741}
3742
3743
3744#endif // defined(FEAT_EVAL)