blob: 31fa824a7bcda79eb52c7ce6a836c9df2d50c923 [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 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100145 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
146 || 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 {
Ernie Rael4d00b832023-09-11 19:54:42 +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 Lakshmanan29bb67f2023-10-14 11:18:50 +0200449 // Private methods are not accessible outside the class
450 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];
491 // Private methods are not accessible outside the class
492 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
549 // Need to lookup the member.
550 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000551 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000552 semsg(_(e_expected_dot_after_name_str), start);
553 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000554 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000555 ++p;
556 if (VIM_ISWHITE(*p))
557 {
558 emsg(_(e_no_white_space_allowed_after_dot));
559 return FAIL;
560 }
561
562 // isolate one name
563 exp_name = p;
564 while (eval_isnamec(*p))
565 ++p;
566 cc = *p;
567 *p = NUL;
568
Bram Moolenaard041f422022-01-12 19:54:00 +0000569 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100570 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
571 // "import autoload './dir/script.vim'" or
572 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100573 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100574
575 if (res == OK)
576 {
577 if (si->sn_autoload_prefix != NULL
578 && si->sn_state == SN_STATE_NOT_LOADED)
579 {
580 char_u *auto_name =
581 concat_str(si->sn_autoload_prefix, exp_name);
582
583 // autoload script must be loaded later, access by the autoload
584 // name. If a '(' follows it must be a function. Otherwise we
585 // don't know, it can be "script.Func".
586 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100587 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100588 else
589 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
590 vim_free(auto_name);
591 done = TRUE;
592 }
593 else if (si->sn_import_autoload
594 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100595 {
596 // If a '(' follows it must be a function. Otherwise we don't
597 // know, it can be "script.Func".
598 if (cc == '(' || paren_follows_after_expr)
599 {
600 char_u sid_name[MAX_FUNC_NAME_LEN];
601
602 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100603 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100604 }
605 else
606 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
607 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100608 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100609 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100610 else
611 {
612 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
613 cctx, NULL, TRUE);
614 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100615 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100616
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000617 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000618 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000619 if (done)
620 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000621
622 if (idx < 0)
623 {
624 if (ufunc != NULL)
625 {
626 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100627 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000628 return OK;
629 }
630 return FAIL;
631 }
632
633 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
634 import->imp_sid,
635 idx,
636 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000637 return OK;
638 }
639
Bram Moolenaard0132f42022-05-12 11:05:40 +0100640 // Can only get here if we know "name" is a script variable and not in a
641 // Vim9 script (variable is not in sn_var_vals): old style script.
642 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000643 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644}
645
646 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000647generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000649 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000650 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000651
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000652 // Reject a global non-autoload function found without the "g:" prefix.
653 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000654 return FAIL;
655
656 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000657 compile_type = get_compile_type(ufunc);
658 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000659 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000660 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100661 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662}
663
664/*
665 * Compile a variable name into a load instruction.
666 * "end" points to just after the name.
667 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
668 * When "error" is FALSE do not give an error when not found.
669 */
670 int
671compile_load(
672 char_u **arg,
673 char_u *end_arg,
674 cctx_T *cctx,
675 int is_expr,
676 int error)
677{
678 type_T *type;
679 char_u *name = NULL;
680 char_u *end = end_arg;
681 int res = FAIL;
682 int prev_called_emsg = called_emsg;
683
684 if (*(*arg + 1) == ':')
685 {
686 if (end <= *arg + 2)
687 {
688 isntype_T isn_type;
689
690 // load dictionary of namespace
691 switch (**arg)
692 {
693 case 'g': isn_type = ISN_LOADGDICT; break;
694 case 'w': isn_type = ISN_LOADWDICT; break;
695 case 't': isn_type = ISN_LOADTDICT; break;
696 case 'b': isn_type = ISN_LOADBDICT; break;
697 default:
698 semsg(_(e_namespace_not_supported_str), *arg);
699 goto theend;
700 }
701 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
702 goto theend;
703 res = OK;
704 }
705 else
706 {
707 isntype_T isn_type = ISN_DROP;
708
709 // load namespaced variable
710 name = vim_strnsave(*arg + 2, end - (*arg + 2));
711 if (name == NULL)
712 return FAIL;
713
714 switch (**arg)
715 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100716 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000718 case 's': if (current_script_is_vim9())
719 {
720 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
721 *arg);
722 vim_free(name);
723 return FAIL;
724 }
Kota Kato948a3892022-08-16 16:09:59 +0100725 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000726 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 else
728 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100729 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 break;
731 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
732 {
733 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000734 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000735 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000736 else
737 isn_type = ISN_LOADG;
738 }
739 else
740 {
741 isn_type = ISN_LOADAUTO;
742 vim_free(name);
743 name = vim_strnsave(*arg, end - *arg);
744 if (name == NULL)
745 return FAIL;
746 }
747 break;
748 case 'w': isn_type = ISN_LOADW; break;
749 case 't': isn_type = ISN_LOADT; break;
750 case 'b': isn_type = ISN_LOADB; break;
751 default: // cannot happen, just in case
752 semsg(_(e_namespace_not_supported_str), *arg);
753 goto theend;
754 }
755 if (isn_type != ISN_DROP)
756 {
757 // Global, Buffer-local, Window-local and Tabpage-local
758 // variables can be defined later, thus we don't check if it
759 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000760 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761 }
762 }
763 }
764 else
765 {
766 size_t len = end - *arg;
767 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200768 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 int gen_load = FALSE;
770 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100771 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100772 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773
774 name = vim_strnsave(*arg, end - *arg);
775 if (name == NULL)
776 return FAIL;
777
Bram Moolenaar58b40092023-01-11 15:59:05 +0000778 if (STRCMP(name, "super") == 0
779 && cctx->ctx_ufunc != NULL
780 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
781 {
782 // super.SomeFunc() in a class function: push &t_super type, this
783 // is recognized in compile_subscript().
784 res = push_type_stack(cctx, &t_super);
785 if (*end != '.')
786 emsg(_(e_super_must_be_followed_by_dot));
787 }
788 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 {
790 script_autoload(name, FALSE);
791 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
792 }
793 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
794 == OK)
795 {
796 if (gen_load_outer == 0)
797 gen_load = TRUE;
798 }
799 else
800 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000801 lvar_T lvar;
802 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803
804 if (lookup_local(*arg, len, &lvar, cctx) == OK)
805 {
806 type = lvar.lv_type;
807 idx = lvar.lv_idx;
808 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100809 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000810 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100811 outer_loop_depth = lvar.lv_loop_depth;
812 outer_loop_idx = lvar.lv_loop_idx;
813 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 else
815 gen_load = TRUE;
816 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200817 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
818 (((idx =
819 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
820 || ((method_idx =
821 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000822 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200823 // Referencing a class variable or method without the class
824 // name. A class variable or method can be referenced without
825 // the class name only in the class where the function is
826 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200827 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200828 {
829 if (idx >= 0)
830 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
831 else
832 {
833 ufunc_T *fp = cl->class_class_functions[method_idx];
834 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
835 NULL);
836 }
837 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200838 else
839 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200840 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200841 name, cl->class_name);
842 res = FAIL;
843 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000844 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845 else
846 {
847 // "var" can be script-local even without using "s:" if it
848 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000849 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000850 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100851 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852
853 // When evaluating an expression and the name starts with an
854 // uppercase letter it can be a user defined function.
855 // generate_funcref() will fail if the function can't be found.
856 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000857 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000858 }
859 }
860 if (gen_load)
861 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
862 if (gen_load_outer > 0)
863 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100864 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
865 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 cctx->ctx_outer_used = TRUE;
867 }
868 }
869
870 *arg = end;
871
872theend:
873 if (res == FAIL && error && called_emsg == prev_called_emsg)
874 semsg(_(e_variable_not_found_str), name);
875 vim_free(name);
876 return res;
877}
878
879/*
880 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100881 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000882 * Returns FAIL if compilation fails.
883 */
884 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100885compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886{
LemonBoyf3b48952022-05-05 13:53:03 +0100887 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 garray_T save_ga = cctx->ctx_instr;
889 int expr_res;
890 int trailing_error;
891 int instr_count;
892 isn_T *instr = NULL;
893
894 // Remove the string type from the stack.
895 --cctx->ctx_type_stack.ga_len;
896
897 // Temporarily reset the list of instructions so that the jump labels are
898 // correct.
899 cctx->ctx_instr.ga_len = 0;
900 cctx->ctx_instr.ga_maxlen = 0;
901 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000902
903 // avoid peeking a next line
904 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
905 cctx->ctx_ufunc->uf_lines.ga_len = 0;
906
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000907 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000908
909 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
910
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000911 s = skipwhite(s);
912 trailing_error = *s != NUL;
913
914 if (expr_res == FAIL || trailing_error
915 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
916 {
917 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000918 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000919 clear_instr_ga(&cctx->ctx_instr);
920 cctx->ctx_instr = save_ga;
921 ++cctx->ctx_type_stack.ga_len;
922 return FAIL;
923 }
924
925 // Move the generated instructions into the ISN_INSTR instruction, then
926 // restore the list of instructions.
927 instr_count = cctx->ctx_instr.ga_len;
928 instr = cctx->ctx_instr.ga_data;
929 instr[instr_count].isn_type = ISN_FINISH;
930
931 cctx->ctx_instr = save_ga;
932 vim_free(isn->isn_arg.string);
933 isn->isn_type = ISN_INSTR;
934 isn->isn_arg.instr = instr;
935 return OK;
936}
937
938/*
939 * Compile the argument expressions.
940 * "arg" points to just after the "(" and is advanced to after the ")"
941 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100942 int
LemonBoyf3b48952022-05-05 13:53:03 +0100943compile_arguments(
944 char_u **arg,
945 cctx_T *cctx,
946 int *argcount,
947 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000948{
949 char_u *p = *arg;
950 char_u *whitep = *arg;
951 int must_end = FALSE;
952 int instr_count;
953
954 for (;;)
955 {
956 if (may_get_next_line(whitep, &p, cctx) == FAIL)
957 goto failret;
958 if (*p == ')')
959 {
960 *arg = p + 1;
961 return OK;
962 }
963 if (must_end)
964 {
965 semsg(_(e_missing_comma_before_argument_str), p);
966 return FAIL;
967 }
968
969 instr_count = cctx->ctx_instr.ga_len;
970 if (compile_expr0(&p, cctx) == FAIL)
971 return FAIL;
972 ++*argcount;
973
LemonBoyf3b48952022-05-05 13:53:03 +0100974 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000975 && cctx->ctx_instr.ga_len == instr_count + 1)
976 {
977 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
978
979 // {skip} argument of searchpair() can be compiled if not empty
980 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100981 compile_string(isn, cctx, 0);
982 }
983 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
984 && cctx->ctx_instr.ga_len == instr_count + 1)
985 {
986 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
987
988 // {sub} argument of substitute() can be compiled if it starts
989 // with \=
990 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100991 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100992 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 }
994
995 if (*p != ',' && *skipwhite(p) == ',')
996 {
997 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
998 p = skipwhite(p);
999 }
1000 if (*p == ',')
1001 {
1002 ++p;
1003 if (*p != NUL && !VIM_ISWHITE(*p))
1004 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1005 }
1006 else
1007 must_end = TRUE;
1008 whitep = p;
1009 p = skipwhite(p);
1010 }
1011failret:
1012 emsg(_(e_missing_closing_paren));
1013 return FAIL;
1014}
1015
1016/*
1017 * Compile a function call: name(arg1, arg2)
1018 * "arg" points to "name", "arg + varlen" to the "(".
1019 * "argcount_init" is 1 for "value->method()"
1020 * Instructions:
1021 * EVAL arg1
1022 * EVAL arg2
1023 * BCALL / DCALL / UCALL
1024 */
1025 static int
1026compile_call(
1027 char_u **arg,
1028 size_t varlen,
1029 cctx_T *cctx,
1030 ppconst_T *ppconst,
1031 int argcount_init)
1032{
1033 char_u *name = *arg;
1034 char_u *p;
1035 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001036 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001037 char_u fname_buf[FLEN_FIXED + 1];
1038 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039 ufunc_T *ufunc = NULL;
1040 int res = FAIL;
1041 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001042 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001043 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001044 imported_T *import;
1045
1046 if (varlen >= sizeof(namebuf))
1047 {
1048 semsg(_(e_name_too_long_str), name);
1049 return FAIL;
1050 }
1051 vim_strncpy(namebuf, *arg, varlen);
1052
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001053 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001054 if (import != NULL)
1055 {
1056 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1057 return FAIL;
1058 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059
1060 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001061 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001063 if ((varlen == 3
1064 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001065 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1066 {
1067 char_u *s = skipwhite(*arg + varlen + 1);
1068 typval_T argvars[2];
1069 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001070 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071
1072 argvars[0].v_type = VAR_UNKNOWN;
1073 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001074 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001075 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001076 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001077 s = skipwhite(s);
1078 if (*s == ')' && argvars[0].v_type == VAR_STRING
1079 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1080 || !is_has))
1081 {
1082 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1083
1084 *arg = s + 1;
1085 argvars[1].v_type = VAR_UNKNOWN;
1086 tv->v_type = VAR_NUMBER;
1087 tv->vval.v_number = 0;
1088 if (is_has)
1089 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001090 else if (is_len)
1091 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092 else
1093 f_exists(argvars, tv);
1094 clear_tv(&argvars[0]);
1095 ++ppconst->pp_used;
1096 return OK;
1097 }
1098 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001099 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100 {
1101 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1102 return FAIL;
1103 }
1104 }
1105
1106 if (generate_ppconst(cctx, ppconst) == FAIL)
1107 return FAIL;
1108
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001109 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001110 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1111
1112 // We handle the "skip" argument of searchpair() and searchpairpos()
1113 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001114 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1115 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1116 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1117 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1118 special_fn = CA_SEARCHPAIR;
1119 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1120 special_fn = CA_SUBSTITUTE;
1121 else
1122 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001123
1124 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001125 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126 goto theend;
1127
1128 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1129 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1130 {
1131 int idx;
1132
1133 // builtin function
1134 idx = find_internal_func(name);
1135 if (idx >= 0)
1136 {
1137 if (STRCMP(name, "flatten") == 0)
1138 {
1139 emsg(_(e_cannot_use_flatten_in_vim9_script));
1140 goto theend;
1141 }
1142
1143 if (STRCMP(name, "add") == 0 && argcount == 2)
1144 {
h-eastb895b0f2023-09-24 15:46:31 +02001145 type_T *type = get_decl_type_on_stack(cctx, 1);
Ernie Raeld8bf87c2023-12-16 14:03:33 +01001146 if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL)
1147 goto theend;
h-eastb895b0f2023-09-24 15:46:31 +02001148
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149 // add() can be compiled to instructions if we know the type
1150 if (type->tt_type == VAR_LIST)
1151 {
1152 // inline "add(list, item)" so that the type can be checked
1153 res = generate_LISTAPPEND(cctx);
1154 idx = -1;
1155 }
1156 else if (type->tt_type == VAR_BLOB)
1157 {
1158 // inline "add(blob, nr)" so that the type can be checked
1159 res = generate_BLOBAPPEND(cctx);
1160 idx = -1;
1161 }
1162 }
1163
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001164 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1165 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001166 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001167 // May have the "D" or "R" flag, reserve a variable for a
1168 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001169 if (get_defer_var_idx(cctx) == 0)
1170 idx = -1;
1171 }
1172
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173 if (idx >= 0)
1174 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1175 }
1176 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001177 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 goto theend;
1179 }
1180
Bram Moolenaar62aec932022-01-29 21:45:34 +00001181 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1182
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183 // An argument or local variable can be a function reference, this
1184 // overrules a function name.
1185 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1186 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1187 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001188 class_T *cl = NULL;
1189 int mi = 0;
1190
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001191 // If we can find the function by name generate the right call.
1192 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001193 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001194 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001196 if (!func_is_global(ufunc))
1197 {
h-eastb895b0f2023-09-24 15:46:31 +02001198 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001199 goto theend;
1200 }
1201 if (!has_g_namespace
1202 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1203 {
1204 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001205 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001206 goto theend;
1207 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001208 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001209 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001210 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001211 // Class method invocation without the class name.
1212 // A class method can be referenced without the class name only in
1213 // the class where the function is defined.
1214 if (cctx->ctx_ufunc->uf_defclass == cl)
1215 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001216 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001217 0, argcount);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001218 }
1219 else
1220 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001221 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001222 name, cl->class_name);
1223 res = FAIL;
1224 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001225 goto theend;
1226 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001227 }
1228
1229 // If the name is a variable, load it and use PCALL.
1230 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001231 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001232 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001233 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1235 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001236 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001237
Christian Brabandtd5475e82023-08-17 23:34:09 +02001238 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001239 goto theend;
1240 }
1241
1242 // If we can find a global function by name generate the right call.
1243 if (ufunc != NULL)
1244 {
h-eastb895b0f2023-09-24 15:46:31 +02001245 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 goto theend;
1247 }
1248
1249 // A global function may be defined only later. Need to figure out at
1250 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001251 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001252 res = generate_UCALL(cctx, name, argcount);
1253 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001254 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255
1256theend:
1257 vim_free(tofree);
1258 return res;
1259}
1260
1261// like NAMESPACE_CHAR but with 'a' and 'l'.
1262#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1263
1264/*
1265 * Find the end of a variable or function name. Unlike find_name_end() this
1266 * does not recognize magic braces.
1267 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1268 * Return a pointer to just after the name. Equal to "arg" if there is no
1269 * valid name.
1270 */
1271 char_u *
1272to_name_end(char_u *arg, int use_namespace)
1273{
1274 char_u *p;
1275
1276 // Quick check for valid starting character.
1277 if (!eval_isnamec1(*arg))
1278 return arg;
1279
1280 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1281 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1282 // and can be used in slice "[n:]".
1283 if (*p == ':' && (p != arg + 1
1284 || !use_namespace
1285 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1286 break;
1287 return p;
1288}
1289
1290/*
1291 * Like to_name_end() but also skip over a list or dict constant.
1292 * Also accept "<SNR>123_Func".
1293 * This intentionally does not handle line continuation.
1294 */
1295 char_u *
1296to_name_const_end(char_u *arg)
1297{
1298 char_u *p = arg;
1299 typval_T rettv;
1300
1301 if (STRNCMP(p, "<SNR>", 5) == 0)
1302 p = skipdigits(p + 5);
1303 p = to_name_end(p, TRUE);
1304 if (p == arg && *arg == '[')
1305 {
1306
1307 // Can be "[1, 2, 3]->Func()".
1308 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1309 p = arg;
1310 }
1311 return p;
1312}
1313
1314/*
1315 * parse a list: [expr, expr]
1316 * "*arg" points to the '['.
1317 * ppconst->pp_is_const is set if all items are a constant.
1318 */
1319 static int
1320compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1321{
1322 char_u *p = skipwhite(*arg + 1);
1323 char_u *whitep = *arg + 1;
1324 int count = 0;
1325 int is_const;
1326 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001327 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328
1329 for (;;)
1330 {
1331 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1332 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001333 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334 return FAIL;
1335 }
1336 if (*p == ',')
1337 {
1338 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1339 return FAIL;
1340 }
1341 if (*p == ']')
1342 {
1343 ++p;
1344 break;
1345 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001346 if (must_end)
1347 {
1348 semsg(_(e_missing_comma_in_list_str), p);
1349 return FAIL;
1350 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1352 return FAIL;
1353 if (!is_const)
1354 is_all_const = FALSE;
1355 ++count;
1356 if (*p == ',')
1357 {
1358 ++p;
1359 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1360 {
1361 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1362 return FAIL;
1363 }
1364 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001365 else
1366 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367 whitep = p;
1368 p = skipwhite(p);
1369 }
1370 *arg = p;
1371
1372 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001373 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374}
1375
1376/*
1377 * Parse a lambda: "(arg, arg) => expr"
1378 * "*arg" points to the '('.
1379 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1380 */
1381 static int
1382compile_lambda(char_u **arg, cctx_T *cctx)
1383{
1384 int r;
1385 typval_T rettv;
1386 ufunc_T *ufunc;
1387 evalarg_T evalarg;
1388
1389 init_evalarg(&evalarg);
1390 evalarg.eval_flags = EVAL_EVALUATE;
1391 evalarg.eval_cctx = cctx;
1392
1393 // Get the funcref in "rettv".
1394 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1395 if (r != OK)
1396 {
1397 clear_evalarg(&evalarg, NULL);
1398 return r;
1399 }
1400
1401 // "rettv" will now be a partial referencing the function.
1402 ufunc = rettv.vval.v_partial->pt_func;
1403 ++ufunc->uf_refcount;
1404 clear_tv(&rettv);
1405
1406 // Compile it here to get the return type. The return type is optional,
1407 // when it's missing use t_unknown. This is recognized in
1408 // compile_return().
1409 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1410 ufunc->uf_ret_type = &t_unknown;
1411 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1412
1413 // When the outer function is compiled for profiling or debugging, the
1414 // lambda may be called without profiling or debugging. Compile it here in
1415 // the right context.
1416 if (cctx->ctx_compile_type == CT_DEBUG
1417#ifdef FEAT_PROFILE
1418 || cctx->ctx_compile_type == CT_PROFILE
1419#endif
1420 )
1421 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1422
Bram Moolenaar139575d2022-03-15 19:29:30 +00001423 // if the outer function is not compiled for debugging or profiling, this
1424 // one might be
1425 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001426 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001427 compiletype_T compile_type = get_compile_type(ufunc);
1428
1429 if (compile_type != CT_NONE)
1430 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001431 }
1432
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1434 // "*arg" may point into it. Point into the original line to avoid a
1435 // dangling pointer.
1436 if (evalarg.eval_using_cmdline)
1437 {
1438 garray_T *gap = &evalarg.eval_tofree_ga;
1439 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1440
1441 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1442 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001443 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001444 }
1445
1446 clear_evalarg(&evalarg, NULL);
1447
1448 if (ufunc->uf_def_status == UF_COMPILED)
1449 {
1450 // The return type will now be known.
1451 set_function_type(ufunc);
1452
1453 // The function reference count will be 1. When the ISN_FUNCREF
1454 // instruction is deleted the reference count is decremented and the
1455 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001456 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001457 }
1458
1459 func_ptr_unref(ufunc);
1460 return FAIL;
1461}
1462
1463/*
1464 * Get a lambda and compile it. Uses Vim9 syntax.
1465 */
1466 int
1467get_lambda_tv_and_compile(
1468 char_u **arg,
1469 typval_T *rettv,
1470 int types_optional,
1471 evalarg_T *evalarg)
1472{
1473 int r;
1474 ufunc_T *ufunc;
1475 int save_sc_version = current_sctx.sc_version;
1476
1477 // Get the funcref in "rettv".
1478 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1479 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1480 current_sctx.sc_version = save_sc_version;
1481 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001482 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483
1484 // "rettv" will now be a partial referencing the function.
1485 ufunc = rettv->vval.v_partial->pt_func;
1486
1487 // Compile it here to get the return type. The return type is optional,
1488 // when it's missing use t_unknown. This is recognized in
1489 // compile_return().
1490 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1491 ufunc->uf_ret_type = &t_unknown;
1492 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1493
1494 if (ufunc->uf_def_status == UF_COMPILED)
1495 {
1496 // The return type will now be known.
1497 set_function_type(ufunc);
1498 return OK;
1499 }
1500 clear_tv(rettv);
1501 return FAIL;
1502}
1503
1504/*
1505 * parse a dict: {key: val, [key]: val}
1506 * "*arg" points to the '{'.
1507 * ppconst->pp_is_const is set if all item values are a constant.
1508 */
1509 static int
1510compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1511{
1512 garray_T *instr = &cctx->ctx_instr;
1513 int count = 0;
1514 dict_T *d = dict_alloc();
1515 dictitem_T *item;
1516 char_u *whitep = *arg + 1;
1517 char_u *p;
1518 int is_const;
1519 int is_all_const = TRUE; // reset when non-const encountered
1520
1521 if (d == NULL)
1522 return FAIL;
1523 if (generate_ppconst(cctx, ppconst) == FAIL)
1524 return FAIL;
1525 for (;;)
1526 {
1527 char_u *key = NULL;
1528
1529 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1530 {
1531 *arg = NULL;
1532 goto failret;
1533 }
1534
1535 if (**arg == '}')
1536 break;
1537
1538 if (**arg == '[')
1539 {
1540 isn_T *isn;
1541
1542 // {[expr]: value} uses an evaluated key.
1543 *arg = skipwhite(*arg + 1);
1544 if (compile_expr0(arg, cctx) == FAIL)
1545 return FAIL;
1546 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1547 if (isn->isn_type == ISN_PUSHNR)
1548 {
1549 char buf[NUMBUFLEN];
1550
1551 // Convert to string at compile time.
1552 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1553 isn->isn_type = ISN_PUSHS;
1554 isn->isn_arg.string = vim_strsave((char_u *)buf);
1555 }
1556 if (isn->isn_type == ISN_PUSHS)
1557 key = isn->isn_arg.string;
1558 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1559 return FAIL;
1560 *arg = skipwhite(*arg);
1561 if (**arg != ']')
1562 {
1563 emsg(_(e_missing_matching_bracket_after_dict_key));
1564 return FAIL;
1565 }
1566 ++*arg;
1567 }
1568 else
1569 {
1570 // {"name": value},
1571 // {'name': value},
1572 // {name: value} use "name" as a literal key
1573 key = get_literal_key(arg);
1574 if (key == NULL)
1575 return FAIL;
1576 if (generate_PUSHS(cctx, &key) == FAIL)
1577 return FAIL;
1578 }
1579
1580 // Check for duplicate keys, if using string keys.
1581 if (key != NULL)
1582 {
1583 item = dict_find(d, key, -1);
1584 if (item != NULL)
1585 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001586 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001587 goto failret;
1588 }
1589 item = dictitem_alloc(key);
1590 if (item != NULL)
1591 {
1592 item->di_tv.v_type = VAR_UNKNOWN;
1593 item->di_tv.v_lock = 0;
1594 if (dict_add(d, item) == FAIL)
1595 dictitem_free(item);
1596 }
1597 }
1598
1599 if (**arg != ':')
1600 {
1601 if (*skipwhite(*arg) == ':')
1602 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1603 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001604 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605 return FAIL;
1606 }
1607 whitep = *arg + 1;
1608 if (!IS_WHITE_OR_NUL(*whitep))
1609 {
1610 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1611 return FAIL;
1612 }
1613
1614 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1615 {
1616 *arg = NULL;
1617 goto failret;
1618 }
1619
1620 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1621 return FAIL;
1622 if (!is_const)
1623 is_all_const = FALSE;
1624 ++count;
1625
1626 whitep = *arg;
1627 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1628 {
1629 *arg = NULL;
1630 goto failret;
1631 }
1632 if (**arg == '}')
1633 break;
1634 if (**arg != ',')
1635 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001636 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001637 goto failret;
1638 }
1639 if (IS_WHITE_OR_NUL(*whitep))
1640 {
1641 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1642 return FAIL;
1643 }
1644 whitep = *arg + 1;
1645 if (!IS_WHITE_OR_NUL(*whitep))
1646 {
1647 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1648 return FAIL;
1649 }
1650 *arg = skipwhite(whitep);
1651 }
1652
1653 *arg = *arg + 1;
1654
1655 // Allow for following comment, after at least one space.
1656 p = skipwhite(*arg);
1657 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1658 *arg += STRLEN(*arg);
1659
1660 dict_unref(d);
1661 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001662 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001663
1664failret:
1665 if (*arg == NULL)
1666 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001667 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001668 *arg = (char_u *)"";
1669 }
1670 dict_unref(d);
1671 return FAIL;
1672}
1673
1674/*
1675 * Compile "&option".
1676 */
1677 static int
1678compile_get_option(char_u **arg, cctx_T *cctx)
1679{
1680 typval_T rettv;
1681 char_u *start = *arg;
1682 int ret;
1683
1684 // parse the option and get the current value to get the type.
1685 rettv.v_type = VAR_UNKNOWN;
1686 ret = eval_option(arg, &rettv, TRUE);
1687 if (ret == OK)
1688 {
1689 // include the '&' in the name, eval_option() expects it.
1690 char_u *name = vim_strnsave(start, *arg - start);
1691 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1692 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1693
1694 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1695 vim_free(name);
1696 }
1697 clear_tv(&rettv);
1698
1699 return ret;
1700}
1701
1702/*
1703 * Compile "$VAR".
1704 */
1705 static int
1706compile_get_env(char_u **arg, cctx_T *cctx)
1707{
1708 char_u *start = *arg;
1709 int len;
1710 int ret;
1711 char_u *name;
1712
1713 ++*arg;
1714 len = get_env_len(arg);
1715 if (len == 0)
1716 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001717 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001718 return FAIL;
1719 }
1720
1721 // include the '$' in the name, eval_env_var() expects it.
1722 name = vim_strnsave(start, len + 1);
1723 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1724 vim_free(name);
1725 return ret;
1726}
1727
1728/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001729 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001730 */
1731 static int
1732compile_interp_string(char_u **arg, cctx_T *cctx)
1733{
1734 typval_T tv;
1735 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001736 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001737 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001738 int count = 0;
1739 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001740
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001741 // *arg is on the '$' character, move it to the first string character.
1742 ++*arg;
1743 quote = **arg;
1744 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001745
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001746 for (;;)
1747 {
1748 // Get the string up to the matching quote or to a single '{'.
1749 // "arg" is advanced to either the quote or the '{'.
1750 if (quote == '"')
1751 ret = eval_string(arg, &tv, evaluate, TRUE);
1752 else
1753 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1754 if (ret == FAIL)
1755 break;
1756 if (evaluate)
1757 {
1758 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1759 || (**arg != '{' && count == 0))
1760 {
1761 // generate non-empty string or empty string if it's the only
1762 // one
1763 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1764 return FAIL;
1765 tv.vval.v_string = NULL; // don't free it now
1766 ++count;
1767 }
1768 clear_tv(&tv);
1769 }
1770
1771 if (**arg != '{')
1772 {
1773 // found terminating quote
1774 ++*arg;
1775 break;
1776 }
1777
1778 p = compile_one_expr_in_str(*arg, cctx);
1779 if (p == NULL)
1780 {
1781 ret = FAIL;
1782 break;
1783 }
1784 ++count;
1785 *arg = p;
1786 }
LemonBoy2eaef102022-05-06 13:14:50 +01001787
1788 if (ret == FAIL || !evaluate)
1789 return ret;
1790
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001791 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1792 if (count > 1)
1793 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001794
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001795 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001796}
1797
1798/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799 * Compile "@r".
1800 */
1801 static int
1802compile_get_register(char_u **arg, cctx_T *cctx)
1803{
1804 int ret;
1805
1806 ++*arg;
1807 if (**arg == NUL)
1808 {
1809 semsg(_(e_syntax_error_at_str), *arg - 1);
1810 return FAIL;
1811 }
1812 if (!valid_yank_reg(**arg, FALSE))
1813 {
1814 emsg_invreg(**arg);
1815 return FAIL;
1816 }
1817 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1818 ++*arg;
1819 return ret;
1820}
1821
1822/*
1823 * Apply leading '!', '-' and '+' to constant "rettv".
1824 * When "numeric_only" is TRUE do not apply '!'.
1825 */
1826 static int
1827apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1828{
1829 char_u *p = *end;
1830
1831 // this works from end to start
1832 while (p > start)
1833 {
1834 --p;
1835 if (*p == '-' || *p == '+')
1836 {
1837 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838 if (rettv->v_type == VAR_FLOAT)
1839 {
1840 if (*p == '-')
1841 rettv->vval.v_float = -rettv->vval.v_float;
1842 }
1843 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001844 {
1845 varnumber_T val;
1846 int error = FALSE;
1847
1848 // tv_get_number_chk() accepts a string, but we don't want that
1849 // here
1850 if (check_not_string(rettv) == FAIL)
1851 return FAIL;
1852 val = tv_get_number_chk(rettv, &error);
1853 clear_tv(rettv);
1854 if (error)
1855 return FAIL;
1856 if (*p == '-')
1857 val = -val;
1858 rettv->v_type = VAR_NUMBER;
1859 rettv->vval.v_number = val;
1860 }
1861 }
1862 else if (numeric_only)
1863 {
1864 ++p;
1865 break;
1866 }
1867 else if (*p == '!')
1868 {
1869 int v = tv2bool(rettv);
1870
1871 // '!' is permissive in the type.
1872 clear_tv(rettv);
1873 rettv->v_type = VAR_BOOL;
1874 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1875 }
1876 }
1877 *end = p;
1878 return OK;
1879}
1880
1881/*
1882 * Recognize v: variables that are constants and set "rettv".
1883 */
1884 static void
1885get_vim_constant(char_u **arg, typval_T *rettv)
1886{
1887 if (STRNCMP(*arg, "v:true", 6) == 0)
1888 {
1889 rettv->v_type = VAR_BOOL;
1890 rettv->vval.v_number = VVAL_TRUE;
1891 *arg += 6;
1892 }
1893 else if (STRNCMP(*arg, "v:false", 7) == 0)
1894 {
1895 rettv->v_type = VAR_BOOL;
1896 rettv->vval.v_number = VVAL_FALSE;
1897 *arg += 7;
1898 }
1899 else if (STRNCMP(*arg, "v:null", 6) == 0)
1900 {
1901 rettv->v_type = VAR_SPECIAL;
1902 rettv->vval.v_number = VVAL_NULL;
1903 *arg += 6;
1904 }
1905 else if (STRNCMP(*arg, "v:none", 6) == 0)
1906 {
1907 rettv->v_type = VAR_SPECIAL;
1908 rettv->vval.v_number = VVAL_NONE;
1909 *arg += 6;
1910 }
1911}
1912
1913 exprtype_T
1914get_compare_type(char_u *p, int *len, int *type_is)
1915{
1916 exprtype_T type = EXPR_UNKNOWN;
1917 int i;
1918
1919 switch (p[0])
1920 {
1921 case '=': if (p[1] == '=')
1922 type = EXPR_EQUAL;
1923 else if (p[1] == '~')
1924 type = EXPR_MATCH;
1925 break;
1926 case '!': if (p[1] == '=')
1927 type = EXPR_NEQUAL;
1928 else if (p[1] == '~')
1929 type = EXPR_NOMATCH;
1930 break;
1931 case '>': if (p[1] != '=')
1932 {
1933 type = EXPR_GREATER;
1934 *len = 1;
1935 }
1936 else
1937 type = EXPR_GEQUAL;
1938 break;
1939 case '<': if (p[1] != '=')
1940 {
1941 type = EXPR_SMALLER;
1942 *len = 1;
1943 }
1944 else
1945 type = EXPR_SEQUAL;
1946 break;
1947 case 'i': if (p[1] == 's')
1948 {
1949 // "is" and "isnot"; but not a prefix of a name
1950 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1951 *len = 5;
1952 i = p[*len];
1953 if (!isalnum(i) && i != '_')
1954 {
1955 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1956 *type_is = TRUE;
1957 }
1958 }
1959 break;
1960 }
1961 return type;
1962}
1963
1964/*
1965 * Skip over an expression, ignoring most errors.
1966 */
1967 void
1968skip_expr_cctx(char_u **arg, cctx_T *cctx)
1969{
1970 evalarg_T evalarg;
1971
1972 init_evalarg(&evalarg);
1973 evalarg.eval_cctx = cctx;
1974 skip_expr(arg, &evalarg);
1975 clear_evalarg(&evalarg, NULL);
1976}
1977
1978/*
1979 * Check that the top of the type stack has a type that can be used as a
1980 * condition. Give an error and return FAIL if not.
1981 */
1982 int
1983bool_on_stack(cctx_T *cctx)
1984{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001985 type_T *type;
1986
Bram Moolenaar078a4612022-01-04 15:17:03 +00001987 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 if (type == &t_bool)
1989 return OK;
1990
Bram Moolenaar4913d422022-10-17 13:13:32 +01001991 if (type->tt_type == VAR_ANY
1992 || type->tt_type == VAR_UNKNOWN
1993 || type->tt_type == VAR_NUMBER
1994 || type == &t_number_bool
1995 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001996 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1997 // This requires a runtime type check.
1998 return generate_COND2BOOL(cctx);
1999
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002000 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002001}
2002
2003/*
2004 * Give the "white on both sides" error, taking the operator from "p[len]".
2005 */
2006 void
2007error_white_both(char_u *op, int len)
2008{
2009 char_u buf[10];
2010
2011 vim_strncpy(buf, op, len);
2012 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2013}
2014
2015/*
2016 * Compile code to apply '-', '+' and '!'.
2017 * When "numeric_only" is TRUE do not apply '!'.
2018 */
2019 static int
2020compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2021{
2022 char_u *p = *end;
2023
2024 // this works from end to start
2025 while (p > start)
2026 {
2027 --p;
2028 while (VIM_ISWHITE(*p))
2029 --p;
2030 if (*p == '-' || *p == '+')
2031 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002032 type_T *type = get_type_on_stack(cctx, 0);
2033 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002034 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002035 return FAIL;
2036
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002037 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002038 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2039 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 }
2041 else if (numeric_only)
2042 {
2043 ++p;
2044 break;
2045 }
2046 else
2047 {
2048 int invert = *p == '!';
2049
2050 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2051 {
2052 if (p[-1] == '!')
2053 invert = !invert;
2054 --p;
2055 }
2056 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2057 return FAIL;
2058 }
2059 }
2060 *end = p;
2061 return OK;
2062}
2063
2064/*
2065 * Compile "(expression)": recursive!
2066 * Return FAIL/OK.
2067 */
2068 static int
2069compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2070{
2071 int ret;
2072 char_u *p = *arg + 1;
2073
2074 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2075 return FAIL;
2076 if (ppconst->pp_used <= PPSIZE - 10)
2077 {
2078 ret = compile_expr1(arg, cctx, ppconst);
2079 }
2080 else
2081 {
2082 // Not enough space in ppconst, flush constants.
2083 if (generate_ppconst(cctx, ppconst) == FAIL)
2084 return FAIL;
2085 ret = compile_expr0(arg, cctx);
2086 }
2087 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2088 return FAIL;
2089 if (**arg == ')')
2090 ++*arg;
2091 else if (ret == OK)
2092 {
2093 emsg(_(e_missing_closing_paren));
2094 ret = FAIL;
2095 }
2096 return ret;
2097}
2098
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002099static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002100
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002101/*
2102 * Compile whatever comes after "name" or "name()".
2103 * Advances "*arg" only when something was recognized.
2104 */
2105 static int
2106compile_subscript(
2107 char_u **arg,
2108 cctx_T *cctx,
2109 char_u *start_leader,
2110 char_u **end_leader,
2111 ppconst_T *ppconst)
2112{
2113 char_u *name_start = *end_leader;
2114 int keeping_dict = FALSE;
2115
2116 for (;;)
2117 {
2118 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002119 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002120
2121 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2122 {
2123 char_u *next = peek_next_line_from_context(cctx);
2124
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002125 // If a following line starts with "->{", "->(" or "->X" advance to
2126 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002127 // Also if a following line starts with ".x".
2128 if (next != NULL &&
2129 ((next[0] == '-' && next[1] == '>'
2130 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002131 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132 || ASCII_ISALPHA(*skipwhite(next + 2))))
2133 || (next[0] == '.' && eval_isdictc(next[1]))))
2134 {
2135 next = next_line_from_context(cctx, TRUE);
2136 if (next == NULL)
2137 return FAIL;
2138 *arg = next;
2139 p = skipwhite(*arg);
2140 }
2141 }
2142
2143 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2144 // is not a function call.
2145 if (**arg == '(')
2146 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002147 int argcount = 0;
2148
2149 if (generate_ppconst(cctx, ppconst) == FAIL)
2150 return FAIL;
2151 ppconst->pp_is_const = FALSE;
2152
2153 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002154 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002155
2156 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002157 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002158 return FAIL;
2159 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2160 return FAIL;
2161 if (keeping_dict)
2162 {
2163 keeping_dict = FALSE;
2164 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2165 return FAIL;
2166 }
2167 }
2168 else if (*p == '-' && p[1] == '>')
2169 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002170 char_u *pstart = p;
2171 int alt;
2172 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002173
Bram Moolenaarc7349932022-01-16 20:59:39 +00002174 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 if (generate_ppconst(cctx, ppconst) == FAIL)
2176 return FAIL;
2177 ppconst->pp_is_const = FALSE;
2178
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002179 // Apply the '!', '-' and '+' first:
2180 // -1.0->func() works like (-1.0)->func()
2181 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2182 return FAIL;
2183
2184 p += 2;
2185 *arg = skipwhite(p);
2186 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002187
2188 // Three alternatives handled here:
2189 // 1. "base->name(" only a name, use compile_call()
2190 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2191 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002192 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002193 alt = 2;
2194 else
2195 {
2196 // alternative 1 or 3
2197 p = *arg;
2198 if (!eval_isnamec1(*p))
2199 {
2200 semsg(_(e_trailing_characters_str), pstart);
2201 return FAIL;
2202 }
2203 if (ASCII_ISALPHA(*p) && p[1] == ':')
2204 p += 2;
2205 for ( ; eval_isnamec(*p); ++p)
2206 ;
2207 if (*p == '(')
2208 {
2209 // alternative 1
2210 alt = 1;
2211 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2212 return FAIL;
2213 }
2214 else
2215 {
2216 // Must be alternative 3, find the "(". Only works within
2217 // one line.
2218 alt = 3;
2219 paren = vim_strchr(p, '(');
2220 if (paren == NULL)
2221 {
2222 semsg(_(e_missing_parenthesis_str), *arg);
2223 return FAIL;
2224 }
2225 }
2226 }
2227
2228 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 {
2230 int argcount = 1;
2231 garray_T *stack = &cctx->ctx_type_stack;
2232 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233 int expr_isn_start = cctx->ctx_instr.ga_len;
2234 int expr_isn_end;
2235 int arg_isn_count;
2236
Bram Moolenaarc7349932022-01-16 20:59:39 +00002237 if (alt == 2)
2238 {
2239 // Funcref call: list->(Refs[2])(arg)
2240 // or lambda: list->((arg) => expr)(arg)
2241 //
2242 // Fist compile the function expression.
2243 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2244 return FAIL;
2245 }
2246 else
2247 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002248 int fail;
2249 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002250 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002251
Bram Moolenaarc7349932022-01-16 20:59:39 +00002252 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002253
2254 // instead of using LOADG for "import.Func" use PUSHFUNC
2255 ++paren_follows_after_expr;
2256
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002257 // do not look in the next line
2258 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002259
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002260 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002261 || *skipwhite(*arg) != NUL;
2262 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002263 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002264 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002265
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002266 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002267 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002268 if (did_emsg == prev_did_emsg)
2269 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002270 return FAIL;
2271 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002272 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002273
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274 // Compile the arguments.
2275 if (**arg != '(')
2276 {
2277 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002278 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002279 else
2280 semsg(_(e_missing_parenthesis_str), *arg);
2281 return FAIL;
2282 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002283
2284 // Remember the next instruction index, where the instructions
2285 // for arguments are being written.
2286 expr_isn_end = cctx->ctx_instr.ga_len;
2287
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002288 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002289 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2290 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002291 return FAIL;
2292
2293 // Move the instructions for the arguments to before the
2294 // instructions of the expression and move the type of the
2295 // expression after the argument types. This is what ISN_PCALL
2296 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2298 if (arg_isn_count > 0)
2299 {
2300 int expr_isn_count = expr_isn_end - expr_isn_start;
2301 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002302 type_T *decl_type;
2303 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304
2305 if (isn == NULL)
2306 return FAIL;
2307 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2308 + expr_isn_start,
2309 sizeof(isn_T) * expr_isn_count);
2310 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2311 + expr_isn_start,
2312 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2313 sizeof(isn_T) * arg_isn_count);
2314 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2315 + expr_isn_start + arg_isn_count,
2316 isn, sizeof(isn_T) * expr_isn_count);
2317 vim_free(isn);
2318
Bram Moolenaar078a4612022-01-04 15:17:03 +00002319 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2320 type = typep->type_curr;
2321 decl_type = typep->type_decl;
2322 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2323 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2324 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002326 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2327 typep->type_curr = type;
2328 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329 }
2330
Bram Moolenaar078a4612022-01-04 15:17:03 +00002331 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002332 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2333 return FAIL;
2334 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002335
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 if (keeping_dict)
2337 {
2338 keeping_dict = FALSE;
2339 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2340 return FAIL;
2341 }
2342 }
2343 else if (**arg == '[')
2344 {
2345 int is_slice = FALSE;
2346
2347 // list index: list[123]
2348 // dict member: dict[key]
2349 // string index: text[123]
2350 // blob index: blob[123]
2351 if (generate_ppconst(cctx, ppconst) == FAIL)
2352 return FAIL;
2353 ppconst->pp_is_const = FALSE;
2354
2355 ++p;
2356 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2357 return FAIL;
2358 if (**arg == ':')
2359 {
2360 // missing first index is equal to zero
2361 generate_PUSHNR(cctx, 0);
2362 }
2363 else
2364 {
2365 if (compile_expr0(arg, cctx) == FAIL)
2366 return FAIL;
2367 if (**arg == ':')
2368 {
2369 semsg(_(e_white_space_required_before_and_after_str_at_str),
2370 ":", *arg);
2371 return FAIL;
2372 }
2373 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2374 return FAIL;
2375 *arg = skipwhite(*arg);
2376 }
2377 if (**arg == ':')
2378 {
2379 is_slice = TRUE;
2380 ++*arg;
2381 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2382 {
2383 semsg(_(e_white_space_required_before_and_after_str_at_str),
2384 ":", *arg);
2385 return FAIL;
2386 }
2387 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2388 return FAIL;
2389 if (**arg == ']')
2390 // missing second index is equal to end of string
2391 generate_PUSHNR(cctx, -1);
2392 else
2393 {
2394 if (compile_expr0(arg, cctx) == FAIL)
2395 return FAIL;
2396 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2397 return FAIL;
2398 *arg = skipwhite(*arg);
2399 }
2400 }
2401
2402 if (**arg != ']')
2403 {
2404 emsg(_(e_missing_closing_square_brace));
2405 return FAIL;
2406 }
2407 *arg = *arg + 1;
2408
2409 if (keeping_dict)
2410 {
2411 keeping_dict = FALSE;
2412 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2413 return FAIL;
2414 }
2415 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2416 return FAIL;
2417 }
2418 else if (*p == '.' && p[1] != '.')
2419 {
2420 // dictionary member: dict.name
2421 if (generate_ppconst(cctx, ppconst) == FAIL)
2422 return FAIL;
2423 ppconst->pp_is_const = FALSE;
2424
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002425 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2426 && (type->tt_type == VAR_CLASS
2427 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002428 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002429 // class member: SomeClass.varname
2430 // class method: SomeClass.SomeMethod()
2431 // class constructor: SomeClass.new()
2432 // object member: someObject.varname, this.varname
2433 // object method: someObject.SomeMethod(), this.SomeMethod()
2434 *arg = p;
2435 if (compile_class_object_index(cctx, arg, type) == FAIL)
2436 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002438 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002439 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002440 *arg = p + 1;
2441 if (IS_WHITE_OR_NUL(**arg))
2442 {
2443 emsg(_(e_missing_name_after_dot));
2444 return FAIL;
2445 }
2446 p = *arg;
2447 if (eval_isdictc(*p))
2448 while (eval_isnamec(*p))
2449 MB_PTR_ADV(p);
2450 if (p == *arg)
2451 {
2452 semsg(_(e_syntax_error_at_str), *arg);
2453 return FAIL;
2454 }
2455 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2456 return FAIL;
2457 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2458 return FAIL;
2459 keeping_dict = TRUE;
2460 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 }
2463 else
2464 break;
2465 }
2466
2467 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2468 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002469 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2470 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002471 return FAIL;
2472
2473 return OK;
2474}
2475
2476/*
2477 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2478 * "arg" is advanced until after the expression, skipping white space.
2479 *
2480 * If the value is a constant "ppconst->pp_used" will be non-zero.
2481 * Before instructions are generated, any values in "ppconst" will generated.
2482 *
2483 * This is the compiling equivalent of eval1(), eval2(), etc.
2484 */
2485
2486/*
2487 * number number constant
2488 * 0zFFFFFFFF Blob constant
2489 * "string" string constant
2490 * 'string' literal string constant
2491 * &option-name option value
2492 * @r register contents
2493 * identifier variable value
2494 * function() function call
2495 * $VAR environment variable
2496 * (expression) nested expression
2497 * [expr, expr] List
2498 * {key: val, [key]: val} Dictionary
2499 *
2500 * Also handle:
2501 * ! in front logical NOT
2502 * - in front unary minus
2503 * + in front unary plus (ignored)
2504 * trailing (arg) funcref/partial call
2505 * trailing [] subscript in String or List
2506 * trailing .name entry in Dictionary
2507 * trailing ->name() method call
2508 */
2509 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002510compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002511 char_u **arg,
2512 cctx_T *cctx,
2513 ppconst_T *ppconst)
2514{
2515 char_u *start_leader, *end_leader;
2516 int ret = OK;
2517 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2518 int used_before = ppconst->pp_used;
2519
2520 ppconst->pp_is_const = FALSE;
2521
2522 /*
2523 * Skip '!', '-' and '+' characters. They are handled later.
2524 */
2525 start_leader = *arg;
2526 if (eval_leader(arg, TRUE) == FAIL)
2527 return FAIL;
2528 end_leader = *arg;
2529
2530 rettv->v_type = VAR_UNKNOWN;
2531 switch (**arg)
2532 {
2533 /*
2534 * Number constant.
2535 */
2536 case '0': // also for blob starting with 0z
2537 case '1':
2538 case '2':
2539 case '3':
2540 case '4':
2541 case '5':
2542 case '6':
2543 case '7':
2544 case '8':
2545 case '9':
2546 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2547 return FAIL;
2548 // Apply "-" and "+" just before the number now, right to
2549 // left. Matters especially when "->" follows. Stops at
2550 // '!'.
2551 if (apply_leader(rettv, TRUE,
2552 start_leader, &end_leader) == FAIL)
2553 {
2554 clear_tv(rettv);
2555 return FAIL;
2556 }
2557 break;
2558
2559 /*
2560 * String constant: "string".
2561 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002562 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 return FAIL;
2564 break;
2565
2566 /*
2567 * Literal string constant: 'str''ing'.
2568 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002569 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 return FAIL;
2571 break;
2572
2573 /*
2574 * Constant Vim variable.
2575 */
2576 case 'v': get_vim_constant(arg, rettv);
2577 ret = NOTDONE;
2578 break;
2579
2580 /*
2581 * "true" constant
2582 */
2583 case 't': if (STRNCMP(*arg, "true", 4) == 0
2584 && !eval_isnamec((*arg)[4]))
2585 {
2586 *arg += 4;
2587 rettv->v_type = VAR_BOOL;
2588 rettv->vval.v_number = VVAL_TRUE;
2589 }
2590 else
2591 ret = NOTDONE;
2592 break;
2593
2594 /*
2595 * "false" constant
2596 */
2597 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2598 && !eval_isnamec((*arg)[5]))
2599 {
2600 *arg += 5;
2601 rettv->v_type = VAR_BOOL;
2602 rettv->vval.v_number = VVAL_FALSE;
2603 }
2604 else
2605 ret = NOTDONE;
2606 break;
2607
2608 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002609 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002610 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002611 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002612 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002613 char_u *p = *arg + 4;
2614 int len;
2615
2616 for (len = 0; eval_isnamec(p[len]); ++len)
2617 ;
2618 ret = handle_predefined(*arg, len + 4, rettv);
2619 if (ret == FAIL)
2620 ret = NOTDONE;
2621 else
2622 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002623 }
2624 else
2625 ret = NOTDONE;
2626 break;
2627
2628 /*
2629 * List: [expr, expr]
2630 */
2631 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2632 return FAIL;
2633 ret = compile_list(arg, cctx, ppconst);
2634 break;
2635
2636 /*
2637 * Dictionary: {'key': val, 'key': val}
2638 */
2639 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2640 return FAIL;
2641 ret = compile_dict(arg, cctx, ppconst);
2642 break;
2643
2644 /*
2645 * Option value: &name
2646 */
2647 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2648 return FAIL;
2649 ret = compile_get_option(arg, cctx);
2650 break;
2651
2652 /*
2653 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002654 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002655 */
2656 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2657 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002658 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2659 ret = compile_interp_string(arg, cctx);
2660 else
2661 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002662 break;
2663
2664 /*
2665 * Register contents: @r.
2666 */
2667 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2668 return FAIL;
2669 ret = compile_get_register(arg, cctx);
2670 break;
2671 /*
2672 * nested expression: (expression).
2673 * lambda: (arg, arg) => expr
2674 * funcref: (arg, arg) => { statement }
2675 */
2676 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2677 ret = compile_lambda(arg, cctx);
2678 if (ret == NOTDONE)
2679 ret = compile_parenthesis(arg, cctx, ppconst);
2680 break;
2681
2682 default: ret = NOTDONE;
2683 break;
2684 }
2685 if (ret == FAIL)
2686 return FAIL;
2687
2688 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2689 {
2690 if (cctx->ctx_skip == SKIP_YES)
2691 clear_tv(rettv);
2692 else
2693 // A constant expression can possibly be handled compile time,
2694 // return the value instead of generating code.
2695 ++ppconst->pp_used;
2696 }
2697 else if (ret == NOTDONE)
2698 {
2699 char_u *p;
2700 int r;
2701
2702 if (!eval_isnamec1(**arg))
2703 {
2704 if (!vim9_bad_comment(*arg))
2705 {
2706 if (ends_excmd(*skipwhite(*arg)))
2707 semsg(_(e_empty_expression_str), *arg);
2708 else
2709 semsg(_(e_name_expected_str), *arg);
2710 }
2711 return FAIL;
2712 }
2713
2714 // "name" or "name()"
2715 p = to_name_end(*arg, TRUE);
2716 if (p - *arg == (size_t)1 && **arg == '_')
2717 {
2718 emsg(_(e_cannot_use_underscore_here));
2719 return FAIL;
2720 }
2721
2722 if (*p == '(')
2723 {
2724 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2725 }
2726 else
2727 {
2728 if (cctx->ctx_skip != SKIP_YES
2729 && generate_ppconst(cctx, ppconst) == FAIL)
2730 return FAIL;
2731 r = compile_load(arg, p, cctx, TRUE, TRUE);
2732 }
2733 if (r == FAIL)
2734 return FAIL;
2735 }
2736
2737 // Handle following "[]", ".member", etc.
2738 // Then deal with prefixed '-', '+' and '!', if not done already.
2739 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2740 ppconst) == FAIL)
2741 return FAIL;
2742 if (ppconst->pp_used > 0)
2743 {
2744 // apply the '!', '-' and '+' before the constant
2745 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2746 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2747 return FAIL;
2748 return OK;
2749 }
2750 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2751 return FAIL;
2752 return OK;
2753}
2754
2755/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002756 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002757 */
2758 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002759compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002760{
2761 type_T *want_type = NULL;
2762
2763 // Recognize <type>
2764 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2765 {
2766 ++*arg;
2767 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2768 if (want_type == NULL)
2769 return FAIL;
2770
2771 if (**arg != '>')
2772 {
2773 if (*skipwhite(*arg) == '>')
2774 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2775 else
2776 emsg(_(e_missing_gt));
2777 return FAIL;
2778 }
2779 ++*arg;
2780 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2781 return FAIL;
2782 }
2783
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002784 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002785 return FAIL;
2786
2787 if (want_type != NULL)
2788 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002789 type_T *actual;
2790 where_T where = WHERE_INIT;
2791
2792 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002793 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002794 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002795 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002796 if (need_type(actual, want_type, FALSE,
2797 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002798 return FAIL;
2799 }
2800 }
2801
2802 return OK;
2803}
2804
2805/*
2806 * * number multiplication
2807 * / number division
2808 * % number modulo
2809 */
2810 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002811compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002812{
2813 char_u *op;
2814 char_u *next;
2815 int ppconst_used = ppconst->pp_used;
2816
2817 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002818 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002819 return FAIL;
2820
2821 /*
2822 * Repeat computing, until no "*", "/" or "%" is following.
2823 */
2824 for (;;)
2825 {
2826 op = may_peek_next_line(cctx, *arg, &next);
2827 if (*op != '*' && *op != '/' && *op != '%')
2828 break;
2829 if (next != NULL)
2830 {
2831 *arg = next_line_from_context(cctx, TRUE);
2832 op = skipwhite(*arg);
2833 }
2834
2835 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2836 {
2837 error_white_both(op, 1);
2838 return FAIL;
2839 }
2840 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2841 return FAIL;
2842
2843 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002844 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002845 return FAIL;
2846
2847 if (ppconst->pp_used == ppconst_used + 2
2848 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2849 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2850 {
2851 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2852 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2853 varnumber_T res = 0;
2854 int failed = FALSE;
2855
2856 // both are numbers: compute the result
2857 switch (*op)
2858 {
2859 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2860 break;
2861 case '/': res = num_divide(tv1->vval.v_number,
2862 tv2->vval.v_number, &failed);
2863 break;
2864 case '%': res = num_modulus(tv1->vval.v_number,
2865 tv2->vval.v_number, &failed);
2866 break;
2867 }
2868 if (failed)
2869 return FAIL;
2870 tv1->vval.v_number = res;
2871 --ppconst->pp_used;
2872 }
2873 else
2874 {
2875 generate_ppconst(cctx, ppconst);
2876 generate_two_op(cctx, op);
2877 }
2878 }
2879
2880 return OK;
2881}
2882
2883/*
2884 * + number addition or list/blobl concatenation
2885 * - number subtraction
2886 * .. string concatenation
2887 */
2888 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002889compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002890{
2891 char_u *op;
2892 char_u *next;
2893 int oplen;
2894 int ppconst_used = ppconst->pp_used;
2895
2896 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002897 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002898 return FAIL;
2899
2900 /*
2901 * Repeat computing, until no "+", "-" or ".." is following.
2902 */
2903 for (;;)
2904 {
2905 op = may_peek_next_line(cctx, *arg, &next);
2906 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2907 break;
2908 if (op[0] == op[1] && *op != '.' && next)
2909 // Finding "++" or "--" on the next line is a separate command.
2910 // But ".." is concatenation.
2911 break;
2912 oplen = (*op == '.' ? 2 : 1);
2913 if (next != NULL)
2914 {
2915 *arg = next_line_from_context(cctx, TRUE);
2916 op = skipwhite(*arg);
2917 }
2918
2919 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2920 {
2921 error_white_both(op, oplen);
2922 return FAIL;
2923 }
2924
2925 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2926 return FAIL;
2927
2928 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002929 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002930 return FAIL;
2931
2932 if (ppconst->pp_used == ppconst_used + 2
2933 && (*op == '.'
2934 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2935 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2936 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2937 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2938 {
2939 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2940 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2941
2942 // concat/subtract/add constant numbers
2943 if (*op == '+')
2944 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2945 else if (*op == '-')
2946 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2947 else
2948 {
2949 // concatenate constant strings
2950 char_u *s1 = tv1->vval.v_string;
2951 char_u *s2 = tv2->vval.v_string;
2952 size_t len1 = STRLEN(s1);
2953
2954 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2955 if (tv1->vval.v_string == NULL)
2956 {
2957 clear_ppconst(ppconst);
2958 return FAIL;
2959 }
2960 mch_memmove(tv1->vval.v_string, s1, len1);
2961 STRCPY(tv1->vval.v_string + len1, s2);
2962 vim_free(s1);
2963 vim_free(s2);
2964 }
2965 --ppconst->pp_used;
2966 }
2967 else
2968 {
2969 generate_ppconst(cctx, ppconst);
2970 ppconst->pp_is_const = FALSE;
2971 if (*op == '.')
2972 {
2973 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2974 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2975 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002976 if (generate_CONCAT(cctx, 2) == FAIL)
2977 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002978 }
2979 else
2980 generate_two_op(cctx, op);
2981 }
2982 }
2983
2984 return OK;
2985}
2986
2987/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002988 * expr6a >> expr6b
2989 * expr6a << expr6b
2990 *
2991 * Produces instructions:
2992 * OPNR bitwise left or right shift
2993 */
2994 static int
2995compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2996{
2997 exprtype_T type = EXPR_UNKNOWN;
2998 char_u *p;
2999 char_u *next;
3000 int len = 2;
3001 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003002 isn_T *isn;
3003
3004 // get the first variable
3005 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3006 return FAIL;
3007
3008 /*
3009 * Repeat computing, until no "+", "-" or ".." is following.
3010 */
3011 for (;;)
3012 {
3013 type = EXPR_UNKNOWN;
3014
3015 p = may_peek_next_line(cctx, *arg, &next);
3016 if (p[0] == '<' && p[1] == '<')
3017 type = EXPR_LSHIFT;
3018 else if (p[0] == '>' && p[1] == '>')
3019 type = EXPR_RSHIFT;
3020
3021 if (type == EXPR_UNKNOWN)
3022 return OK;
3023
3024 // Handle a bitwise left or right shift operator
3025 if (ppconst->pp_used == ppconst_used + 1)
3026 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003027 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003028 {
3029 // left operand should be a number
3030 emsg(_(e_bitshift_ops_must_be_number));
3031 return FAIL;
3032 }
3033 }
3034 else
3035 {
3036 type_T *t = get_type_on_stack(cctx, 0);
3037
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003038 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003039 {
3040 emsg(_(e_bitshift_ops_must_be_number));
3041 return FAIL;
3042 }
3043 }
3044
3045 if (next != NULL)
3046 {
3047 *arg = next_line_from_context(cctx, TRUE);
3048 p = skipwhite(*arg);
3049 }
3050
3051 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3052 {
3053 error_white_both(p, len);
3054 return FAIL;
3055 }
3056
3057 // get the second variable
3058 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3059 return FAIL;
3060
3061 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3062 return FAIL;
3063
3064 if (ppconst->pp_used == ppconst_used + 2)
3065 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003066 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3067 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3068
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003069 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003070 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3071 {
3072 // right operand should be a positive number
3073 if (tv2->v_type != VAR_NUMBER)
3074 emsg(_(e_bitshift_ops_must_be_number));
3075 else
dundargocc57b5bc2022-11-02 13:30:51 +00003076 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003077 return FAIL;
3078 }
3079
3080 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3081 tv1->vval.v_number = 0;
3082 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003083 tv1->vval.v_number =
3084 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003085 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003086 tv1->vval.v_number =
3087 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003088 clear_tv(tv2);
3089 --ppconst->pp_used;
3090 }
3091 else
3092 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003093 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3094 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003095 {
3096 emsg(_(e_bitshift_ops_must_be_number));
3097 return FAIL;
3098 }
3099
3100 generate_ppconst(cctx, ppconst);
3101
3102 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3103 if (isn == NULL)
3104 return FAIL;
3105
3106 if (isn != NULL)
3107 isn->isn_arg.op.op_type = type;
3108 }
3109 }
3110
3111 return OK;
3112}
3113
3114/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003115 * expr5a == expr5b
3116 * expr5a =~ expr5b
3117 * expr5a != expr5b
3118 * expr5a !~ expr5b
3119 * expr5a > expr5b
3120 * expr5a >= expr5b
3121 * expr5a < expr5b
3122 * expr5a <= expr5b
3123 * expr5a is expr5b
3124 * expr5a isnot expr5b
3125 *
3126 * Produces instructions:
3127 * EVAL expr5a Push result of "expr5a"
3128 * EVAL expr5b Push result of "expr5b"
3129 * COMPARE one of the compare instructions
3130 */
3131 static int
3132compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3133{
3134 exprtype_T type = EXPR_UNKNOWN;
3135 char_u *p;
3136 char_u *next;
3137 int len = 2;
3138 int type_is = FALSE;
3139 int ppconst_used = ppconst->pp_used;
3140
3141 // get the first variable
3142 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3143 return FAIL;
3144
3145 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003146
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003147 type = get_compare_type(p, &len, &type_is);
3148
3149 /*
3150 * If there is a comparative operator, use it.
3151 */
3152 if (type != EXPR_UNKNOWN)
3153 {
3154 int ic = FALSE; // Default: do not ignore case
3155
3156 if (next != NULL)
3157 {
3158 *arg = next_line_from_context(cctx, TRUE);
3159 p = skipwhite(*arg);
3160 }
3161 if (type_is && (p[len] == '?' || p[len] == '#'))
3162 {
3163 semsg(_(e_invalid_expression_str), *arg);
3164 return FAIL;
3165 }
3166 // extra question mark appended: ignore case
3167 if (p[len] == '?')
3168 {
3169 ic = TRUE;
3170 ++len;
3171 }
3172 // extra '#' appended: match case (ignored)
3173 else if (p[len] == '#')
3174 ++len;
3175 // nothing appended: match case
3176
3177 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3178 {
3179 error_white_both(p, len);
3180 return FAIL;
3181 }
3182
3183 // get the second variable
3184 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3185 return FAIL;
3186
3187 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3188 return FAIL;
3189
3190 if (ppconst->pp_used == ppconst_used + 2)
3191 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003192 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003193 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3194 int ret;
3195
3196 // Both sides are a constant, compute the result now.
3197 // First check for a valid combination of types, this is more
3198 // strict than typval_compare().
3199 if (check_compare_types(type, tv1, tv2) == FAIL)
3200 ret = FAIL;
3201 else
3202 {
3203 ret = typval_compare(tv1, tv2, type, ic);
3204 tv1->v_type = VAR_BOOL;
3205 tv1->vval.v_number = tv1->vval.v_number
3206 ? VVAL_TRUE : VVAL_FALSE;
3207 clear_tv(tv2);
3208 --ppconst->pp_used;
3209 }
3210 return ret;
3211 }
3212
3213 generate_ppconst(cctx, ppconst);
3214 return generate_COMPARE(cctx, type, ic);
3215 }
3216
3217 return OK;
3218}
3219
3220static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3221
3222/*
3223 * Compile || or &&.
3224 */
3225 static int
3226compile_and_or(
3227 char_u **arg,
3228 cctx_T *cctx,
3229 char *op,
3230 ppconst_T *ppconst,
3231 int ppconst_used UNUSED)
3232{
3233 char_u *next;
3234 char_u *p = may_peek_next_line(cctx, *arg, &next);
3235 int opchar = *op;
3236
3237 if (p[0] == opchar && p[1] == opchar)
3238 {
3239 garray_T *instr = &cctx->ctx_instr;
3240 garray_T end_ga;
3241 int save_skip = cctx->ctx_skip;
3242
3243 /*
3244 * Repeat until there is no following "||" or "&&"
3245 */
3246 ga_init2(&end_ga, sizeof(int), 10);
3247 while (p[0] == opchar && p[1] == opchar)
3248 {
3249 long start_lnum = SOURCING_LNUM;
3250 long save_sourcing_lnum;
3251 int start_ctx_lnum = cctx->ctx_lnum;
3252 int save_lnum;
3253 int const_used;
3254 int status;
3255 jumpwhen_T jump_when = opchar == '|'
3256 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3257
3258 if (next != NULL)
3259 {
3260 *arg = next_line_from_context(cctx, TRUE);
3261 p = skipwhite(*arg);
3262 }
3263
3264 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3265 {
3266 semsg(_(e_white_space_required_before_and_after_str_at_str),
3267 op, p);
3268 ga_clear(&end_ga);
3269 return FAIL;
3270 }
3271
3272 save_sourcing_lnum = SOURCING_LNUM;
3273 SOURCING_LNUM = start_lnum;
3274 save_lnum = cctx->ctx_lnum;
3275 cctx->ctx_lnum = start_ctx_lnum;
3276
3277 status = check_ppconst_bool(ppconst);
3278 if (status != FAIL)
3279 {
3280 // Use the last ppconst if possible.
3281 if (ppconst->pp_used > 0)
3282 {
3283 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3284 int is_true = tv2bool(tv);
3285
3286 if ((is_true && opchar == '|')
3287 || (!is_true && opchar == '&'))
3288 {
3289 // For "false && expr" and "true || expr" the "expr"
3290 // does not need to be evaluated.
3291 cctx->ctx_skip = SKIP_YES;
3292 clear_tv(tv);
3293 tv->v_type = VAR_BOOL;
3294 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3295 }
3296 else
3297 {
3298 // For "true && expr" and "false || expr" only "expr"
3299 // needs to be evaluated.
3300 --ppconst->pp_used;
3301 jump_when = JUMP_NEVER;
3302 }
3303 }
3304 else
3305 {
3306 // Every part must evaluate to a bool.
3307 status = bool_on_stack(cctx);
3308 }
3309 }
3310 if (status != FAIL)
3311 status = ga_grow(&end_ga, 1);
3312 cctx->ctx_lnum = save_lnum;
3313 if (status == FAIL)
3314 {
3315 ga_clear(&end_ga);
3316 return FAIL;
3317 }
3318
3319 if (jump_when != JUMP_NEVER)
3320 {
3321 if (cctx->ctx_skip != SKIP_YES)
3322 {
3323 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3324 ++end_ga.ga_len;
3325 }
3326 generate_JUMP(cctx, jump_when, 0);
3327 }
3328
3329 // eval the next expression
3330 SOURCING_LNUM = save_sourcing_lnum;
3331 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3332 {
3333 ga_clear(&end_ga);
3334 return FAIL;
3335 }
3336
3337 const_used = ppconst->pp_used;
3338 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3339 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3340 {
3341 ga_clear(&end_ga);
3342 return FAIL;
3343 }
3344
3345 // "0 || 1" results in true, "1 && 0" results in false.
3346 if (ppconst->pp_used == const_used + 1)
3347 {
3348 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3349
3350 if (tv->v_type == VAR_NUMBER
3351 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3352 {
3353 tv->vval.v_number = tv->vval.v_number == 1
3354 ? VVAL_TRUE : VVAL_FALSE;
3355 tv->v_type = VAR_BOOL;
3356 }
3357 }
3358
3359 p = may_peek_next_line(cctx, *arg, &next);
3360 }
3361
3362 if (check_ppconst_bool(ppconst) == FAIL)
3363 {
3364 ga_clear(&end_ga);
3365 return FAIL;
3366 }
3367
3368 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3369 // Every part must evaluate to a bool.
3370 if (bool_on_stack(cctx) == FAIL)
3371 {
3372 ga_clear(&end_ga);
3373 return FAIL;
3374 }
3375
3376 if (end_ga.ga_len > 0)
3377 {
3378 // Fill in the end label in all jumps.
3379 generate_ppconst(cctx, ppconst);
3380 while (end_ga.ga_len > 0)
3381 {
3382 isn_T *isn;
3383
3384 --end_ga.ga_len;
3385 isn = ((isn_T *)instr->ga_data)
3386 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3387 isn->isn_arg.jump.jump_where = instr->ga_len;
3388 }
3389 }
3390 ga_clear(&end_ga);
3391
3392 cctx->ctx_skip = save_skip;
3393 }
3394
3395 return OK;
3396}
3397
3398/*
3399 * expr4a && expr4a && expr4a logical AND
3400 *
3401 * Produces instructions:
3402 * EVAL expr4a Push result of "expr4a"
3403 * COND2BOOL convert to bool if needed
3404 * JUMP_IF_COND_FALSE end
3405 * EVAL expr4b Push result of "expr4b"
3406 * JUMP_IF_COND_FALSE end
3407 * EVAL expr4c Push result of "expr4c"
3408 * end:
3409 */
3410 static int
3411compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3412{
3413 int ppconst_used = ppconst->pp_used;
3414
3415 // get the first variable
3416 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3417 return FAIL;
3418
3419 // || and && work almost the same
3420 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3421}
3422
3423/*
3424 * expr3a || expr3b || expr3c logical OR
3425 *
3426 * Produces instructions:
3427 * EVAL expr3a Push result of "expr3a"
3428 * COND2BOOL convert to bool if needed
3429 * JUMP_IF_COND_TRUE end
3430 * EVAL expr3b Push result of "expr3b"
3431 * JUMP_IF_COND_TRUE end
3432 * EVAL expr3c Push result of "expr3c"
3433 * end:
3434 */
3435 static int
3436compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3437{
3438 int ppconst_used = ppconst->pp_used;
3439
3440 // eval the first expression
3441 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3442 return FAIL;
3443
3444 // || and && work almost the same
3445 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3446}
3447
3448/*
3449 * Toplevel expression: expr2 ? expr1a : expr1b
3450 * Produces instructions:
3451 * EVAL expr2 Push result of "expr2"
3452 * JUMP_IF_FALSE alt jump if false
3453 * EVAL expr1a
3454 * JUMP_ALWAYS end
3455 * alt: EVAL expr1b
3456 * end:
3457 *
3458 * Toplevel expression: expr2 ?? expr1
3459 * Produces instructions:
3460 * EVAL expr2 Push result of "expr2"
3461 * JUMP_AND_KEEP_IF_TRUE end jump if true
3462 * EVAL expr1
3463 * end:
3464 */
3465 int
3466compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3467{
3468 char_u *p;
3469 int ppconst_used = ppconst->pp_used;
3470 char_u *next;
3471
3472 // Ignore all kinds of errors when not producing code.
3473 if (cctx->ctx_skip == SKIP_YES)
3474 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003475 int prev_did_emsg = did_emsg;
3476
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003477 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003478 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003479 }
3480
3481 // Evaluate the first expression.
3482 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3483 return FAIL;
3484
3485 p = may_peek_next_line(cctx, *arg, &next);
3486 if (*p == '?')
3487 {
3488 int op_falsy = p[1] == '?';
3489 garray_T *instr = &cctx->ctx_instr;
3490 garray_T *stack = &cctx->ctx_type_stack;
3491 int alt_idx = instr->ga_len;
3492 int end_idx = 0;
3493 isn_T *isn;
3494 type_T *type1 = NULL;
3495 int has_const_expr = FALSE;
3496 int const_value = FALSE;
3497 int save_skip = cctx->ctx_skip;
3498
3499 if (next != NULL)
3500 {
3501 *arg = next_line_from_context(cctx, TRUE);
3502 p = skipwhite(*arg);
3503 }
3504
3505 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3506 {
3507 semsg(_(e_white_space_required_before_and_after_str_at_str),
3508 op_falsy ? "??" : "?", p);
3509 return FAIL;
3510 }
3511
3512 if (ppconst->pp_used == ppconst_used + 1)
3513 {
3514 // the condition is a constant, we know whether the ? or the :
3515 // expression is to be evaluated.
3516 has_const_expr = TRUE;
3517 if (op_falsy)
3518 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3519 else
3520 {
3521 int error = FALSE;
3522
3523 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3524 &error);
3525 if (error)
3526 return FAIL;
3527 }
3528 cctx->ctx_skip = save_skip == SKIP_YES ||
3529 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3530
3531 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3532 // "left ?? right" and "left" is truthy: produce "left"
3533 generate_ppconst(cctx, ppconst);
3534 else
3535 {
3536 clear_tv(&ppconst->pp_tv[ppconst_used]);
3537 --ppconst->pp_used;
3538 }
3539 }
3540 else
3541 {
3542 generate_ppconst(cctx, ppconst);
3543 if (op_falsy)
3544 end_idx = instr->ga_len;
3545 generate_JUMP(cctx, op_falsy
3546 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3547 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003548 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003549 }
3550
3551 // evaluate the second expression; any type is accepted
3552 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3553 return FAIL;
3554 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3555 return FAIL;
3556
3557 if (!has_const_expr)
3558 {
3559 generate_ppconst(cctx, ppconst);
3560
3561 if (!op_falsy)
3562 {
3563 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003564 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003565 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003566
3567 end_idx = instr->ga_len;
3568 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3569
3570 // jump here from JUMP_IF_FALSE
3571 isn = ((isn_T *)instr->ga_data) + alt_idx;
3572 isn->isn_arg.jump.jump_where = instr->ga_len;
3573 }
3574 }
3575
3576 if (!op_falsy)
3577 {
3578 // Check for the ":".
3579 p = may_peek_next_line(cctx, *arg, &next);
3580 if (*p != ':')
3581 {
3582 emsg(_(e_missing_colon_after_questionmark));
3583 return FAIL;
3584 }
3585 if (next != NULL)
3586 {
3587 *arg = next_line_from_context(cctx, TRUE);
3588 p = skipwhite(*arg);
3589 }
3590
3591 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3592 {
3593 semsg(_(e_white_space_required_before_and_after_str_at_str),
3594 ":", p);
3595 return FAIL;
3596 }
3597
3598 // evaluate the third expression
3599 if (has_const_expr)
3600 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3601 ? SKIP_YES : SKIP_NOT;
3602 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3603 return FAIL;
3604 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3605 return FAIL;
3606 }
3607
3608 if (!has_const_expr)
3609 {
3610 type_T **typep;
3611
3612 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003613 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003614
3615 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003616 typep = &((((type2_T *)stack->ga_data)
3617 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003618 common_type(type1, *typep, typep, cctx->ctx_type_list);
3619
3620 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3621 isn = ((isn_T *)instr->ga_data) + end_idx;
3622 isn->isn_arg.jump.jump_where = instr->ga_len;
3623 }
3624
3625 cctx->ctx_skip = save_skip;
3626 }
3627 return OK;
3628}
3629
3630/*
3631 * Toplevel expression.
3632 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3633 * Returns OK or FAIL.
3634 */
3635 int
3636compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3637{
3638 ppconst_T ppconst;
3639
3640 CLEAR_FIELD(ppconst);
3641 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3642 {
3643 clear_ppconst(&ppconst);
3644 return FAIL;
3645 }
3646 if (is_const != NULL)
3647 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3648 if (generate_ppconst(cctx, &ppconst) == FAIL)
3649 return FAIL;
3650 return OK;
3651}
3652
3653/*
3654 * Toplevel expression.
3655 */
3656 int
3657compile_expr0(char_u **arg, cctx_T *cctx)
3658{
3659 return compile_expr0_ext(arg, cctx, NULL);
3660}
3661
3662
3663#endif // defined(FEAT_EVAL)