blob: 41ed7e74014b5af6ce7ffb31838ddc0722045581 [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:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000241 case VAR_UNKNOWN:
242 case VAR_ANY:
243 case VAR_VOID:
244 emsg(_(e_cannot_index_special_variable));
245 break;
246 default:
247 emsg(_(e_string_list_dict_or_blob_required));
248 }
249 return FAIL;
250 }
251 return OK;
252}
253
254/*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200255 * Returns TRUE if the current function is inside the class "cl" or one of the
256 * parent classes.
257 */
258 static int
259inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
260{
261 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
262 {
263 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
264 {
265 class_T *clp = cctx->ctx_ufunc->uf_class;
266 while (clp != NULL)
267 {
268 if (clp == cl)
269 return TRUE;
270 clp = clp->class_extends;
271 }
272 }
273 }
274
275 return FALSE;
276}
277
278/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000279 * Compile ".member" coming after an object or class.
280 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000281 static int
282compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
283{
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200284 int m_idx;
285
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000286 if (VIM_ISWHITE((*arg)[1]))
287 {
288 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
289 return FAIL;
290 }
291
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000292 class_T *cl = type->tt_class;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000293 int is_super = type->tt_flags & TTFLAG_SUPER;
294 if (type == &t_super)
295 {
296 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000297 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200298 emsg(_(e_using_super_not_in_class_method));
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000299 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000300 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000301 is_super = TRUE;
302 cl = cctx->ctx_ufunc->uf_class;
303 // Remove &t_super from the stack.
304 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000305 }
306 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000307 {
308 garray_T *instr = &cctx->ctx_instr;
309 if (instr->ga_len > 0)
310 {
311 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
312 if (isn->isn_type == ISN_LOADSCRIPT)
313 {
314 // The class was recognized as a script item. We only need
315 // to know what class it is, drop the instruction.
316 --instr->ga_len;
317 vim_free(isn->isn_arg.script.scriptref);
318 }
319 }
320 }
321
Ernie Raelf77a7f72023-03-03 15:05:30 +0000322 if (cl == NULL)
323 {
324 // TODO: this should not give an error but be handled at runtime
325 emsg(_(e_incomplete_type));
326 return FAIL;
327 }
328
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000329 ++*arg;
330 char_u *name = *arg;
331 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
332 if (name_end == name)
333 return FAIL;
334 size_t len = name_end - name;
335
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000336 if (*name_end == '(')
337 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000338 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000339 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000340 ufunc_T **functions;
341
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000342 if (type->tt_type == VAR_CLASS)
343 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000344 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000345 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000346 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000347 }
348 else
349 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000350 // type->tt_type == VAR_OBJECT: method call
351 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000352 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000353 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000354 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000355
356 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000357 int fi;
358 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000360 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000361 // Use a separate pointer to avoid that ASAN complains about
362 // uf_name[] only being 4 characters.
363 char_u *ufname = (char_u *)fp->uf_name;
364 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
365 {
366 ufunc = fp;
367 break;
368 }
369 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200370 ocmember_T *ocm = NULL;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000371 if (ufunc == NULL)
372 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200373 // could be a funcref in a member variable
374 ocm = member_lookup(cl, type->tt_type, name, len, &m_idx);
375 if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC)
376 {
377 method_not_found_msg(cl, type->tt_type, name, len);
378 return FAIL;
379 }
380 if (type->tt_type == VAR_CLASS)
381 {
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200382 // Remove the class type from the stack
383 --cctx->ctx_type_stack.ga_len;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200384 if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL)
385 return FAIL;
386 }
387 else
388 {
389 if (generate_GET_OBJ_MEMBER(cctx, m_idx, ocm->ocm_type) ==
390 FAIL)
391 return FAIL;
392 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000393 }
394
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200395 // A private object method can be used only inside the class where it
396 // is defined or in one of the child classes.
397 // A private class method can be used only in the class where it is
398 // defined.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200399 if (ocm == NULL && *ufunc->uf_name == '_' &&
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200400 ((type->tt_type == VAR_OBJECT
401 && !inside_class_hierarchy(cctx, cl))
402 || (type->tt_type == VAR_CLASS
403 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200404 {
405 semsg(_(e_cannot_access_private_method_str), name);
406 return FAIL;
407 }
408
Bram Moolenaar574950d2023-01-03 19:08:50 +0000409 // Compile the arguments and call the class function or object method.
410 // The object method will know that the object is on the stack, just
411 // before the arguments.
412 *arg = skipwhite(name_end + 1);
413 int argcount = 0;
414 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
415 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000416
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200417 if (ocm != NULL)
418 return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE);
Bram Moolenaard0200c82023-01-28 15:19:40 +0000419 if (type->tt_type == VAR_OBJECT
420 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-eastb895b0f2023-09-24 15:46:31 +0200421 return generate_CALL(cctx, ufunc, cl, fi, argcount);
422 return generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000423 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000424
425 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000426 {
Ernie Rael4d00b832023-09-11 19:54:42 +0200427 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200428 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000429 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200430 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000431 {
Ernie Raele6c9aa52023-10-06 19:55:52 +0200432 emsg_var_cl_define(e_cannot_access_private_variable_str,
433 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200434 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200435 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200436
437 *arg = name_end;
438 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200439 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
440 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200441 }
442
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200443 // Could be an object method reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200444 m_idx = object_method_idx(cl, name, len);
445 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000446 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200447 ufunc_T *fp = cl->class_obj_methods[m_idx];
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200448 // Private methods are not accessible outside the class
449 if (*name == '_' && !inside_class(cctx, cl))
450 {
451 semsg(_(e_cannot_access_private_method_str), fp->uf_name);
452 return FAIL;
453 }
454 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200455 // Remove the object type from the stack
456 --cctx->ctx_type_stack.ga_len;
457 return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000458 }
459
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200460 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000461 }
462 else
463 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000464 // load class member
465 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200466 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
467 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000468 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200469 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200470 // A private class variable can be accessed only in the class where
471 // it is defined.
472 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200473 {
Ernie Raele6c9aa52023-10-06 19:55:52 +0200474 emsg_var_cl_define(e_cannot_access_private_variable_str,
475 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200476 return FAIL;
477 }
478
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000479 *arg = name_end;
Yegappan Lakshmanand7b616d2023-10-19 10:47:53 +0200480 // Remove the class type from the stack
481 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000482 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
483 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200484
485 // Could be a class method reference: "class.Func".
486 m_idx = class_method_idx(cl, name, len);
487 if (m_idx >= 0)
488 {
489 ufunc_T *fp = cl->class_class_functions[m_idx];
490 // Private methods are not accessible outside the class
491 if (*name == '_' && !inside_class(cctx, cl))
492 {
493 semsg(_(e_cannot_access_private_method_str), fp->uf_name);
494 return FAIL;
495 }
496 *arg = name_end;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +0200497 // Remove the class type from the stack
498 --cctx->ctx_type_stack.ga_len;
499 return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200500 }
501
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200502 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000503 }
504
505 return FAIL;
506}
507
508/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000509 * Generate an instruction to load script-local variable "name", without the
510 * leading "s:".
511 * Also finds imported variables.
512 */
513 int
514compile_load_scriptvar(
515 cctx_T *cctx,
516 char_u *name, // variable NUL terminated
517 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100518 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000519{
520 scriptitem_T *si;
521 int idx;
522 imported_T *import;
523
524 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
525 return FAIL;
526 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000527 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 if (idx >= 0)
529 {
530 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
531
532 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
533 current_sctx.sc_sid, idx, sv->sv_type);
534 return OK;
535 }
536
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000537 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000538 if (import != NULL)
539 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000540 char_u *p = skipwhite(*end);
541 char_u *exp_name;
542 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100543 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000544 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000545 int done = FALSE;
546 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000547
548 // Need to lookup the member.
549 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000550 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000551 semsg(_(e_expected_dot_after_name_str), start);
552 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000553 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000554 ++p;
555 if (VIM_ISWHITE(*p))
556 {
557 emsg(_(e_no_white_space_allowed_after_dot));
558 return FAIL;
559 }
560
561 // isolate one name
562 exp_name = p;
563 while (eval_isnamec(*p))
564 ++p;
565 cc = *p;
566 *p = NUL;
567
Bram Moolenaard041f422022-01-12 19:54:00 +0000568 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100569 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
570 // "import autoload './dir/script.vim'" or
571 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100572 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100573
574 if (res == OK)
575 {
576 if (si->sn_autoload_prefix != NULL
577 && si->sn_state == SN_STATE_NOT_LOADED)
578 {
579 char_u *auto_name =
580 concat_str(si->sn_autoload_prefix, exp_name);
581
582 // autoload script must be loaded later, access by the autoload
583 // name. If a '(' follows it must be a function. Otherwise we
584 // don't know, it can be "script.Func".
585 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100586 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100587 else
588 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
589 vim_free(auto_name);
590 done = TRUE;
591 }
592 else if (si->sn_import_autoload
593 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100594 {
595 // If a '(' follows it must be a function. Otherwise we don't
596 // know, it can be "script.Func".
597 if (cc == '(' || paren_follows_after_expr)
598 {
599 char_u sid_name[MAX_FUNC_NAME_LEN];
600
601 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100602 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100603 }
604 else
605 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
606 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100607 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100608 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100609 else
610 {
611 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
612 cctx, NULL, TRUE);
613 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100614 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100615
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000616 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000617 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000618 if (done)
619 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000620
621 if (idx < 0)
622 {
623 if (ufunc != NULL)
624 {
625 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100626 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000627 return OK;
628 }
629 return FAIL;
630 }
631
632 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
633 import->imp_sid,
634 idx,
635 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 return OK;
637 }
638
Bram Moolenaard0132f42022-05-12 11:05:40 +0100639 // Can only get here if we know "name" is a script variable and not in a
640 // Vim9 script (variable is not in sn_var_vals): old style script.
641 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000642 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000643}
644
645 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000646generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000647{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000648 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000649 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000650
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000651 // Reject a global non-autoload function found without the "g:" prefix.
652 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000653 return FAIL;
654
655 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000656 compile_type = get_compile_type(ufunc);
657 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000658 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100660 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000661}
662
663/*
664 * Compile a variable name into a load instruction.
665 * "end" points to just after the name.
666 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
667 * When "error" is FALSE do not give an error when not found.
668 */
669 int
670compile_load(
671 char_u **arg,
672 char_u *end_arg,
673 cctx_T *cctx,
674 int is_expr,
675 int error)
676{
677 type_T *type;
678 char_u *name = NULL;
679 char_u *end = end_arg;
680 int res = FAIL;
681 int prev_called_emsg = called_emsg;
682
683 if (*(*arg + 1) == ':')
684 {
685 if (end <= *arg + 2)
686 {
687 isntype_T isn_type;
688
689 // load dictionary of namespace
690 switch (**arg)
691 {
692 case 'g': isn_type = ISN_LOADGDICT; break;
693 case 'w': isn_type = ISN_LOADWDICT; break;
694 case 't': isn_type = ISN_LOADTDICT; break;
695 case 'b': isn_type = ISN_LOADBDICT; break;
696 default:
697 semsg(_(e_namespace_not_supported_str), *arg);
698 goto theend;
699 }
700 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
701 goto theend;
702 res = OK;
703 }
704 else
705 {
706 isntype_T isn_type = ISN_DROP;
707
708 // load namespaced variable
709 name = vim_strnsave(*arg + 2, end - (*arg + 2));
710 if (name == NULL)
711 return FAIL;
712
713 switch (**arg)
714 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100715 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000717 case 's': if (current_script_is_vim9())
718 {
719 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
720 *arg);
721 vim_free(name);
722 return FAIL;
723 }
Kota Kato948a3892022-08-16 16:09:59 +0100724 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000725 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000726 else
727 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100728 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000729 break;
730 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
731 {
732 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000733 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000734 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000735 else
736 isn_type = ISN_LOADG;
737 }
738 else
739 {
740 isn_type = ISN_LOADAUTO;
741 vim_free(name);
742 name = vim_strnsave(*arg, end - *arg);
743 if (name == NULL)
744 return FAIL;
745 }
746 break;
747 case 'w': isn_type = ISN_LOADW; break;
748 case 't': isn_type = ISN_LOADT; break;
749 case 'b': isn_type = ISN_LOADB; break;
750 default: // cannot happen, just in case
751 semsg(_(e_namespace_not_supported_str), *arg);
752 goto theend;
753 }
754 if (isn_type != ISN_DROP)
755 {
756 // Global, Buffer-local, Window-local and Tabpage-local
757 // variables can be defined later, thus we don't check if it
758 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000759 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000760 }
761 }
762 }
763 else
764 {
765 size_t len = end - *arg;
766 int idx;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200767 int method_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768 int gen_load = FALSE;
769 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100770 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100771 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772
773 name = vim_strnsave(*arg, end - *arg);
774 if (name == NULL)
775 return FAIL;
776
Bram Moolenaar58b40092023-01-11 15:59:05 +0000777 if (STRCMP(name, "super") == 0
778 && cctx->ctx_ufunc != NULL
779 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
780 {
781 // super.SomeFunc() in a class function: push &t_super type, this
782 // is recognized in compile_subscript().
783 res = push_type_stack(cctx, &t_super);
784 if (*end != '.')
785 emsg(_(e_super_must_be_followed_by_dot));
786 }
787 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 {
789 script_autoload(name, FALSE);
790 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
791 }
792 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
793 == OK)
794 {
795 if (gen_load_outer == 0)
796 gen_load = TRUE;
797 }
798 else
799 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000800 lvar_T lvar;
801 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802
803 if (lookup_local(*arg, len, &lvar, cctx) == OK)
804 {
805 type = lvar.lv_type;
806 idx = lvar.lv_idx;
807 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100808 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100810 outer_loop_depth = lvar.lv_loop_depth;
811 outer_loop_idx = lvar.lv_loop_idx;
812 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000813 else
814 gen_load = TRUE;
815 }
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200816 else if (cctx->ctx_ufunc->uf_defclass != NULL &&
817 (((idx =
818 cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
819 || ((method_idx =
820 cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0)))
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000821 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200822 // Referencing a class variable or method without the class
823 // name. A class variable or method can be referenced without
824 // the class name only in the class where the function is
825 // defined.
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200826 if (cctx->ctx_ufunc->uf_defclass == cl)
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200827 {
828 if (idx >= 0)
829 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
830 else
831 {
832 ufunc_T *fp = cl->class_class_functions[method_idx];
833 res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx,
834 NULL);
835 }
836 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200837 else
838 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200839 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200840 name, cl->class_name);
841 res = FAIL;
842 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000843 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000844 else
845 {
846 // "var" can be script-local even without using "s:" if it
847 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000848 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000849 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100850 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851
852 // When evaluating an expression and the name starts with an
853 // uppercase letter it can be a user defined function.
854 // generate_funcref() will fail if the function can't be found.
855 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000856 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857 }
858 }
859 if (gen_load)
860 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
861 if (gen_load_outer > 0)
862 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100863 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
864 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 cctx->ctx_outer_used = TRUE;
866 }
867 }
868
869 *arg = end;
870
871theend:
872 if (res == FAIL && error && called_emsg == prev_called_emsg)
873 semsg(_(e_variable_not_found_str), name);
874 vim_free(name);
875 return res;
876}
877
878/*
879 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100880 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 * Returns FAIL if compilation fails.
882 */
883 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100884compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885{
LemonBoyf3b48952022-05-05 13:53:03 +0100886 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000887 garray_T save_ga = cctx->ctx_instr;
888 int expr_res;
889 int trailing_error;
890 int instr_count;
891 isn_T *instr = NULL;
892
893 // Remove the string type from the stack.
894 --cctx->ctx_type_stack.ga_len;
895
896 // Temporarily reset the list of instructions so that the jump labels are
897 // correct.
898 cctx->ctx_instr.ga_len = 0;
899 cctx->ctx_instr.ga_maxlen = 0;
900 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000901
902 // avoid peeking a next line
903 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
904 cctx->ctx_ufunc->uf_lines.ga_len = 0;
905
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000907
908 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
909
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 s = skipwhite(s);
911 trailing_error = *s != NUL;
912
913 if (expr_res == FAIL || trailing_error
914 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
915 {
916 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000917 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 clear_instr_ga(&cctx->ctx_instr);
919 cctx->ctx_instr = save_ga;
920 ++cctx->ctx_type_stack.ga_len;
921 return FAIL;
922 }
923
924 // Move the generated instructions into the ISN_INSTR instruction, then
925 // restore the list of instructions.
926 instr_count = cctx->ctx_instr.ga_len;
927 instr = cctx->ctx_instr.ga_data;
928 instr[instr_count].isn_type = ISN_FINISH;
929
930 cctx->ctx_instr = save_ga;
931 vim_free(isn->isn_arg.string);
932 isn->isn_type = ISN_INSTR;
933 isn->isn_arg.instr = instr;
934 return OK;
935}
936
937/*
938 * Compile the argument expressions.
939 * "arg" points to just after the "(" and is advanced to after the ")"
940 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941 int
LemonBoyf3b48952022-05-05 13:53:03 +0100942compile_arguments(
943 char_u **arg,
944 cctx_T *cctx,
945 int *argcount,
946 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000947{
948 char_u *p = *arg;
949 char_u *whitep = *arg;
950 int must_end = FALSE;
951 int instr_count;
952
953 for (;;)
954 {
955 if (may_get_next_line(whitep, &p, cctx) == FAIL)
956 goto failret;
957 if (*p == ')')
958 {
959 *arg = p + 1;
960 return OK;
961 }
962 if (must_end)
963 {
964 semsg(_(e_missing_comma_before_argument_str), p);
965 return FAIL;
966 }
967
968 instr_count = cctx->ctx_instr.ga_len;
969 if (compile_expr0(&p, cctx) == FAIL)
970 return FAIL;
971 ++*argcount;
972
LemonBoyf3b48952022-05-05 13:53:03 +0100973 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 && cctx->ctx_instr.ga_len == instr_count + 1)
975 {
976 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
977
978 // {skip} argument of searchpair() can be compiled if not empty
979 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100980 compile_string(isn, cctx, 0);
981 }
982 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
983 && cctx->ctx_instr.ga_len == instr_count + 1)
984 {
985 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
986
987 // {sub} argument of substitute() can be compiled if it starts
988 // with \=
989 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100990 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100991 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000992 }
993
994 if (*p != ',' && *skipwhite(p) == ',')
995 {
996 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
997 p = skipwhite(p);
998 }
999 if (*p == ',')
1000 {
1001 ++p;
1002 if (*p != NUL && !VIM_ISWHITE(*p))
1003 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1004 }
1005 else
1006 must_end = TRUE;
1007 whitep = p;
1008 p = skipwhite(p);
1009 }
1010failret:
1011 emsg(_(e_missing_closing_paren));
1012 return FAIL;
1013}
1014
1015/*
1016 * Compile a function call: name(arg1, arg2)
1017 * "arg" points to "name", "arg + varlen" to the "(".
1018 * "argcount_init" is 1 for "value->method()"
1019 * Instructions:
1020 * EVAL arg1
1021 * EVAL arg2
1022 * BCALL / DCALL / UCALL
1023 */
1024 static int
1025compile_call(
1026 char_u **arg,
1027 size_t varlen,
1028 cctx_T *cctx,
1029 ppconst_T *ppconst,
1030 int argcount_init)
1031{
1032 char_u *name = *arg;
1033 char_u *p;
1034 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001035 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 char_u fname_buf[FLEN_FIXED + 1];
1037 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001038 ufunc_T *ufunc = NULL;
1039 int res = FAIL;
1040 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001041 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +01001042 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001043 imported_T *import;
1044
1045 if (varlen >= sizeof(namebuf))
1046 {
1047 semsg(_(e_name_too_long_str), name);
1048 return FAIL;
1049 }
1050 vim_strncpy(namebuf, *arg, varlen);
1051
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001052 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +00001053 if (import != NULL)
1054 {
1055 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
1056 return FAIL;
1057 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001058
1059 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001060 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001061 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001062 if ((varlen == 3
1063 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001064 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1065 {
1066 char_u *s = skipwhite(*arg + varlen + 1);
1067 typval_T argvars[2];
1068 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001069 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001070
1071 argvars[0].v_type = VAR_UNKNOWN;
1072 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001073 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001074 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001075 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001076 s = skipwhite(s);
1077 if (*s == ')' && argvars[0].v_type == VAR_STRING
1078 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1079 || !is_has))
1080 {
1081 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1082
1083 *arg = s + 1;
1084 argvars[1].v_type = VAR_UNKNOWN;
1085 tv->v_type = VAR_NUMBER;
1086 tv->vval.v_number = 0;
1087 if (is_has)
1088 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001089 else if (is_len)
1090 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001091 else
1092 f_exists(argvars, tv);
1093 clear_tv(&argvars[0]);
1094 ++ppconst->pp_used;
1095 return OK;
1096 }
1097 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001098 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 {
1100 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1101 return FAIL;
1102 }
1103 }
1104
1105 if (generate_ppconst(cctx, ppconst) == FAIL)
1106 return FAIL;
1107
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001108 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001109 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1110
1111 // We handle the "skip" argument of searchpair() and searchpairpos()
1112 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001113 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1114 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1115 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1116 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1117 special_fn = CA_SEARCHPAIR;
1118 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1119 special_fn = CA_SUBSTITUTE;
1120 else
1121 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001122
1123 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001124 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125 goto theend;
1126
1127 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1128 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1129 {
1130 int idx;
1131
1132 // builtin function
1133 idx = find_internal_func(name);
1134 if (idx >= 0)
1135 {
1136 if (STRCMP(name, "flatten") == 0)
1137 {
1138 emsg(_(e_cannot_use_flatten_in_vim9_script));
1139 goto theend;
1140 }
1141
1142 if (STRCMP(name, "add") == 0 && argcount == 2)
1143 {
h-eastb895b0f2023-09-24 15:46:31 +02001144 type_T *type = get_decl_type_on_stack(cctx, 1);
1145
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001146 // add() can be compiled to instructions if we know the type
1147 if (type->tt_type == VAR_LIST)
1148 {
1149 // inline "add(list, item)" so that the type can be checked
1150 res = generate_LISTAPPEND(cctx);
1151 idx = -1;
1152 }
1153 else if (type->tt_type == VAR_BLOB)
1154 {
1155 // inline "add(blob, nr)" so that the type can be checked
1156 res = generate_BLOBAPPEND(cctx);
1157 idx = -1;
1158 }
1159 }
1160
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001161 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1162 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001163 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001164 // May have the "D" or "R" flag, reserve a variable for a
1165 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001166 if (get_defer_var_idx(cctx) == 0)
1167 idx = -1;
1168 }
1169
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170 if (idx >= 0)
1171 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1172 }
1173 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001174 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001175 goto theend;
1176 }
1177
Bram Moolenaar62aec932022-01-29 21:45:34 +00001178 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1179
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180 // An argument or local variable can be a function reference, this
1181 // overrules a function name.
1182 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1183 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1184 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001185 class_T *cl = NULL;
1186 int mi = 0;
1187
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 // If we can find the function by name generate the right call.
1189 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001190 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001191 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001192 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001193 if (!func_is_global(ufunc))
1194 {
h-eastb895b0f2023-09-24 15:46:31 +02001195 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001196 goto theend;
1197 }
1198 if (!has_g_namespace
1199 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1200 {
1201 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001202 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001203 goto theend;
1204 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001206 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001207 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001208 // Class method invocation without the class name.
1209 // A class method can be referenced without the class name only in
1210 // the class where the function is defined.
1211 if (cctx->ctx_ufunc->uf_defclass == cl)
1212 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001213 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001214 0, argcount);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001215 }
1216 else
1217 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001218 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001219 name, cl->class_name);
1220 res = FAIL;
1221 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001222 goto theend;
1223 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224 }
1225
1226 // If the name is a variable, load it and use PCALL.
1227 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001228 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001230 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1232 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001233 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234
Christian Brabandtd5475e82023-08-17 23:34:09 +02001235 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001236 goto theend;
1237 }
1238
1239 // If we can find a global function by name generate the right call.
1240 if (ufunc != NULL)
1241 {
h-eastb895b0f2023-09-24 15:46:31 +02001242 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 goto theend;
1244 }
1245
1246 // A global function may be defined only later. Need to figure out at
1247 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001248 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 res = generate_UCALL(cctx, name, argcount);
1250 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001251 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001252
1253theend:
1254 vim_free(tofree);
1255 return res;
1256}
1257
1258// like NAMESPACE_CHAR but with 'a' and 'l'.
1259#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1260
1261/*
1262 * Find the end of a variable or function name. Unlike find_name_end() this
1263 * does not recognize magic braces.
1264 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1265 * Return a pointer to just after the name. Equal to "arg" if there is no
1266 * valid name.
1267 */
1268 char_u *
1269to_name_end(char_u *arg, int use_namespace)
1270{
1271 char_u *p;
1272
1273 // Quick check for valid starting character.
1274 if (!eval_isnamec1(*arg))
1275 return arg;
1276
1277 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1278 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1279 // and can be used in slice "[n:]".
1280 if (*p == ':' && (p != arg + 1
1281 || !use_namespace
1282 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1283 break;
1284 return p;
1285}
1286
1287/*
1288 * Like to_name_end() but also skip over a list or dict constant.
1289 * Also accept "<SNR>123_Func".
1290 * This intentionally does not handle line continuation.
1291 */
1292 char_u *
1293to_name_const_end(char_u *arg)
1294{
1295 char_u *p = arg;
1296 typval_T rettv;
1297
1298 if (STRNCMP(p, "<SNR>", 5) == 0)
1299 p = skipdigits(p + 5);
1300 p = to_name_end(p, TRUE);
1301 if (p == arg && *arg == '[')
1302 {
1303
1304 // Can be "[1, 2, 3]->Func()".
1305 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1306 p = arg;
1307 }
1308 return p;
1309}
1310
1311/*
1312 * parse a list: [expr, expr]
1313 * "*arg" points to the '['.
1314 * ppconst->pp_is_const is set if all items are a constant.
1315 */
1316 static int
1317compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1318{
1319 char_u *p = skipwhite(*arg + 1);
1320 char_u *whitep = *arg + 1;
1321 int count = 0;
1322 int is_const;
1323 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001324 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001325
1326 for (;;)
1327 {
1328 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1329 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001330 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331 return FAIL;
1332 }
1333 if (*p == ',')
1334 {
1335 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1336 return FAIL;
1337 }
1338 if (*p == ']')
1339 {
1340 ++p;
1341 break;
1342 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001343 if (must_end)
1344 {
1345 semsg(_(e_missing_comma_in_list_str), p);
1346 return FAIL;
1347 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1349 return FAIL;
1350 if (!is_const)
1351 is_all_const = FALSE;
1352 ++count;
1353 if (*p == ',')
1354 {
1355 ++p;
1356 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1357 {
1358 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1359 return FAIL;
1360 }
1361 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001362 else
1363 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364 whitep = p;
1365 p = skipwhite(p);
1366 }
1367 *arg = p;
1368
1369 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001370 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371}
1372
1373/*
1374 * Parse a lambda: "(arg, arg) => expr"
1375 * "*arg" points to the '('.
1376 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1377 */
1378 static int
1379compile_lambda(char_u **arg, cctx_T *cctx)
1380{
1381 int r;
1382 typval_T rettv;
1383 ufunc_T *ufunc;
1384 evalarg_T evalarg;
1385
1386 init_evalarg(&evalarg);
1387 evalarg.eval_flags = EVAL_EVALUATE;
1388 evalarg.eval_cctx = cctx;
1389
1390 // Get the funcref in "rettv".
1391 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1392 if (r != OK)
1393 {
1394 clear_evalarg(&evalarg, NULL);
1395 return r;
1396 }
1397
1398 // "rettv" will now be a partial referencing the function.
1399 ufunc = rettv.vval.v_partial->pt_func;
1400 ++ufunc->uf_refcount;
1401 clear_tv(&rettv);
1402
1403 // Compile it here to get the return type. The return type is optional,
1404 // when it's missing use t_unknown. This is recognized in
1405 // compile_return().
1406 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1407 ufunc->uf_ret_type = &t_unknown;
1408 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1409
1410 // When the outer function is compiled for profiling or debugging, the
1411 // lambda may be called without profiling or debugging. Compile it here in
1412 // the right context.
1413 if (cctx->ctx_compile_type == CT_DEBUG
1414#ifdef FEAT_PROFILE
1415 || cctx->ctx_compile_type == CT_PROFILE
1416#endif
1417 )
1418 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1419
Bram Moolenaar139575d2022-03-15 19:29:30 +00001420 // if the outer function is not compiled for debugging or profiling, this
1421 // one might be
1422 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001423 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001424 compiletype_T compile_type = get_compile_type(ufunc);
1425
1426 if (compile_type != CT_NONE)
1427 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001428 }
1429
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001430 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1431 // "*arg" may point into it. Point into the original line to avoid a
1432 // dangling pointer.
1433 if (evalarg.eval_using_cmdline)
1434 {
1435 garray_T *gap = &evalarg.eval_tofree_ga;
1436 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1437
1438 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1439 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001440 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441 }
1442
1443 clear_evalarg(&evalarg, NULL);
1444
1445 if (ufunc->uf_def_status == UF_COMPILED)
1446 {
1447 // The return type will now be known.
1448 set_function_type(ufunc);
1449
1450 // The function reference count will be 1. When the ISN_FUNCREF
1451 // instruction is deleted the reference count is decremented and the
1452 // function is freed.
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001453 return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 }
1455
1456 func_ptr_unref(ufunc);
1457 return FAIL;
1458}
1459
1460/*
1461 * Get a lambda and compile it. Uses Vim9 syntax.
1462 */
1463 int
1464get_lambda_tv_and_compile(
1465 char_u **arg,
1466 typval_T *rettv,
1467 int types_optional,
1468 evalarg_T *evalarg)
1469{
1470 int r;
1471 ufunc_T *ufunc;
1472 int save_sc_version = current_sctx.sc_version;
1473
1474 // Get the funcref in "rettv".
1475 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1476 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1477 current_sctx.sc_version = save_sc_version;
1478 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001479 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480
1481 // "rettv" will now be a partial referencing the function.
1482 ufunc = rettv->vval.v_partial->pt_func;
1483
1484 // Compile it here to get the return type. The return type is optional,
1485 // when it's missing use t_unknown. This is recognized in
1486 // compile_return().
1487 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1488 ufunc->uf_ret_type = &t_unknown;
1489 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1490
1491 if (ufunc->uf_def_status == UF_COMPILED)
1492 {
1493 // The return type will now be known.
1494 set_function_type(ufunc);
1495 return OK;
1496 }
1497 clear_tv(rettv);
1498 return FAIL;
1499}
1500
1501/*
1502 * parse a dict: {key: val, [key]: val}
1503 * "*arg" points to the '{'.
1504 * ppconst->pp_is_const is set if all item values are a constant.
1505 */
1506 static int
1507compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1508{
1509 garray_T *instr = &cctx->ctx_instr;
1510 int count = 0;
1511 dict_T *d = dict_alloc();
1512 dictitem_T *item;
1513 char_u *whitep = *arg + 1;
1514 char_u *p;
1515 int is_const;
1516 int is_all_const = TRUE; // reset when non-const encountered
1517
1518 if (d == NULL)
1519 return FAIL;
1520 if (generate_ppconst(cctx, ppconst) == FAIL)
1521 return FAIL;
1522 for (;;)
1523 {
1524 char_u *key = NULL;
1525
1526 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1527 {
1528 *arg = NULL;
1529 goto failret;
1530 }
1531
1532 if (**arg == '}')
1533 break;
1534
1535 if (**arg == '[')
1536 {
1537 isn_T *isn;
1538
1539 // {[expr]: value} uses an evaluated key.
1540 *arg = skipwhite(*arg + 1);
1541 if (compile_expr0(arg, cctx) == FAIL)
1542 return FAIL;
1543 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1544 if (isn->isn_type == ISN_PUSHNR)
1545 {
1546 char buf[NUMBUFLEN];
1547
1548 // Convert to string at compile time.
1549 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1550 isn->isn_type = ISN_PUSHS;
1551 isn->isn_arg.string = vim_strsave((char_u *)buf);
1552 }
1553 if (isn->isn_type == ISN_PUSHS)
1554 key = isn->isn_arg.string;
1555 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1556 return FAIL;
1557 *arg = skipwhite(*arg);
1558 if (**arg != ']')
1559 {
1560 emsg(_(e_missing_matching_bracket_after_dict_key));
1561 return FAIL;
1562 }
1563 ++*arg;
1564 }
1565 else
1566 {
1567 // {"name": value},
1568 // {'name': value},
1569 // {name: value} use "name" as a literal key
1570 key = get_literal_key(arg);
1571 if (key == NULL)
1572 return FAIL;
1573 if (generate_PUSHS(cctx, &key) == FAIL)
1574 return FAIL;
1575 }
1576
1577 // Check for duplicate keys, if using string keys.
1578 if (key != NULL)
1579 {
1580 item = dict_find(d, key, -1);
1581 if (item != NULL)
1582 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001583 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001584 goto failret;
1585 }
1586 item = dictitem_alloc(key);
1587 if (item != NULL)
1588 {
1589 item->di_tv.v_type = VAR_UNKNOWN;
1590 item->di_tv.v_lock = 0;
1591 if (dict_add(d, item) == FAIL)
1592 dictitem_free(item);
1593 }
1594 }
1595
1596 if (**arg != ':')
1597 {
1598 if (*skipwhite(*arg) == ':')
1599 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1600 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001601 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001602 return FAIL;
1603 }
1604 whitep = *arg + 1;
1605 if (!IS_WHITE_OR_NUL(*whitep))
1606 {
1607 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1608 return FAIL;
1609 }
1610
1611 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1612 {
1613 *arg = NULL;
1614 goto failret;
1615 }
1616
1617 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1618 return FAIL;
1619 if (!is_const)
1620 is_all_const = FALSE;
1621 ++count;
1622
1623 whitep = *arg;
1624 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1625 {
1626 *arg = NULL;
1627 goto failret;
1628 }
1629 if (**arg == '}')
1630 break;
1631 if (**arg != ',')
1632 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001633 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001634 goto failret;
1635 }
1636 if (IS_WHITE_OR_NUL(*whitep))
1637 {
1638 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1639 return FAIL;
1640 }
1641 whitep = *arg + 1;
1642 if (!IS_WHITE_OR_NUL(*whitep))
1643 {
1644 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1645 return FAIL;
1646 }
1647 *arg = skipwhite(whitep);
1648 }
1649
1650 *arg = *arg + 1;
1651
1652 // Allow for following comment, after at least one space.
1653 p = skipwhite(*arg);
1654 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1655 *arg += STRLEN(*arg);
1656
1657 dict_unref(d);
1658 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001659 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001660
1661failret:
1662 if (*arg == NULL)
1663 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001664 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001665 *arg = (char_u *)"";
1666 }
1667 dict_unref(d);
1668 return FAIL;
1669}
1670
1671/*
1672 * Compile "&option".
1673 */
1674 static int
1675compile_get_option(char_u **arg, cctx_T *cctx)
1676{
1677 typval_T rettv;
1678 char_u *start = *arg;
1679 int ret;
1680
1681 // parse the option and get the current value to get the type.
1682 rettv.v_type = VAR_UNKNOWN;
1683 ret = eval_option(arg, &rettv, TRUE);
1684 if (ret == OK)
1685 {
1686 // include the '&' in the name, eval_option() expects it.
1687 char_u *name = vim_strnsave(start, *arg - start);
1688 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1689 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1690
1691 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1692 vim_free(name);
1693 }
1694 clear_tv(&rettv);
1695
1696 return ret;
1697}
1698
1699/*
1700 * Compile "$VAR".
1701 */
1702 static int
1703compile_get_env(char_u **arg, cctx_T *cctx)
1704{
1705 char_u *start = *arg;
1706 int len;
1707 int ret;
1708 char_u *name;
1709
1710 ++*arg;
1711 len = get_env_len(arg);
1712 if (len == 0)
1713 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001714 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715 return FAIL;
1716 }
1717
1718 // include the '$' in the name, eval_env_var() expects it.
1719 name = vim_strnsave(start, len + 1);
1720 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1721 vim_free(name);
1722 return ret;
1723}
1724
1725/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001726 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001727 */
1728 static int
1729compile_interp_string(char_u **arg, cctx_T *cctx)
1730{
1731 typval_T tv;
1732 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001733 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001734 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001735 int count = 0;
1736 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001737
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001738 // *arg is on the '$' character, move it to the first string character.
1739 ++*arg;
1740 quote = **arg;
1741 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001742
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001743 for (;;)
1744 {
1745 // Get the string up to the matching quote or to a single '{'.
1746 // "arg" is advanced to either the quote or the '{'.
1747 if (quote == '"')
1748 ret = eval_string(arg, &tv, evaluate, TRUE);
1749 else
1750 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1751 if (ret == FAIL)
1752 break;
1753 if (evaluate)
1754 {
1755 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1756 || (**arg != '{' && count == 0))
1757 {
1758 // generate non-empty string or empty string if it's the only
1759 // one
1760 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1761 return FAIL;
1762 tv.vval.v_string = NULL; // don't free it now
1763 ++count;
1764 }
1765 clear_tv(&tv);
1766 }
1767
1768 if (**arg != '{')
1769 {
1770 // found terminating quote
1771 ++*arg;
1772 break;
1773 }
1774
1775 p = compile_one_expr_in_str(*arg, cctx);
1776 if (p == NULL)
1777 {
1778 ret = FAIL;
1779 break;
1780 }
1781 ++count;
1782 *arg = p;
1783 }
LemonBoy2eaef102022-05-06 13:14:50 +01001784
1785 if (ret == FAIL || !evaluate)
1786 return ret;
1787
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001788 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1789 if (count > 1)
1790 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001791
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001792 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001793}
1794
1795/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001796 * Compile "@r".
1797 */
1798 static int
1799compile_get_register(char_u **arg, cctx_T *cctx)
1800{
1801 int ret;
1802
1803 ++*arg;
1804 if (**arg == NUL)
1805 {
1806 semsg(_(e_syntax_error_at_str), *arg - 1);
1807 return FAIL;
1808 }
1809 if (!valid_yank_reg(**arg, FALSE))
1810 {
1811 emsg_invreg(**arg);
1812 return FAIL;
1813 }
1814 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1815 ++*arg;
1816 return ret;
1817}
1818
1819/*
1820 * Apply leading '!', '-' and '+' to constant "rettv".
1821 * When "numeric_only" is TRUE do not apply '!'.
1822 */
1823 static int
1824apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1825{
1826 char_u *p = *end;
1827
1828 // this works from end to start
1829 while (p > start)
1830 {
1831 --p;
1832 if (*p == '-' || *p == '+')
1833 {
1834 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001835 if (rettv->v_type == VAR_FLOAT)
1836 {
1837 if (*p == '-')
1838 rettv->vval.v_float = -rettv->vval.v_float;
1839 }
1840 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001841 {
1842 varnumber_T val;
1843 int error = FALSE;
1844
1845 // tv_get_number_chk() accepts a string, but we don't want that
1846 // here
1847 if (check_not_string(rettv) == FAIL)
1848 return FAIL;
1849 val = tv_get_number_chk(rettv, &error);
1850 clear_tv(rettv);
1851 if (error)
1852 return FAIL;
1853 if (*p == '-')
1854 val = -val;
1855 rettv->v_type = VAR_NUMBER;
1856 rettv->vval.v_number = val;
1857 }
1858 }
1859 else if (numeric_only)
1860 {
1861 ++p;
1862 break;
1863 }
1864 else if (*p == '!')
1865 {
1866 int v = tv2bool(rettv);
1867
1868 // '!' is permissive in the type.
1869 clear_tv(rettv);
1870 rettv->v_type = VAR_BOOL;
1871 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1872 }
1873 }
1874 *end = p;
1875 return OK;
1876}
1877
1878/*
1879 * Recognize v: variables that are constants and set "rettv".
1880 */
1881 static void
1882get_vim_constant(char_u **arg, typval_T *rettv)
1883{
1884 if (STRNCMP(*arg, "v:true", 6) == 0)
1885 {
1886 rettv->v_type = VAR_BOOL;
1887 rettv->vval.v_number = VVAL_TRUE;
1888 *arg += 6;
1889 }
1890 else if (STRNCMP(*arg, "v:false", 7) == 0)
1891 {
1892 rettv->v_type = VAR_BOOL;
1893 rettv->vval.v_number = VVAL_FALSE;
1894 *arg += 7;
1895 }
1896 else if (STRNCMP(*arg, "v:null", 6) == 0)
1897 {
1898 rettv->v_type = VAR_SPECIAL;
1899 rettv->vval.v_number = VVAL_NULL;
1900 *arg += 6;
1901 }
1902 else if (STRNCMP(*arg, "v:none", 6) == 0)
1903 {
1904 rettv->v_type = VAR_SPECIAL;
1905 rettv->vval.v_number = VVAL_NONE;
1906 *arg += 6;
1907 }
1908}
1909
1910 exprtype_T
1911get_compare_type(char_u *p, int *len, int *type_is)
1912{
1913 exprtype_T type = EXPR_UNKNOWN;
1914 int i;
1915
1916 switch (p[0])
1917 {
1918 case '=': if (p[1] == '=')
1919 type = EXPR_EQUAL;
1920 else if (p[1] == '~')
1921 type = EXPR_MATCH;
1922 break;
1923 case '!': if (p[1] == '=')
1924 type = EXPR_NEQUAL;
1925 else if (p[1] == '~')
1926 type = EXPR_NOMATCH;
1927 break;
1928 case '>': if (p[1] != '=')
1929 {
1930 type = EXPR_GREATER;
1931 *len = 1;
1932 }
1933 else
1934 type = EXPR_GEQUAL;
1935 break;
1936 case '<': if (p[1] != '=')
1937 {
1938 type = EXPR_SMALLER;
1939 *len = 1;
1940 }
1941 else
1942 type = EXPR_SEQUAL;
1943 break;
1944 case 'i': if (p[1] == 's')
1945 {
1946 // "is" and "isnot"; but not a prefix of a name
1947 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1948 *len = 5;
1949 i = p[*len];
1950 if (!isalnum(i) && i != '_')
1951 {
1952 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1953 *type_is = TRUE;
1954 }
1955 }
1956 break;
1957 }
1958 return type;
1959}
1960
1961/*
1962 * Skip over an expression, ignoring most errors.
1963 */
1964 void
1965skip_expr_cctx(char_u **arg, cctx_T *cctx)
1966{
1967 evalarg_T evalarg;
1968
1969 init_evalarg(&evalarg);
1970 evalarg.eval_cctx = cctx;
1971 skip_expr(arg, &evalarg);
1972 clear_evalarg(&evalarg, NULL);
1973}
1974
1975/*
1976 * Check that the top of the type stack has a type that can be used as a
1977 * condition. Give an error and return FAIL if not.
1978 */
1979 int
1980bool_on_stack(cctx_T *cctx)
1981{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982 type_T *type;
1983
Bram Moolenaar078a4612022-01-04 15:17:03 +00001984 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001985 if (type == &t_bool)
1986 return OK;
1987
Bram Moolenaar4913d422022-10-17 13:13:32 +01001988 if (type->tt_type == VAR_ANY
1989 || type->tt_type == VAR_UNKNOWN
1990 || type->tt_type == VAR_NUMBER
1991 || type == &t_number_bool
1992 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001993 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1994 // This requires a runtime type check.
1995 return generate_COND2BOOL(cctx);
1996
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001997 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001998}
1999
2000/*
2001 * Give the "white on both sides" error, taking the operator from "p[len]".
2002 */
2003 void
2004error_white_both(char_u *op, int len)
2005{
2006 char_u buf[10];
2007
2008 vim_strncpy(buf, op, len);
2009 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
2010}
2011
2012/*
2013 * Compile code to apply '-', '+' and '!'.
2014 * When "numeric_only" is TRUE do not apply '!'.
2015 */
2016 static int
2017compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
2018{
2019 char_u *p = *end;
2020
2021 // this works from end to start
2022 while (p > start)
2023 {
2024 --p;
2025 while (VIM_ISWHITE(*p))
2026 --p;
2027 if (*p == '-' || *p == '+')
2028 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00002029 type_T *type = get_type_on_stack(cctx, 0);
2030 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002031 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032 return FAIL;
2033
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00002035 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
2036 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002037 }
2038 else if (numeric_only)
2039 {
2040 ++p;
2041 break;
2042 }
2043 else
2044 {
2045 int invert = *p == '!';
2046
2047 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
2048 {
2049 if (p[-1] == '!')
2050 invert = !invert;
2051 --p;
2052 }
2053 if (generate_2BOOL(cctx, invert, -1) == FAIL)
2054 return FAIL;
2055 }
2056 }
2057 *end = p;
2058 return OK;
2059}
2060
2061/*
2062 * Compile "(expression)": recursive!
2063 * Return FAIL/OK.
2064 */
2065 static int
2066compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2067{
2068 int ret;
2069 char_u *p = *arg + 1;
2070
2071 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2072 return FAIL;
2073 if (ppconst->pp_used <= PPSIZE - 10)
2074 {
2075 ret = compile_expr1(arg, cctx, ppconst);
2076 }
2077 else
2078 {
2079 // Not enough space in ppconst, flush constants.
2080 if (generate_ppconst(cctx, ppconst) == FAIL)
2081 return FAIL;
2082 ret = compile_expr0(arg, cctx);
2083 }
2084 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2085 return FAIL;
2086 if (**arg == ')')
2087 ++*arg;
2088 else if (ret == OK)
2089 {
2090 emsg(_(e_missing_closing_paren));
2091 ret = FAIL;
2092 }
2093 return ret;
2094}
2095
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002096static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002097
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098/*
2099 * Compile whatever comes after "name" or "name()".
2100 * Advances "*arg" only when something was recognized.
2101 */
2102 static int
2103compile_subscript(
2104 char_u **arg,
2105 cctx_T *cctx,
2106 char_u *start_leader,
2107 char_u **end_leader,
2108 ppconst_T *ppconst)
2109{
2110 char_u *name_start = *end_leader;
2111 int keeping_dict = FALSE;
2112
2113 for (;;)
2114 {
2115 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002116 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002117
2118 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2119 {
2120 char_u *next = peek_next_line_from_context(cctx);
2121
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002122 // If a following line starts with "->{", "->(" or "->X" advance to
2123 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124 // Also if a following line starts with ".x".
2125 if (next != NULL &&
2126 ((next[0] == '-' && next[1] == '>'
2127 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002128 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129 || ASCII_ISALPHA(*skipwhite(next + 2))))
2130 || (next[0] == '.' && eval_isdictc(next[1]))))
2131 {
2132 next = next_line_from_context(cctx, TRUE);
2133 if (next == NULL)
2134 return FAIL;
2135 *arg = next;
2136 p = skipwhite(*arg);
2137 }
2138 }
2139
2140 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2141 // is not a function call.
2142 if (**arg == '(')
2143 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002144 int argcount = 0;
2145
2146 if (generate_ppconst(cctx, ppconst) == FAIL)
2147 return FAIL;
2148 ppconst->pp_is_const = FALSE;
2149
2150 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002151 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152
2153 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002154 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002155 return FAIL;
2156 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2157 return FAIL;
2158 if (keeping_dict)
2159 {
2160 keeping_dict = FALSE;
2161 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2162 return FAIL;
2163 }
2164 }
2165 else if (*p == '-' && p[1] == '>')
2166 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002167 char_u *pstart = p;
2168 int alt;
2169 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002170
Bram Moolenaarc7349932022-01-16 20:59:39 +00002171 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002172 if (generate_ppconst(cctx, ppconst) == FAIL)
2173 return FAIL;
2174 ppconst->pp_is_const = FALSE;
2175
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002176 // Apply the '!', '-' and '+' first:
2177 // -1.0->func() works like (-1.0)->func()
2178 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2179 return FAIL;
2180
2181 p += 2;
2182 *arg = skipwhite(p);
2183 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002184
2185 // Three alternatives handled here:
2186 // 1. "base->name(" only a name, use compile_call()
2187 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2188 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002189 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002190 alt = 2;
2191 else
2192 {
2193 // alternative 1 or 3
2194 p = *arg;
2195 if (!eval_isnamec1(*p))
2196 {
2197 semsg(_(e_trailing_characters_str), pstart);
2198 return FAIL;
2199 }
2200 if (ASCII_ISALPHA(*p) && p[1] == ':')
2201 p += 2;
2202 for ( ; eval_isnamec(*p); ++p)
2203 ;
2204 if (*p == '(')
2205 {
2206 // alternative 1
2207 alt = 1;
2208 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2209 return FAIL;
2210 }
2211 else
2212 {
2213 // Must be alternative 3, find the "(". Only works within
2214 // one line.
2215 alt = 3;
2216 paren = vim_strchr(p, '(');
2217 if (paren == NULL)
2218 {
2219 semsg(_(e_missing_parenthesis_str), *arg);
2220 return FAIL;
2221 }
2222 }
2223 }
2224
2225 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 {
2227 int argcount = 1;
2228 garray_T *stack = &cctx->ctx_type_stack;
2229 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002230 int expr_isn_start = cctx->ctx_instr.ga_len;
2231 int expr_isn_end;
2232 int arg_isn_count;
2233
Bram Moolenaarc7349932022-01-16 20:59:39 +00002234 if (alt == 2)
2235 {
2236 // Funcref call: list->(Refs[2])(arg)
2237 // or lambda: list->((arg) => expr)(arg)
2238 //
2239 // Fist compile the function expression.
2240 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2241 return FAIL;
2242 }
2243 else
2244 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002245 int fail;
2246 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002247 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002248
Bram Moolenaarc7349932022-01-16 20:59:39 +00002249 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002250
2251 // instead of using LOADG for "import.Func" use PUSHFUNC
2252 ++paren_follows_after_expr;
2253
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002254 // do not look in the next line
2255 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002256
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002257 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002258 || *skipwhite(*arg) != NUL;
2259 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002260 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002261 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002262
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002263 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002264 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002265 if (did_emsg == prev_did_emsg)
2266 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002267 return FAIL;
2268 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002269 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002271 // Compile the arguments.
2272 if (**arg != '(')
2273 {
2274 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002275 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276 else
2277 semsg(_(e_missing_parenthesis_str), *arg);
2278 return FAIL;
2279 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002280
2281 // Remember the next instruction index, where the instructions
2282 // for arguments are being written.
2283 expr_isn_end = cctx->ctx_instr.ga_len;
2284
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002285 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002286 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2287 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002288 return FAIL;
2289
2290 // Move the instructions for the arguments to before the
2291 // instructions of the expression and move the type of the
2292 // expression after the argument types. This is what ISN_PCALL
2293 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2295 if (arg_isn_count > 0)
2296 {
2297 int expr_isn_count = expr_isn_end - expr_isn_start;
2298 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002299 type_T *decl_type;
2300 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002301
2302 if (isn == NULL)
2303 return FAIL;
2304 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2305 + expr_isn_start,
2306 sizeof(isn_T) * expr_isn_count);
2307 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2308 + expr_isn_start,
2309 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2310 sizeof(isn_T) * arg_isn_count);
2311 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2312 + expr_isn_start + arg_isn_count,
2313 isn, sizeof(isn_T) * expr_isn_count);
2314 vim_free(isn);
2315
Bram Moolenaar078a4612022-01-04 15:17:03 +00002316 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2317 type = typep->type_curr;
2318 decl_type = typep->type_decl;
2319 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2320 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2321 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002322 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002323 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2324 typep->type_curr = type;
2325 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 }
2327
Bram Moolenaar078a4612022-01-04 15:17:03 +00002328 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2330 return FAIL;
2331 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002332
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002333 if (keeping_dict)
2334 {
2335 keeping_dict = FALSE;
2336 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2337 return FAIL;
2338 }
2339 }
2340 else if (**arg == '[')
2341 {
2342 int is_slice = FALSE;
2343
2344 // list index: list[123]
2345 // dict member: dict[key]
2346 // string index: text[123]
2347 // blob index: blob[123]
2348 if (generate_ppconst(cctx, ppconst) == FAIL)
2349 return FAIL;
2350 ppconst->pp_is_const = FALSE;
2351
2352 ++p;
2353 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2354 return FAIL;
2355 if (**arg == ':')
2356 {
2357 // missing first index is equal to zero
2358 generate_PUSHNR(cctx, 0);
2359 }
2360 else
2361 {
2362 if (compile_expr0(arg, cctx) == FAIL)
2363 return FAIL;
2364 if (**arg == ':')
2365 {
2366 semsg(_(e_white_space_required_before_and_after_str_at_str),
2367 ":", *arg);
2368 return FAIL;
2369 }
2370 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2371 return FAIL;
2372 *arg = skipwhite(*arg);
2373 }
2374 if (**arg == ':')
2375 {
2376 is_slice = TRUE;
2377 ++*arg;
2378 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2379 {
2380 semsg(_(e_white_space_required_before_and_after_str_at_str),
2381 ":", *arg);
2382 return FAIL;
2383 }
2384 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2385 return FAIL;
2386 if (**arg == ']')
2387 // missing second index is equal to end of string
2388 generate_PUSHNR(cctx, -1);
2389 else
2390 {
2391 if (compile_expr0(arg, cctx) == FAIL)
2392 return FAIL;
2393 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2394 return FAIL;
2395 *arg = skipwhite(*arg);
2396 }
2397 }
2398
2399 if (**arg != ']')
2400 {
2401 emsg(_(e_missing_closing_square_brace));
2402 return FAIL;
2403 }
2404 *arg = *arg + 1;
2405
2406 if (keeping_dict)
2407 {
2408 keeping_dict = FALSE;
2409 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2410 return FAIL;
2411 }
2412 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2413 return FAIL;
2414 }
2415 else if (*p == '.' && p[1] != '.')
2416 {
2417 // dictionary member: dict.name
2418 if (generate_ppconst(cctx, ppconst) == FAIL)
2419 return FAIL;
2420 ppconst->pp_is_const = FALSE;
2421
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002422 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2423 && (type->tt_type == VAR_CLASS
2424 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002425 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002426 // class member: SomeClass.varname
2427 // class method: SomeClass.SomeMethod()
2428 // class constructor: SomeClass.new()
2429 // object member: someObject.varname, this.varname
2430 // object method: someObject.SomeMethod(), this.SomeMethod()
2431 *arg = p;
2432 if (compile_class_object_index(cctx, arg, type) == FAIL)
2433 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002434 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002435 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002437 *arg = p + 1;
2438 if (IS_WHITE_OR_NUL(**arg))
2439 {
2440 emsg(_(e_missing_name_after_dot));
2441 return FAIL;
2442 }
2443 p = *arg;
2444 if (eval_isdictc(*p))
2445 while (eval_isnamec(*p))
2446 MB_PTR_ADV(p);
2447 if (p == *arg)
2448 {
2449 semsg(_(e_syntax_error_at_str), *arg);
2450 return FAIL;
2451 }
2452 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2453 return FAIL;
2454 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2455 return FAIL;
2456 keeping_dict = TRUE;
2457 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002458 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002459 }
2460 else
2461 break;
2462 }
2463
2464 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2465 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002466 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2467 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002468 return FAIL;
2469
2470 return OK;
2471}
2472
2473/*
2474 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2475 * "arg" is advanced until after the expression, skipping white space.
2476 *
2477 * If the value is a constant "ppconst->pp_used" will be non-zero.
2478 * Before instructions are generated, any values in "ppconst" will generated.
2479 *
2480 * This is the compiling equivalent of eval1(), eval2(), etc.
2481 */
2482
2483/*
2484 * number number constant
2485 * 0zFFFFFFFF Blob constant
2486 * "string" string constant
2487 * 'string' literal string constant
2488 * &option-name option value
2489 * @r register contents
2490 * identifier variable value
2491 * function() function call
2492 * $VAR environment variable
2493 * (expression) nested expression
2494 * [expr, expr] List
2495 * {key: val, [key]: val} Dictionary
2496 *
2497 * Also handle:
2498 * ! in front logical NOT
2499 * - in front unary minus
2500 * + in front unary plus (ignored)
2501 * trailing (arg) funcref/partial call
2502 * trailing [] subscript in String or List
2503 * trailing .name entry in Dictionary
2504 * trailing ->name() method call
2505 */
2506 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002507compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002508 char_u **arg,
2509 cctx_T *cctx,
2510 ppconst_T *ppconst)
2511{
2512 char_u *start_leader, *end_leader;
2513 int ret = OK;
2514 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2515 int used_before = ppconst->pp_used;
2516
2517 ppconst->pp_is_const = FALSE;
2518
2519 /*
2520 * Skip '!', '-' and '+' characters. They are handled later.
2521 */
2522 start_leader = *arg;
2523 if (eval_leader(arg, TRUE) == FAIL)
2524 return FAIL;
2525 end_leader = *arg;
2526
2527 rettv->v_type = VAR_UNKNOWN;
2528 switch (**arg)
2529 {
2530 /*
2531 * Number constant.
2532 */
2533 case '0': // also for blob starting with 0z
2534 case '1':
2535 case '2':
2536 case '3':
2537 case '4':
2538 case '5':
2539 case '6':
2540 case '7':
2541 case '8':
2542 case '9':
2543 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2544 return FAIL;
2545 // Apply "-" and "+" just before the number now, right to
2546 // left. Matters especially when "->" follows. Stops at
2547 // '!'.
2548 if (apply_leader(rettv, TRUE,
2549 start_leader, &end_leader) == FAIL)
2550 {
2551 clear_tv(rettv);
2552 return FAIL;
2553 }
2554 break;
2555
2556 /*
2557 * String constant: "string".
2558 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002559 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002560 return FAIL;
2561 break;
2562
2563 /*
2564 * Literal string constant: 'str''ing'.
2565 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002566 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002567 return FAIL;
2568 break;
2569
2570 /*
2571 * Constant Vim variable.
2572 */
2573 case 'v': get_vim_constant(arg, rettv);
2574 ret = NOTDONE;
2575 break;
2576
2577 /*
2578 * "true" constant
2579 */
2580 case 't': if (STRNCMP(*arg, "true", 4) == 0
2581 && !eval_isnamec((*arg)[4]))
2582 {
2583 *arg += 4;
2584 rettv->v_type = VAR_BOOL;
2585 rettv->vval.v_number = VVAL_TRUE;
2586 }
2587 else
2588 ret = NOTDONE;
2589 break;
2590
2591 /*
2592 * "false" constant
2593 */
2594 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2595 && !eval_isnamec((*arg)[5]))
2596 {
2597 *arg += 5;
2598 rettv->v_type = VAR_BOOL;
2599 rettv->vval.v_number = VVAL_FALSE;
2600 }
2601 else
2602 ret = NOTDONE;
2603 break;
2604
2605 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002606 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002607 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002608 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002609 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002610 char_u *p = *arg + 4;
2611 int len;
2612
2613 for (len = 0; eval_isnamec(p[len]); ++len)
2614 ;
2615 ret = handle_predefined(*arg, len + 4, rettv);
2616 if (ret == FAIL)
2617 ret = NOTDONE;
2618 else
2619 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002620 }
2621 else
2622 ret = NOTDONE;
2623 break;
2624
2625 /*
2626 * List: [expr, expr]
2627 */
2628 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2629 return FAIL;
2630 ret = compile_list(arg, cctx, ppconst);
2631 break;
2632
2633 /*
2634 * Dictionary: {'key': val, 'key': val}
2635 */
2636 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2637 return FAIL;
2638 ret = compile_dict(arg, cctx, ppconst);
2639 break;
2640
2641 /*
2642 * Option value: &name
2643 */
2644 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2645 return FAIL;
2646 ret = compile_get_option(arg, cctx);
2647 break;
2648
2649 /*
2650 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002651 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002652 */
2653 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2654 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002655 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2656 ret = compile_interp_string(arg, cctx);
2657 else
2658 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659 break;
2660
2661 /*
2662 * Register contents: @r.
2663 */
2664 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2665 return FAIL;
2666 ret = compile_get_register(arg, cctx);
2667 break;
2668 /*
2669 * nested expression: (expression).
2670 * lambda: (arg, arg) => expr
2671 * funcref: (arg, arg) => { statement }
2672 */
2673 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2674 ret = compile_lambda(arg, cctx);
2675 if (ret == NOTDONE)
2676 ret = compile_parenthesis(arg, cctx, ppconst);
2677 break;
2678
2679 default: ret = NOTDONE;
2680 break;
2681 }
2682 if (ret == FAIL)
2683 return FAIL;
2684
2685 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2686 {
2687 if (cctx->ctx_skip == SKIP_YES)
2688 clear_tv(rettv);
2689 else
2690 // A constant expression can possibly be handled compile time,
2691 // return the value instead of generating code.
2692 ++ppconst->pp_used;
2693 }
2694 else if (ret == NOTDONE)
2695 {
2696 char_u *p;
2697 int r;
2698
2699 if (!eval_isnamec1(**arg))
2700 {
2701 if (!vim9_bad_comment(*arg))
2702 {
2703 if (ends_excmd(*skipwhite(*arg)))
2704 semsg(_(e_empty_expression_str), *arg);
2705 else
2706 semsg(_(e_name_expected_str), *arg);
2707 }
2708 return FAIL;
2709 }
2710
2711 // "name" or "name()"
2712 p = to_name_end(*arg, TRUE);
2713 if (p - *arg == (size_t)1 && **arg == '_')
2714 {
2715 emsg(_(e_cannot_use_underscore_here));
2716 return FAIL;
2717 }
2718
2719 if (*p == '(')
2720 {
2721 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2722 }
2723 else
2724 {
2725 if (cctx->ctx_skip != SKIP_YES
2726 && generate_ppconst(cctx, ppconst) == FAIL)
2727 return FAIL;
2728 r = compile_load(arg, p, cctx, TRUE, TRUE);
2729 }
2730 if (r == FAIL)
2731 return FAIL;
2732 }
2733
2734 // Handle following "[]", ".member", etc.
2735 // Then deal with prefixed '-', '+' and '!', if not done already.
2736 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2737 ppconst) == FAIL)
2738 return FAIL;
2739 if (ppconst->pp_used > 0)
2740 {
2741 // apply the '!', '-' and '+' before the constant
2742 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2743 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2744 return FAIL;
2745 return OK;
2746 }
2747 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2748 return FAIL;
2749 return OK;
2750}
2751
2752/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002753 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002754 */
2755 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002756compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002757{
2758 type_T *want_type = NULL;
2759
2760 // Recognize <type>
2761 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2762 {
2763 ++*arg;
2764 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2765 if (want_type == NULL)
2766 return FAIL;
2767
2768 if (**arg != '>')
2769 {
2770 if (*skipwhite(*arg) == '>')
2771 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2772 else
2773 emsg(_(e_missing_gt));
2774 return FAIL;
2775 }
2776 ++*arg;
2777 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2778 return FAIL;
2779 }
2780
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002781 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002782 return FAIL;
2783
2784 if (want_type != NULL)
2785 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002786 type_T *actual;
2787 where_T where = WHERE_INIT;
2788
2789 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002790 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002791 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002792 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002793 if (need_type(actual, want_type, FALSE,
2794 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002795 return FAIL;
2796 }
2797 }
2798
2799 return OK;
2800}
2801
2802/*
2803 * * number multiplication
2804 * / number division
2805 * % number modulo
2806 */
2807 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002808compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002809{
2810 char_u *op;
2811 char_u *next;
2812 int ppconst_used = ppconst->pp_used;
2813
2814 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002815 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002816 return FAIL;
2817
2818 /*
2819 * Repeat computing, until no "*", "/" or "%" is following.
2820 */
2821 for (;;)
2822 {
2823 op = may_peek_next_line(cctx, *arg, &next);
2824 if (*op != '*' && *op != '/' && *op != '%')
2825 break;
2826 if (next != NULL)
2827 {
2828 *arg = next_line_from_context(cctx, TRUE);
2829 op = skipwhite(*arg);
2830 }
2831
2832 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2833 {
2834 error_white_both(op, 1);
2835 return FAIL;
2836 }
2837 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2838 return FAIL;
2839
2840 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002841 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002842 return FAIL;
2843
2844 if (ppconst->pp_used == ppconst_used + 2
2845 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2846 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2847 {
2848 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2849 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2850 varnumber_T res = 0;
2851 int failed = FALSE;
2852
2853 // both are numbers: compute the result
2854 switch (*op)
2855 {
2856 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2857 break;
2858 case '/': res = num_divide(tv1->vval.v_number,
2859 tv2->vval.v_number, &failed);
2860 break;
2861 case '%': res = num_modulus(tv1->vval.v_number,
2862 tv2->vval.v_number, &failed);
2863 break;
2864 }
2865 if (failed)
2866 return FAIL;
2867 tv1->vval.v_number = res;
2868 --ppconst->pp_used;
2869 }
2870 else
2871 {
2872 generate_ppconst(cctx, ppconst);
2873 generate_two_op(cctx, op);
2874 }
2875 }
2876
2877 return OK;
2878}
2879
2880/*
2881 * + number addition or list/blobl concatenation
2882 * - number subtraction
2883 * .. string concatenation
2884 */
2885 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002886compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002887{
2888 char_u *op;
2889 char_u *next;
2890 int oplen;
2891 int ppconst_used = ppconst->pp_used;
2892
2893 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002894 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002895 return FAIL;
2896
2897 /*
2898 * Repeat computing, until no "+", "-" or ".." is following.
2899 */
2900 for (;;)
2901 {
2902 op = may_peek_next_line(cctx, *arg, &next);
2903 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2904 break;
2905 if (op[0] == op[1] && *op != '.' && next)
2906 // Finding "++" or "--" on the next line is a separate command.
2907 // But ".." is concatenation.
2908 break;
2909 oplen = (*op == '.' ? 2 : 1);
2910 if (next != NULL)
2911 {
2912 *arg = next_line_from_context(cctx, TRUE);
2913 op = skipwhite(*arg);
2914 }
2915
2916 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2917 {
2918 error_white_both(op, oplen);
2919 return FAIL;
2920 }
2921
2922 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2923 return FAIL;
2924
2925 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002926 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002927 return FAIL;
2928
2929 if (ppconst->pp_used == ppconst_used + 2
2930 && (*op == '.'
2931 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2932 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2933 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2934 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2935 {
2936 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2937 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2938
2939 // concat/subtract/add constant numbers
2940 if (*op == '+')
2941 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2942 else if (*op == '-')
2943 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2944 else
2945 {
2946 // concatenate constant strings
2947 char_u *s1 = tv1->vval.v_string;
2948 char_u *s2 = tv2->vval.v_string;
2949 size_t len1 = STRLEN(s1);
2950
2951 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2952 if (tv1->vval.v_string == NULL)
2953 {
2954 clear_ppconst(ppconst);
2955 return FAIL;
2956 }
2957 mch_memmove(tv1->vval.v_string, s1, len1);
2958 STRCPY(tv1->vval.v_string + len1, s2);
2959 vim_free(s1);
2960 vim_free(s2);
2961 }
2962 --ppconst->pp_used;
2963 }
2964 else
2965 {
2966 generate_ppconst(cctx, ppconst);
2967 ppconst->pp_is_const = FALSE;
2968 if (*op == '.')
2969 {
2970 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2971 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2972 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002973 if (generate_CONCAT(cctx, 2) == FAIL)
2974 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002975 }
2976 else
2977 generate_two_op(cctx, op);
2978 }
2979 }
2980
2981 return OK;
2982}
2983
2984/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002985 * expr6a >> expr6b
2986 * expr6a << expr6b
2987 *
2988 * Produces instructions:
2989 * OPNR bitwise left or right shift
2990 */
2991 static int
2992compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2993{
2994 exprtype_T type = EXPR_UNKNOWN;
2995 char_u *p;
2996 char_u *next;
2997 int len = 2;
2998 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002999 isn_T *isn;
3000
3001 // get the first variable
3002 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3003 return FAIL;
3004
3005 /*
3006 * Repeat computing, until no "+", "-" or ".." is following.
3007 */
3008 for (;;)
3009 {
3010 type = EXPR_UNKNOWN;
3011
3012 p = may_peek_next_line(cctx, *arg, &next);
3013 if (p[0] == '<' && p[1] == '<')
3014 type = EXPR_LSHIFT;
3015 else if (p[0] == '>' && p[1] == '>')
3016 type = EXPR_RSHIFT;
3017
3018 if (type == EXPR_UNKNOWN)
3019 return OK;
3020
3021 // Handle a bitwise left or right shift operator
3022 if (ppconst->pp_used == ppconst_used + 1)
3023 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003024 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003025 {
3026 // left operand should be a number
3027 emsg(_(e_bitshift_ops_must_be_number));
3028 return FAIL;
3029 }
3030 }
3031 else
3032 {
3033 type_T *t = get_type_on_stack(cctx, 0);
3034
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003035 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003036 {
3037 emsg(_(e_bitshift_ops_must_be_number));
3038 return FAIL;
3039 }
3040 }
3041
3042 if (next != NULL)
3043 {
3044 *arg = next_line_from_context(cctx, TRUE);
3045 p = skipwhite(*arg);
3046 }
3047
3048 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3049 {
3050 error_white_both(p, len);
3051 return FAIL;
3052 }
3053
3054 // get the second variable
3055 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3056 return FAIL;
3057
3058 if (compile_expr6(arg, cctx, ppconst) == FAIL)
3059 return FAIL;
3060
3061 if (ppconst->pp_used == ppconst_used + 2)
3062 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003063 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3064 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3065
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003066 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003067 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3068 {
3069 // right operand should be a positive number
3070 if (tv2->v_type != VAR_NUMBER)
3071 emsg(_(e_bitshift_ops_must_be_number));
3072 else
dundargocc57b5bc2022-11-02 13:30:51 +00003073 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003074 return FAIL;
3075 }
3076
3077 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3078 tv1->vval.v_number = 0;
3079 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003080 tv1->vval.v_number =
3081 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003082 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003083 tv1->vval.v_number =
3084 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003085 clear_tv(tv2);
3086 --ppconst->pp_used;
3087 }
3088 else
3089 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003090 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3091 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003092 {
3093 emsg(_(e_bitshift_ops_must_be_number));
3094 return FAIL;
3095 }
3096
3097 generate_ppconst(cctx, ppconst);
3098
3099 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3100 if (isn == NULL)
3101 return FAIL;
3102
3103 if (isn != NULL)
3104 isn->isn_arg.op.op_type = type;
3105 }
3106 }
3107
3108 return OK;
3109}
3110
3111/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003112 * expr5a == expr5b
3113 * expr5a =~ expr5b
3114 * expr5a != expr5b
3115 * expr5a !~ expr5b
3116 * expr5a > expr5b
3117 * expr5a >= expr5b
3118 * expr5a < expr5b
3119 * expr5a <= expr5b
3120 * expr5a is expr5b
3121 * expr5a isnot expr5b
3122 *
3123 * Produces instructions:
3124 * EVAL expr5a Push result of "expr5a"
3125 * EVAL expr5b Push result of "expr5b"
3126 * COMPARE one of the compare instructions
3127 */
3128 static int
3129compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3130{
3131 exprtype_T type = EXPR_UNKNOWN;
3132 char_u *p;
3133 char_u *next;
3134 int len = 2;
3135 int type_is = FALSE;
3136 int ppconst_used = ppconst->pp_used;
3137
3138 // get the first variable
3139 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3140 return FAIL;
3141
3142 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003143
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003144 type = get_compare_type(p, &len, &type_is);
3145
3146 /*
3147 * If there is a comparative operator, use it.
3148 */
3149 if (type != EXPR_UNKNOWN)
3150 {
3151 int ic = FALSE; // Default: do not ignore case
3152
3153 if (next != NULL)
3154 {
3155 *arg = next_line_from_context(cctx, TRUE);
3156 p = skipwhite(*arg);
3157 }
3158 if (type_is && (p[len] == '?' || p[len] == '#'))
3159 {
3160 semsg(_(e_invalid_expression_str), *arg);
3161 return FAIL;
3162 }
3163 // extra question mark appended: ignore case
3164 if (p[len] == '?')
3165 {
3166 ic = TRUE;
3167 ++len;
3168 }
3169 // extra '#' appended: match case (ignored)
3170 else if (p[len] == '#')
3171 ++len;
3172 // nothing appended: match case
3173
3174 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3175 {
3176 error_white_both(p, len);
3177 return FAIL;
3178 }
3179
3180 // get the second variable
3181 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3182 return FAIL;
3183
3184 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3185 return FAIL;
3186
3187 if (ppconst->pp_used == ppconst_used + 2)
3188 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003189 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003190 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3191 int ret;
3192
3193 // Both sides are a constant, compute the result now.
3194 // First check for a valid combination of types, this is more
3195 // strict than typval_compare().
3196 if (check_compare_types(type, tv1, tv2) == FAIL)
3197 ret = FAIL;
3198 else
3199 {
3200 ret = typval_compare(tv1, tv2, type, ic);
3201 tv1->v_type = VAR_BOOL;
3202 tv1->vval.v_number = tv1->vval.v_number
3203 ? VVAL_TRUE : VVAL_FALSE;
3204 clear_tv(tv2);
3205 --ppconst->pp_used;
3206 }
3207 return ret;
3208 }
3209
3210 generate_ppconst(cctx, ppconst);
3211 return generate_COMPARE(cctx, type, ic);
3212 }
3213
3214 return OK;
3215}
3216
3217static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3218
3219/*
3220 * Compile || or &&.
3221 */
3222 static int
3223compile_and_or(
3224 char_u **arg,
3225 cctx_T *cctx,
3226 char *op,
3227 ppconst_T *ppconst,
3228 int ppconst_used UNUSED)
3229{
3230 char_u *next;
3231 char_u *p = may_peek_next_line(cctx, *arg, &next);
3232 int opchar = *op;
3233
3234 if (p[0] == opchar && p[1] == opchar)
3235 {
3236 garray_T *instr = &cctx->ctx_instr;
3237 garray_T end_ga;
3238 int save_skip = cctx->ctx_skip;
3239
3240 /*
3241 * Repeat until there is no following "||" or "&&"
3242 */
3243 ga_init2(&end_ga, sizeof(int), 10);
3244 while (p[0] == opchar && p[1] == opchar)
3245 {
3246 long start_lnum = SOURCING_LNUM;
3247 long save_sourcing_lnum;
3248 int start_ctx_lnum = cctx->ctx_lnum;
3249 int save_lnum;
3250 int const_used;
3251 int status;
3252 jumpwhen_T jump_when = opchar == '|'
3253 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3254
3255 if (next != NULL)
3256 {
3257 *arg = next_line_from_context(cctx, TRUE);
3258 p = skipwhite(*arg);
3259 }
3260
3261 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3262 {
3263 semsg(_(e_white_space_required_before_and_after_str_at_str),
3264 op, p);
3265 ga_clear(&end_ga);
3266 return FAIL;
3267 }
3268
3269 save_sourcing_lnum = SOURCING_LNUM;
3270 SOURCING_LNUM = start_lnum;
3271 save_lnum = cctx->ctx_lnum;
3272 cctx->ctx_lnum = start_ctx_lnum;
3273
3274 status = check_ppconst_bool(ppconst);
3275 if (status != FAIL)
3276 {
3277 // Use the last ppconst if possible.
3278 if (ppconst->pp_used > 0)
3279 {
3280 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3281 int is_true = tv2bool(tv);
3282
3283 if ((is_true && opchar == '|')
3284 || (!is_true && opchar == '&'))
3285 {
3286 // For "false && expr" and "true || expr" the "expr"
3287 // does not need to be evaluated.
3288 cctx->ctx_skip = SKIP_YES;
3289 clear_tv(tv);
3290 tv->v_type = VAR_BOOL;
3291 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3292 }
3293 else
3294 {
3295 // For "true && expr" and "false || expr" only "expr"
3296 // needs to be evaluated.
3297 --ppconst->pp_used;
3298 jump_when = JUMP_NEVER;
3299 }
3300 }
3301 else
3302 {
3303 // Every part must evaluate to a bool.
3304 status = bool_on_stack(cctx);
3305 }
3306 }
3307 if (status != FAIL)
3308 status = ga_grow(&end_ga, 1);
3309 cctx->ctx_lnum = save_lnum;
3310 if (status == FAIL)
3311 {
3312 ga_clear(&end_ga);
3313 return FAIL;
3314 }
3315
3316 if (jump_when != JUMP_NEVER)
3317 {
3318 if (cctx->ctx_skip != SKIP_YES)
3319 {
3320 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3321 ++end_ga.ga_len;
3322 }
3323 generate_JUMP(cctx, jump_when, 0);
3324 }
3325
3326 // eval the next expression
3327 SOURCING_LNUM = save_sourcing_lnum;
3328 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3329 {
3330 ga_clear(&end_ga);
3331 return FAIL;
3332 }
3333
3334 const_used = ppconst->pp_used;
3335 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3336 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3337 {
3338 ga_clear(&end_ga);
3339 return FAIL;
3340 }
3341
3342 // "0 || 1" results in true, "1 && 0" results in false.
3343 if (ppconst->pp_used == const_used + 1)
3344 {
3345 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3346
3347 if (tv->v_type == VAR_NUMBER
3348 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3349 {
3350 tv->vval.v_number = tv->vval.v_number == 1
3351 ? VVAL_TRUE : VVAL_FALSE;
3352 tv->v_type = VAR_BOOL;
3353 }
3354 }
3355
3356 p = may_peek_next_line(cctx, *arg, &next);
3357 }
3358
3359 if (check_ppconst_bool(ppconst) == FAIL)
3360 {
3361 ga_clear(&end_ga);
3362 return FAIL;
3363 }
3364
3365 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3366 // Every part must evaluate to a bool.
3367 if (bool_on_stack(cctx) == FAIL)
3368 {
3369 ga_clear(&end_ga);
3370 return FAIL;
3371 }
3372
3373 if (end_ga.ga_len > 0)
3374 {
3375 // Fill in the end label in all jumps.
3376 generate_ppconst(cctx, ppconst);
3377 while (end_ga.ga_len > 0)
3378 {
3379 isn_T *isn;
3380
3381 --end_ga.ga_len;
3382 isn = ((isn_T *)instr->ga_data)
3383 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3384 isn->isn_arg.jump.jump_where = instr->ga_len;
3385 }
3386 }
3387 ga_clear(&end_ga);
3388
3389 cctx->ctx_skip = save_skip;
3390 }
3391
3392 return OK;
3393}
3394
3395/*
3396 * expr4a && expr4a && expr4a logical AND
3397 *
3398 * Produces instructions:
3399 * EVAL expr4a Push result of "expr4a"
3400 * COND2BOOL convert to bool if needed
3401 * JUMP_IF_COND_FALSE end
3402 * EVAL expr4b Push result of "expr4b"
3403 * JUMP_IF_COND_FALSE end
3404 * EVAL expr4c Push result of "expr4c"
3405 * end:
3406 */
3407 static int
3408compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3409{
3410 int ppconst_used = ppconst->pp_used;
3411
3412 // get the first variable
3413 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3414 return FAIL;
3415
3416 // || and && work almost the same
3417 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3418}
3419
3420/*
3421 * expr3a || expr3b || expr3c logical OR
3422 *
3423 * Produces instructions:
3424 * EVAL expr3a Push result of "expr3a"
3425 * COND2BOOL convert to bool if needed
3426 * JUMP_IF_COND_TRUE end
3427 * EVAL expr3b Push result of "expr3b"
3428 * JUMP_IF_COND_TRUE end
3429 * EVAL expr3c Push result of "expr3c"
3430 * end:
3431 */
3432 static int
3433compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3434{
3435 int ppconst_used = ppconst->pp_used;
3436
3437 // eval the first expression
3438 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3439 return FAIL;
3440
3441 // || and && work almost the same
3442 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3443}
3444
3445/*
3446 * Toplevel expression: expr2 ? expr1a : expr1b
3447 * Produces instructions:
3448 * EVAL expr2 Push result of "expr2"
3449 * JUMP_IF_FALSE alt jump if false
3450 * EVAL expr1a
3451 * JUMP_ALWAYS end
3452 * alt: EVAL expr1b
3453 * end:
3454 *
3455 * Toplevel expression: expr2 ?? expr1
3456 * Produces instructions:
3457 * EVAL expr2 Push result of "expr2"
3458 * JUMP_AND_KEEP_IF_TRUE end jump if true
3459 * EVAL expr1
3460 * end:
3461 */
3462 int
3463compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3464{
3465 char_u *p;
3466 int ppconst_used = ppconst->pp_used;
3467 char_u *next;
3468
3469 // Ignore all kinds of errors when not producing code.
3470 if (cctx->ctx_skip == SKIP_YES)
3471 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003472 int prev_did_emsg = did_emsg;
3473
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003474 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003475 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003476 }
3477
3478 // Evaluate the first expression.
3479 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3480 return FAIL;
3481
3482 p = may_peek_next_line(cctx, *arg, &next);
3483 if (*p == '?')
3484 {
3485 int op_falsy = p[1] == '?';
3486 garray_T *instr = &cctx->ctx_instr;
3487 garray_T *stack = &cctx->ctx_type_stack;
3488 int alt_idx = instr->ga_len;
3489 int end_idx = 0;
3490 isn_T *isn;
3491 type_T *type1 = NULL;
3492 int has_const_expr = FALSE;
3493 int const_value = FALSE;
3494 int save_skip = cctx->ctx_skip;
3495
3496 if (next != NULL)
3497 {
3498 *arg = next_line_from_context(cctx, TRUE);
3499 p = skipwhite(*arg);
3500 }
3501
3502 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3503 {
3504 semsg(_(e_white_space_required_before_and_after_str_at_str),
3505 op_falsy ? "??" : "?", p);
3506 return FAIL;
3507 }
3508
3509 if (ppconst->pp_used == ppconst_used + 1)
3510 {
3511 // the condition is a constant, we know whether the ? or the :
3512 // expression is to be evaluated.
3513 has_const_expr = TRUE;
3514 if (op_falsy)
3515 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3516 else
3517 {
3518 int error = FALSE;
3519
3520 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3521 &error);
3522 if (error)
3523 return FAIL;
3524 }
3525 cctx->ctx_skip = save_skip == SKIP_YES ||
3526 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3527
3528 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3529 // "left ?? right" and "left" is truthy: produce "left"
3530 generate_ppconst(cctx, ppconst);
3531 else
3532 {
3533 clear_tv(&ppconst->pp_tv[ppconst_used]);
3534 --ppconst->pp_used;
3535 }
3536 }
3537 else
3538 {
3539 generate_ppconst(cctx, ppconst);
3540 if (op_falsy)
3541 end_idx = instr->ga_len;
3542 generate_JUMP(cctx, op_falsy
3543 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3544 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003545 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003546 }
3547
3548 // evaluate the second expression; any type is accepted
3549 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3550 return FAIL;
3551 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3552 return FAIL;
3553
3554 if (!has_const_expr)
3555 {
3556 generate_ppconst(cctx, ppconst);
3557
3558 if (!op_falsy)
3559 {
3560 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003561 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003562 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003563
3564 end_idx = instr->ga_len;
3565 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3566
3567 // jump here from JUMP_IF_FALSE
3568 isn = ((isn_T *)instr->ga_data) + alt_idx;
3569 isn->isn_arg.jump.jump_where = instr->ga_len;
3570 }
3571 }
3572
3573 if (!op_falsy)
3574 {
3575 // Check for the ":".
3576 p = may_peek_next_line(cctx, *arg, &next);
3577 if (*p != ':')
3578 {
3579 emsg(_(e_missing_colon_after_questionmark));
3580 return FAIL;
3581 }
3582 if (next != NULL)
3583 {
3584 *arg = next_line_from_context(cctx, TRUE);
3585 p = skipwhite(*arg);
3586 }
3587
3588 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3589 {
3590 semsg(_(e_white_space_required_before_and_after_str_at_str),
3591 ":", p);
3592 return FAIL;
3593 }
3594
3595 // evaluate the third expression
3596 if (has_const_expr)
3597 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3598 ? SKIP_YES : SKIP_NOT;
3599 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3600 return FAIL;
3601 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3602 return FAIL;
3603 }
3604
3605 if (!has_const_expr)
3606 {
3607 type_T **typep;
3608
3609 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003610 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003611
3612 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003613 typep = &((((type2_T *)stack->ga_data)
3614 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003615 common_type(type1, *typep, typep, cctx->ctx_type_list);
3616
3617 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3618 isn = ((isn_T *)instr->ga_data) + end_idx;
3619 isn->isn_arg.jump.jump_where = instr->ga_len;
3620 }
3621
3622 cctx->ctx_skip = save_skip;
3623 }
3624 return OK;
3625}
3626
3627/*
3628 * Toplevel expression.
3629 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3630 * Returns OK or FAIL.
3631 */
3632 int
3633compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3634{
3635 ppconst_T ppconst;
3636
3637 CLEAR_FIELD(ppconst);
3638 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3639 {
3640 clear_ppconst(&ppconst);
3641 return FAIL;
3642 }
3643 if (is_const != NULL)
3644 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3645 if (generate_ppconst(cctx, &ppconst) == FAIL)
3646 return FAIL;
3647 return OK;
3648}
3649
3650/*
3651 * Toplevel expression.
3652 */
3653 int
3654compile_expr0(char_u **arg, cctx_T *cctx)
3655{
3656 return compile_expr0_ext(arg, cctx, NULL);
3657}
3658
3659
3660#endif // defined(FEAT_EVAL)