blob: c2428f4bbeb60a0dc7ec8127b8a84e8d7f0089da [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
352 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000353 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000354 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000355 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000356
357 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000358 int fi;
359 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000360 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000361 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000362 // Use a separate pointer to avoid that ASAN complains about
363 // uf_name[] only being 4 characters.
364 char_u *ufname = (char_u *)fp->uf_name;
365 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
366 {
367 ufunc = fp;
368 break;
369 }
370 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200371 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000372 if (ufunc == NULL)
373 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200374 // could be a funcref in a member variable
375 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
376 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
377 {
378 method_not_found_msg(cl, type->tt_type, name, len);
379 return FAIL;
380 }
381 if (type->tt_type == VAR_CLASS)
382 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200383 // Remove the class type from the stack
384 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200385 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
386 return FAIL;
387 }
388 else
389 {
390 if (generate_GET_OBJ_MEMBER(cctx, m_idx, ocm->ocm_type) ==
391 FAIL)
392 return FAIL;
393 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000394 }
395
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200396 // A private object method can be used only inside the class where it
397 // is defined or in one of the child classes.
398 // A private class method can be used only in the class where it is
399 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200400 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200401 ((type->tt_type == VAR_OBJECT
402 && !inside_class_hierarchy(cctx, cl))
403 || (type->tt_type == VAR_CLASS
404 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200405 {
Ernie Rael03042a22023-11-11 08:53:32 +0100406 semsg(_(e_cannot_access_protected_method_str), name);
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200407 return FAIL;
408 }
409
Bram Moolenaar574950d2023-01-03 19:08:50 +0000410 // Compile the arguments and call the class function or object method.
411 // The object method will know that the object is on the stack, just
412 // before the arguments.
413 *arg = skipwhite(name_end + 1);
414 int argcount = 0;
415 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
416 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000417
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200418 if (ocm != NULL)
419 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000420 if (type->tt_type == VAR_OBJECT
421 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-eastb895b0f2023-09-24 15:46:31 +0200422 return generate_CALL(cctx, ufunc, cl, fi, argcount);
423 return generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000424 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000425
426 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000427 {
zeertzjqd9be94c2024-07-14 10:20:20 +0200428 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200429 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000430 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200431 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000432 {
Ernie Rael03042a22023-11-11 08:53:32 +0100433 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200434 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200435 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200436 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200437
438 *arg = name_end;
439 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200440 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
441 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200442 }
443
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200444 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200445 m_idx = object_method_idx(cl, name, len);
446 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000447 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200448 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100449 // Private object methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200450 if (*name == '_' && !inside_class(cctx, cl))
451 {
Ernie Rael03042a22023-11-11 08:53:32 +0100452 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200453 return FAIL;
454 }
455 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200456 // Remove the object type from the stack
457 --cctx->ctx_type_stack.ga_len;
458 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000459 }
460
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200461 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000462 }
463 else
464 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000465 // load class member
466 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200467 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
468 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000469 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200470 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200471 // A private class variable can be accessed only in the class where
472 // it is defined.
473 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200474 {
Ernie Rael03042a22023-11-11 08:53:32 +0100475 emsg_var_cl_define(e_cannot_access_protected_variable_str,
Ernie Raele6c9aa52023-10-06 19:55:52 +0200476 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200477 return FAIL;
478 }
479
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000480 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200481 // Remove the class type from the stack
482 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000483 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
484 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200485
486 // Could be a class method reference: "class.Func".
487 m_idx = class_method_idx(cl, name, len);
488 if (m_idx >= 0)
489 {
490 ufunc_T *fp = cl->class_class_functions[m_idx];
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100491 // Private class methods are not accessible outside the class
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200492 if (*name == '_' && !inside_class(cctx, cl))
493 {
Ernie Rael03042a22023-11-11 08:53:32 +0100494 semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200495 return FAIL;
496 }
497 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200498 // Remove the class type from the stack
499 --cctx->ctx_type_stack.ga_len;
500 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200501 }
502
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200503 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000504 }
505
506 return FAIL;
507}
508
509/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000510 * Generate an instruction to load script-local variable "name", without the
511 * leading "s:".
512 * Also finds imported variables.
513 */
514 int
515compile_load_scriptvar(
516 cctx_T *cctx,
517 char_u *name, // variable NUL terminated
518 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100519 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000520{
521 scriptitem_T *si;
522 int idx;
523 imported_T *import;
524
525 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
526 return FAIL;
527 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000528 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529 if (idx >= 0)
530 {
531 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
532
533 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
534 current_sctx.sc_sid, idx, sv->sv_type);
535 return OK;
536 }
537
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000538 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539 if (import != NULL)
540 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000541 char_u *p = skipwhite(*end);
542 char_u *exp_name;
543 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100544 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000545 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000546 int done = FALSE;
547 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000548
Ernie Rael9a901792024-04-16 22:11:56 +0200549 check_script_symlink(import->imp_sid);
550 import_check_sourced_sid(&import->imp_sid);
551
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000552 // Need to lookup the member.
553 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000554 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000555 semsg(_(e_expected_dot_after_name_str), start);
556 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000557 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000558 ++p;
559 if (VIM_ISWHITE(*p))
560 {
561 emsg(_(e_no_white_space_allowed_after_dot));
562 return FAIL;
563 }
564
565 // isolate one name
566 exp_name = p;
567 while (eval_isnamec(*p))
568 ++p;
569 cc = *p;
570 *p = NUL;
571
Bram Moolenaard041f422022-01-12 19:54:00 +0000572 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100573 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
574 // "import autoload './dir/script.vim'" or
575 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100576 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100577
578 if (res == OK)
579 {
580 if (si->sn_autoload_prefix != NULL
581 && si->sn_state == SN_STATE_NOT_LOADED)
582 {
583 char_u *auto_name =
584 concat_str(si->sn_autoload_prefix, exp_name);
585
586 // autoload script must be loaded later, access by the autoload
587 // name. If a '(' follows it must be a function. Otherwise we
588 // don't know, it can be "script.Func".
589 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100590 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100591 else
592 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
593 vim_free(auto_name);
594 done = TRUE;
595 }
596 else if (si->sn_import_autoload
597 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100598 {
599 // If a '(' follows it must be a function. Otherwise we don't
600 // know, it can be "script.Func".
601 if (cc == '(' || paren_follows_after_expr)
602 {
603 char_u sid_name[MAX_FUNC_NAME_LEN];
604
605 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100606 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100607 }
608 else
609 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
610 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100611 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100612 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100613 else
614 {
615 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
616 cctx, NULL, TRUE);
617 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100618 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100619
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000620 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000621 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000622 if (done)
623 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000624
625 if (idx < 0)
626 {
627 if (ufunc != NULL)
628 {
629 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100630 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000631 return OK;
632 }
633 return FAIL;
634 }
635
636 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
637 import->imp_sid,
638 idx,
639 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640 return OK;
641 }
642
Bram Moolenaard0132f42022-05-12 11:05:40 +0100643 // Can only get here if we know "name" is a script variable and not in a
644 // Vim9 script (variable is not in sn_var_vals): old style script.
645 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000646 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000647}
648
649 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000650generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000651{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000652 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000653 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000654
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000655 // Reject a global non-autoload function found without the "g:" prefix.
656 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657 return FAIL;
658
659 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000660 compile_type = get_compile_type(ufunc);
661 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000662 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000663 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100664 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000665}
666
667/*
668 * Compile a variable name into a load instruction.
669 * "end" points to just after the name.
670 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
671 * When "error" is FALSE do not give an error when not found.
672 */
673 int
674compile_load(
675 char_u **arg,
676 char_u *end_arg,
677 cctx_T *cctx,
678 int is_expr,
679 int error)
680{
681 type_T *type;
682 char_u *name = NULL;
683 char_u *end = end_arg;
684 int res = FAIL;
685 int prev_called_emsg = called_emsg;
686
687 if (*(*arg + 1) == ':')
688 {
689 if (end <= *arg + 2)
690 {
691 isntype_T isn_type;
692
693 // load dictionary of namespace
694 switch (**arg)
695 {
696 case 'g': isn_type = ISN_LOADGDICT; break;
697 case 'w': isn_type = ISN_LOADWDICT; break;
698 case 't': isn_type = ISN_LOADTDICT; break;
699 case 'b': isn_type = ISN_LOADBDICT; break;
700 default:
701 semsg(_(e_namespace_not_supported_str), *arg);
702 goto theend;
703 }
704 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
705 goto theend;
706 res = OK;
707 }
708 else
709 {
710 isntype_T isn_type = ISN_DROP;
711
712 // load namespaced variable
713 name = vim_strnsave(*arg + 2, end - (*arg + 2));
714 if (name == NULL)
715 return FAIL;
716
717 switch (**arg)
718 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100719 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000720 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000721 case 's': if (current_script_is_vim9())
722 {
723 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
724 *arg);
725 vim_free(name);
726 return FAIL;
727 }
Kota Kato948a3892022-08-16 16:09:59 +0100728 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000729 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 else
731 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100732 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733 break;
734 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
735 {
736 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000737 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000738 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000739 else
740 isn_type = ISN_LOADG;
741 }
742 else
743 {
744 isn_type = ISN_LOADAUTO;
745 vim_free(name);
746 name = vim_strnsave(*arg, end - *arg);
747 if (name == NULL)
748 return FAIL;
749 }
750 break;
751 case 'w': isn_type = ISN_LOADW; break;
752 case 't': isn_type = ISN_LOADT; break;
753 case 'b': isn_type = ISN_LOADB; break;
754 default: // cannot happen, just in case
755 semsg(_(e_namespace_not_supported_str), *arg);
756 goto theend;
757 }
758 if (isn_type != ISN_DROP)
759 {
760 // Global, Buffer-local, Window-local and Tabpage-local
761 // variables can be defined later, thus we don't check if it
762 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000763 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764 }
765 }
766 }
767 else
768 {
769 size_t len = end - *arg;
770 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200771 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 int gen_load = FALSE;
773 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100774 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100775 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776
777 name = vim_strnsave(*arg, end - *arg);
778 if (name == NULL)
779 return FAIL;
780
Bram Moolenaar58b40092023-01-11 15:59:05 +0000781 if (STRCMP(name, "super") == 0
782 && cctx->ctx_ufunc != NULL
783 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
784 {
785 // super.SomeFunc() in a class function: push &t_super type, this
786 // is recognized in compile_subscript().
787 res = push_type_stack(cctx, &t_super);
788 if (*end != '.')
789 emsg(_(e_super_must_be_followed_by_dot));
790 }
791 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 {
793 script_autoload(name, FALSE);
794 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
795 }
796 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
797 == OK)
798 {
799 if (gen_load_outer == 0)
800 gen_load = TRUE;
801 }
802 else
803 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000804 lvar_T lvar;
805 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806
807 if (lookup_local(*arg, len, &lvar, cctx) == OK)
808 {
809 type = lvar.lv_type;
810 idx = lvar.lv_idx;
811 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100812 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000813 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100814 outer_loop_depth = lvar.lv_loop_depth;
815 outer_loop_idx = lvar.lv_loop_idx;
816 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 else
818 gen_load = TRUE;
819 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200820 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
821 (((idx =
822 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
823 || ((method_idx =
824 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000825 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200826 // Referencing a class variable or method without the class
827 // name. A class variable or method can be referenced without
828 // the class name only in the class where the function is
829 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200830 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200831 {
832 if (idx >= 0)
833 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
834 else
835 {
836 ufunc_T *fp = cl->class_class_functions[method_idx];
837 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
838 NULL);
839 }
840 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200841 else
842 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200843 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200844 name, cl->class_name);
845 res = FAIL;
846 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000847 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 else
849 {
850 // "var" can be script-local even without using "s:" if it
851 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000852 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000853 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100854 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855
856 // When evaluating an expression and the name starts with an
857 // uppercase letter it can be a user defined function.
858 // generate_funcref() will fail if the function can't be found.
859 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000860 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861 }
862 }
863 if (gen_load)
864 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
865 if (gen_load_outer > 0)
866 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100867 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
868 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869 cctx->ctx_outer_used = TRUE;
870 }
871 }
872
873 *arg = end;
874
875theend:
876 if (res == FAIL && error && called_emsg == prev_called_emsg)
877 semsg(_(e_variable_not_found_str), name);
878 vim_free(name);
879 return res;
880}
881
882/*
883 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100884 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 * Returns FAIL if compilation fails.
886 */
887 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100888compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889{
LemonBoyf3b48952022-05-05 13:53:03 +0100890 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891 garray_T save_ga = cctx->ctx_instr;
892 int expr_res;
893 int trailing_error;
894 int instr_count;
895 isn_T *instr = NULL;
896
897 // Remove the string type from the stack.
898 --cctx->ctx_type_stack.ga_len;
899
900 // Temporarily reset the list of instructions so that the jump labels are
901 // correct.
902 cctx->ctx_instr.ga_len = 0;
903 cctx->ctx_instr.ga_maxlen = 0;
904 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000905
906 // avoid peeking a next line
907 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
908 cctx->ctx_ufunc->uf_lines.ga_len = 0;
909
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000911
912 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
913
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000914 s = skipwhite(s);
915 trailing_error = *s != NUL;
916
917 if (expr_res == FAIL || trailing_error
918 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
919 {
920 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000921 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 clear_instr_ga(&cctx->ctx_instr);
923 cctx->ctx_instr = save_ga;
924 ++cctx->ctx_type_stack.ga_len;
925 return FAIL;
926 }
927
928 // Move the generated instructions into the ISN_INSTR instruction, then
929 // restore the list of instructions.
930 instr_count = cctx->ctx_instr.ga_len;
931 instr = cctx->ctx_instr.ga_data;
932 instr[instr_count].isn_type = ISN_FINISH;
933
934 cctx->ctx_instr = save_ga;
935 vim_free(isn->isn_arg.string);
936 isn->isn_type = ISN_INSTR;
937 isn->isn_arg.instr = instr;
938 return OK;
939}
940
941/*
942 * Compile the argument expressions.
943 * "arg" points to just after the "(" and is advanced to after the ")"
944 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100945 int
LemonBoyf3b48952022-05-05 13:53:03 +0100946compile_arguments(
947 char_u **arg,
948 cctx_T *cctx,
949 int *argcount,
950 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000951{
952 char_u *p = *arg;
953 char_u *whitep = *arg;
954 int must_end = FALSE;
955 int instr_count;
956
957 for (;;)
958 {
959 if (may_get_next_line(whitep, &p, cctx) == FAIL)
960 goto failret;
961 if (*p == ')')
962 {
963 *arg = p + 1;
964 return OK;
965 }
966 if (must_end)
967 {
968 semsg(_(e_missing_comma_before_argument_str), p);
969 return FAIL;
970 }
971
972 instr_count = cctx->ctx_instr.ga_len;
973 if (compile_expr0(&p, cctx) == FAIL)
974 return FAIL;
975 ++*argcount;
976
LemonBoyf3b48952022-05-05 13:53:03 +0100977 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978 && cctx->ctx_instr.ga_len == instr_count + 1)
979 {
980 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
981
982 // {skip} argument of searchpair() can be compiled if not empty
983 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100984 compile_string(isn, cctx, 0);
985 }
986 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
987 && cctx->ctx_instr.ga_len == instr_count + 1)
988 {
989 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
990
991 // {sub} argument of substitute() can be compiled if it starts
992 // with \=
993 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100994 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100995 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996 }
997
998 if (*p != ',' && *skipwhite(p) == ',')
999 {
1000 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1001 p = skipwhite(p);
1002 }
1003 if (*p == ',')
1004 {
1005 ++p;
1006 if (*p != NUL && !VIM_ISWHITE(*p))
1007 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1008 }
1009 else
1010 must_end = TRUE;
1011 whitep = p;
1012 p = skipwhite(p);
1013 }
1014failret:
1015 emsg(_(e_missing_closing_paren));
1016 return FAIL;
1017}
1018
1019/*
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001020 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1021 * etc.) if the class implements it.
1022 */
1023 static int
1024compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1025{
1026 type_T *type = get_decl_type_on_stack(cctx, 0);
1027 int res = FAIL;
1028
1029 // If the built in function is invoked on an object and the class
1030 // implements the corresponding built in method, then invoke the object
1031 // method.
1032 if (type->tt_type == VAR_OBJECT)
1033 {
1034 int method_idx;
1035 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1036 &method_idx);
1037 if (uf != NULL)
1038 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0);
1039 }
1040
1041 return res;
1042}
1043
1044
1045/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001046 * Compile a function call: name(arg1, arg2)
1047 * "arg" points to "name", "arg + varlen" to the "(".
1048 * "argcount_init" is 1 for "value->method()"
1049 * Instructions:
1050 * EVAL arg1
1051 * EVAL arg2
1052 * BCALL / DCALL / UCALL
1053 */
1054 static int
1055compile_call(
1056 char_u **arg,
1057 size_t varlen,
1058 cctx_T *cctx,
1059 ppconst_T *ppconst,
1060 int argcount_init)
1061{
1062 char_u *name = *arg;
1063 char_u *p;
1064 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001065 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001066 char_u fname_buf[FLEN_FIXED + 1];
1067 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001068 ufunc_T *ufunc = NULL;
1069 int res = FAIL;
1070 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001071 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001072 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001073 imported_T *import;
1074
1075 if (varlen >= sizeof(namebuf))
1076 {
1077 semsg(_(e_name_too_long_str), name);
1078 return FAIL;
1079 }
1080 vim_strncpy(namebuf, *arg, varlen);
1081
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001082 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001083 if (import != NULL)
1084 {
1085 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1086 return FAIL;
1087 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001088
1089 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001090 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001091 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001092 if ((varlen == 3
1093 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001094 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1095 {
1096 char_u *s = skipwhite(*arg + varlen + 1);
1097 typval_T argvars[2];
1098 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001099 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100
1101 argvars[0].v_type = VAR_UNKNOWN;
1102 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001103 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001104 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001105 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 s = skipwhite(s);
1107 if (*s == ')' && argvars[0].v_type == VAR_STRING
1108 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1109 || !is_has))
1110 {
1111 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1112
1113 *arg = s + 1;
1114 argvars[1].v_type = VAR_UNKNOWN;
1115 tv->v_type = VAR_NUMBER;
1116 tv->vval.v_number = 0;
1117 if (is_has)
1118 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001119 else if (is_len)
1120 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001121 else
1122 f_exists(argvars, tv);
1123 clear_tv(&argvars[0]);
1124 ++ppconst->pp_used;
1125 return OK;
1126 }
1127 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001128 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129 {
1130 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1131 return FAIL;
1132 }
1133 }
1134
1135 if (generate_ppconst(cctx, ppconst) == FAIL)
1136 return FAIL;
1137
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001138 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1140
1141 // We handle the "skip" argument of searchpair() and searchpairpos()
1142 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001143 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1144 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1145 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1146 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1147 special_fn = CA_SEARCHPAIR;
1148 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1149 special_fn = CA_SUBSTITUTE;
1150 else
1151 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001152
1153 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001154 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001155 goto theend;
1156
1157 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1158 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1159 {
1160 int idx;
1161
1162 // builtin function
1163 idx = find_internal_func(name);
1164 if (idx >= 0)
1165 {
1166 if (STRCMP(name, "flatten") == 0)
1167 {
1168 emsg(_(e_cannot_use_flatten_in_vim9_script));
1169 goto theend;
1170 }
1171
1172 if (STRCMP(name, "add") == 0 && argcount == 2)
1173 {
h-eastb895b0f2023-09-24 15:46:31 +02001174 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001175 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1176 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001177
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 // add() can be compiled to instructions if we know the type
1179 if (type->tt_type == VAR_LIST)
1180 {
1181 // inline "add(list, item)" so that the type can be checked
1182 res = generate_LISTAPPEND(cctx);
1183 idx = -1;
1184 }
1185 else if (type->tt_type == VAR_BLOB)
1186 {
1187 // inline "add(blob, nr)" so that the type can be checked
1188 res = generate_BLOBAPPEND(cctx);
1189 idx = -1;
1190 }
1191 }
1192
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001193 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1194 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001195 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001196 // May have the "D" or "R" flag, reserve a variable for a
1197 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001198 if (get_defer_var_idx(cctx) == 0)
1199 idx = -1;
1200 }
1201
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01001202 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1203 if (STRCMP(name, "string") == 0)
1204 builtin_method = CLASS_BUILTIN_STRING;
1205 else if (STRCMP(name, "empty") == 0)
1206 builtin_method = CLASS_BUILTIN_EMPTY;
1207 else if (STRCMP(name, "len") == 0)
1208 builtin_method = CLASS_BUILTIN_LEN;
1209 if (builtin_method != CLASS_BUILTIN_INVALID)
1210 {
1211 res = compile_builtin_method_call(cctx, builtin_method);
1212 if (res == OK)
1213 idx = -1;
1214 }
1215
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216 if (idx >= 0)
1217 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1218 }
1219 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001220 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001221 goto theend;
1222 }
1223
Bram Moolenaar62aec932022-01-29 21:45:34 +00001224 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1225
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001226 // An argument or local variable can be a function reference, this
1227 // overrules a function name.
1228 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1229 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1230 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001231 class_T *cl = NULL;
1232 int mi = 0;
1233
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234 // If we can find the function by name generate the right call.
1235 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001236 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001237 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001238 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001239 if (!func_is_global(ufunc))
1240 {
h-eastb895b0f2023-09-24 15:46:31 +02001241 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001242 goto theend;
1243 }
1244 if (!has_g_namespace
1245 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1246 {
1247 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001248 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001249 goto theend;
1250 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001252 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001253 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001254 // Class method invocation without the class name.
1255 // A class method can be referenced without the class name only in
1256 // the class where the function is defined.
1257 if (cctx->ctx_ufunc->uf_defclass == cl)
1258 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001259 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001260 0, argcount);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001261 }
1262 else
1263 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001264 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001265 name, cl->class_name);
1266 res = FAIL;
1267 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001268 goto theend;
1269 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 }
1271
1272 // If the name is a variable, load it and use PCALL.
1273 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001274 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001275 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001276 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1278 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001279 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280
Christian Brabandtd5475e82023-08-17 23:34:09 +02001281 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282 goto theend;
1283 }
1284
1285 // If we can find a global function by name generate the right call.
1286 if (ufunc != NULL)
1287 {
h-eastb895b0f2023-09-24 15:46:31 +02001288 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001289 goto theend;
1290 }
1291
1292 // A global function may be defined only later. Need to figure out at
1293 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001294 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001295 res = generate_UCALL(cctx, name, argcount);
1296 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001297 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298
1299theend:
1300 vim_free(tofree);
1301 return res;
1302}
1303
1304// like NAMESPACE_CHAR but with 'a' and 'l'.
1305#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1306
1307/*
1308 * Find the end of a variable or function name. Unlike find_name_end() this
1309 * does not recognize magic braces.
1310 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1311 * Return a pointer to just after the name. Equal to "arg" if there is no
1312 * valid name.
1313 */
1314 char_u *
1315to_name_end(char_u *arg, int use_namespace)
1316{
1317 char_u *p;
1318
1319 // Quick check for valid starting character.
1320 if (!eval_isnamec1(*arg))
1321 return arg;
1322
1323 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1324 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1325 // and can be used in slice "[n:]".
1326 if (*p == ':' && (p != arg + 1
1327 || !use_namespace
1328 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1329 break;
1330 return p;
1331}
1332
1333/*
1334 * Like to_name_end() but also skip over a list or dict constant.
1335 * Also accept "<SNR>123_Func".
1336 * This intentionally does not handle line continuation.
1337 */
1338 char_u *
1339to_name_const_end(char_u *arg)
1340{
1341 char_u *p = arg;
1342 typval_T rettv;
1343
1344 if (STRNCMP(p, "<SNR>", 5) == 0)
1345 p = skipdigits(p + 5);
1346 p = to_name_end(p, TRUE);
1347 if (p == arg && *arg == '[')
1348 {
1349
1350 // Can be "[1, 2, 3]->Func()".
1351 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1352 p = arg;
1353 }
1354 return p;
1355}
1356
1357/*
1358 * parse a list: [expr, expr]
1359 * "*arg" points to the '['.
1360 * ppconst->pp_is_const is set if all items are a constant.
1361 */
1362 static int
1363compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1364{
1365 char_u *p = skipwhite(*arg + 1);
1366 char_u *whitep = *arg + 1;
1367 int count = 0;
1368 int is_const;
1369 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001370 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371
1372 for (;;)
1373 {
1374 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1375 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001376 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 return FAIL;
1378 }
1379 if (*p == ',')
1380 {
1381 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1382 return FAIL;
1383 }
1384 if (*p == ']')
1385 {
1386 ++p;
1387 break;
1388 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001389 if (must_end)
1390 {
1391 semsg(_(e_missing_comma_in_list_str), p);
1392 return FAIL;
1393 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001394 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1395 return FAIL;
1396 if (!is_const)
1397 is_all_const = FALSE;
1398 ++count;
1399 if (*p == ',')
1400 {
1401 ++p;
1402 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1403 {
1404 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1405 return FAIL;
1406 }
1407 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001408 else
1409 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410 whitep = p;
1411 p = skipwhite(p);
1412 }
1413 *arg = p;
1414
1415 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001416 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001417}
1418
1419/*
1420 * Parse a lambda: "(arg, arg) => expr"
1421 * "*arg" points to the '('.
1422 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1423 */
1424 static int
1425compile_lambda(char_u **arg, cctx_T *cctx)
1426{
1427 int r;
1428 typval_T rettv;
1429 ufunc_T *ufunc;
1430 evalarg_T evalarg;
1431
1432 init_evalarg(&evalarg);
1433 evalarg.eval_flags = EVAL_EVALUATE;
1434 evalarg.eval_cctx = cctx;
1435
1436 // Get the funcref in "rettv".
1437 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1438 if (r != OK)
1439 {
1440 clear_evalarg(&evalarg, NULL);
1441 return r;
1442 }
1443
1444 // "rettv" will now be a partial referencing the function.
1445 ufunc = rettv.vval.v_partial->pt_func;
1446 ++ufunc->uf_refcount;
1447 clear_tv(&rettv);
1448
1449 // Compile it here to get the return type. The return type is optional,
1450 // when it's missing use t_unknown. This is recognized in
1451 // compile_return().
1452 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1453 ufunc->uf_ret_type = &t_unknown;
1454 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1455
1456 // When the outer function is compiled for profiling or debugging, the
1457 // lambda may be called without profiling or debugging. Compile it here in
1458 // the right context.
1459 if (cctx->ctx_compile_type == CT_DEBUG
1460#ifdef FEAT_PROFILE
1461 || cctx->ctx_compile_type == CT_PROFILE
1462#endif
1463 )
1464 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1465
Bram Moolenaar139575d2022-03-15 19:29:30 +00001466 // if the outer function is not compiled for debugging or profiling, this
1467 // one might be
1468 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001469 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001470 compiletype_T compile_type = get_compile_type(ufunc);
1471
1472 if (compile_type != CT_NONE)
1473 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001474 }
1475
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1477 // "*arg" may point into it. Point into the original line to avoid a
1478 // dangling pointer.
1479 if (evalarg.eval_using_cmdline)
1480 {
1481 garray_T *gap = &evalarg.eval_tofree_ga;
1482 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1483
1484 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1485 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001486 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487 }
1488
1489 clear_evalarg(&evalarg, NULL);
1490
1491 if (ufunc->uf_def_status == UF_COMPILED)
1492 {
1493 // The return type will now be known.
1494 set_function_type(ufunc);
1495
1496 // The function reference count will be 1. When the ISN_FUNCREF
1497 // instruction is deleted the reference count is decremented and the
1498 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001499 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001500 }
1501
1502 func_ptr_unref(ufunc);
1503 return FAIL;
1504}
1505
1506/*
1507 * Get a lambda and compile it. Uses Vim9 syntax.
1508 */
1509 int
1510get_lambda_tv_and_compile(
1511 char_u **arg,
1512 typval_T *rettv,
1513 int types_optional,
1514 evalarg_T *evalarg)
1515{
1516 int r;
1517 ufunc_T *ufunc;
1518 int save_sc_version = current_sctx.sc_version;
1519
1520 // Get the funcref in "rettv".
1521 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1522 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1523 current_sctx.sc_version = save_sc_version;
1524 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001525 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001526
1527 // "rettv" will now be a partial referencing the function.
1528 ufunc = rettv->vval.v_partial->pt_func;
1529
1530 // Compile it here to get the return type. The return type is optional,
1531 // when it's missing use t_unknown. This is recognized in
1532 // compile_return().
1533 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1534 ufunc->uf_ret_type = &t_unknown;
1535 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1536
1537 if (ufunc->uf_def_status == UF_COMPILED)
1538 {
1539 // The return type will now be known.
1540 set_function_type(ufunc);
1541 return OK;
1542 }
1543 clear_tv(rettv);
1544 return FAIL;
1545}
1546
1547/*
1548 * parse a dict: {key: val, [key]: val}
1549 * "*arg" points to the '{'.
1550 * ppconst->pp_is_const is set if all item values are a constant.
1551 */
1552 static int
1553compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1554{
1555 garray_T *instr = &cctx->ctx_instr;
1556 int count = 0;
1557 dict_T *d = dict_alloc();
1558 dictitem_T *item;
1559 char_u *whitep = *arg + 1;
1560 char_u *p;
1561 int is_const;
1562 int is_all_const = TRUE; // reset when non-const encountered
1563
1564 if (d == NULL)
1565 return FAIL;
1566 if (generate_ppconst(cctx, ppconst) == FAIL)
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001567 {
1568 dict_unref(d);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569 return FAIL;
Christian Brabandt915f3bf2024-04-05 20:12:19 +02001570 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001571 for (;;)
1572 {
1573 char_u *key = NULL;
1574
1575 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1576 {
1577 *arg = NULL;
1578 goto failret;
1579 }
1580
1581 if (**arg == '}')
1582 break;
1583
1584 if (**arg == '[')
1585 {
1586 isn_T *isn;
1587
1588 // {[expr]: value} uses an evaluated key.
1589 *arg = skipwhite(*arg + 1);
1590 if (compile_expr0(arg, cctx) == FAIL)
1591 return FAIL;
1592 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1593 if (isn->isn_type == ISN_PUSHNR)
1594 {
1595 char buf[NUMBUFLEN];
1596
1597 // Convert to string at compile time.
1598 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1599 isn->isn_type = ISN_PUSHS;
1600 isn->isn_arg.string = vim_strsave((char_u *)buf);
1601 }
1602 if (isn->isn_type == ISN_PUSHS)
1603 key = isn->isn_arg.string;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001604 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605 return FAIL;
1606 *arg = skipwhite(*arg);
1607 if (**arg != ']')
1608 {
1609 emsg(_(e_missing_matching_bracket_after_dict_key));
1610 return FAIL;
1611 }
1612 ++*arg;
1613 }
1614 else
1615 {
1616 // {"name": value},
1617 // {'name': value},
1618 // {name: value} use "name" as a literal key
1619 key = get_literal_key(arg);
1620 if (key == NULL)
1621 return FAIL;
1622 if (generate_PUSHS(cctx, &key) == FAIL)
1623 return FAIL;
1624 }
1625
1626 // Check for duplicate keys, if using string keys.
1627 if (key != NULL)
1628 {
1629 item = dict_find(d, key, -1);
1630 if (item != NULL)
1631 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001632 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 goto failret;
1634 }
1635 item = dictitem_alloc(key);
1636 if (item != NULL)
1637 {
1638 item->di_tv.v_type = VAR_UNKNOWN;
1639 item->di_tv.v_lock = 0;
1640 if (dict_add(d, item) == FAIL)
1641 dictitem_free(item);
1642 }
1643 }
1644
1645 if (**arg != ':')
1646 {
1647 if (*skipwhite(*arg) == ':')
1648 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1649 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001650 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651 return FAIL;
1652 }
1653 whitep = *arg + 1;
1654 if (!IS_WHITE_OR_NUL(*whitep))
1655 {
1656 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1657 return FAIL;
1658 }
1659
1660 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1661 {
1662 *arg = NULL;
1663 goto failret;
1664 }
1665
1666 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1667 return FAIL;
1668 if (!is_const)
1669 is_all_const = FALSE;
1670 ++count;
1671
1672 whitep = *arg;
1673 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1674 {
1675 *arg = NULL;
1676 goto failret;
1677 }
1678 if (**arg == '}')
1679 break;
1680 if (**arg != ',')
1681 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001682 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001683 goto failret;
1684 }
1685 if (IS_WHITE_OR_NUL(*whitep))
1686 {
1687 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1688 return FAIL;
1689 }
1690 whitep = *arg + 1;
1691 if (!IS_WHITE_OR_NUL(*whitep))
1692 {
1693 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1694 return FAIL;
1695 }
1696 *arg = skipwhite(whitep);
1697 }
1698
1699 *arg = *arg + 1;
1700
1701 // Allow for following comment, after at least one space.
1702 p = skipwhite(*arg);
1703 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1704 *arg += STRLEN(*arg);
1705
1706 dict_unref(d);
1707 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001708 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001709
1710failret:
1711 if (*arg == NULL)
1712 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001713 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714 *arg = (char_u *)"";
1715 }
1716 dict_unref(d);
1717 return FAIL;
1718}
1719
1720/*
1721 * Compile "&option".
1722 */
1723 static int
1724compile_get_option(char_u **arg, cctx_T *cctx)
1725{
1726 typval_T rettv;
1727 char_u *start = *arg;
1728 int ret;
1729
1730 // parse the option and get the current value to get the type.
1731 rettv.v_type = VAR_UNKNOWN;
1732 ret = eval_option(arg, &rettv, TRUE);
1733 if (ret == OK)
1734 {
1735 // include the '&' in the name, eval_option() expects it.
1736 char_u *name = vim_strnsave(start, *arg - start);
1737 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1738 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1739
1740 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1741 vim_free(name);
1742 }
1743 clear_tv(&rettv);
1744
1745 return ret;
1746}
1747
1748/*
1749 * Compile "$VAR".
1750 */
1751 static int
1752compile_get_env(char_u **arg, cctx_T *cctx)
1753{
1754 char_u *start = *arg;
1755 int len;
1756 int ret;
1757 char_u *name;
1758
1759 ++*arg;
1760 len = get_env_len(arg);
1761 if (len == 0)
1762 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001763 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001764 return FAIL;
1765 }
1766
1767 // include the '$' in the name, eval_env_var() expects it.
1768 name = vim_strnsave(start, len + 1);
1769 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1770 vim_free(name);
1771 return ret;
1772}
1773
1774/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001775 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001776 */
1777 static int
1778compile_interp_string(char_u **arg, cctx_T *cctx)
1779{
1780 typval_T tv;
1781 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001782 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001783 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001784 int count = 0;
1785 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001786
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001787 // *arg is on the '$' character, move it to the first string character.
1788 ++*arg;
1789 quote = **arg;
1790 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001791
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001792 for (;;)
1793 {
1794 // Get the string up to the matching quote or to a single '{'.
1795 // "arg" is advanced to either the quote or the '{'.
1796 if (quote == '"')
1797 ret = eval_string(arg, &tv, evaluate, TRUE);
1798 else
1799 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1800 if (ret == FAIL)
1801 break;
1802 if (evaluate)
1803 {
1804 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1805 || (**arg != '{' && count == 0))
1806 {
1807 // generate non-empty string or empty string if it's the only
1808 // one
1809 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1810 return FAIL;
1811 tv.vval.v_string = NULL; // don't free it now
1812 ++count;
1813 }
1814 clear_tv(&tv);
1815 }
1816
1817 if (**arg != '{')
1818 {
1819 // found terminating quote
1820 ++*arg;
1821 break;
1822 }
1823
1824 p = compile_one_expr_in_str(*arg, cctx);
1825 if (p == NULL)
1826 {
1827 ret = FAIL;
1828 break;
1829 }
1830 ++count;
1831 *arg = p;
1832 }
LemonBoy2eaef102022-05-06 13:14:50 +01001833
1834 if (ret == FAIL || !evaluate)
1835 return ret;
1836
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001837 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1838 if (count > 1)
1839 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001840
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001841 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001842}
1843
1844/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001845 * Compile "@r".
1846 */
1847 static int
1848compile_get_register(char_u **arg, cctx_T *cctx)
1849{
1850 int ret;
1851
1852 ++*arg;
1853 if (**arg == NUL)
1854 {
1855 semsg(_(e_syntax_error_at_str), *arg - 1);
1856 return FAIL;
1857 }
1858 if (!valid_yank_reg(**arg, FALSE))
1859 {
1860 emsg_invreg(**arg);
1861 return FAIL;
1862 }
1863 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1864 ++*arg;
1865 return ret;
1866}
1867
1868/*
1869 * Apply leading '!', '-' and '+' to constant "rettv".
1870 * When "numeric_only" is TRUE do not apply '!'.
1871 */
1872 static int
1873apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1874{
1875 char_u *p = *end;
1876
1877 // this works from end to start
1878 while (p > start)
1879 {
1880 --p;
1881 if (*p == '-' || *p == '+')
1882 {
1883 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884 if (rettv->v_type == VAR_FLOAT)
1885 {
1886 if (*p == '-')
1887 rettv->vval.v_float = -rettv->vval.v_float;
1888 }
1889 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890 {
1891 varnumber_T val;
1892 int error = FALSE;
1893
1894 // tv_get_number_chk() accepts a string, but we don't want that
1895 // here
1896 if (check_not_string(rettv) == FAIL)
1897 return FAIL;
1898 val = tv_get_number_chk(rettv, &error);
1899 clear_tv(rettv);
1900 if (error)
1901 return FAIL;
1902 if (*p == '-')
1903 val = -val;
1904 rettv->v_type = VAR_NUMBER;
1905 rettv->vval.v_number = val;
1906 }
1907 }
1908 else if (numeric_only)
1909 {
1910 ++p;
1911 break;
1912 }
1913 else if (*p == '!')
1914 {
1915 int v = tv2bool(rettv);
1916
1917 // '!' is permissive in the type.
1918 clear_tv(rettv);
1919 rettv->v_type = VAR_BOOL;
1920 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1921 }
1922 }
1923 *end = p;
1924 return OK;
1925}
1926
1927/*
1928 * Recognize v: variables that are constants and set "rettv".
1929 */
1930 static void
1931get_vim_constant(char_u **arg, typval_T *rettv)
1932{
1933 if (STRNCMP(*arg, "v:true", 6) == 0)
1934 {
1935 rettv->v_type = VAR_BOOL;
1936 rettv->vval.v_number = VVAL_TRUE;
1937 *arg += 6;
1938 }
1939 else if (STRNCMP(*arg, "v:false", 7) == 0)
1940 {
1941 rettv->v_type = VAR_BOOL;
1942 rettv->vval.v_number = VVAL_FALSE;
1943 *arg += 7;
1944 }
1945 else if (STRNCMP(*arg, "v:null", 6) == 0)
1946 {
1947 rettv->v_type = VAR_SPECIAL;
1948 rettv->vval.v_number = VVAL_NULL;
1949 *arg += 6;
1950 }
1951 else if (STRNCMP(*arg, "v:none", 6) == 0)
1952 {
1953 rettv->v_type = VAR_SPECIAL;
1954 rettv->vval.v_number = VVAL_NONE;
1955 *arg += 6;
1956 }
1957}
1958
1959 exprtype_T
1960get_compare_type(char_u *p, int *len, int *type_is)
1961{
1962 exprtype_T type = EXPR_UNKNOWN;
1963 int i;
1964
1965 switch (p[0])
1966 {
1967 case '=': if (p[1] == '=')
1968 type = EXPR_EQUAL;
1969 else if (p[1] == '~')
1970 type = EXPR_MATCH;
1971 break;
1972 case '!': if (p[1] == '=')
1973 type = EXPR_NEQUAL;
1974 else if (p[1] == '~')
1975 type = EXPR_NOMATCH;
1976 break;
1977 case '>': if (p[1] != '=')
1978 {
1979 type = EXPR_GREATER;
1980 *len = 1;
1981 }
1982 else
1983 type = EXPR_GEQUAL;
1984 break;
1985 case '<': if (p[1] != '=')
1986 {
1987 type = EXPR_SMALLER;
1988 *len = 1;
1989 }
1990 else
1991 type = EXPR_SEQUAL;
1992 break;
1993 case 'i': if (p[1] == 's')
1994 {
1995 // "is" and "isnot"; but not a prefix of a name
1996 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1997 *len = 5;
1998 i = p[*len];
Keith Thompson184f71c2024-01-04 21:19:04 +01001999 if (!SAFE_isalnum(i) && i != '_')
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002000 {
2001 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2002 *type_is = TRUE;
2003 }
2004 }
2005 break;
2006 }
2007 return type;
2008}
2009
2010/*
2011 * Skip over an expression, ignoring most errors.
2012 */
2013 void
2014skip_expr_cctx(char_u **arg, cctx_T *cctx)
2015{
2016 evalarg_T evalarg;
2017
2018 init_evalarg(&evalarg);
2019 evalarg.eval_cctx = cctx;
2020 skip_expr(arg, &evalarg);
2021 clear_evalarg(&evalarg, NULL);
2022}
2023
2024/*
2025 * Check that the top of the type stack has a type that can be used as a
2026 * condition. Give an error and return FAIL if not.
2027 */
2028 int
2029bool_on_stack(cctx_T *cctx)
2030{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002031 type_T *type;
2032
Bram Moolenaar078a4612022-01-04 15:17:03 +00002033 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034 if (type == &t_bool)
2035 return OK;
2036
Bram Moolenaar4913d422022-10-17 13:13:32 +01002037 if (type->tt_type == VAR_ANY
2038 || type->tt_type == VAR_UNKNOWN
2039 || type->tt_type == VAR_NUMBER
2040 || type == &t_number_bool
2041 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002042 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2043 // This requires a runtime type check.
2044 return generate_COND2BOOL(cctx);
2045
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002046 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047}
2048
2049/*
2050 * Give the "white on both sides" error, taking the operator from "p[len]".
2051 */
2052 void
2053error_white_both(char_u *op, int len)
2054{
2055 char_u buf[10];
2056
2057 vim_strncpy(buf, op, len);
2058 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2059}
2060
2061/*
2062 * Compile code to apply '-', '+' and '!'.
2063 * When "numeric_only" is TRUE do not apply '!'.
2064 */
2065 static int
2066compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2067{
2068 char_u *p = *end;
2069
2070 // this works from end to start
2071 while (p > start)
2072 {
2073 --p;
2074 while (VIM_ISWHITE(*p))
2075 --p;
2076 if (*p == '-' || *p == '+')
2077 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002078 type_T *type = get_type_on_stack(cctx, 0);
2079 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002080 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002081 return FAIL;
2082
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002083 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002084 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2085 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002086 }
2087 else if (numeric_only)
2088 {
2089 ++p;
2090 break;
2091 }
2092 else
2093 {
2094 int invert = *p == '!';
2095
2096 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2097 {
2098 if (p[-1] == '!')
2099 invert = !invert;
2100 --p;
2101 }
2102 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2103 return FAIL;
2104 }
2105 }
2106 *end = p;
2107 return OK;
2108}
2109
2110/*
2111 * Compile "(expression)": recursive!
2112 * Return FAIL/OK.
2113 */
2114 static int
2115compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2116{
2117 int ret;
2118 char_u *p = *arg + 1;
2119
2120 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2121 return FAIL;
2122 if (ppconst->pp_used <= PPSIZE - 10)
2123 {
2124 ret = compile_expr1(arg, cctx, ppconst);
2125 }
2126 else
2127 {
2128 // Not enough space in ppconst, flush constants.
2129 if (generate_ppconst(cctx, ppconst) == FAIL)
2130 return FAIL;
2131 ret = compile_expr0(arg, cctx);
2132 }
2133 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2134 return FAIL;
2135 if (**arg == ')')
2136 ++*arg;
2137 else if (ret == OK)
2138 {
2139 emsg(_(e_missing_closing_paren));
2140 ret = FAIL;
2141 }
2142 return ret;
2143}
2144
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002145static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002146
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002147/*
2148 * Compile whatever comes after "name" or "name()".
2149 * Advances "*arg" only when something was recognized.
2150 */
2151 static int
2152compile_subscript(
2153 char_u **arg,
2154 cctx_T *cctx,
2155 char_u *start_leader,
2156 char_u **end_leader,
2157 ppconst_T *ppconst)
2158{
2159 char_u *name_start = *end_leader;
2160 int keeping_dict = FALSE;
2161
2162 for (;;)
2163 {
2164 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002165 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166
2167 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2168 {
2169 char_u *next = peek_next_line_from_context(cctx);
2170
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002171 // If a following line starts with "->{", "->(" or "->X" advance to
2172 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002173 // Also if a following line starts with ".x".
2174 if (next != NULL &&
2175 ((next[0] == '-' && next[1] == '>'
2176 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002177 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178 || ASCII_ISALPHA(*skipwhite(next + 2))))
2179 || (next[0] == '.' && eval_isdictc(next[1]))))
2180 {
2181 next = next_line_from_context(cctx, TRUE);
2182 if (next == NULL)
2183 return FAIL;
2184 *arg = next;
2185 p = skipwhite(*arg);
2186 }
2187 }
2188
2189 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2190 // is not a function call.
2191 if (**arg == '(')
2192 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002193 int argcount = 0;
2194
2195 if (generate_ppconst(cctx, ppconst) == FAIL)
2196 return FAIL;
2197 ppconst->pp_is_const = FALSE;
2198
2199 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002200 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201
2202 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002203 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002204 return FAIL;
2205 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2206 return FAIL;
2207 if (keeping_dict)
2208 {
2209 keeping_dict = FALSE;
2210 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2211 return FAIL;
2212 }
2213 }
2214 else if (*p == '-' && p[1] == '>')
2215 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002216 char_u *pstart = p;
2217 int alt;
2218 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002219
Bram Moolenaarc7349932022-01-16 20:59:39 +00002220 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002221 if (generate_ppconst(cctx, ppconst) == FAIL)
2222 return FAIL;
2223 ppconst->pp_is_const = FALSE;
2224
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002225 // Apply the '!', '-' and '+' first:
2226 // -1.0->func() works like (-1.0)->func()
2227 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2228 return FAIL;
2229
2230 p += 2;
2231 *arg = skipwhite(p);
2232 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002233
2234 // Three alternatives handled here:
2235 // 1. "base->name(" only a name, use compile_call()
2236 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2237 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002238 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002239 alt = 2;
2240 else
2241 {
2242 // alternative 1 or 3
2243 p = *arg;
2244 if (!eval_isnamec1(*p))
2245 {
2246 semsg(_(e_trailing_characters_str), pstart);
2247 return FAIL;
2248 }
2249 if (ASCII_ISALPHA(*p) && p[1] == ':')
2250 p += 2;
2251 for ( ; eval_isnamec(*p); ++p)
2252 ;
2253 if (*p == '(')
2254 {
2255 // alternative 1
2256 alt = 1;
2257 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2258 return FAIL;
2259 }
2260 else
2261 {
2262 // Must be alternative 3, find the "(". Only works within
2263 // one line.
2264 alt = 3;
2265 paren = vim_strchr(p, '(');
2266 if (paren == NULL)
2267 {
2268 semsg(_(e_missing_parenthesis_str), *arg);
2269 return FAIL;
2270 }
2271 }
2272 }
2273
2274 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002275 {
2276 int argcount = 1;
2277 garray_T *stack = &cctx->ctx_type_stack;
2278 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002279 int expr_isn_start = cctx->ctx_instr.ga_len;
2280 int expr_isn_end;
2281 int arg_isn_count;
2282
Bram Moolenaarc7349932022-01-16 20:59:39 +00002283 if (alt == 2)
2284 {
2285 // Funcref call: list->(Refs[2])(arg)
2286 // or lambda: list->((arg) => expr)(arg)
2287 //
2288 // Fist compile the function expression.
2289 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2290 return FAIL;
2291 }
2292 else
2293 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002294 int fail;
2295 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002296 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002297
Bram Moolenaarc7349932022-01-16 20:59:39 +00002298 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002299
2300 // instead of using LOADG for "import.Func" use PUSHFUNC
2301 ++paren_follows_after_expr;
2302
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002303 // do not look in the next line
2304 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002305
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002306 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002307 || *skipwhite(*arg) != NUL;
2308 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002309 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002310 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002311
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002312 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002313 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002314 if (did_emsg == prev_did_emsg)
2315 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002316 return FAIL;
2317 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002318 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002319
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002320 // Compile the arguments.
2321 if (**arg != '(')
2322 {
2323 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002324 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 else
2326 semsg(_(e_missing_parenthesis_str), *arg);
2327 return FAIL;
2328 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002329
2330 // Remember the next instruction index, where the instructions
2331 // for arguments are being written.
2332 expr_isn_end = cctx->ctx_instr.ga_len;
2333
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002335 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2336 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337 return FAIL;
2338
2339 // Move the instructions for the arguments to before the
2340 // instructions of the expression and move the type of the
2341 // expression after the argument types. This is what ISN_PCALL
2342 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002343 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2344 if (arg_isn_count > 0)
2345 {
2346 int expr_isn_count = expr_isn_end - expr_isn_start;
2347 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002348 type_T *decl_type;
2349 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002350
2351 if (isn == NULL)
2352 return FAIL;
2353 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2354 + expr_isn_start,
2355 sizeof(isn_T) * expr_isn_count);
2356 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2357 + expr_isn_start,
2358 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2359 sizeof(isn_T) * arg_isn_count);
2360 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2361 + expr_isn_start + arg_isn_count,
2362 isn, sizeof(isn_T) * expr_isn_count);
2363 vim_free(isn);
2364
Bram Moolenaar078a4612022-01-04 15:17:03 +00002365 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2366 type = typep->type_curr;
2367 decl_type = typep->type_decl;
2368 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2369 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2370 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002372 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2373 typep->type_curr = type;
2374 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002375 }
2376
Bram Moolenaar078a4612022-01-04 15:17:03 +00002377 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002378 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2379 return FAIL;
2380 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002381
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382 if (keeping_dict)
2383 {
2384 keeping_dict = FALSE;
2385 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2386 return FAIL;
2387 }
2388 }
2389 else if (**arg == '[')
2390 {
2391 int is_slice = FALSE;
2392
2393 // list index: list[123]
2394 // dict member: dict[key]
2395 // string index: text[123]
2396 // blob index: blob[123]
2397 if (generate_ppconst(cctx, ppconst) == FAIL)
2398 return FAIL;
2399 ppconst->pp_is_const = FALSE;
2400
2401 ++p;
2402 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2403 return FAIL;
2404 if (**arg == ':')
2405 {
2406 // missing first index is equal to zero
2407 generate_PUSHNR(cctx, 0);
2408 }
2409 else
2410 {
2411 if (compile_expr0(arg, cctx) == FAIL)
2412 return FAIL;
2413 if (**arg == ':')
2414 {
2415 semsg(_(e_white_space_required_before_and_after_str_at_str),
2416 ":", *arg);
2417 return FAIL;
2418 }
2419 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2420 return FAIL;
2421 *arg = skipwhite(*arg);
2422 }
2423 if (**arg == ':')
2424 {
2425 is_slice = TRUE;
2426 ++*arg;
2427 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2428 {
2429 semsg(_(e_white_space_required_before_and_after_str_at_str),
2430 ":", *arg);
2431 return FAIL;
2432 }
2433 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2434 return FAIL;
2435 if (**arg == ']')
2436 // missing second index is equal to end of string
2437 generate_PUSHNR(cctx, -1);
2438 else
2439 {
2440 if (compile_expr0(arg, cctx) == FAIL)
2441 return FAIL;
2442 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2443 return FAIL;
2444 *arg = skipwhite(*arg);
2445 }
2446 }
2447
2448 if (**arg != ']')
2449 {
2450 emsg(_(e_missing_closing_square_brace));
2451 return FAIL;
2452 }
2453 *arg = *arg + 1;
2454
2455 if (keeping_dict)
2456 {
2457 keeping_dict = FALSE;
2458 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2459 return FAIL;
2460 }
2461 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2462 return FAIL;
2463 }
2464 else if (*p == '.' && p[1] != '.')
2465 {
2466 // dictionary member: dict.name
2467 if (generate_ppconst(cctx, ppconst) == FAIL)
2468 return FAIL;
2469 ppconst->pp_is_const = FALSE;
2470
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002471 type = get_type_on_stack(cctx, 0);
2472 if (type != &t_unknown
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002473 && (type->tt_type == VAR_CLASS
2474 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002475 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002476 // class member: SomeClass.varname
2477 // class method: SomeClass.SomeMethod()
2478 // class constructor: SomeClass.new()
2479 // object member: someObject.varname, this.varname
2480 // object method: someObject.SomeMethod(), this.SomeMethod()
2481 *arg = p;
2482 if (compile_class_object_index(cctx, arg, type) == FAIL)
2483 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002484 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002485 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002486 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002487 *arg = p + 1;
2488 if (IS_WHITE_OR_NUL(**arg))
2489 {
2490 emsg(_(e_missing_name_after_dot));
2491 return FAIL;
2492 }
2493 p = *arg;
2494 if (eval_isdictc(*p))
2495 while (eval_isnamec(*p))
2496 MB_PTR_ADV(p);
2497 if (p == *arg)
2498 {
2499 semsg(_(e_syntax_error_at_str), *arg);
2500 return FAIL;
2501 }
2502 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2503 return FAIL;
2504 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2505 return FAIL;
2506 keeping_dict = TRUE;
2507 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002508 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002509 }
2510 else
2511 break;
2512 }
2513
2514 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2515 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002516 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2517 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002518 return FAIL;
2519
2520 return OK;
2521}
2522
2523/*
2524 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2525 * "arg" is advanced until after the expression, skipping white space.
2526 *
2527 * If the value is a constant "ppconst->pp_used" will be non-zero.
2528 * Before instructions are generated, any values in "ppconst" will generated.
2529 *
2530 * This is the compiling equivalent of eval1(), eval2(), etc.
2531 */
2532
2533/*
2534 * number number constant
2535 * 0zFFFFFFFF Blob constant
2536 * "string" string constant
2537 * 'string' literal string constant
2538 * &option-name option value
2539 * @r register contents
2540 * identifier variable value
2541 * function() function call
2542 * $VAR environment variable
2543 * (expression) nested expression
2544 * [expr, expr] List
2545 * {key: val, [key]: val} Dictionary
2546 *
2547 * Also handle:
2548 * ! in front logical NOT
2549 * - in front unary minus
2550 * + in front unary plus (ignored)
2551 * trailing (arg) funcref/partial call
2552 * trailing [] subscript in String or List
2553 * trailing .name entry in Dictionary
2554 * trailing ->name() method call
2555 */
2556 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002557compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002558 char_u **arg,
2559 cctx_T *cctx,
2560 ppconst_T *ppconst)
2561{
2562 char_u *start_leader, *end_leader;
2563 int ret = OK;
2564 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2565 int used_before = ppconst->pp_used;
2566
2567 ppconst->pp_is_const = FALSE;
2568
2569 /*
2570 * Skip '!', '-' and '+' characters. They are handled later.
2571 */
2572 start_leader = *arg;
2573 if (eval_leader(arg, TRUE) == FAIL)
2574 return FAIL;
2575 end_leader = *arg;
2576
2577 rettv->v_type = VAR_UNKNOWN;
2578 switch (**arg)
2579 {
2580 /*
2581 * Number constant.
2582 */
2583 case '0': // also for blob starting with 0z
2584 case '1':
2585 case '2':
2586 case '3':
2587 case '4':
2588 case '5':
2589 case '6':
2590 case '7':
2591 case '8':
2592 case '9':
2593 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2594 return FAIL;
2595 // Apply "-" and "+" just before the number now, right to
2596 // left. Matters especially when "->" follows. Stops at
2597 // '!'.
2598 if (apply_leader(rettv, TRUE,
2599 start_leader, &end_leader) == FAIL)
2600 {
2601 clear_tv(rettv);
2602 return FAIL;
2603 }
2604 break;
2605
2606 /*
2607 * String constant: "string".
2608 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002609 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002610 return FAIL;
2611 break;
2612
2613 /*
2614 * Literal string constant: 'str''ing'.
2615 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002616 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002617 return FAIL;
2618 break;
2619
2620 /*
2621 * Constant Vim variable.
2622 */
2623 case 'v': get_vim_constant(arg, rettv);
2624 ret = NOTDONE;
2625 break;
2626
2627 /*
2628 * "true" constant
2629 */
2630 case 't': if (STRNCMP(*arg, "true", 4) == 0
2631 && !eval_isnamec((*arg)[4]))
2632 {
2633 *arg += 4;
2634 rettv->v_type = VAR_BOOL;
2635 rettv->vval.v_number = VVAL_TRUE;
2636 }
2637 else
2638 ret = NOTDONE;
2639 break;
2640
2641 /*
2642 * "false" constant
2643 */
2644 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2645 && !eval_isnamec((*arg)[5]))
2646 {
2647 *arg += 5;
2648 rettv->v_type = VAR_BOOL;
2649 rettv->vval.v_number = VVAL_FALSE;
2650 }
2651 else
2652 ret = NOTDONE;
2653 break;
2654
2655 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002656 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002657 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002658 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002660 char_u *p = *arg + 4;
2661 int len;
2662
2663 for (len = 0; eval_isnamec(p[len]); ++len)
2664 ;
2665 ret = handle_predefined(*arg, len + 4, rettv);
2666 if (ret == FAIL)
2667 ret = NOTDONE;
2668 else
2669 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002670 }
2671 else
2672 ret = NOTDONE;
2673 break;
2674
2675 /*
2676 * List: [expr, expr]
2677 */
2678 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2679 return FAIL;
2680 ret = compile_list(arg, cctx, ppconst);
2681 break;
2682
2683 /*
2684 * Dictionary: {'key': val, 'key': val}
2685 */
2686 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2687 return FAIL;
2688 ret = compile_dict(arg, cctx, ppconst);
2689 break;
2690
2691 /*
2692 * Option value: &name
2693 */
2694 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2695 return FAIL;
2696 ret = compile_get_option(arg, cctx);
2697 break;
2698
2699 /*
2700 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002701 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002702 */
2703 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2704 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002705 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2706 ret = compile_interp_string(arg, cctx);
2707 else
2708 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002709 break;
2710
2711 /*
2712 * Register contents: @r.
2713 */
2714 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2715 return FAIL;
2716 ret = compile_get_register(arg, cctx);
2717 break;
2718 /*
2719 * nested expression: (expression).
2720 * lambda: (arg, arg) => expr
2721 * funcref: (arg, arg) => { statement }
2722 */
2723 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2724 ret = compile_lambda(arg, cctx);
2725 if (ret == NOTDONE)
2726 ret = compile_parenthesis(arg, cctx, ppconst);
2727 break;
2728
2729 default: ret = NOTDONE;
2730 break;
2731 }
2732 if (ret == FAIL)
2733 return FAIL;
2734
2735 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2736 {
2737 if (cctx->ctx_skip == SKIP_YES)
2738 clear_tv(rettv);
2739 else
2740 // A constant expression can possibly be handled compile time,
2741 // return the value instead of generating code.
2742 ++ppconst->pp_used;
2743 }
2744 else if (ret == NOTDONE)
2745 {
2746 char_u *p;
2747 int r;
2748
2749 if (!eval_isnamec1(**arg))
2750 {
2751 if (!vim9_bad_comment(*arg))
2752 {
2753 if (ends_excmd(*skipwhite(*arg)))
2754 semsg(_(e_empty_expression_str), *arg);
2755 else
2756 semsg(_(e_name_expected_str), *arg);
2757 }
2758 return FAIL;
2759 }
2760
2761 // "name" or "name()"
2762 p = to_name_end(*arg, TRUE);
2763 if (p - *arg == (size_t)1 && **arg == '_')
2764 {
2765 emsg(_(e_cannot_use_underscore_here));
2766 return FAIL;
2767 }
2768
2769 if (*p == '(')
2770 {
2771 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2772 }
2773 else
2774 {
2775 if (cctx->ctx_skip != SKIP_YES
2776 && generate_ppconst(cctx, ppconst) == FAIL)
2777 return FAIL;
2778 r = compile_load(arg, p, cctx, TRUE, TRUE);
2779 }
2780 if (r == FAIL)
2781 return FAIL;
2782 }
2783
2784 // Handle following "[]", ".member", etc.
2785 // Then deal with prefixed '-', '+' and '!', if not done already.
2786 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2787 ppconst) == FAIL)
2788 return FAIL;
Yegappan Lakshmanan5d773642024-03-22 19:37:29 +01002789 if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002790 {
2791 // apply the '!', '-' and '+' before the constant
2792 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2793 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2794 return FAIL;
2795 return OK;
2796 }
2797 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2798 return FAIL;
2799 return OK;
2800}
2801
2802/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002803 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002804 */
2805 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002806compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002807{
2808 type_T *want_type = NULL;
2809
2810 // Recognize <type>
2811 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2812 {
2813 ++*arg;
2814 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2815 if (want_type == NULL)
2816 return FAIL;
2817
2818 if (**arg != '>')
2819 {
2820 if (*skipwhite(*arg) == '>')
2821 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2822 else
2823 emsg(_(e_missing_gt));
2824 return FAIL;
2825 }
2826 ++*arg;
2827 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2828 return FAIL;
2829 }
2830
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002831 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002832 return FAIL;
2833
2834 if (want_type != NULL)
2835 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002836 type_T *actual;
2837 where_T where = WHERE_INIT;
2838
LemonBoy50d48542024-07-04 17:03:17 +02002839 where.wt_kind = WT_CAST;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002840 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002841 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002842 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002843 {
LemonBoy50d48542024-07-04 17:03:17 +02002844 if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE)
2845 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002846 return FAIL;
2847 }
2848 }
2849
2850 return OK;
2851}
2852
2853/*
2854 * * number multiplication
2855 * / number division
2856 * % number modulo
2857 */
2858 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002859compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002860{
2861 char_u *op;
2862 char_u *next;
2863 int ppconst_used = ppconst->pp_used;
2864
2865 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002866 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002867 return FAIL;
2868
2869 /*
2870 * Repeat computing, until no "*", "/" or "%" is following.
2871 */
2872 for (;;)
2873 {
2874 op = may_peek_next_line(cctx, *arg, &next);
2875 if (*op != '*' && *op != '/' && *op != '%')
2876 break;
2877 if (next != NULL)
2878 {
2879 *arg = next_line_from_context(cctx, TRUE);
2880 op = skipwhite(*arg);
2881 }
2882
2883 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2884 {
2885 error_white_both(op, 1);
2886 return FAIL;
2887 }
2888 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2889 return FAIL;
2890
2891 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002892 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002893 return FAIL;
2894
2895 if (ppconst->pp_used == ppconst_used + 2
2896 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2897 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2898 {
2899 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2900 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2901 varnumber_T res = 0;
2902 int failed = FALSE;
2903
2904 // both are numbers: compute the result
2905 switch (*op)
2906 {
2907 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2908 break;
2909 case '/': res = num_divide(tv1->vval.v_number,
2910 tv2->vval.v_number, &failed);
2911 break;
2912 case '%': res = num_modulus(tv1->vval.v_number,
2913 tv2->vval.v_number, &failed);
2914 break;
2915 }
2916 if (failed)
2917 return FAIL;
2918 tv1->vval.v_number = res;
2919 --ppconst->pp_used;
2920 }
2921 else
2922 {
2923 generate_ppconst(cctx, ppconst);
2924 generate_two_op(cctx, op);
2925 }
2926 }
2927
2928 return OK;
2929}
2930
2931/*
2932 * + number addition or list/blobl concatenation
2933 * - number subtraction
2934 * .. string concatenation
2935 */
2936 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002937compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002938{
2939 char_u *op;
2940 char_u *next;
2941 int oplen;
2942 int ppconst_used = ppconst->pp_used;
2943
2944 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002945 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002946 return FAIL;
2947
2948 /*
2949 * Repeat computing, until no "+", "-" or ".." is following.
2950 */
2951 for (;;)
2952 {
2953 op = may_peek_next_line(cctx, *arg, &next);
2954 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2955 break;
2956 if (op[0] == op[1] && *op != '.' && next)
2957 // Finding "++" or "--" on the next line is a separate command.
2958 // But ".." is concatenation.
2959 break;
2960 oplen = (*op == '.' ? 2 : 1);
2961 if (next != NULL)
2962 {
2963 *arg = next_line_from_context(cctx, TRUE);
2964 op = skipwhite(*arg);
2965 }
2966
2967 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2968 {
2969 error_white_both(op, oplen);
2970 return FAIL;
2971 }
2972
2973 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2974 return FAIL;
2975
2976 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002977 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002978 return FAIL;
2979
2980 if (ppconst->pp_used == ppconst_used + 2
2981 && (*op == '.'
2982 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2983 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2984 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2985 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2986 {
2987 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2988 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2989
2990 // concat/subtract/add constant numbers
2991 if (*op == '+')
2992 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2993 else if (*op == '-')
2994 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2995 else
2996 {
2997 // concatenate constant strings
2998 char_u *s1 = tv1->vval.v_string;
2999 char_u *s2 = tv2->vval.v_string;
3000 size_t len1 = STRLEN(s1);
3001
3002 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3003 if (tv1->vval.v_string == NULL)
3004 {
3005 clear_ppconst(ppconst);
3006 return FAIL;
3007 }
3008 mch_memmove(tv1->vval.v_string, s1, len1);
3009 STRCPY(tv1->vval.v_string + len1, s2);
3010 vim_free(s1);
3011 vim_free(s2);
3012 }
3013 --ppconst->pp_used;
3014 }
3015 else
3016 {
3017 generate_ppconst(cctx, ppconst);
3018 ppconst->pp_is_const = FALSE;
3019 if (*op == '.')
3020 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02003021 if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL
3022 || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003023 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01003024 if (generate_CONCAT(cctx, 2) == FAIL)
3025 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003026 }
3027 else
3028 generate_two_op(cctx, op);
3029 }
3030 }
3031
3032 return OK;
3033}
3034
3035/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003036 * expr6a >> expr6b
3037 * expr6a << expr6b
3038 *
3039 * Produces instructions:
3040 * OPNR bitwise left or right shift
3041 */
3042 static int
3043compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3044{
3045 exprtype_T type = EXPR_UNKNOWN;
3046 char_u *p;
3047 char_u *next;
3048 int len = 2;
3049 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003050 isn_T *isn;
3051
3052 // get the first variable
3053 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3054 return FAIL;
3055
3056 /*
3057 * Repeat computing, until no "+", "-" or ".." is following.
3058 */
3059 for (;;)
3060 {
3061 type = EXPR_UNKNOWN;
3062
3063 p = may_peek_next_line(cctx, *arg, &next);
3064 if (p[0] == '<' && p[1] == '<')
3065 type = EXPR_LSHIFT;
3066 else if (p[0] == '>' && p[1] == '>')
3067 type = EXPR_RSHIFT;
3068
3069 if (type == EXPR_UNKNOWN)
3070 return OK;
3071
3072 // Handle a bitwise left or right shift operator
3073 if (ppconst->pp_used == ppconst_used + 1)
3074 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003075 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003076 {
3077 // left operand should be a number
3078 emsg(_(e_bitshift_ops_must_be_number));
3079 return FAIL;
3080 }
3081 }
3082 else
3083 {
3084 type_T *t = get_type_on_stack(cctx, 0);
3085
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003086 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003087 {
3088 emsg(_(e_bitshift_ops_must_be_number));
3089 return FAIL;
3090 }
3091 }
3092
3093 if (next != NULL)
3094 {
3095 *arg = next_line_from_context(cctx, TRUE);
3096 p = skipwhite(*arg);
3097 }
3098
3099 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3100 {
3101 error_white_both(p, len);
3102 return FAIL;
3103 }
3104
3105 // get the second variable
3106 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3107 return FAIL;
3108
3109 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3110 return FAIL;
3111
3112 if (ppconst->pp_used == ppconst_used + 2)
3113 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003114 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3115 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3116
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003117 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003118 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3119 {
3120 // right operand should be a positive number
3121 if (tv2->v_type != VAR_NUMBER)
3122 emsg(_(e_bitshift_ops_must_be_number));
3123 else
dundargocc57b5bc2022-11-02 13:30:51 +00003124 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003125 return FAIL;
3126 }
3127
3128 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3129 tv1->vval.v_number = 0;
3130 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003131 tv1->vval.v_number =
3132 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003133 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003134 tv1->vval.v_number =
3135 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003136 clear_tv(tv2);
3137 --ppconst->pp_used;
3138 }
3139 else
3140 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003141 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3142 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003143 {
3144 emsg(_(e_bitshift_ops_must_be_number));
3145 return FAIL;
3146 }
3147
3148 generate_ppconst(cctx, ppconst);
3149
3150 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3151 if (isn == NULL)
3152 return FAIL;
3153
3154 if (isn != NULL)
3155 isn->isn_arg.op.op_type = type;
3156 }
3157 }
3158
3159 return OK;
3160}
3161
3162/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003163 * expr5a == expr5b
3164 * expr5a =~ expr5b
3165 * expr5a != expr5b
3166 * expr5a !~ expr5b
3167 * expr5a > expr5b
3168 * expr5a >= expr5b
3169 * expr5a < expr5b
3170 * expr5a <= expr5b
3171 * expr5a is expr5b
3172 * expr5a isnot expr5b
3173 *
3174 * Produces instructions:
3175 * EVAL expr5a Push result of "expr5a"
3176 * EVAL expr5b Push result of "expr5b"
3177 * COMPARE one of the compare instructions
3178 */
3179 static int
3180compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3181{
3182 exprtype_T type = EXPR_UNKNOWN;
3183 char_u *p;
3184 char_u *next;
3185 int len = 2;
3186 int type_is = FALSE;
3187 int ppconst_used = ppconst->pp_used;
3188
3189 // get the first variable
3190 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3191 return FAIL;
3192
3193 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003194
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003195 type = get_compare_type(p, &len, &type_is);
3196
3197 /*
3198 * If there is a comparative operator, use it.
3199 */
3200 if (type != EXPR_UNKNOWN)
3201 {
3202 int ic = FALSE; // Default: do not ignore case
3203
3204 if (next != NULL)
3205 {
3206 *arg = next_line_from_context(cctx, TRUE);
3207 p = skipwhite(*arg);
3208 }
3209 if (type_is && (p[len] == '?' || p[len] == '#'))
3210 {
3211 semsg(_(e_invalid_expression_str), *arg);
3212 return FAIL;
3213 }
3214 // extra question mark appended: ignore case
3215 if (p[len] == '?')
3216 {
3217 ic = TRUE;
3218 ++len;
3219 }
3220 // extra '#' appended: match case (ignored)
3221 else if (p[len] == '#')
3222 ++len;
3223 // nothing appended: match case
3224
3225 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3226 {
3227 error_white_both(p, len);
3228 return FAIL;
3229 }
3230
3231 // get the second variable
3232 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3233 return FAIL;
3234
3235 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3236 return FAIL;
3237
3238 if (ppconst->pp_used == ppconst_used + 2)
3239 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003240 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003241 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3242 int ret;
3243
3244 // Both sides are a constant, compute the result now.
3245 // First check for a valid combination of types, this is more
3246 // strict than typval_compare().
3247 if (check_compare_types(type, tv1, tv2) == FAIL)
3248 ret = FAIL;
3249 else
3250 {
3251 ret = typval_compare(tv1, tv2, type, ic);
3252 tv1->v_type = VAR_BOOL;
3253 tv1->vval.v_number = tv1->vval.v_number
3254 ? VVAL_TRUE : VVAL_FALSE;
3255 clear_tv(tv2);
3256 --ppconst->pp_used;
3257 }
3258 return ret;
3259 }
3260
3261 generate_ppconst(cctx, ppconst);
3262 return generate_COMPARE(cctx, type, ic);
3263 }
3264
3265 return OK;
3266}
3267
3268static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3269
3270/*
3271 * Compile || or &&.
3272 */
3273 static int
3274compile_and_or(
3275 char_u **arg,
3276 cctx_T *cctx,
3277 char *op,
3278 ppconst_T *ppconst,
3279 int ppconst_used UNUSED)
3280{
3281 char_u *next;
3282 char_u *p = may_peek_next_line(cctx, *arg, &next);
3283 int opchar = *op;
3284
3285 if (p[0] == opchar && p[1] == opchar)
3286 {
3287 garray_T *instr = &cctx->ctx_instr;
3288 garray_T end_ga;
3289 int save_skip = cctx->ctx_skip;
3290
3291 /*
3292 * Repeat until there is no following "||" or "&&"
3293 */
3294 ga_init2(&end_ga, sizeof(int), 10);
3295 while (p[0] == opchar && p[1] == opchar)
3296 {
3297 long start_lnum = SOURCING_LNUM;
3298 long save_sourcing_lnum;
3299 int start_ctx_lnum = cctx->ctx_lnum;
3300 int save_lnum;
3301 int const_used;
3302 int status;
3303 jumpwhen_T jump_when = opchar == '|'
3304 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3305
3306 if (next != NULL)
3307 {
3308 *arg = next_line_from_context(cctx, TRUE);
3309 p = skipwhite(*arg);
3310 }
3311
3312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3313 {
3314 semsg(_(e_white_space_required_before_and_after_str_at_str),
3315 op, p);
3316 ga_clear(&end_ga);
3317 return FAIL;
3318 }
3319
3320 save_sourcing_lnum = SOURCING_LNUM;
3321 SOURCING_LNUM = start_lnum;
3322 save_lnum = cctx->ctx_lnum;
3323 cctx->ctx_lnum = start_ctx_lnum;
3324
3325 status = check_ppconst_bool(ppconst);
3326 if (status != FAIL)
3327 {
3328 // Use the last ppconst if possible.
3329 if (ppconst->pp_used > 0)
3330 {
3331 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3332 int is_true = tv2bool(tv);
3333
3334 if ((is_true && opchar == '|')
3335 || (!is_true && opchar == '&'))
3336 {
3337 // For "false && expr" and "true || expr" the "expr"
3338 // does not need to be evaluated.
3339 cctx->ctx_skip = SKIP_YES;
3340 clear_tv(tv);
3341 tv->v_type = VAR_BOOL;
3342 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3343 }
3344 else
3345 {
3346 // For "true && expr" and "false || expr" only "expr"
3347 // needs to be evaluated.
3348 --ppconst->pp_used;
3349 jump_when = JUMP_NEVER;
3350 }
3351 }
3352 else
3353 {
3354 // Every part must evaluate to a bool.
3355 status = bool_on_stack(cctx);
3356 }
3357 }
3358 if (status != FAIL)
3359 status = ga_grow(&end_ga, 1);
3360 cctx->ctx_lnum = save_lnum;
3361 if (status == FAIL)
3362 {
3363 ga_clear(&end_ga);
3364 return FAIL;
3365 }
3366
3367 if (jump_when != JUMP_NEVER)
3368 {
3369 if (cctx->ctx_skip != SKIP_YES)
3370 {
3371 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3372 ++end_ga.ga_len;
3373 }
3374 generate_JUMP(cctx, jump_when, 0);
3375 }
3376
3377 // eval the next expression
3378 SOURCING_LNUM = save_sourcing_lnum;
3379 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3380 {
3381 ga_clear(&end_ga);
3382 return FAIL;
3383 }
3384
3385 const_used = ppconst->pp_used;
3386 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3387 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3388 {
3389 ga_clear(&end_ga);
3390 return FAIL;
3391 }
3392
3393 // "0 || 1" results in true, "1 && 0" results in false.
3394 if (ppconst->pp_used == const_used + 1)
3395 {
3396 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3397
3398 if (tv->v_type == VAR_NUMBER
3399 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3400 {
3401 tv->vval.v_number = tv->vval.v_number == 1
3402 ? VVAL_TRUE : VVAL_FALSE;
3403 tv->v_type = VAR_BOOL;
3404 }
3405 }
3406
3407 p = may_peek_next_line(cctx, *arg, &next);
3408 }
3409
3410 if (check_ppconst_bool(ppconst) == FAIL)
3411 {
3412 ga_clear(&end_ga);
3413 return FAIL;
3414 }
3415
3416 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3417 // Every part must evaluate to a bool.
3418 if (bool_on_stack(cctx) == FAIL)
3419 {
3420 ga_clear(&end_ga);
3421 return FAIL;
3422 }
3423
3424 if (end_ga.ga_len > 0)
3425 {
3426 // Fill in the end label in all jumps.
3427 generate_ppconst(cctx, ppconst);
3428 while (end_ga.ga_len > 0)
3429 {
3430 isn_T *isn;
3431
3432 --end_ga.ga_len;
3433 isn = ((isn_T *)instr->ga_data)
3434 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3435 isn->isn_arg.jump.jump_where = instr->ga_len;
3436 }
3437 }
3438 ga_clear(&end_ga);
3439
3440 cctx->ctx_skip = save_skip;
3441 }
3442
3443 return OK;
3444}
3445
3446/*
3447 * expr4a && expr4a && expr4a logical AND
3448 *
3449 * Produces instructions:
3450 * EVAL expr4a Push result of "expr4a"
3451 * COND2BOOL convert to bool if needed
3452 * JUMP_IF_COND_FALSE end
3453 * EVAL expr4b Push result of "expr4b"
3454 * JUMP_IF_COND_FALSE end
3455 * EVAL expr4c Push result of "expr4c"
3456 * end:
3457 */
3458 static int
3459compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3460{
3461 int ppconst_used = ppconst->pp_used;
3462
3463 // get the first variable
3464 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3465 return FAIL;
3466
3467 // || and && work almost the same
3468 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3469}
3470
3471/*
3472 * expr3a || expr3b || expr3c logical OR
3473 *
3474 * Produces instructions:
3475 * EVAL expr3a Push result of "expr3a"
3476 * COND2BOOL convert to bool if needed
3477 * JUMP_IF_COND_TRUE end
3478 * EVAL expr3b Push result of "expr3b"
3479 * JUMP_IF_COND_TRUE end
3480 * EVAL expr3c Push result of "expr3c"
3481 * end:
3482 */
3483 static int
3484compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3485{
3486 int ppconst_used = ppconst->pp_used;
3487
3488 // eval the first expression
3489 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3490 return FAIL;
3491
3492 // || and && work almost the same
3493 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3494}
3495
3496/*
3497 * Toplevel expression: expr2 ? expr1a : expr1b
3498 * Produces instructions:
3499 * EVAL expr2 Push result of "expr2"
3500 * JUMP_IF_FALSE alt jump if false
3501 * EVAL expr1a
3502 * JUMP_ALWAYS end
3503 * alt: EVAL expr1b
3504 * end:
3505 *
3506 * Toplevel expression: expr2 ?? expr1
3507 * Produces instructions:
3508 * EVAL expr2 Push result of "expr2"
3509 * JUMP_AND_KEEP_IF_TRUE end jump if true
3510 * EVAL expr1
3511 * end:
3512 */
3513 int
3514compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3515{
3516 char_u *p;
3517 int ppconst_used = ppconst->pp_used;
3518 char_u *next;
3519
3520 // Ignore all kinds of errors when not producing code.
3521 if (cctx->ctx_skip == SKIP_YES)
3522 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003523 int prev_did_emsg = did_emsg;
3524
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003525 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003526 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003527 }
3528
3529 // Evaluate the first expression.
3530 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3531 return FAIL;
3532
3533 p = may_peek_next_line(cctx, *arg, &next);
3534 if (*p == '?')
3535 {
3536 int op_falsy = p[1] == '?';
3537 garray_T *instr = &cctx->ctx_instr;
3538 garray_T *stack = &cctx->ctx_type_stack;
3539 int alt_idx = instr->ga_len;
3540 int end_idx = 0;
3541 isn_T *isn;
3542 type_T *type1 = NULL;
3543 int has_const_expr = FALSE;
3544 int const_value = FALSE;
3545 int save_skip = cctx->ctx_skip;
3546
3547 if (next != NULL)
3548 {
3549 *arg = next_line_from_context(cctx, TRUE);
3550 p = skipwhite(*arg);
3551 }
3552
3553 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3554 {
3555 semsg(_(e_white_space_required_before_and_after_str_at_str),
3556 op_falsy ? "??" : "?", p);
3557 return FAIL;
3558 }
3559
3560 if (ppconst->pp_used == ppconst_used + 1)
3561 {
3562 // the condition is a constant, we know whether the ? or the :
3563 // expression is to be evaluated.
3564 has_const_expr = TRUE;
3565 if (op_falsy)
3566 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3567 else
3568 {
3569 int error = FALSE;
3570
3571 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3572 &error);
3573 if (error)
3574 return FAIL;
3575 }
3576 cctx->ctx_skip = save_skip == SKIP_YES ||
3577 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3578
3579 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3580 // "left ?? right" and "left" is truthy: produce "left"
3581 generate_ppconst(cctx, ppconst);
3582 else
3583 {
3584 clear_tv(&ppconst->pp_tv[ppconst_used]);
3585 --ppconst->pp_used;
3586 }
3587 }
3588 else
3589 {
3590 generate_ppconst(cctx, ppconst);
3591 if (op_falsy)
3592 end_idx = instr->ga_len;
3593 generate_JUMP(cctx, op_falsy
3594 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3595 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003596 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003597 }
3598
3599 // evaluate the second expression; any type is accepted
3600 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3601 return FAIL;
3602 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3603 return FAIL;
3604
3605 if (!has_const_expr)
3606 {
3607 generate_ppconst(cctx, ppconst);
3608
3609 if (!op_falsy)
3610 {
3611 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003612 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003613 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003614
3615 end_idx = instr->ga_len;
3616 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3617
3618 // jump here from JUMP_IF_FALSE
3619 isn = ((isn_T *)instr->ga_data) + alt_idx;
3620 isn->isn_arg.jump.jump_where = instr->ga_len;
3621 }
3622 }
3623
3624 if (!op_falsy)
3625 {
3626 // Check for the ":".
3627 p = may_peek_next_line(cctx, *arg, &next);
3628 if (*p != ':')
3629 {
3630 emsg(_(e_missing_colon_after_questionmark));
3631 return FAIL;
3632 }
3633 if (next != NULL)
3634 {
3635 *arg = next_line_from_context(cctx, TRUE);
3636 p = skipwhite(*arg);
3637 }
3638
3639 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3640 {
3641 semsg(_(e_white_space_required_before_and_after_str_at_str),
3642 ":", p);
3643 return FAIL;
3644 }
3645
3646 // evaluate the third expression
3647 if (has_const_expr)
3648 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3649 ? SKIP_YES : SKIP_NOT;
3650 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3651 return FAIL;
3652 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3653 return FAIL;
3654 }
3655
3656 if (!has_const_expr)
3657 {
3658 type_T **typep;
3659
3660 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003661 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003662
3663 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003664 typep = &((((type2_T *)stack->ga_data)
3665 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003666 common_type(type1, *typep, typep, cctx->ctx_type_list);
3667
3668 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3669 isn = ((isn_T *)instr->ga_data) + end_idx;
3670 isn->isn_arg.jump.jump_where = instr->ga_len;
3671 }
3672
3673 cctx->ctx_skip = save_skip;
3674 }
3675 return OK;
3676}
3677
3678/*
3679 * Toplevel expression.
3680 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3681 * Returns OK or FAIL.
3682 */
3683 int
3684compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3685{
3686 ppconst_T ppconst;
3687
3688 CLEAR_FIELD(ppconst);
3689 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3690 {
3691 clear_ppconst(&ppconst);
3692 return FAIL;
3693 }
3694 if (is_const != NULL)
3695 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3696 if (generate_ppconst(cctx, &ppconst) == FAIL)
3697 return FAIL;
3698 return OK;
3699}
3700
3701/*
3702 * Toplevel expression.
3703 */
3704 int
3705compile_expr0(char_u **arg, cctx_T *cctx)
3706{
3707 return compile_expr0_ext(arg, cctx, NULL);
3708}
3709
3710
3711#endif // defined(FEAT_EVAL)