blob: c001d9e3ddbeb43dc9b050f5bbea83fa9c33ea14 [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 {
406 semsg(_(e_cannot_access_private_method_str), name);
407 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 Raele6c9aa52023-10-06 19:55:52 +0200433 emsg_var_cl_define(e_cannot_access_private_variable_str,
434 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 {
452 semsg(_(e_cannot_access_private_method_str), fp->uf_name);
453 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 Raele6c9aa52023-10-06 19:55:52 +0200475 emsg_var_cl_define(e_cannot_access_private_variable_str,
476 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 {
494 semsg(_(e_cannot_access_private_method_str), fp->uf_name);
495 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
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200533 if (sv->sv_tv->v_type == VAR_TYPEALIAS)
534 {
535 semsg(_(e_using_typealias_as_variable),
536 sv->sv_tv->vval.v_typealias->ta_name);
537 return FAIL;
538 }
539
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000540 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
541 current_sctx.sc_sid, idx, sv->sv_type);
542 return OK;
543 }
544
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000545 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546 if (import != NULL)
547 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000548 char_u *p = skipwhite(*end);
549 char_u *exp_name;
550 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100551 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000552 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000553 int done = FALSE;
554 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000555
556 // Need to lookup the member.
557 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000558 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000559 semsg(_(e_expected_dot_after_name_str), start);
560 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000561 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000562 ++p;
563 if (VIM_ISWHITE(*p))
564 {
565 emsg(_(e_no_white_space_allowed_after_dot));
566 return FAIL;
567 }
568
569 // isolate one name
570 exp_name = p;
571 while (eval_isnamec(*p))
572 ++p;
573 cc = *p;
574 *p = NUL;
575
Bram Moolenaard041f422022-01-12 19:54:00 +0000576 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100577 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
578 // "import autoload './dir/script.vim'" or
579 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100580 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100581
582 if (res == OK)
583 {
584 if (si->sn_autoload_prefix != NULL
585 && si->sn_state == SN_STATE_NOT_LOADED)
586 {
587 char_u *auto_name =
588 concat_str(si->sn_autoload_prefix, exp_name);
589
590 // autoload script must be loaded later, access by the autoload
591 // name. If a '(' follows it must be a function. Otherwise we
592 // don't know, it can be "script.Func".
593 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100594 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100595 else
596 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
597 vim_free(auto_name);
598 done = TRUE;
599 }
600 else if (si->sn_import_autoload
601 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100602 {
603 // If a '(' follows it must be a function. Otherwise we don't
604 // know, it can be "script.Func".
605 if (cc == '(' || paren_follows_after_expr)
606 {
607 char_u sid_name[MAX_FUNC_NAME_LEN];
608
609 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100610 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100611 }
612 else
613 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
614 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100615 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100616 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100617 else
618 {
619 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
620 cctx, NULL, TRUE);
621 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100622 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100623
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000624 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000625 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000626 if (done)
627 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000628
629 if (idx < 0)
630 {
631 if (ufunc != NULL)
632 {
633 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100634 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000635 return OK;
636 }
637 return FAIL;
638 }
639
640 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
641 import->imp_sid,
642 idx,
643 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644 return OK;
645 }
646
Bram Moolenaard0132f42022-05-12 11:05:40 +0100647 // Can only get here if we know "name" is a script variable and not in a
648 // Vim9 script (variable is not in sn_var_vals): old style script.
649 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000650 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000651}
652
653 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000654generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000655{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000656 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000657 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000658
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000659 // Reject a global non-autoload function found without the "g:" prefix.
660 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000661 return FAIL;
662
663 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000664 compile_type = get_compile_type(ufunc);
665 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000666 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000667 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100668 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669}
670
671/*
672 * Compile a variable name into a load instruction.
673 * "end" points to just after the name.
674 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
675 * When "error" is FALSE do not give an error when not found.
676 */
677 int
678compile_load(
679 char_u **arg,
680 char_u *end_arg,
681 cctx_T *cctx,
682 int is_expr,
683 int error)
684{
685 type_T *type;
686 char_u *name = NULL;
687 char_u *end = end_arg;
688 int res = FAIL;
689 int prev_called_emsg = called_emsg;
690
691 if (*(*arg + 1) == ':')
692 {
693 if (end <= *arg + 2)
694 {
695 isntype_T isn_type;
696
697 // load dictionary of namespace
698 switch (**arg)
699 {
700 case 'g': isn_type = ISN_LOADGDICT; break;
701 case 'w': isn_type = ISN_LOADWDICT; break;
702 case 't': isn_type = ISN_LOADTDICT; break;
703 case 'b': isn_type = ISN_LOADBDICT; break;
704 default:
705 semsg(_(e_namespace_not_supported_str), *arg);
706 goto theend;
707 }
708 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
709 goto theend;
710 res = OK;
711 }
712 else
713 {
714 isntype_T isn_type = ISN_DROP;
715
716 // load namespaced variable
717 name = vim_strnsave(*arg + 2, end - (*arg + 2));
718 if (name == NULL)
719 return FAIL;
720
721 switch (**arg)
722 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100723 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000724 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000725 case 's': if (current_script_is_vim9())
726 {
727 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
728 *arg);
729 vim_free(name);
730 return FAIL;
731 }
Kota Kato948a3892022-08-16 16:09:59 +0100732 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000733 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734 else
735 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100736 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737 break;
738 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
739 {
740 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000741 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000742 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000743 else
744 isn_type = ISN_LOADG;
745 }
746 else
747 {
748 isn_type = ISN_LOADAUTO;
749 vim_free(name);
750 name = vim_strnsave(*arg, end - *arg);
751 if (name == NULL)
752 return FAIL;
753 }
754 break;
755 case 'w': isn_type = ISN_LOADW; break;
756 case 't': isn_type = ISN_LOADT; break;
757 case 'b': isn_type = ISN_LOADB; break;
758 default: // cannot happen, just in case
759 semsg(_(e_namespace_not_supported_str), *arg);
760 goto theend;
761 }
762 if (isn_type != ISN_DROP)
763 {
764 // Global, Buffer-local, Window-local and Tabpage-local
765 // variables can be defined later, thus we don't check if it
766 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000767 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768 }
769 }
770 }
771 else
772 {
773 size_t len = end - *arg;
774 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200775 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776 int gen_load = FALSE;
777 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100778 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100779 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780
781 name = vim_strnsave(*arg, end - *arg);
782 if (name == NULL)
783 return FAIL;
784
Bram Moolenaar58b40092023-01-11 15:59:05 +0000785 if (STRCMP(name, "super") == 0
786 && cctx->ctx_ufunc != NULL
787 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
788 {
789 // super.SomeFunc() in a class function: push &t_super type, this
790 // is recognized in compile_subscript().
791 res = push_type_stack(cctx, &t_super);
792 if (*end != '.')
793 emsg(_(e_super_must_be_followed_by_dot));
794 }
795 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000796 {
797 script_autoload(name, FALSE);
798 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
799 }
800 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
801 == OK)
802 {
803 if (gen_load_outer == 0)
804 gen_load = TRUE;
805 }
806 else
807 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000808 lvar_T lvar;
809 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000810
811 if (lookup_local(*arg, len, &lvar, cctx) == OK)
812 {
813 type = lvar.lv_type;
814 idx = lvar.lv_idx;
815 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100816 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100818 outer_loop_depth = lvar.lv_loop_depth;
819 outer_loop_idx = lvar.lv_loop_idx;
820 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 else
822 gen_load = TRUE;
823 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200824 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
825 (((idx =
826 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
827 || ((method_idx =
828 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000829 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200830 // Referencing a class variable or method without the class
831 // name. A class variable or method can be referenced without
832 // the class name only in the class where the function is
833 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200834 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200835 {
836 if (idx >= 0)
837 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
838 else
839 {
840 ufunc_T *fp = cl->class_class_functions[method_idx];
841 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
842 NULL);
843 }
844 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200845 else
846 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200847 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200848 name, cl->class_name);
849 res = FAIL;
850 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000851 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852 else
853 {
854 // "var" can be script-local even without using "s:" if it
855 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000856 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000857 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100858 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000859
860 // When evaluating an expression and the name starts with an
861 // uppercase letter it can be a user defined function.
862 // generate_funcref() will fail if the function can't be found.
863 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000864 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 }
866 }
867 if (gen_load)
868 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
869 if (gen_load_outer > 0)
870 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100871 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
872 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 cctx->ctx_outer_used = TRUE;
874 }
875 }
876
877 *arg = end;
878
879theend:
880 if (res == FAIL && error && called_emsg == prev_called_emsg)
881 semsg(_(e_variable_not_found_str), name);
882 vim_free(name);
883 return res;
884}
885
886/*
887 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100888 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 * Returns FAIL if compilation fails.
890 */
891 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100892compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893{
LemonBoyf3b48952022-05-05 13:53:03 +0100894 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 garray_T save_ga = cctx->ctx_instr;
896 int expr_res;
897 int trailing_error;
898 int instr_count;
899 isn_T *instr = NULL;
900
901 // Remove the string type from the stack.
902 --cctx->ctx_type_stack.ga_len;
903
904 // Temporarily reset the list of instructions so that the jump labels are
905 // correct.
906 cctx->ctx_instr.ga_len = 0;
907 cctx->ctx_instr.ga_maxlen = 0;
908 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000909
910 // avoid peeking a next line
911 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
912 cctx->ctx_ufunc->uf_lines.ga_len = 0;
913
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000914 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000915
916 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
917
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 s = skipwhite(s);
919 trailing_error = *s != NUL;
920
921 if (expr_res == FAIL || trailing_error
922 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
923 {
924 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000925 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926 clear_instr_ga(&cctx->ctx_instr);
927 cctx->ctx_instr = save_ga;
928 ++cctx->ctx_type_stack.ga_len;
929 return FAIL;
930 }
931
932 // Move the generated instructions into the ISN_INSTR instruction, then
933 // restore the list of instructions.
934 instr_count = cctx->ctx_instr.ga_len;
935 instr = cctx->ctx_instr.ga_data;
936 instr[instr_count].isn_type = ISN_FINISH;
937
938 cctx->ctx_instr = save_ga;
939 vim_free(isn->isn_arg.string);
940 isn->isn_type = ISN_INSTR;
941 isn->isn_arg.instr = instr;
942 return OK;
943}
944
945/*
946 * Compile the argument expressions.
947 * "arg" points to just after the "(" and is advanced to after the ")"
948 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100949 int
LemonBoyf3b48952022-05-05 13:53:03 +0100950compile_arguments(
951 char_u **arg,
952 cctx_T *cctx,
953 int *argcount,
954 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000955{
956 char_u *p = *arg;
957 char_u *whitep = *arg;
958 int must_end = FALSE;
959 int instr_count;
960
961 for (;;)
962 {
963 if (may_get_next_line(whitep, &p, cctx) == FAIL)
964 goto failret;
965 if (*p == ')')
966 {
967 *arg = p + 1;
968 return OK;
969 }
970 if (must_end)
971 {
972 semsg(_(e_missing_comma_before_argument_str), p);
973 return FAIL;
974 }
975
976 instr_count = cctx->ctx_instr.ga_len;
977 if (compile_expr0(&p, cctx) == FAIL)
978 return FAIL;
979 ++*argcount;
980
LemonBoyf3b48952022-05-05 13:53:03 +0100981 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000982 && cctx->ctx_instr.ga_len == instr_count + 1)
983 {
984 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
985
986 // {skip} argument of searchpair() can be compiled if not empty
987 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100988 compile_string(isn, cctx, 0);
989 }
990 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
991 && cctx->ctx_instr.ga_len == instr_count + 1)
992 {
993 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
994
995 // {sub} argument of substitute() can be compiled if it starts
996 // with \=
997 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100998 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100999 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001000 }
1001
1002 if (*p != ',' && *skipwhite(p) == ',')
1003 {
1004 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1005 p = skipwhite(p);
1006 }
1007 if (*p == ',')
1008 {
1009 ++p;
1010 if (*p != NUL && !VIM_ISWHITE(*p))
1011 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1012 }
1013 else
1014 must_end = TRUE;
1015 whitep = p;
1016 p = skipwhite(p);
1017 }
1018failret:
1019 emsg(_(e_missing_closing_paren));
1020 return FAIL;
1021}
1022
1023/*
1024 * Compile a function call: name(arg1, arg2)
1025 * "arg" points to "name", "arg + varlen" to the "(".
1026 * "argcount_init" is 1 for "value->method()"
1027 * Instructions:
1028 * EVAL arg1
1029 * EVAL arg2
1030 * BCALL / DCALL / UCALL
1031 */
1032 static int
1033compile_call(
1034 char_u **arg,
1035 size_t varlen,
1036 cctx_T *cctx,
1037 ppconst_T *ppconst,
1038 int argcount_init)
1039{
1040 char_u *name = *arg;
1041 char_u *p;
1042 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001043 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 char_u fname_buf[FLEN_FIXED + 1];
1045 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001046 ufunc_T *ufunc = NULL;
1047 int res = FAIL;
1048 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001049 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001050 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001051 imported_T *import;
1052
1053 if (varlen >= sizeof(namebuf))
1054 {
1055 semsg(_(e_name_too_long_str), name);
1056 return FAIL;
1057 }
1058 vim_strncpy(namebuf, *arg, varlen);
1059
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001060 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001061 if (import != NULL)
1062 {
1063 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1064 return FAIL;
1065 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001066
1067 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001068 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001069 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001070 if ((varlen == 3
1071 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001072 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1073 {
1074 char_u *s = skipwhite(*arg + varlen + 1);
1075 typval_T argvars[2];
1076 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001077 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001078
1079 argvars[0].v_type = VAR_UNKNOWN;
1080 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001081 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001083 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084 s = skipwhite(s);
1085 if (*s == ')' && argvars[0].v_type == VAR_STRING
1086 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1087 || !is_has))
1088 {
1089 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1090
1091 *arg = s + 1;
1092 argvars[1].v_type = VAR_UNKNOWN;
1093 tv->v_type = VAR_NUMBER;
1094 tv->vval.v_number = 0;
1095 if (is_has)
1096 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001097 else if (is_len)
1098 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 else
1100 f_exists(argvars, tv);
1101 clear_tv(&argvars[0]);
1102 ++ppconst->pp_used;
1103 return OK;
1104 }
1105 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001106 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107 {
1108 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1109 return FAIL;
1110 }
1111 }
1112
1113 if (generate_ppconst(cctx, ppconst) == FAIL)
1114 return FAIL;
1115
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001116 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1118
1119 // We handle the "skip" argument of searchpair() and searchpairpos()
1120 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001121 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1122 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1123 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1124 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1125 special_fn = CA_SEARCHPAIR;
1126 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1127 special_fn = CA_SUBSTITUTE;
1128 else
1129 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130
1131 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001132 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001133 goto theend;
1134
1135 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1136 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1137 {
1138 int idx;
1139
1140 // builtin function
1141 idx = find_internal_func(name);
1142 if (idx >= 0)
1143 {
1144 if (STRCMP(name, "flatten") == 0)
1145 {
1146 emsg(_(e_cannot_use_flatten_in_vim9_script));
1147 goto theend;
1148 }
1149
1150 if (STRCMP(name, "add") == 0 && argcount == 2)
1151 {
h-eastb895b0f2023-09-24 15:46:31 +02001152 type_T *type = get_decl_type_on_stack(cctx, 1);
1153
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154 // add() can be compiled to instructions if we know the type
1155 if (type->tt_type == VAR_LIST)
1156 {
1157 // inline "add(list, item)" so that the type can be checked
1158 res = generate_LISTAPPEND(cctx);
1159 idx = -1;
1160 }
1161 else if (type->tt_type == VAR_BLOB)
1162 {
1163 // inline "add(blob, nr)" so that the type can be checked
1164 res = generate_BLOBAPPEND(cctx);
1165 idx = -1;
1166 }
1167 }
1168
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001169 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1170 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001171 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001172 // May have the "D" or "R" flag, reserve a variable for a
1173 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001174 if (get_defer_var_idx(cctx) == 0)
1175 idx = -1;
1176 }
1177
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 if (idx >= 0)
1179 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1180 }
1181 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001182 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183 goto theend;
1184 }
1185
Bram Moolenaar62aec932022-01-29 21:45:34 +00001186 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1187
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 // An argument or local variable can be a function reference, this
1189 // overrules a function name.
1190 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1191 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1192 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001193 class_T *cl = NULL;
1194 int mi = 0;
1195
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001196 // If we can find the function by name generate the right call.
1197 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001198 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001199 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001201 if (!func_is_global(ufunc))
1202 {
h-eastb895b0f2023-09-24 15:46:31 +02001203 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001204 goto theend;
1205 }
1206 if (!has_g_namespace
1207 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1208 {
1209 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001210 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001211 goto theend;
1212 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001213 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001214 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001215 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001216 // Class method invocation without the class name.
1217 // A class method can be referenced without the class name only in
1218 // the class where the function is defined.
1219 if (cctx->ctx_ufunc->uf_defclass == cl)
1220 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001221 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001222 0, argcount);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001223 }
1224 else
1225 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001226 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001227 name, cl->class_name);
1228 res = FAIL;
1229 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001230 goto theend;
1231 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001232 }
1233
1234 // If the name is a variable, load it and use PCALL.
1235 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001236 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001237 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001238 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001239 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1240 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001241 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001242
Christian Brabandtd5475e82023-08-17 23:34:09 +02001243 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244 goto theend;
1245 }
1246
1247 // If we can find a global function by name generate the right call.
1248 if (ufunc != NULL)
1249 {
h-eastb895b0f2023-09-24 15:46:31 +02001250 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 goto theend;
1252 }
1253
1254 // A global function may be defined only later. Need to figure out at
1255 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001256 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001257 res = generate_UCALL(cctx, name, argcount);
1258 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001259 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001260
1261theend:
1262 vim_free(tofree);
1263 return res;
1264}
1265
1266// like NAMESPACE_CHAR but with 'a' and 'l'.
1267#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1268
1269/*
1270 * Find the end of a variable or function name. Unlike find_name_end() this
1271 * does not recognize magic braces.
1272 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1273 * Return a pointer to just after the name. Equal to "arg" if there is no
1274 * valid name.
1275 */
1276 char_u *
1277to_name_end(char_u *arg, int use_namespace)
1278{
1279 char_u *p;
1280
1281 // Quick check for valid starting character.
1282 if (!eval_isnamec1(*arg))
1283 return arg;
1284
1285 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1286 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1287 // and can be used in slice "[n:]".
1288 if (*p == ':' && (p != arg + 1
1289 || !use_namespace
1290 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1291 break;
1292 return p;
1293}
1294
1295/*
1296 * Like to_name_end() but also skip over a list or dict constant.
1297 * Also accept "<SNR>123_Func".
1298 * This intentionally does not handle line continuation.
1299 */
1300 char_u *
1301to_name_const_end(char_u *arg)
1302{
1303 char_u *p = arg;
1304 typval_T rettv;
1305
1306 if (STRNCMP(p, "<SNR>", 5) == 0)
1307 p = skipdigits(p + 5);
1308 p = to_name_end(p, TRUE);
1309 if (p == arg && *arg == '[')
1310 {
1311
1312 // Can be "[1, 2, 3]->Func()".
1313 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1314 p = arg;
1315 }
1316 return p;
1317}
1318
1319/*
1320 * parse a list: [expr, expr]
1321 * "*arg" points to the '['.
1322 * ppconst->pp_is_const is set if all items are a constant.
1323 */
1324 static int
1325compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1326{
1327 char_u *p = skipwhite(*arg + 1);
1328 char_u *whitep = *arg + 1;
1329 int count = 0;
1330 int is_const;
1331 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001332 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333
1334 for (;;)
1335 {
1336 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1337 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001338 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001339 return FAIL;
1340 }
1341 if (*p == ',')
1342 {
1343 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1344 return FAIL;
1345 }
1346 if (*p == ']')
1347 {
1348 ++p;
1349 break;
1350 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001351 if (must_end)
1352 {
1353 semsg(_(e_missing_comma_in_list_str), p);
1354 return FAIL;
1355 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1357 return FAIL;
1358 if (!is_const)
1359 is_all_const = FALSE;
1360 ++count;
1361 if (*p == ',')
1362 {
1363 ++p;
1364 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1365 {
1366 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1367 return FAIL;
1368 }
1369 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001370 else
1371 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372 whitep = p;
1373 p = skipwhite(p);
1374 }
1375 *arg = p;
1376
1377 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001378 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379}
1380
1381/*
1382 * Parse a lambda: "(arg, arg) => expr"
1383 * "*arg" points to the '('.
1384 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1385 */
1386 static int
1387compile_lambda(char_u **arg, cctx_T *cctx)
1388{
1389 int r;
1390 typval_T rettv;
1391 ufunc_T *ufunc;
1392 evalarg_T evalarg;
1393
1394 init_evalarg(&evalarg);
1395 evalarg.eval_flags = EVAL_EVALUATE;
1396 evalarg.eval_cctx = cctx;
1397
1398 // Get the funcref in "rettv".
1399 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1400 if (r != OK)
1401 {
1402 clear_evalarg(&evalarg, NULL);
1403 return r;
1404 }
1405
1406 // "rettv" will now be a partial referencing the function.
1407 ufunc = rettv.vval.v_partial->pt_func;
1408 ++ufunc->uf_refcount;
1409 clear_tv(&rettv);
1410
1411 // Compile it here to get the return type. The return type is optional,
1412 // when it's missing use t_unknown. This is recognized in
1413 // compile_return().
1414 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1415 ufunc->uf_ret_type = &t_unknown;
1416 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1417
1418 // When the outer function is compiled for profiling or debugging, the
1419 // lambda may be called without profiling or debugging. Compile it here in
1420 // the right context.
1421 if (cctx->ctx_compile_type == CT_DEBUG
1422#ifdef FEAT_PROFILE
1423 || cctx->ctx_compile_type == CT_PROFILE
1424#endif
1425 )
1426 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1427
Bram Moolenaar139575d2022-03-15 19:29:30 +00001428 // if the outer function is not compiled for debugging or profiling, this
1429 // one might be
1430 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001431 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001432 compiletype_T compile_type = get_compile_type(ufunc);
1433
1434 if (compile_type != CT_NONE)
1435 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001436 }
1437
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001438 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1439 // "*arg" may point into it. Point into the original line to avoid a
1440 // dangling pointer.
1441 if (evalarg.eval_using_cmdline)
1442 {
1443 garray_T *gap = &evalarg.eval_tofree_ga;
1444 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1445
1446 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1447 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001448 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001449 }
1450
1451 clear_evalarg(&evalarg, NULL);
1452
1453 if (ufunc->uf_def_status == UF_COMPILED)
1454 {
1455 // The return type will now be known.
1456 set_function_type(ufunc);
1457
1458 // The function reference count will be 1. When the ISN_FUNCREF
1459 // instruction is deleted the reference count is decremented and the
1460 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001461 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001462 }
1463
1464 func_ptr_unref(ufunc);
1465 return FAIL;
1466}
1467
1468/*
1469 * Get a lambda and compile it. Uses Vim9 syntax.
1470 */
1471 int
1472get_lambda_tv_and_compile(
1473 char_u **arg,
1474 typval_T *rettv,
1475 int types_optional,
1476 evalarg_T *evalarg)
1477{
1478 int r;
1479 ufunc_T *ufunc;
1480 int save_sc_version = current_sctx.sc_version;
1481
1482 // Get the funcref in "rettv".
1483 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1484 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1485 current_sctx.sc_version = save_sc_version;
1486 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001487 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001488
1489 // "rettv" will now be a partial referencing the function.
1490 ufunc = rettv->vval.v_partial->pt_func;
1491
1492 // Compile it here to get the return type. The return type is optional,
1493 // when it's missing use t_unknown. This is recognized in
1494 // compile_return().
1495 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1496 ufunc->uf_ret_type = &t_unknown;
1497 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1498
1499 if (ufunc->uf_def_status == UF_COMPILED)
1500 {
1501 // The return type will now be known.
1502 set_function_type(ufunc);
1503 return OK;
1504 }
1505 clear_tv(rettv);
1506 return FAIL;
1507}
1508
1509/*
1510 * parse a dict: {key: val, [key]: val}
1511 * "*arg" points to the '{'.
1512 * ppconst->pp_is_const is set if all item values are a constant.
1513 */
1514 static int
1515compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1516{
1517 garray_T *instr = &cctx->ctx_instr;
1518 int count = 0;
1519 dict_T *d = dict_alloc();
1520 dictitem_T *item;
1521 char_u *whitep = *arg + 1;
1522 char_u *p;
1523 int is_const;
1524 int is_all_const = TRUE; // reset when non-const encountered
1525
1526 if (d == NULL)
1527 return FAIL;
1528 if (generate_ppconst(cctx, ppconst) == FAIL)
1529 return FAIL;
1530 for (;;)
1531 {
1532 char_u *key = NULL;
1533
1534 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1535 {
1536 *arg = NULL;
1537 goto failret;
1538 }
1539
1540 if (**arg == '}')
1541 break;
1542
1543 if (**arg == '[')
1544 {
1545 isn_T *isn;
1546
1547 // {[expr]: value} uses an evaluated key.
1548 *arg = skipwhite(*arg + 1);
1549 if (compile_expr0(arg, cctx) == FAIL)
1550 return FAIL;
1551 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1552 if (isn->isn_type == ISN_PUSHNR)
1553 {
1554 char buf[NUMBUFLEN];
1555
1556 // Convert to string at compile time.
1557 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1558 isn->isn_type = ISN_PUSHS;
1559 isn->isn_arg.string = vim_strsave((char_u *)buf);
1560 }
1561 if (isn->isn_type == ISN_PUSHS)
1562 key = isn->isn_arg.string;
1563 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1564 return FAIL;
1565 *arg = skipwhite(*arg);
1566 if (**arg != ']')
1567 {
1568 emsg(_(e_missing_matching_bracket_after_dict_key));
1569 return FAIL;
1570 }
1571 ++*arg;
1572 }
1573 else
1574 {
1575 // {"name": value},
1576 // {'name': value},
1577 // {name: value} use "name" as a literal key
1578 key = get_literal_key(arg);
1579 if (key == NULL)
1580 return FAIL;
1581 if (generate_PUSHS(cctx, &key) == FAIL)
1582 return FAIL;
1583 }
1584
1585 // Check for duplicate keys, if using string keys.
1586 if (key != NULL)
1587 {
1588 item = dict_find(d, key, -1);
1589 if (item != NULL)
1590 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001591 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001592 goto failret;
1593 }
1594 item = dictitem_alloc(key);
1595 if (item != NULL)
1596 {
1597 item->di_tv.v_type = VAR_UNKNOWN;
1598 item->di_tv.v_lock = 0;
1599 if (dict_add(d, item) == FAIL)
1600 dictitem_free(item);
1601 }
1602 }
1603
1604 if (**arg != ':')
1605 {
1606 if (*skipwhite(*arg) == ':')
1607 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1608 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001609 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001610 return FAIL;
1611 }
1612 whitep = *arg + 1;
1613 if (!IS_WHITE_OR_NUL(*whitep))
1614 {
1615 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1616 return FAIL;
1617 }
1618
1619 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1620 {
1621 *arg = NULL;
1622 goto failret;
1623 }
1624
1625 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1626 return FAIL;
1627 if (!is_const)
1628 is_all_const = FALSE;
1629 ++count;
1630
1631 whitep = *arg;
1632 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1633 {
1634 *arg = NULL;
1635 goto failret;
1636 }
1637 if (**arg == '}')
1638 break;
1639 if (**arg != ',')
1640 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001641 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001642 goto failret;
1643 }
1644 if (IS_WHITE_OR_NUL(*whitep))
1645 {
1646 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1647 return FAIL;
1648 }
1649 whitep = *arg + 1;
1650 if (!IS_WHITE_OR_NUL(*whitep))
1651 {
1652 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1653 return FAIL;
1654 }
1655 *arg = skipwhite(whitep);
1656 }
1657
1658 *arg = *arg + 1;
1659
1660 // Allow for following comment, after at least one space.
1661 p = skipwhite(*arg);
1662 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1663 *arg += STRLEN(*arg);
1664
1665 dict_unref(d);
1666 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001667 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001668
1669failret:
1670 if (*arg == NULL)
1671 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001672 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001673 *arg = (char_u *)"";
1674 }
1675 dict_unref(d);
1676 return FAIL;
1677}
1678
1679/*
1680 * Compile "&option".
1681 */
1682 static int
1683compile_get_option(char_u **arg, cctx_T *cctx)
1684{
1685 typval_T rettv;
1686 char_u *start = *arg;
1687 int ret;
1688
1689 // parse the option and get the current value to get the type.
1690 rettv.v_type = VAR_UNKNOWN;
1691 ret = eval_option(arg, &rettv, TRUE);
1692 if (ret == OK)
1693 {
1694 // include the '&' in the name, eval_option() expects it.
1695 char_u *name = vim_strnsave(start, *arg - start);
1696 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1697 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1698
1699 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1700 vim_free(name);
1701 }
1702 clear_tv(&rettv);
1703
1704 return ret;
1705}
1706
1707/*
1708 * Compile "$VAR".
1709 */
1710 static int
1711compile_get_env(char_u **arg, cctx_T *cctx)
1712{
1713 char_u *start = *arg;
1714 int len;
1715 int ret;
1716 char_u *name;
1717
1718 ++*arg;
1719 len = get_env_len(arg);
1720 if (len == 0)
1721 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001722 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001723 return FAIL;
1724 }
1725
1726 // include the '$' in the name, eval_env_var() expects it.
1727 name = vim_strnsave(start, len + 1);
1728 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1729 vim_free(name);
1730 return ret;
1731}
1732
1733/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001734 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001735 */
1736 static int
1737compile_interp_string(char_u **arg, cctx_T *cctx)
1738{
1739 typval_T tv;
1740 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001741 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001742 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001743 int count = 0;
1744 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001745
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001746 // *arg is on the '$' character, move it to the first string character.
1747 ++*arg;
1748 quote = **arg;
1749 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001750
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001751 for (;;)
1752 {
1753 // Get the string up to the matching quote or to a single '{'.
1754 // "arg" is advanced to either the quote or the '{'.
1755 if (quote == '"')
1756 ret = eval_string(arg, &tv, evaluate, TRUE);
1757 else
1758 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1759 if (ret == FAIL)
1760 break;
1761 if (evaluate)
1762 {
1763 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1764 || (**arg != '{' && count == 0))
1765 {
1766 // generate non-empty string or empty string if it's the only
1767 // one
1768 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1769 return FAIL;
1770 tv.vval.v_string = NULL; // don't free it now
1771 ++count;
1772 }
1773 clear_tv(&tv);
1774 }
1775
1776 if (**arg != '{')
1777 {
1778 // found terminating quote
1779 ++*arg;
1780 break;
1781 }
1782
1783 p = compile_one_expr_in_str(*arg, cctx);
1784 if (p == NULL)
1785 {
1786 ret = FAIL;
1787 break;
1788 }
1789 ++count;
1790 *arg = p;
1791 }
LemonBoy2eaef102022-05-06 13:14:50 +01001792
1793 if (ret == FAIL || !evaluate)
1794 return ret;
1795
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001796 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1797 if (count > 1)
1798 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001799
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001800 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001801}
1802
1803/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804 * Compile "@r".
1805 */
1806 static int
1807compile_get_register(char_u **arg, cctx_T *cctx)
1808{
1809 int ret;
1810
1811 ++*arg;
1812 if (**arg == NUL)
1813 {
1814 semsg(_(e_syntax_error_at_str), *arg - 1);
1815 return FAIL;
1816 }
1817 if (!valid_yank_reg(**arg, FALSE))
1818 {
1819 emsg_invreg(**arg);
1820 return FAIL;
1821 }
1822 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1823 ++*arg;
1824 return ret;
1825}
1826
1827/*
1828 * Apply leading '!', '-' and '+' to constant "rettv".
1829 * When "numeric_only" is TRUE do not apply '!'.
1830 */
1831 static int
1832apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1833{
1834 char_u *p = *end;
1835
1836 // this works from end to start
1837 while (p > start)
1838 {
1839 --p;
1840 if (*p == '-' || *p == '+')
1841 {
1842 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 if (rettv->v_type == VAR_FLOAT)
1844 {
1845 if (*p == '-')
1846 rettv->vval.v_float = -rettv->vval.v_float;
1847 }
1848 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 {
1850 varnumber_T val;
1851 int error = FALSE;
1852
1853 // tv_get_number_chk() accepts a string, but we don't want that
1854 // here
1855 if (check_not_string(rettv) == FAIL)
1856 return FAIL;
1857 val = tv_get_number_chk(rettv, &error);
1858 clear_tv(rettv);
1859 if (error)
1860 return FAIL;
1861 if (*p == '-')
1862 val = -val;
1863 rettv->v_type = VAR_NUMBER;
1864 rettv->vval.v_number = val;
1865 }
1866 }
1867 else if (numeric_only)
1868 {
1869 ++p;
1870 break;
1871 }
1872 else if (*p == '!')
1873 {
1874 int v = tv2bool(rettv);
1875
1876 // '!' is permissive in the type.
1877 clear_tv(rettv);
1878 rettv->v_type = VAR_BOOL;
1879 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1880 }
1881 }
1882 *end = p;
1883 return OK;
1884}
1885
1886/*
1887 * Recognize v: variables that are constants and set "rettv".
1888 */
1889 static void
1890get_vim_constant(char_u **arg, typval_T *rettv)
1891{
1892 if (STRNCMP(*arg, "v:true", 6) == 0)
1893 {
1894 rettv->v_type = VAR_BOOL;
1895 rettv->vval.v_number = VVAL_TRUE;
1896 *arg += 6;
1897 }
1898 else if (STRNCMP(*arg, "v:false", 7) == 0)
1899 {
1900 rettv->v_type = VAR_BOOL;
1901 rettv->vval.v_number = VVAL_FALSE;
1902 *arg += 7;
1903 }
1904 else if (STRNCMP(*arg, "v:null", 6) == 0)
1905 {
1906 rettv->v_type = VAR_SPECIAL;
1907 rettv->vval.v_number = VVAL_NULL;
1908 *arg += 6;
1909 }
1910 else if (STRNCMP(*arg, "v:none", 6) == 0)
1911 {
1912 rettv->v_type = VAR_SPECIAL;
1913 rettv->vval.v_number = VVAL_NONE;
1914 *arg += 6;
1915 }
1916}
1917
1918 exprtype_T
1919get_compare_type(char_u *p, int *len, int *type_is)
1920{
1921 exprtype_T type = EXPR_UNKNOWN;
1922 int i;
1923
1924 switch (p[0])
1925 {
1926 case '=': if (p[1] == '=')
1927 type = EXPR_EQUAL;
1928 else if (p[1] == '~')
1929 type = EXPR_MATCH;
1930 break;
1931 case '!': if (p[1] == '=')
1932 type = EXPR_NEQUAL;
1933 else if (p[1] == '~')
1934 type = EXPR_NOMATCH;
1935 break;
1936 case '>': if (p[1] != '=')
1937 {
1938 type = EXPR_GREATER;
1939 *len = 1;
1940 }
1941 else
1942 type = EXPR_GEQUAL;
1943 break;
1944 case '<': if (p[1] != '=')
1945 {
1946 type = EXPR_SMALLER;
1947 *len = 1;
1948 }
1949 else
1950 type = EXPR_SEQUAL;
1951 break;
1952 case 'i': if (p[1] == 's')
1953 {
1954 // "is" and "isnot"; but not a prefix of a name
1955 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1956 *len = 5;
1957 i = p[*len];
1958 if (!isalnum(i) && i != '_')
1959 {
1960 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1961 *type_is = TRUE;
1962 }
1963 }
1964 break;
1965 }
1966 return type;
1967}
1968
1969/*
1970 * Skip over an expression, ignoring most errors.
1971 */
1972 void
1973skip_expr_cctx(char_u **arg, cctx_T *cctx)
1974{
1975 evalarg_T evalarg;
1976
1977 init_evalarg(&evalarg);
1978 evalarg.eval_cctx = cctx;
1979 skip_expr(arg, &evalarg);
1980 clear_evalarg(&evalarg, NULL);
1981}
1982
1983/*
1984 * Check that the top of the type stack has a type that can be used as a
1985 * condition. Give an error and return FAIL if not.
1986 */
1987 int
1988bool_on_stack(cctx_T *cctx)
1989{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990 type_T *type;
1991
Bram Moolenaar078a4612022-01-04 15:17:03 +00001992 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001993 if (type == &t_bool)
1994 return OK;
1995
Bram Moolenaar4913d422022-10-17 13:13:32 +01001996 if (type->tt_type == VAR_ANY
1997 || type->tt_type == VAR_UNKNOWN
1998 || type->tt_type == VAR_NUMBER
1999 || type == &t_number_bool
2000 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002001 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
2002 // This requires a runtime type check.
2003 return generate_COND2BOOL(cctx);
2004
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002005 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002006}
2007
2008/*
2009 * Give the "white on both sides" error, taking the operator from "p[len]".
2010 */
2011 void
2012error_white_both(char_u *op, int len)
2013{
2014 char_u buf[10];
2015
2016 vim_strncpy(buf, op, len);
2017 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2018}
2019
2020/*
2021 * Compile code to apply '-', '+' and '!'.
2022 * When "numeric_only" is TRUE do not apply '!'.
2023 */
2024 static int
2025compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2026{
2027 char_u *p = *end;
2028
2029 // this works from end to start
2030 while (p > start)
2031 {
2032 --p;
2033 while (VIM_ISWHITE(*p))
2034 --p;
2035 if (*p == '-' || *p == '+')
2036 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002037 type_T *type = get_type_on_stack(cctx, 0);
2038 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002039 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 return FAIL;
2041
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002042 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002043 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2044 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002045 }
2046 else if (numeric_only)
2047 {
2048 ++p;
2049 break;
2050 }
2051 else
2052 {
2053 int invert = *p == '!';
2054
2055 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2056 {
2057 if (p[-1] == '!')
2058 invert = !invert;
2059 --p;
2060 }
2061 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2062 return FAIL;
2063 }
2064 }
2065 *end = p;
2066 return OK;
2067}
2068
2069/*
2070 * Compile "(expression)": recursive!
2071 * Return FAIL/OK.
2072 */
2073 static int
2074compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2075{
2076 int ret;
2077 char_u *p = *arg + 1;
2078
2079 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2080 return FAIL;
2081 if (ppconst->pp_used <= PPSIZE - 10)
2082 {
2083 ret = compile_expr1(arg, cctx, ppconst);
2084 }
2085 else
2086 {
2087 // Not enough space in ppconst, flush constants.
2088 if (generate_ppconst(cctx, ppconst) == FAIL)
2089 return FAIL;
2090 ret = compile_expr0(arg, cctx);
2091 }
2092 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2093 return FAIL;
2094 if (**arg == ')')
2095 ++*arg;
2096 else if (ret == OK)
2097 {
2098 emsg(_(e_missing_closing_paren));
2099 ret = FAIL;
2100 }
2101 return ret;
2102}
2103
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002104static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002105
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106/*
2107 * Compile whatever comes after "name" or "name()".
2108 * Advances "*arg" only when something was recognized.
2109 */
2110 static int
2111compile_subscript(
2112 char_u **arg,
2113 cctx_T *cctx,
2114 char_u *start_leader,
2115 char_u **end_leader,
2116 ppconst_T *ppconst)
2117{
2118 char_u *name_start = *end_leader;
2119 int keeping_dict = FALSE;
2120
2121 for (;;)
2122 {
2123 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002124 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125
2126 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2127 {
2128 char_u *next = peek_next_line_from_context(cctx);
2129
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002130 // If a following line starts with "->{", "->(" or "->X" advance to
2131 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132 // Also if a following line starts with ".x".
2133 if (next != NULL &&
2134 ((next[0] == '-' && next[1] == '>'
2135 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002136 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 || ASCII_ISALPHA(*skipwhite(next + 2))))
2138 || (next[0] == '.' && eval_isdictc(next[1]))))
2139 {
2140 next = next_line_from_context(cctx, TRUE);
2141 if (next == NULL)
2142 return FAIL;
2143 *arg = next;
2144 p = skipwhite(*arg);
2145 }
2146 }
2147
2148 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2149 // is not a function call.
2150 if (**arg == '(')
2151 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152 int argcount = 0;
2153
2154 if (generate_ppconst(cctx, ppconst) == FAIL)
2155 return FAIL;
2156 ppconst->pp_is_const = FALSE;
2157
2158 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002159 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002160
2161 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002162 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002163 return FAIL;
2164 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2165 return FAIL;
2166 if (keeping_dict)
2167 {
2168 keeping_dict = FALSE;
2169 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2170 return FAIL;
2171 }
2172 }
2173 else if (*p == '-' && p[1] == '>')
2174 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002175 char_u *pstart = p;
2176 int alt;
2177 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178
Bram Moolenaarc7349932022-01-16 20:59:39 +00002179 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002180 if (generate_ppconst(cctx, ppconst) == FAIL)
2181 return FAIL;
2182 ppconst->pp_is_const = FALSE;
2183
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002184 // Apply the '!', '-' and '+' first:
2185 // -1.0->func() works like (-1.0)->func()
2186 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2187 return FAIL;
2188
2189 p += 2;
2190 *arg = skipwhite(p);
2191 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002192
2193 // Three alternatives handled here:
2194 // 1. "base->name(" only a name, use compile_call()
2195 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2196 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002197 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002198 alt = 2;
2199 else
2200 {
2201 // alternative 1 or 3
2202 p = *arg;
2203 if (!eval_isnamec1(*p))
2204 {
2205 semsg(_(e_trailing_characters_str), pstart);
2206 return FAIL;
2207 }
2208 if (ASCII_ISALPHA(*p) && p[1] == ':')
2209 p += 2;
2210 for ( ; eval_isnamec(*p); ++p)
2211 ;
2212 if (*p == '(')
2213 {
2214 // alternative 1
2215 alt = 1;
2216 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2217 return FAIL;
2218 }
2219 else
2220 {
2221 // Must be alternative 3, find the "(". Only works within
2222 // one line.
2223 alt = 3;
2224 paren = vim_strchr(p, '(');
2225 if (paren == NULL)
2226 {
2227 semsg(_(e_missing_parenthesis_str), *arg);
2228 return FAIL;
2229 }
2230 }
2231 }
2232
2233 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 {
2235 int argcount = 1;
2236 garray_T *stack = &cctx->ctx_type_stack;
2237 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002238 int expr_isn_start = cctx->ctx_instr.ga_len;
2239 int expr_isn_end;
2240 int arg_isn_count;
2241
Bram Moolenaarc7349932022-01-16 20:59:39 +00002242 if (alt == 2)
2243 {
2244 // Funcref call: list->(Refs[2])(arg)
2245 // or lambda: list->((arg) => expr)(arg)
2246 //
2247 // Fist compile the function expression.
2248 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2249 return FAIL;
2250 }
2251 else
2252 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002253 int fail;
2254 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002255 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002256
Bram Moolenaarc7349932022-01-16 20:59:39 +00002257 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002258
2259 // instead of using LOADG for "import.Func" use PUSHFUNC
2260 ++paren_follows_after_expr;
2261
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002262 // do not look in the next line
2263 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002264
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002265 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002266 || *skipwhite(*arg) != NUL;
2267 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002268 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002269 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002270
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002271 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002272 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002273 if (did_emsg == prev_did_emsg)
2274 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002275 return FAIL;
2276 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002277 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002279 // Compile the arguments.
2280 if (**arg != '(')
2281 {
2282 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002283 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002284 else
2285 semsg(_(e_missing_parenthesis_str), *arg);
2286 return FAIL;
2287 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002288
2289 // Remember the next instruction index, where the instructions
2290 // for arguments are being written.
2291 expr_isn_end = cctx->ctx_instr.ga_len;
2292
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002293 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002294 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2295 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002296 return FAIL;
2297
2298 // Move the instructions for the arguments to before the
2299 // instructions of the expression and move the type of the
2300 // expression after the argument types. This is what ISN_PCALL
2301 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002302 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2303 if (arg_isn_count > 0)
2304 {
2305 int expr_isn_count = expr_isn_end - expr_isn_start;
2306 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002307 type_T *decl_type;
2308 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002309
2310 if (isn == NULL)
2311 return FAIL;
2312 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2313 + expr_isn_start,
2314 sizeof(isn_T) * expr_isn_count);
2315 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2316 + expr_isn_start,
2317 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2318 sizeof(isn_T) * arg_isn_count);
2319 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2320 + expr_isn_start + arg_isn_count,
2321 isn, sizeof(isn_T) * expr_isn_count);
2322 vim_free(isn);
2323
Bram Moolenaar078a4612022-01-04 15:17:03 +00002324 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2325 type = typep->type_curr;
2326 decl_type = typep->type_decl;
2327 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2328 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2329 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002330 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002331 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2332 typep->type_curr = type;
2333 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 }
2335
Bram Moolenaar078a4612022-01-04 15:17:03 +00002336 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2338 return FAIL;
2339 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002340
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002341 if (keeping_dict)
2342 {
2343 keeping_dict = FALSE;
2344 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2345 return FAIL;
2346 }
2347 }
2348 else if (**arg == '[')
2349 {
2350 int is_slice = FALSE;
2351
2352 // list index: list[123]
2353 // dict member: dict[key]
2354 // string index: text[123]
2355 // blob index: blob[123]
2356 if (generate_ppconst(cctx, ppconst) == FAIL)
2357 return FAIL;
2358 ppconst->pp_is_const = FALSE;
2359
2360 ++p;
2361 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2362 return FAIL;
2363 if (**arg == ':')
2364 {
2365 // missing first index is equal to zero
2366 generate_PUSHNR(cctx, 0);
2367 }
2368 else
2369 {
2370 if (compile_expr0(arg, cctx) == FAIL)
2371 return FAIL;
2372 if (**arg == ':')
2373 {
2374 semsg(_(e_white_space_required_before_and_after_str_at_str),
2375 ":", *arg);
2376 return FAIL;
2377 }
2378 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2379 return FAIL;
2380 *arg = skipwhite(*arg);
2381 }
2382 if (**arg == ':')
2383 {
2384 is_slice = TRUE;
2385 ++*arg;
2386 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2387 {
2388 semsg(_(e_white_space_required_before_and_after_str_at_str),
2389 ":", *arg);
2390 return FAIL;
2391 }
2392 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2393 return FAIL;
2394 if (**arg == ']')
2395 // missing second index is equal to end of string
2396 generate_PUSHNR(cctx, -1);
2397 else
2398 {
2399 if (compile_expr0(arg, cctx) == FAIL)
2400 return FAIL;
2401 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2402 return FAIL;
2403 *arg = skipwhite(*arg);
2404 }
2405 }
2406
2407 if (**arg != ']')
2408 {
2409 emsg(_(e_missing_closing_square_brace));
2410 return FAIL;
2411 }
2412 *arg = *arg + 1;
2413
2414 if (keeping_dict)
2415 {
2416 keeping_dict = FALSE;
2417 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2418 return FAIL;
2419 }
2420 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2421 return FAIL;
2422 }
2423 else if (*p == '.' && p[1] != '.')
2424 {
2425 // dictionary member: dict.name
2426 if (generate_ppconst(cctx, ppconst) == FAIL)
2427 return FAIL;
2428 ppconst->pp_is_const = FALSE;
2429
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002430 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2431 && (type->tt_type == VAR_CLASS
2432 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002434 // class member: SomeClass.varname
2435 // class method: SomeClass.SomeMethod()
2436 // class constructor: SomeClass.new()
2437 // object member: someObject.varname, this.varname
2438 // object method: someObject.SomeMethod(), this.SomeMethod()
2439 *arg = p;
2440 if (compile_class_object_index(cctx, arg, type) == FAIL)
2441 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002442 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002443 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002445 *arg = p + 1;
2446 if (IS_WHITE_OR_NUL(**arg))
2447 {
2448 emsg(_(e_missing_name_after_dot));
2449 return FAIL;
2450 }
2451 p = *arg;
2452 if (eval_isdictc(*p))
2453 while (eval_isnamec(*p))
2454 MB_PTR_ADV(p);
2455 if (p == *arg)
2456 {
2457 semsg(_(e_syntax_error_at_str), *arg);
2458 return FAIL;
2459 }
2460 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2461 return FAIL;
2462 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2463 return FAIL;
2464 keeping_dict = TRUE;
2465 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002466 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 }
2468 else
2469 break;
2470 }
2471
2472 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2473 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002474 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2475 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002476 return FAIL;
2477
2478 return OK;
2479}
2480
2481/*
2482 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2483 * "arg" is advanced until after the expression, skipping white space.
2484 *
2485 * If the value is a constant "ppconst->pp_used" will be non-zero.
2486 * Before instructions are generated, any values in "ppconst" will generated.
2487 *
2488 * This is the compiling equivalent of eval1(), eval2(), etc.
2489 */
2490
2491/*
2492 * number number constant
2493 * 0zFFFFFFFF Blob constant
2494 * "string" string constant
2495 * 'string' literal string constant
2496 * &option-name option value
2497 * @r register contents
2498 * identifier variable value
2499 * function() function call
2500 * $VAR environment variable
2501 * (expression) nested expression
2502 * [expr, expr] List
2503 * {key: val, [key]: val} Dictionary
2504 *
2505 * Also handle:
2506 * ! in front logical NOT
2507 * - in front unary minus
2508 * + in front unary plus (ignored)
2509 * trailing (arg) funcref/partial call
2510 * trailing [] subscript in String or List
2511 * trailing .name entry in Dictionary
2512 * trailing ->name() method call
2513 */
2514 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002515compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002516 char_u **arg,
2517 cctx_T *cctx,
2518 ppconst_T *ppconst)
2519{
2520 char_u *start_leader, *end_leader;
2521 int ret = OK;
2522 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2523 int used_before = ppconst->pp_used;
2524
2525 ppconst->pp_is_const = FALSE;
2526
2527 /*
2528 * Skip '!', '-' and '+' characters. They are handled later.
2529 */
2530 start_leader = *arg;
2531 if (eval_leader(arg, TRUE) == FAIL)
2532 return FAIL;
2533 end_leader = *arg;
2534
2535 rettv->v_type = VAR_UNKNOWN;
2536 switch (**arg)
2537 {
2538 /*
2539 * Number constant.
2540 */
2541 case '0': // also for blob starting with 0z
2542 case '1':
2543 case '2':
2544 case '3':
2545 case '4':
2546 case '5':
2547 case '6':
2548 case '7':
2549 case '8':
2550 case '9':
2551 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2552 return FAIL;
2553 // Apply "-" and "+" just before the number now, right to
2554 // left. Matters especially when "->" follows. Stops at
2555 // '!'.
2556 if (apply_leader(rettv, TRUE,
2557 start_leader, &end_leader) == FAIL)
2558 {
2559 clear_tv(rettv);
2560 return FAIL;
2561 }
2562 break;
2563
2564 /*
2565 * String constant: "string".
2566 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002567 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002568 return FAIL;
2569 break;
2570
2571 /*
2572 * Literal string constant: 'str''ing'.
2573 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002574 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002575 return FAIL;
2576 break;
2577
2578 /*
2579 * Constant Vim variable.
2580 */
2581 case 'v': get_vim_constant(arg, rettv);
2582 ret = NOTDONE;
2583 break;
2584
2585 /*
2586 * "true" constant
2587 */
2588 case 't': if (STRNCMP(*arg, "true", 4) == 0
2589 && !eval_isnamec((*arg)[4]))
2590 {
2591 *arg += 4;
2592 rettv->v_type = VAR_BOOL;
2593 rettv->vval.v_number = VVAL_TRUE;
2594 }
2595 else
2596 ret = NOTDONE;
2597 break;
2598
2599 /*
2600 * "false" constant
2601 */
2602 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2603 && !eval_isnamec((*arg)[5]))
2604 {
2605 *arg += 5;
2606 rettv->v_type = VAR_BOOL;
2607 rettv->vval.v_number = VVAL_FALSE;
2608 }
2609 else
2610 ret = NOTDONE;
2611 break;
2612
2613 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002614 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002616 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002617 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002618 char_u *p = *arg + 4;
2619 int len;
2620
2621 for (len = 0; eval_isnamec(p[len]); ++len)
2622 ;
2623 ret = handle_predefined(*arg, len + 4, rettv);
2624 if (ret == FAIL)
2625 ret = NOTDONE;
2626 else
2627 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 }
2629 else
2630 ret = NOTDONE;
2631 break;
2632
2633 /*
2634 * List: [expr, expr]
2635 */
2636 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2637 return FAIL;
2638 ret = compile_list(arg, cctx, ppconst);
2639 break;
2640
2641 /*
2642 * Dictionary: {'key': val, 'key': val}
2643 */
2644 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2645 return FAIL;
2646 ret = compile_dict(arg, cctx, ppconst);
2647 break;
2648
2649 /*
2650 * Option value: &name
2651 */
2652 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2653 return FAIL;
2654 ret = compile_get_option(arg, cctx);
2655 break;
2656
2657 /*
2658 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002659 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002660 */
2661 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2662 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002663 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2664 ret = compile_interp_string(arg, cctx);
2665 else
2666 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002667 break;
2668
2669 /*
2670 * Register contents: @r.
2671 */
2672 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2673 return FAIL;
2674 ret = compile_get_register(arg, cctx);
2675 break;
2676 /*
2677 * nested expression: (expression).
2678 * lambda: (arg, arg) => expr
2679 * funcref: (arg, arg) => { statement }
2680 */
2681 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2682 ret = compile_lambda(arg, cctx);
2683 if (ret == NOTDONE)
2684 ret = compile_parenthesis(arg, cctx, ppconst);
2685 break;
2686
2687 default: ret = NOTDONE;
2688 break;
2689 }
2690 if (ret == FAIL)
2691 return FAIL;
2692
2693 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2694 {
2695 if (cctx->ctx_skip == SKIP_YES)
2696 clear_tv(rettv);
2697 else
2698 // A constant expression can possibly be handled compile time,
2699 // return the value instead of generating code.
2700 ++ppconst->pp_used;
2701 }
2702 else if (ret == NOTDONE)
2703 {
2704 char_u *p;
2705 int r;
2706
2707 if (!eval_isnamec1(**arg))
2708 {
2709 if (!vim9_bad_comment(*arg))
2710 {
2711 if (ends_excmd(*skipwhite(*arg)))
2712 semsg(_(e_empty_expression_str), *arg);
2713 else
2714 semsg(_(e_name_expected_str), *arg);
2715 }
2716 return FAIL;
2717 }
2718
2719 // "name" or "name()"
2720 p = to_name_end(*arg, TRUE);
2721 if (p - *arg == (size_t)1 && **arg == '_')
2722 {
2723 emsg(_(e_cannot_use_underscore_here));
2724 return FAIL;
2725 }
2726
2727 if (*p == '(')
2728 {
2729 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2730 }
2731 else
2732 {
2733 if (cctx->ctx_skip != SKIP_YES
2734 && generate_ppconst(cctx, ppconst) == FAIL)
2735 return FAIL;
2736 r = compile_load(arg, p, cctx, TRUE, TRUE);
2737 }
2738 if (r == FAIL)
2739 return FAIL;
2740 }
2741
2742 // Handle following "[]", ".member", etc.
2743 // Then deal with prefixed '-', '+' and '!', if not done already.
2744 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2745 ppconst) == FAIL)
2746 return FAIL;
2747 if (ppconst->pp_used > 0)
2748 {
2749 // apply the '!', '-' and '+' before the constant
2750 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2751 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2752 return FAIL;
2753 return OK;
2754 }
2755 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2756 return FAIL;
2757 return OK;
2758}
2759
2760/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002761 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002762 */
2763 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002764compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002765{
2766 type_T *want_type = NULL;
2767
2768 // Recognize <type>
2769 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2770 {
2771 ++*arg;
2772 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2773 if (want_type == NULL)
2774 return FAIL;
2775
2776 if (**arg != '>')
2777 {
2778 if (*skipwhite(*arg) == '>')
2779 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2780 else
2781 emsg(_(e_missing_gt));
2782 return FAIL;
2783 }
2784 ++*arg;
2785 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2786 return FAIL;
2787 }
2788
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002789 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002790 return FAIL;
2791
2792 if (want_type != NULL)
2793 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002794 type_T *actual;
2795 where_T where = WHERE_INIT;
2796
2797 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002798 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002799 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002800 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002801 if (need_type(actual, want_type, FALSE,
2802 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002803 return FAIL;
2804 }
2805 }
2806
2807 return OK;
2808}
2809
2810/*
2811 * * number multiplication
2812 * / number division
2813 * % number modulo
2814 */
2815 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002816compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002817{
2818 char_u *op;
2819 char_u *next;
2820 int ppconst_used = ppconst->pp_used;
2821
2822 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002823 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002824 return FAIL;
2825
2826 /*
2827 * Repeat computing, until no "*", "/" or "%" is following.
2828 */
2829 for (;;)
2830 {
2831 op = may_peek_next_line(cctx, *arg, &next);
2832 if (*op != '*' && *op != '/' && *op != '%')
2833 break;
2834 if (next != NULL)
2835 {
2836 *arg = next_line_from_context(cctx, TRUE);
2837 op = skipwhite(*arg);
2838 }
2839
2840 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2841 {
2842 error_white_both(op, 1);
2843 return FAIL;
2844 }
2845 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2846 return FAIL;
2847
2848 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002849 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002850 return FAIL;
2851
2852 if (ppconst->pp_used == ppconst_used + 2
2853 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2854 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2855 {
2856 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2857 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2858 varnumber_T res = 0;
2859 int failed = FALSE;
2860
2861 // both are numbers: compute the result
2862 switch (*op)
2863 {
2864 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2865 break;
2866 case '/': res = num_divide(tv1->vval.v_number,
2867 tv2->vval.v_number, &failed);
2868 break;
2869 case '%': res = num_modulus(tv1->vval.v_number,
2870 tv2->vval.v_number, &failed);
2871 break;
2872 }
2873 if (failed)
2874 return FAIL;
2875 tv1->vval.v_number = res;
2876 --ppconst->pp_used;
2877 }
2878 else
2879 {
2880 generate_ppconst(cctx, ppconst);
2881 generate_two_op(cctx, op);
2882 }
2883 }
2884
2885 return OK;
2886}
2887
2888/*
2889 * + number addition or list/blobl concatenation
2890 * - number subtraction
2891 * .. string concatenation
2892 */
2893 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002894compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002895{
2896 char_u *op;
2897 char_u *next;
2898 int oplen;
2899 int ppconst_used = ppconst->pp_used;
2900
2901 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002902 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002903 return FAIL;
2904
2905 /*
2906 * Repeat computing, until no "+", "-" or ".." is following.
2907 */
2908 for (;;)
2909 {
2910 op = may_peek_next_line(cctx, *arg, &next);
2911 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2912 break;
2913 if (op[0] == op[1] && *op != '.' && next)
2914 // Finding "++" or "--" on the next line is a separate command.
2915 // But ".." is concatenation.
2916 break;
2917 oplen = (*op == '.' ? 2 : 1);
2918 if (next != NULL)
2919 {
2920 *arg = next_line_from_context(cctx, TRUE);
2921 op = skipwhite(*arg);
2922 }
2923
2924 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2925 {
2926 error_white_both(op, oplen);
2927 return FAIL;
2928 }
2929
2930 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2931 return FAIL;
2932
2933 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002934 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002935 return FAIL;
2936
2937 if (ppconst->pp_used == ppconst_used + 2
2938 && (*op == '.'
2939 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2940 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2941 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2942 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2943 {
2944 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2945 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2946
2947 // concat/subtract/add constant numbers
2948 if (*op == '+')
2949 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2950 else if (*op == '-')
2951 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2952 else
2953 {
2954 // concatenate constant strings
2955 char_u *s1 = tv1->vval.v_string;
2956 char_u *s2 = tv2->vval.v_string;
2957 size_t len1 = STRLEN(s1);
2958
2959 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2960 if (tv1->vval.v_string == NULL)
2961 {
2962 clear_ppconst(ppconst);
2963 return FAIL;
2964 }
2965 mch_memmove(tv1->vval.v_string, s1, len1);
2966 STRCPY(tv1->vval.v_string + len1, s2);
2967 vim_free(s1);
2968 vim_free(s2);
2969 }
2970 --ppconst->pp_used;
2971 }
2972 else
2973 {
2974 generate_ppconst(cctx, ppconst);
2975 ppconst->pp_is_const = FALSE;
2976 if (*op == '.')
2977 {
2978 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2979 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2980 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002981 if (generate_CONCAT(cctx, 2) == FAIL)
2982 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002983 }
2984 else
2985 generate_two_op(cctx, op);
2986 }
2987 }
2988
2989 return OK;
2990}
2991
2992/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002993 * expr6a >> expr6b
2994 * expr6a << expr6b
2995 *
2996 * Produces instructions:
2997 * OPNR bitwise left or right shift
2998 */
2999 static int
3000compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3001{
3002 exprtype_T type = EXPR_UNKNOWN;
3003 char_u *p;
3004 char_u *next;
3005 int len = 2;
3006 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003007 isn_T *isn;
3008
3009 // get the first variable
3010 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3011 return FAIL;
3012
3013 /*
3014 * Repeat computing, until no "+", "-" or ".." is following.
3015 */
3016 for (;;)
3017 {
3018 type = EXPR_UNKNOWN;
3019
3020 p = may_peek_next_line(cctx, *arg, &next);
3021 if (p[0] == '<' && p[1] == '<')
3022 type = EXPR_LSHIFT;
3023 else if (p[0] == '>' && p[1] == '>')
3024 type = EXPR_RSHIFT;
3025
3026 if (type == EXPR_UNKNOWN)
3027 return OK;
3028
3029 // Handle a bitwise left or right shift operator
3030 if (ppconst->pp_used == ppconst_used + 1)
3031 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003032 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003033 {
3034 // left operand should be a number
3035 emsg(_(e_bitshift_ops_must_be_number));
3036 return FAIL;
3037 }
3038 }
3039 else
3040 {
3041 type_T *t = get_type_on_stack(cctx, 0);
3042
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003043 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003044 {
3045 emsg(_(e_bitshift_ops_must_be_number));
3046 return FAIL;
3047 }
3048 }
3049
3050 if (next != NULL)
3051 {
3052 *arg = next_line_from_context(cctx, TRUE);
3053 p = skipwhite(*arg);
3054 }
3055
3056 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3057 {
3058 error_white_both(p, len);
3059 return FAIL;
3060 }
3061
3062 // get the second variable
3063 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3064 return FAIL;
3065
3066 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3067 return FAIL;
3068
3069 if (ppconst->pp_used == ppconst_used + 2)
3070 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003071 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3072 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3073
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003074 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003075 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3076 {
3077 // right operand should be a positive number
3078 if (tv2->v_type != VAR_NUMBER)
3079 emsg(_(e_bitshift_ops_must_be_number));
3080 else
dundargocc57b5bc2022-11-02 13:30:51 +00003081 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003082 return FAIL;
3083 }
3084
3085 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3086 tv1->vval.v_number = 0;
3087 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003088 tv1->vval.v_number =
3089 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003090 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003091 tv1->vval.v_number =
3092 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003093 clear_tv(tv2);
3094 --ppconst->pp_used;
3095 }
3096 else
3097 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003098 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3099 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003100 {
3101 emsg(_(e_bitshift_ops_must_be_number));
3102 return FAIL;
3103 }
3104
3105 generate_ppconst(cctx, ppconst);
3106
3107 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3108 if (isn == NULL)
3109 return FAIL;
3110
3111 if (isn != NULL)
3112 isn->isn_arg.op.op_type = type;
3113 }
3114 }
3115
3116 return OK;
3117}
3118
3119/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003120 * expr5a == expr5b
3121 * expr5a =~ expr5b
3122 * expr5a != expr5b
3123 * expr5a !~ expr5b
3124 * expr5a > expr5b
3125 * expr5a >= expr5b
3126 * expr5a < expr5b
3127 * expr5a <= expr5b
3128 * expr5a is expr5b
3129 * expr5a isnot expr5b
3130 *
3131 * Produces instructions:
3132 * EVAL expr5a Push result of "expr5a"
3133 * EVAL expr5b Push result of "expr5b"
3134 * COMPARE one of the compare instructions
3135 */
3136 static int
3137compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3138{
3139 exprtype_T type = EXPR_UNKNOWN;
3140 char_u *p;
3141 char_u *next;
3142 int len = 2;
3143 int type_is = FALSE;
3144 int ppconst_used = ppconst->pp_used;
3145
3146 // get the first variable
3147 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3148 return FAIL;
3149
3150 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003151
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003152 type = get_compare_type(p, &len, &type_is);
3153
3154 /*
3155 * If there is a comparative operator, use it.
3156 */
3157 if (type != EXPR_UNKNOWN)
3158 {
3159 int ic = FALSE; // Default: do not ignore case
3160
3161 if (next != NULL)
3162 {
3163 *arg = next_line_from_context(cctx, TRUE);
3164 p = skipwhite(*arg);
3165 }
3166 if (type_is && (p[len] == '?' || p[len] == '#'))
3167 {
3168 semsg(_(e_invalid_expression_str), *arg);
3169 return FAIL;
3170 }
3171 // extra question mark appended: ignore case
3172 if (p[len] == '?')
3173 {
3174 ic = TRUE;
3175 ++len;
3176 }
3177 // extra '#' appended: match case (ignored)
3178 else if (p[len] == '#')
3179 ++len;
3180 // nothing appended: match case
3181
3182 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3183 {
3184 error_white_both(p, len);
3185 return FAIL;
3186 }
3187
3188 // get the second variable
3189 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3190 return FAIL;
3191
3192 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3193 return FAIL;
3194
3195 if (ppconst->pp_used == ppconst_used + 2)
3196 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003197 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003198 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3199 int ret;
3200
3201 // Both sides are a constant, compute the result now.
3202 // First check for a valid combination of types, this is more
3203 // strict than typval_compare().
3204 if (check_compare_types(type, tv1, tv2) == FAIL)
3205 ret = FAIL;
3206 else
3207 {
3208 ret = typval_compare(tv1, tv2, type, ic);
3209 tv1->v_type = VAR_BOOL;
3210 tv1->vval.v_number = tv1->vval.v_number
3211 ? VVAL_TRUE : VVAL_FALSE;
3212 clear_tv(tv2);
3213 --ppconst->pp_used;
3214 }
3215 return ret;
3216 }
3217
3218 generate_ppconst(cctx, ppconst);
3219 return generate_COMPARE(cctx, type, ic);
3220 }
3221
3222 return OK;
3223}
3224
3225static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3226
3227/*
3228 * Compile || or &&.
3229 */
3230 static int
3231compile_and_or(
3232 char_u **arg,
3233 cctx_T *cctx,
3234 char *op,
3235 ppconst_T *ppconst,
3236 int ppconst_used UNUSED)
3237{
3238 char_u *next;
3239 char_u *p = may_peek_next_line(cctx, *arg, &next);
3240 int opchar = *op;
3241
3242 if (p[0] == opchar && p[1] == opchar)
3243 {
3244 garray_T *instr = &cctx->ctx_instr;
3245 garray_T end_ga;
3246 int save_skip = cctx->ctx_skip;
3247
3248 /*
3249 * Repeat until there is no following "||" or "&&"
3250 */
3251 ga_init2(&end_ga, sizeof(int), 10);
3252 while (p[0] == opchar && p[1] == opchar)
3253 {
3254 long start_lnum = SOURCING_LNUM;
3255 long save_sourcing_lnum;
3256 int start_ctx_lnum = cctx->ctx_lnum;
3257 int save_lnum;
3258 int const_used;
3259 int status;
3260 jumpwhen_T jump_when = opchar == '|'
3261 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3262
3263 if (next != NULL)
3264 {
3265 *arg = next_line_from_context(cctx, TRUE);
3266 p = skipwhite(*arg);
3267 }
3268
3269 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3270 {
3271 semsg(_(e_white_space_required_before_and_after_str_at_str),
3272 op, p);
3273 ga_clear(&end_ga);
3274 return FAIL;
3275 }
3276
3277 save_sourcing_lnum = SOURCING_LNUM;
3278 SOURCING_LNUM = start_lnum;
3279 save_lnum = cctx->ctx_lnum;
3280 cctx->ctx_lnum = start_ctx_lnum;
3281
3282 status = check_ppconst_bool(ppconst);
3283 if (status != FAIL)
3284 {
3285 // Use the last ppconst if possible.
3286 if (ppconst->pp_used > 0)
3287 {
3288 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3289 int is_true = tv2bool(tv);
3290
3291 if ((is_true && opchar == '|')
3292 || (!is_true && opchar == '&'))
3293 {
3294 // For "false && expr" and "true || expr" the "expr"
3295 // does not need to be evaluated.
3296 cctx->ctx_skip = SKIP_YES;
3297 clear_tv(tv);
3298 tv->v_type = VAR_BOOL;
3299 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3300 }
3301 else
3302 {
3303 // For "true && expr" and "false || expr" only "expr"
3304 // needs to be evaluated.
3305 --ppconst->pp_used;
3306 jump_when = JUMP_NEVER;
3307 }
3308 }
3309 else
3310 {
3311 // Every part must evaluate to a bool.
3312 status = bool_on_stack(cctx);
3313 }
3314 }
3315 if (status != FAIL)
3316 status = ga_grow(&end_ga, 1);
3317 cctx->ctx_lnum = save_lnum;
3318 if (status == FAIL)
3319 {
3320 ga_clear(&end_ga);
3321 return FAIL;
3322 }
3323
3324 if (jump_when != JUMP_NEVER)
3325 {
3326 if (cctx->ctx_skip != SKIP_YES)
3327 {
3328 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3329 ++end_ga.ga_len;
3330 }
3331 generate_JUMP(cctx, jump_when, 0);
3332 }
3333
3334 // eval the next expression
3335 SOURCING_LNUM = save_sourcing_lnum;
3336 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3337 {
3338 ga_clear(&end_ga);
3339 return FAIL;
3340 }
3341
3342 const_used = ppconst->pp_used;
3343 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3344 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3345 {
3346 ga_clear(&end_ga);
3347 return FAIL;
3348 }
3349
3350 // "0 || 1" results in true, "1 && 0" results in false.
3351 if (ppconst->pp_used == const_used + 1)
3352 {
3353 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3354
3355 if (tv->v_type == VAR_NUMBER
3356 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3357 {
3358 tv->vval.v_number = tv->vval.v_number == 1
3359 ? VVAL_TRUE : VVAL_FALSE;
3360 tv->v_type = VAR_BOOL;
3361 }
3362 }
3363
3364 p = may_peek_next_line(cctx, *arg, &next);
3365 }
3366
3367 if (check_ppconst_bool(ppconst) == FAIL)
3368 {
3369 ga_clear(&end_ga);
3370 return FAIL;
3371 }
3372
3373 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3374 // Every part must evaluate to a bool.
3375 if (bool_on_stack(cctx) == FAIL)
3376 {
3377 ga_clear(&end_ga);
3378 return FAIL;
3379 }
3380
3381 if (end_ga.ga_len > 0)
3382 {
3383 // Fill in the end label in all jumps.
3384 generate_ppconst(cctx, ppconst);
3385 while (end_ga.ga_len > 0)
3386 {
3387 isn_T *isn;
3388
3389 --end_ga.ga_len;
3390 isn = ((isn_T *)instr->ga_data)
3391 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3392 isn->isn_arg.jump.jump_where = instr->ga_len;
3393 }
3394 }
3395 ga_clear(&end_ga);
3396
3397 cctx->ctx_skip = save_skip;
3398 }
3399
3400 return OK;
3401}
3402
3403/*
3404 * expr4a && expr4a && expr4a logical AND
3405 *
3406 * Produces instructions:
3407 * EVAL expr4a Push result of "expr4a"
3408 * COND2BOOL convert to bool if needed
3409 * JUMP_IF_COND_FALSE end
3410 * EVAL expr4b Push result of "expr4b"
3411 * JUMP_IF_COND_FALSE end
3412 * EVAL expr4c Push result of "expr4c"
3413 * end:
3414 */
3415 static int
3416compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3417{
3418 int ppconst_used = ppconst->pp_used;
3419
3420 // get the first variable
3421 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3422 return FAIL;
3423
3424 // || and && work almost the same
3425 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3426}
3427
3428/*
3429 * expr3a || expr3b || expr3c logical OR
3430 *
3431 * Produces instructions:
3432 * EVAL expr3a Push result of "expr3a"
3433 * COND2BOOL convert to bool if needed
3434 * JUMP_IF_COND_TRUE end
3435 * EVAL expr3b Push result of "expr3b"
3436 * JUMP_IF_COND_TRUE end
3437 * EVAL expr3c Push result of "expr3c"
3438 * end:
3439 */
3440 static int
3441compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3442{
3443 int ppconst_used = ppconst->pp_used;
3444
3445 // eval the first expression
3446 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3447 return FAIL;
3448
3449 // || and && work almost the same
3450 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3451}
3452
3453/*
3454 * Toplevel expression: expr2 ? expr1a : expr1b
3455 * Produces instructions:
3456 * EVAL expr2 Push result of "expr2"
3457 * JUMP_IF_FALSE alt jump if false
3458 * EVAL expr1a
3459 * JUMP_ALWAYS end
3460 * alt: EVAL expr1b
3461 * end:
3462 *
3463 * Toplevel expression: expr2 ?? expr1
3464 * Produces instructions:
3465 * EVAL expr2 Push result of "expr2"
3466 * JUMP_AND_KEEP_IF_TRUE end jump if true
3467 * EVAL expr1
3468 * end:
3469 */
3470 int
3471compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3472{
3473 char_u *p;
3474 int ppconst_used = ppconst->pp_used;
3475 char_u *next;
3476
3477 // Ignore all kinds of errors when not producing code.
3478 if (cctx->ctx_skip == SKIP_YES)
3479 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003480 int prev_did_emsg = did_emsg;
3481
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003482 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003483 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003484 }
3485
3486 // Evaluate the first expression.
3487 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3488 return FAIL;
3489
3490 p = may_peek_next_line(cctx, *arg, &next);
3491 if (*p == '?')
3492 {
3493 int op_falsy = p[1] == '?';
3494 garray_T *instr = &cctx->ctx_instr;
3495 garray_T *stack = &cctx->ctx_type_stack;
3496 int alt_idx = instr->ga_len;
3497 int end_idx = 0;
3498 isn_T *isn;
3499 type_T *type1 = NULL;
3500 int has_const_expr = FALSE;
3501 int const_value = FALSE;
3502 int save_skip = cctx->ctx_skip;
3503
3504 if (next != NULL)
3505 {
3506 *arg = next_line_from_context(cctx, TRUE);
3507 p = skipwhite(*arg);
3508 }
3509
3510 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3511 {
3512 semsg(_(e_white_space_required_before_and_after_str_at_str),
3513 op_falsy ? "??" : "?", p);
3514 return FAIL;
3515 }
3516
3517 if (ppconst->pp_used == ppconst_used + 1)
3518 {
3519 // the condition is a constant, we know whether the ? or the :
3520 // expression is to be evaluated.
3521 has_const_expr = TRUE;
3522 if (op_falsy)
3523 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3524 else
3525 {
3526 int error = FALSE;
3527
3528 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3529 &error);
3530 if (error)
3531 return FAIL;
3532 }
3533 cctx->ctx_skip = save_skip == SKIP_YES ||
3534 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3535
3536 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3537 // "left ?? right" and "left" is truthy: produce "left"
3538 generate_ppconst(cctx, ppconst);
3539 else
3540 {
3541 clear_tv(&ppconst->pp_tv[ppconst_used]);
3542 --ppconst->pp_used;
3543 }
3544 }
3545 else
3546 {
3547 generate_ppconst(cctx, ppconst);
3548 if (op_falsy)
3549 end_idx = instr->ga_len;
3550 generate_JUMP(cctx, op_falsy
3551 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3552 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003553 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003554 }
3555
3556 // evaluate the second expression; any type is accepted
3557 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3558 return FAIL;
3559 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3560 return FAIL;
3561
3562 if (!has_const_expr)
3563 {
3564 generate_ppconst(cctx, ppconst);
3565
3566 if (!op_falsy)
3567 {
3568 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003569 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003570 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003571
3572 end_idx = instr->ga_len;
3573 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3574
3575 // jump here from JUMP_IF_FALSE
3576 isn = ((isn_T *)instr->ga_data) + alt_idx;
3577 isn->isn_arg.jump.jump_where = instr->ga_len;
3578 }
3579 }
3580
3581 if (!op_falsy)
3582 {
3583 // Check for the ":".
3584 p = may_peek_next_line(cctx, *arg, &next);
3585 if (*p != ':')
3586 {
3587 emsg(_(e_missing_colon_after_questionmark));
3588 return FAIL;
3589 }
3590 if (next != NULL)
3591 {
3592 *arg = next_line_from_context(cctx, TRUE);
3593 p = skipwhite(*arg);
3594 }
3595
3596 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3597 {
3598 semsg(_(e_white_space_required_before_and_after_str_at_str),
3599 ":", p);
3600 return FAIL;
3601 }
3602
3603 // evaluate the third expression
3604 if (has_const_expr)
3605 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3606 ? SKIP_YES : SKIP_NOT;
3607 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3608 return FAIL;
3609 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3610 return FAIL;
3611 }
3612
3613 if (!has_const_expr)
3614 {
3615 type_T **typep;
3616
3617 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003618 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003619
3620 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003621 typep = &((((type2_T *)stack->ga_data)
3622 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003623 common_type(type1, *typep, typep, cctx->ctx_type_list);
3624
3625 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3626 isn = ((isn_T *)instr->ga_data) + end_idx;
3627 isn->isn_arg.jump.jump_where = instr->ga_len;
3628 }
3629
3630 cctx->ctx_skip = save_skip;
3631 }
3632 return OK;
3633}
3634
3635/*
3636 * Toplevel expression.
3637 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3638 * Returns OK or FAIL.
3639 */
3640 int
3641compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3642{
3643 ppconst_T ppconst;
3644
3645 CLEAR_FIELD(ppconst);
3646 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3647 {
3648 clear_ppconst(&ppconst);
3649 return FAIL;
3650 }
3651 if (is_const != NULL)
3652 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3653 if (generate_ppconst(cctx, &ppconst) == FAIL)
3654 return FAIL;
3655 return OK;
3656}
3657
3658/*
3659 * Toplevel expression.
3660 */
3661 int
3662compile_expr0(char_u **arg, cctx_T *cctx)
3663{
3664 return compile_expr0_ext(arg, cctx, NULL);
3665}
3666
3667
3668#endif // defined(FEAT_EVAL)