blob: 5a302d68843f596fb8eb62371c52d13d727f836a [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{
284 if (VIM_ISWHITE((*arg)[1]))
285 {
286 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
287 return FAIL;
288 }
289
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000290 class_T *cl = type->tt_class;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000291 int is_super = type->tt_flags & TTFLAG_SUPER;
292 if (type == &t_super)
293 {
294 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000295 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200296 emsg(_(e_using_super_not_in_class_method));
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000297 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000298 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000299 is_super = TRUE;
300 cl = cctx->ctx_ufunc->uf_class;
301 // Remove &t_super from the stack.
302 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000303 }
304 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000305 {
306 garray_T *instr = &cctx->ctx_instr;
307 if (instr->ga_len > 0)
308 {
309 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
310 if (isn->isn_type == ISN_LOADSCRIPT)
311 {
312 // The class was recognized as a script item. We only need
313 // to know what class it is, drop the instruction.
314 --instr->ga_len;
315 vim_free(isn->isn_arg.script.scriptref);
316 }
317 }
318 }
319
Ernie Raelf77a7f72023-03-03 15:05:30 +0000320 if (cl == NULL)
321 {
322 // TODO: this should not give an error but be handled at runtime
323 emsg(_(e_incomplete_type));
324 return FAIL;
325 }
326
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000327 ++*arg;
328 char_u *name = *arg;
329 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
330 if (name_end == name)
331 return FAIL;
332 size_t len = name_end - name;
333
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000334 if (*name_end == '(')
335 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000336 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000337 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000338 ufunc_T **functions;
339
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000340 if (type->tt_type == VAR_CLASS)
341 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000342 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000343 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000344 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000345 }
346 else
347 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000348 // type->tt_type == VAR_OBJECT: method call
349 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000350 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000351 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000352 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000353
354 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000355 int fi;
356 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000357 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000358 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359 // Use a separate pointer to avoid that ASAN complains about
360 // uf_name[] only being 4 characters.
361 char_u *ufname = (char_u *)fp->uf_name;
362 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
363 {
364 ufunc = fp;
365 break;
366 }
367 }
368 if (ufunc == NULL)
369 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200370 method_not_found_msg(cl, type->tt_type, name, len);
Bram Moolenaar574950d2023-01-03 19:08:50 +0000371 return FAIL;
372 }
373
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200374 // A private object method can be used only inside the class where it
375 // is defined or in one of the child classes.
376 // A private class method can be used only in the class where it is
377 // defined.
378 if (*ufunc->uf_name == '_' &&
379 ((type->tt_type == VAR_OBJECT
380 && !inside_class_hierarchy(cctx, cl))
381 || (type->tt_type == VAR_CLASS
382 && cctx->ctx_ufunc->uf_class != cl)))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200383 {
384 semsg(_(e_cannot_access_private_method_str), name);
385 return FAIL;
386 }
387
Bram Moolenaar574950d2023-01-03 19:08:50 +0000388 // Compile the arguments and call the class function or object method.
389 // The object method will know that the object is on the stack, just
390 // before the arguments.
391 *arg = skipwhite(name_end + 1);
392 int argcount = 0;
393 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
394 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000395
396 if (type->tt_type == VAR_OBJECT
397 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-eastb895b0f2023-09-24 15:46:31 +0200398 return generate_CALL(cctx, ufunc, cl, fi, argcount);
399 return generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000400 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000401
402 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000403 {
Ernie Rael4d00b832023-09-11 19:54:42 +0200404 int m_idx;
405 ocmember_T *m = object_member_lookup(cl, name, len, &m_idx);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200406 if (m_idx >= 0)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000407 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200408 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000409 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200410 semsg(_(e_cannot_access_private_variable_str), m->ocm_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200411 return FAIL;
Ernie Rael18143d32023-09-04 22:30:41 +0200412 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200413
414 *arg = name_end;
415 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200416 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type);
417 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type);
Ernie Rael18143d32023-09-04 22:30:41 +0200418 }
419
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000420 // Could be a function reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200421 m_idx = object_method_idx(cl, name, len);
422 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000423 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200424 ufunc_T *fp = cl->class_obj_methods[m_idx];
425 if (type->tt_type == VAR_OBJECT
426 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
427 return generate_FUNCREF(cctx, fp, cl, m_idx, NULL);
428 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000429 }
430
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200431 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000432 }
433 else
434 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000435 // load class member
436 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200437 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
438 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000439 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200440 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200441 // A private class variable can be accessed only in the class where
442 // it is defined.
443 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200444 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200445 semsg(_(e_cannot_access_private_variable_str), m->ocm_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200446 return FAIL;
447 }
448
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000449 *arg = name_end;
450 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
451 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200452 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000453 }
454
455 return FAIL;
456}
457
458/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 * Generate an instruction to load script-local variable "name", without the
460 * leading "s:".
461 * Also finds imported variables.
462 */
463 int
464compile_load_scriptvar(
465 cctx_T *cctx,
466 char_u *name, // variable NUL terminated
467 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100468 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469{
470 scriptitem_T *si;
471 int idx;
472 imported_T *import;
473
474 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
475 return FAIL;
476 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000477 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 if (idx >= 0)
479 {
480 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
481
482 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
483 current_sctx.sc_sid, idx, sv->sv_type);
484 return OK;
485 }
486
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000487 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000488 if (import != NULL)
489 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000490 char_u *p = skipwhite(*end);
491 char_u *exp_name;
492 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100493 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000494 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000495 int done = FALSE;
496 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000497
498 // Need to lookup the member.
499 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000501 semsg(_(e_expected_dot_after_name_str), start);
502 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000504 ++p;
505 if (VIM_ISWHITE(*p))
506 {
507 emsg(_(e_no_white_space_allowed_after_dot));
508 return FAIL;
509 }
510
511 // isolate one name
512 exp_name = p;
513 while (eval_isnamec(*p))
514 ++p;
515 cc = *p;
516 *p = NUL;
517
Bram Moolenaard041f422022-01-12 19:54:00 +0000518 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100519 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
520 // "import autoload './dir/script.vim'" or
521 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100522 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100523
524 if (res == OK)
525 {
526 if (si->sn_autoload_prefix != NULL
527 && si->sn_state == SN_STATE_NOT_LOADED)
528 {
529 char_u *auto_name =
530 concat_str(si->sn_autoload_prefix, exp_name);
531
532 // autoload script must be loaded later, access by the autoload
533 // name. If a '(' follows it must be a function. Otherwise we
534 // don't know, it can be "script.Func".
535 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100536 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100537 else
538 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
539 vim_free(auto_name);
540 done = TRUE;
541 }
542 else if (si->sn_import_autoload
543 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100544 {
545 // If a '(' follows it must be a function. Otherwise we don't
546 // know, it can be "script.Func".
547 if (cc == '(' || paren_follows_after_expr)
548 {
549 char_u sid_name[MAX_FUNC_NAME_LEN];
550
551 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100552 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100553 }
554 else
555 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
556 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100557 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100558 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100559 else
560 {
561 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
562 cctx, NULL, TRUE);
563 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100564 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100565
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000566 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000567 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000568 if (done)
569 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000570
571 if (idx < 0)
572 {
573 if (ufunc != NULL)
574 {
575 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100576 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000577 return OK;
578 }
579 return FAIL;
580 }
581
582 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
583 import->imp_sid,
584 idx,
585 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000586 return OK;
587 }
588
Bram Moolenaard0132f42022-05-12 11:05:40 +0100589 // Can only get here if we know "name" is a script variable and not in a
590 // Vim9 script (variable is not in sn_var_vals): old style script.
591 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000592 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000593}
594
595 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000596generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000597{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000598 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000599 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000601 // Reject a global non-autoload function found without the "g:" prefix.
602 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000603 return FAIL;
604
605 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000606 compile_type = get_compile_type(ufunc);
607 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000608 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000609 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100610 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611}
612
613/*
614 * Compile a variable name into a load instruction.
615 * "end" points to just after the name.
616 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
617 * When "error" is FALSE do not give an error when not found.
618 */
619 int
620compile_load(
621 char_u **arg,
622 char_u *end_arg,
623 cctx_T *cctx,
624 int is_expr,
625 int error)
626{
627 type_T *type;
628 char_u *name = NULL;
629 char_u *end = end_arg;
630 int res = FAIL;
631 int prev_called_emsg = called_emsg;
632
633 if (*(*arg + 1) == ':')
634 {
635 if (end <= *arg + 2)
636 {
637 isntype_T isn_type;
638
639 // load dictionary of namespace
640 switch (**arg)
641 {
642 case 'g': isn_type = ISN_LOADGDICT; break;
643 case 'w': isn_type = ISN_LOADWDICT; break;
644 case 't': isn_type = ISN_LOADTDICT; break;
645 case 'b': isn_type = ISN_LOADBDICT; break;
646 default:
647 semsg(_(e_namespace_not_supported_str), *arg);
648 goto theend;
649 }
650 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
651 goto theend;
652 res = OK;
653 }
654 else
655 {
656 isntype_T isn_type = ISN_DROP;
657
658 // load namespaced variable
659 name = vim_strnsave(*arg + 2, end - (*arg + 2));
660 if (name == NULL)
661 return FAIL;
662
663 switch (**arg)
664 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100665 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000667 case 's': if (current_script_is_vim9())
668 {
669 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
670 *arg);
671 vim_free(name);
672 return FAIL;
673 }
Kota Kato948a3892022-08-16 16:09:59 +0100674 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000675 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676 else
677 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100678 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000679 break;
680 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
681 {
682 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000683 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000684 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000685 else
686 isn_type = ISN_LOADG;
687 }
688 else
689 {
690 isn_type = ISN_LOADAUTO;
691 vim_free(name);
692 name = vim_strnsave(*arg, end - *arg);
693 if (name == NULL)
694 return FAIL;
695 }
696 break;
697 case 'w': isn_type = ISN_LOADW; break;
698 case 't': isn_type = ISN_LOADT; break;
699 case 'b': isn_type = ISN_LOADB; break;
700 default: // cannot happen, just in case
701 semsg(_(e_namespace_not_supported_str), *arg);
702 goto theend;
703 }
704 if (isn_type != ISN_DROP)
705 {
706 // Global, Buffer-local, Window-local and Tabpage-local
707 // variables can be defined later, thus we don't check if it
708 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000709 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000710 }
711 }
712 }
713 else
714 {
715 size_t len = end - *arg;
716 int idx;
717 int gen_load = FALSE;
718 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100719 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100720 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721
722 name = vim_strnsave(*arg, end - *arg);
723 if (name == NULL)
724 return FAIL;
725
Bram Moolenaar58b40092023-01-11 15:59:05 +0000726 if (STRCMP(name, "super") == 0
727 && cctx->ctx_ufunc != NULL
728 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
729 {
730 // super.SomeFunc() in a class function: push &t_super type, this
731 // is recognized in compile_subscript().
732 res = push_type_stack(cctx, &t_super);
733 if (*end != '.')
734 emsg(_(e_super_must_be_followed_by_dot));
735 }
736 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737 {
738 script_autoload(name, FALSE);
739 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
740 }
741 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
742 == OK)
743 {
744 if (gen_load_outer == 0)
745 gen_load = TRUE;
746 }
747 else
748 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000749 lvar_T lvar;
750 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751
752 if (lookup_local(*arg, len, &lvar, cctx) == OK)
753 {
754 type = lvar.lv_type;
755 idx = lvar.lv_idx;
756 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100757 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100759 outer_loop_depth = lvar.lv_loop_depth;
760 outer_loop_idx = lvar.lv_loop_idx;
761 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 else
763 gen_load = TRUE;
764 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200765 else if ((idx = cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000766 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200767 // Referencing a class variable without the class name.
768 // A class variable can be referenced without the class name
769 // only in the class where the function is defined.
770 if (cctx->ctx_ufunc->uf_defclass == cl)
771 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
772 else
773 {
RestorerZ7fe8f432023-09-24 23:21:24 +0200774 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200775 name, cl->class_name);
776 res = FAIL;
777 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000778 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 else
780 {
781 // "var" can be script-local even without using "s:" if it
782 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000783 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000784 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100785 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786
787 // When evaluating an expression and the name starts with an
788 // uppercase letter it can be a user defined function.
789 // generate_funcref() will fail if the function can't be found.
790 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000791 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 }
793 }
794 if (gen_load)
795 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
796 if (gen_load_outer > 0)
797 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100798 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
799 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000800 cctx->ctx_outer_used = TRUE;
801 }
802 }
803
804 *arg = end;
805
806theend:
807 if (res == FAIL && error && called_emsg == prev_called_emsg)
808 semsg(_(e_variable_not_found_str), name);
809 vim_free(name);
810 return res;
811}
812
813/*
814 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100815 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 * Returns FAIL if compilation fails.
817 */
818 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100819compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820{
LemonBoyf3b48952022-05-05 13:53:03 +0100821 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 garray_T save_ga = cctx->ctx_instr;
823 int expr_res;
824 int trailing_error;
825 int instr_count;
826 isn_T *instr = NULL;
827
828 // Remove the string type from the stack.
829 --cctx->ctx_type_stack.ga_len;
830
831 // Temporarily reset the list of instructions so that the jump labels are
832 // correct.
833 cctx->ctx_instr.ga_len = 0;
834 cctx->ctx_instr.ga_maxlen = 0;
835 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000836
837 // avoid peeking a next line
838 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
839 cctx->ctx_ufunc->uf_lines.ga_len = 0;
840
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000841 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000842
843 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
844
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845 s = skipwhite(s);
846 trailing_error = *s != NUL;
847
848 if (expr_res == FAIL || trailing_error
849 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
850 {
851 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000852 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 clear_instr_ga(&cctx->ctx_instr);
854 cctx->ctx_instr = save_ga;
855 ++cctx->ctx_type_stack.ga_len;
856 return FAIL;
857 }
858
859 // Move the generated instructions into the ISN_INSTR instruction, then
860 // restore the list of instructions.
861 instr_count = cctx->ctx_instr.ga_len;
862 instr = cctx->ctx_instr.ga_data;
863 instr[instr_count].isn_type = ISN_FINISH;
864
865 cctx->ctx_instr = save_ga;
866 vim_free(isn->isn_arg.string);
867 isn->isn_type = ISN_INSTR;
868 isn->isn_arg.instr = instr;
869 return OK;
870}
871
872/*
873 * Compile the argument expressions.
874 * "arg" points to just after the "(" and is advanced to after the ")"
875 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100876 int
LemonBoyf3b48952022-05-05 13:53:03 +0100877compile_arguments(
878 char_u **arg,
879 cctx_T *cctx,
880 int *argcount,
881 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000882{
883 char_u *p = *arg;
884 char_u *whitep = *arg;
885 int must_end = FALSE;
886 int instr_count;
887
888 for (;;)
889 {
890 if (may_get_next_line(whitep, &p, cctx) == FAIL)
891 goto failret;
892 if (*p == ')')
893 {
894 *arg = p + 1;
895 return OK;
896 }
897 if (must_end)
898 {
899 semsg(_(e_missing_comma_before_argument_str), p);
900 return FAIL;
901 }
902
903 instr_count = cctx->ctx_instr.ga_len;
904 if (compile_expr0(&p, cctx) == FAIL)
905 return FAIL;
906 ++*argcount;
907
LemonBoyf3b48952022-05-05 13:53:03 +0100908 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000909 && cctx->ctx_instr.ga_len == instr_count + 1)
910 {
911 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
912
913 // {skip} argument of searchpair() can be compiled if not empty
914 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100915 compile_string(isn, cctx, 0);
916 }
917 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
918 && cctx->ctx_instr.ga_len == instr_count + 1)
919 {
920 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
921
922 // {sub} argument of substitute() can be compiled if it starts
923 // with \=
924 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100925 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100926 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 }
928
929 if (*p != ',' && *skipwhite(p) == ',')
930 {
931 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
932 p = skipwhite(p);
933 }
934 if (*p == ',')
935 {
936 ++p;
937 if (*p != NUL && !VIM_ISWHITE(*p))
938 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
939 }
940 else
941 must_end = TRUE;
942 whitep = p;
943 p = skipwhite(p);
944 }
945failret:
946 emsg(_(e_missing_closing_paren));
947 return FAIL;
948}
949
950/*
951 * Compile a function call: name(arg1, arg2)
952 * "arg" points to "name", "arg + varlen" to the "(".
953 * "argcount_init" is 1 for "value->method()"
954 * Instructions:
955 * EVAL arg1
956 * EVAL arg2
957 * BCALL / DCALL / UCALL
958 */
959 static int
960compile_call(
961 char_u **arg,
962 size_t varlen,
963 cctx_T *cctx,
964 ppconst_T *ppconst,
965 int argcount_init)
966{
967 char_u *name = *arg;
968 char_u *p;
969 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100970 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971 char_u fname_buf[FLEN_FIXED + 1];
972 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000973 ufunc_T *ufunc = NULL;
974 int res = FAIL;
975 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000976 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100977 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000978 imported_T *import;
979
980 if (varlen >= sizeof(namebuf))
981 {
982 semsg(_(e_name_too_long_str), name);
983 return FAIL;
984 }
985 vim_strncpy(namebuf, *arg, varlen);
986
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000987 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000988 if (import != NULL)
989 {
990 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
991 return FAIL;
992 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993
994 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100995 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100997 if ((varlen == 3
998 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000999 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1000 {
1001 char_u *s = skipwhite(*arg + varlen + 1);
1002 typval_T argvars[2];
1003 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001004 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001005
1006 argvars[0].v_type = VAR_UNKNOWN;
1007 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001008 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001009 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001010 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011 s = skipwhite(s);
1012 if (*s == ')' && argvars[0].v_type == VAR_STRING
1013 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1014 || !is_has))
1015 {
1016 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1017
1018 *arg = s + 1;
1019 argvars[1].v_type = VAR_UNKNOWN;
1020 tv->v_type = VAR_NUMBER;
1021 tv->vval.v_number = 0;
1022 if (is_has)
1023 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001024 else if (is_len)
1025 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001026 else
1027 f_exists(argvars, tv);
1028 clear_tv(&argvars[0]);
1029 ++ppconst->pp_used;
1030 return OK;
1031 }
1032 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001033 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001034 {
1035 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1036 return FAIL;
1037 }
1038 }
1039
1040 if (generate_ppconst(cctx, ppconst) == FAIL)
1041 return FAIL;
1042
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001043 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1045
1046 // We handle the "skip" argument of searchpair() and searchpairpos()
1047 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001048 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1049 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1050 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1051 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1052 special_fn = CA_SEARCHPAIR;
1053 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1054 special_fn = CA_SUBSTITUTE;
1055 else
1056 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001057
1058 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001059 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001060 goto theend;
1061
1062 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1063 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1064 {
1065 int idx;
1066
1067 // builtin function
1068 idx = find_internal_func(name);
1069 if (idx >= 0)
1070 {
1071 if (STRCMP(name, "flatten") == 0)
1072 {
1073 emsg(_(e_cannot_use_flatten_in_vim9_script));
1074 goto theend;
1075 }
1076
1077 if (STRCMP(name, "add") == 0 && argcount == 2)
1078 {
h-eastb895b0f2023-09-24 15:46:31 +02001079 type_T *type = get_decl_type_on_stack(cctx, 1);
1080
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 // add() can be compiled to instructions if we know the type
1082 if (type->tt_type == VAR_LIST)
1083 {
1084 // inline "add(list, item)" so that the type can be checked
1085 res = generate_LISTAPPEND(cctx);
1086 idx = -1;
1087 }
1088 else if (type->tt_type == VAR_BLOB)
1089 {
1090 // inline "add(blob, nr)" so that the type can be checked
1091 res = generate_BLOBAPPEND(cctx);
1092 idx = -1;
1093 }
1094 }
1095
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001096 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1097 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001098 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001099 // May have the "D" or "R" flag, reserve a variable for a
1100 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001101 if (get_defer_var_idx(cctx) == 0)
1102 idx = -1;
1103 }
1104
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105 if (idx >= 0)
1106 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1107 }
1108 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001109 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001110 goto theend;
1111 }
1112
Bram Moolenaar62aec932022-01-29 21:45:34 +00001113 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1114
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 // An argument or local variable can be a function reference, this
1116 // overrules a function name.
1117 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1118 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1119 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001120 class_T *cl = NULL;
1121 int mi = 0;
1122
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001123 // If we can find the function by name generate the right call.
1124 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001125 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001126 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001127 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001128 if (!func_is_global(ufunc))
1129 {
h-eastb895b0f2023-09-24 15:46:31 +02001130 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001131 goto theend;
1132 }
1133 if (!has_g_namespace
1134 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1135 {
1136 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001137 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001138 goto theend;
1139 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001141 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001142 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001143 // Class method invocation without the class name.
1144 // A class method can be referenced without the class name only in
1145 // the class where the function is defined.
1146 if (cctx->ctx_ufunc->uf_defclass == cl)
1147 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001148 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001149 0, argcount);
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001150 }
1151 else
1152 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001153 semsg(_(e_class_method_str_accessible_only_inside_class_str),
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001154 name, cl->class_name);
1155 res = FAIL;
1156 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001157 goto theend;
1158 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001159 }
1160
1161 // If the name is a variable, load it and use PCALL.
1162 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001163 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001165 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001166 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1167 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001168 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001169
Christian Brabandtd5475e82023-08-17 23:34:09 +02001170 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171 goto theend;
1172 }
1173
1174 // If we can find a global function by name generate the right call.
1175 if (ufunc != NULL)
1176 {
h-eastb895b0f2023-09-24 15:46:31 +02001177 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 goto theend;
1179 }
1180
1181 // A global function may be defined only later. Need to figure out at
1182 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001183 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184 res = generate_UCALL(cctx, name, argcount);
1185 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001186 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001187
1188theend:
1189 vim_free(tofree);
1190 return res;
1191}
1192
1193// like NAMESPACE_CHAR but with 'a' and 'l'.
1194#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1195
1196/*
1197 * Find the end of a variable or function name. Unlike find_name_end() this
1198 * does not recognize magic braces.
1199 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1200 * Return a pointer to just after the name. Equal to "arg" if there is no
1201 * valid name.
1202 */
1203 char_u *
1204to_name_end(char_u *arg, int use_namespace)
1205{
1206 char_u *p;
1207
1208 // Quick check for valid starting character.
1209 if (!eval_isnamec1(*arg))
1210 return arg;
1211
1212 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1213 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1214 // and can be used in slice "[n:]".
1215 if (*p == ':' && (p != arg + 1
1216 || !use_namespace
1217 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1218 break;
1219 return p;
1220}
1221
1222/*
1223 * Like to_name_end() but also skip over a list or dict constant.
1224 * Also accept "<SNR>123_Func".
1225 * This intentionally does not handle line continuation.
1226 */
1227 char_u *
1228to_name_const_end(char_u *arg)
1229{
1230 char_u *p = arg;
1231 typval_T rettv;
1232
1233 if (STRNCMP(p, "<SNR>", 5) == 0)
1234 p = skipdigits(p + 5);
1235 p = to_name_end(p, TRUE);
1236 if (p == arg && *arg == '[')
1237 {
1238
1239 // Can be "[1, 2, 3]->Func()".
1240 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1241 p = arg;
1242 }
1243 return p;
1244}
1245
1246/*
1247 * parse a list: [expr, expr]
1248 * "*arg" points to the '['.
1249 * ppconst->pp_is_const is set if all items are a constant.
1250 */
1251 static int
1252compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1253{
1254 char_u *p = skipwhite(*arg + 1);
1255 char_u *whitep = *arg + 1;
1256 int count = 0;
1257 int is_const;
1258 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001259 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001260
1261 for (;;)
1262 {
1263 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1264 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001265 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001266 return FAIL;
1267 }
1268 if (*p == ',')
1269 {
1270 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1271 return FAIL;
1272 }
1273 if (*p == ']')
1274 {
1275 ++p;
1276 break;
1277 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001278 if (must_end)
1279 {
1280 semsg(_(e_missing_comma_in_list_str), p);
1281 return FAIL;
1282 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001283 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1284 return FAIL;
1285 if (!is_const)
1286 is_all_const = FALSE;
1287 ++count;
1288 if (*p == ',')
1289 {
1290 ++p;
1291 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1292 {
1293 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1294 return FAIL;
1295 }
1296 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001297 else
1298 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299 whitep = p;
1300 p = skipwhite(p);
1301 }
1302 *arg = p;
1303
1304 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001305 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001306}
1307
1308/*
1309 * Parse a lambda: "(arg, arg) => expr"
1310 * "*arg" points to the '('.
1311 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1312 */
1313 static int
1314compile_lambda(char_u **arg, cctx_T *cctx)
1315{
1316 int r;
1317 typval_T rettv;
1318 ufunc_T *ufunc;
1319 evalarg_T evalarg;
1320
1321 init_evalarg(&evalarg);
1322 evalarg.eval_flags = EVAL_EVALUATE;
1323 evalarg.eval_cctx = cctx;
1324
1325 // Get the funcref in "rettv".
1326 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1327 if (r != OK)
1328 {
1329 clear_evalarg(&evalarg, NULL);
1330 return r;
1331 }
1332
1333 // "rettv" will now be a partial referencing the function.
1334 ufunc = rettv.vval.v_partial->pt_func;
1335 ++ufunc->uf_refcount;
1336 clear_tv(&rettv);
1337
1338 // Compile it here to get the return type. The return type is optional,
1339 // when it's missing use t_unknown. This is recognized in
1340 // compile_return().
1341 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1342 ufunc->uf_ret_type = &t_unknown;
1343 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1344
1345 // When the outer function is compiled for profiling or debugging, the
1346 // lambda may be called without profiling or debugging. Compile it here in
1347 // the right context.
1348 if (cctx->ctx_compile_type == CT_DEBUG
1349#ifdef FEAT_PROFILE
1350 || cctx->ctx_compile_type == CT_PROFILE
1351#endif
1352 )
1353 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1354
Bram Moolenaar139575d2022-03-15 19:29:30 +00001355 // if the outer function is not compiled for debugging or profiling, this
1356 // one might be
1357 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001358 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001359 compiletype_T compile_type = get_compile_type(ufunc);
1360
1361 if (compile_type != CT_NONE)
1362 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001363 }
1364
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001365 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1366 // "*arg" may point into it. Point into the original line to avoid a
1367 // dangling pointer.
1368 if (evalarg.eval_using_cmdline)
1369 {
1370 garray_T *gap = &evalarg.eval_tofree_ga;
1371 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1372
1373 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1374 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001375 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001376 }
1377
1378 clear_evalarg(&evalarg, NULL);
1379
1380 if (ufunc->uf_def_status == UF_COMPILED)
1381 {
1382 // The return type will now be known.
1383 set_function_type(ufunc);
1384
1385 // The function reference count will be 1. When the ISN_FUNCREF
1386 // instruction is deleted the reference count is decremented and the
1387 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001388 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389 }
1390
1391 func_ptr_unref(ufunc);
1392 return FAIL;
1393}
1394
1395/*
1396 * Get a lambda and compile it. Uses Vim9 syntax.
1397 */
1398 int
1399get_lambda_tv_and_compile(
1400 char_u **arg,
1401 typval_T *rettv,
1402 int types_optional,
1403 evalarg_T *evalarg)
1404{
1405 int r;
1406 ufunc_T *ufunc;
1407 int save_sc_version = current_sctx.sc_version;
1408
1409 // Get the funcref in "rettv".
1410 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1411 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1412 current_sctx.sc_version = save_sc_version;
1413 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001414 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001415
1416 // "rettv" will now be a partial referencing the function.
1417 ufunc = rettv->vval.v_partial->pt_func;
1418
1419 // Compile it here to get the return type. The return type is optional,
1420 // when it's missing use t_unknown. This is recognized in
1421 // compile_return().
1422 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1423 ufunc->uf_ret_type = &t_unknown;
1424 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1425
1426 if (ufunc->uf_def_status == UF_COMPILED)
1427 {
1428 // The return type will now be known.
1429 set_function_type(ufunc);
1430 return OK;
1431 }
1432 clear_tv(rettv);
1433 return FAIL;
1434}
1435
1436/*
1437 * parse a dict: {key: val, [key]: val}
1438 * "*arg" points to the '{'.
1439 * ppconst->pp_is_const is set if all item values are a constant.
1440 */
1441 static int
1442compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1443{
1444 garray_T *instr = &cctx->ctx_instr;
1445 int count = 0;
1446 dict_T *d = dict_alloc();
1447 dictitem_T *item;
1448 char_u *whitep = *arg + 1;
1449 char_u *p;
1450 int is_const;
1451 int is_all_const = TRUE; // reset when non-const encountered
1452
1453 if (d == NULL)
1454 return FAIL;
1455 if (generate_ppconst(cctx, ppconst) == FAIL)
1456 return FAIL;
1457 for (;;)
1458 {
1459 char_u *key = NULL;
1460
1461 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1462 {
1463 *arg = NULL;
1464 goto failret;
1465 }
1466
1467 if (**arg == '}')
1468 break;
1469
1470 if (**arg == '[')
1471 {
1472 isn_T *isn;
1473
1474 // {[expr]: value} uses an evaluated key.
1475 *arg = skipwhite(*arg + 1);
1476 if (compile_expr0(arg, cctx) == FAIL)
1477 return FAIL;
1478 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1479 if (isn->isn_type == ISN_PUSHNR)
1480 {
1481 char buf[NUMBUFLEN];
1482
1483 // Convert to string at compile time.
1484 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1485 isn->isn_type = ISN_PUSHS;
1486 isn->isn_arg.string = vim_strsave((char_u *)buf);
1487 }
1488 if (isn->isn_type == ISN_PUSHS)
1489 key = isn->isn_arg.string;
1490 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1491 return FAIL;
1492 *arg = skipwhite(*arg);
1493 if (**arg != ']')
1494 {
1495 emsg(_(e_missing_matching_bracket_after_dict_key));
1496 return FAIL;
1497 }
1498 ++*arg;
1499 }
1500 else
1501 {
1502 // {"name": value},
1503 // {'name': value},
1504 // {name: value} use "name" as a literal key
1505 key = get_literal_key(arg);
1506 if (key == NULL)
1507 return FAIL;
1508 if (generate_PUSHS(cctx, &key) == FAIL)
1509 return FAIL;
1510 }
1511
1512 // Check for duplicate keys, if using string keys.
1513 if (key != NULL)
1514 {
1515 item = dict_find(d, key, -1);
1516 if (item != NULL)
1517 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001518 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001519 goto failret;
1520 }
1521 item = dictitem_alloc(key);
1522 if (item != NULL)
1523 {
1524 item->di_tv.v_type = VAR_UNKNOWN;
1525 item->di_tv.v_lock = 0;
1526 if (dict_add(d, item) == FAIL)
1527 dictitem_free(item);
1528 }
1529 }
1530
1531 if (**arg != ':')
1532 {
1533 if (*skipwhite(*arg) == ':')
1534 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1535 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001536 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001537 return FAIL;
1538 }
1539 whitep = *arg + 1;
1540 if (!IS_WHITE_OR_NUL(*whitep))
1541 {
1542 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1543 return FAIL;
1544 }
1545
1546 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1547 {
1548 *arg = NULL;
1549 goto failret;
1550 }
1551
1552 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1553 return FAIL;
1554 if (!is_const)
1555 is_all_const = FALSE;
1556 ++count;
1557
1558 whitep = *arg;
1559 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1560 {
1561 *arg = NULL;
1562 goto failret;
1563 }
1564 if (**arg == '}')
1565 break;
1566 if (**arg != ',')
1567 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001568 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569 goto failret;
1570 }
1571 if (IS_WHITE_OR_NUL(*whitep))
1572 {
1573 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1574 return FAIL;
1575 }
1576 whitep = *arg + 1;
1577 if (!IS_WHITE_OR_NUL(*whitep))
1578 {
1579 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1580 return FAIL;
1581 }
1582 *arg = skipwhite(whitep);
1583 }
1584
1585 *arg = *arg + 1;
1586
1587 // Allow for following comment, after at least one space.
1588 p = skipwhite(*arg);
1589 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1590 *arg += STRLEN(*arg);
1591
1592 dict_unref(d);
1593 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001594 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001595
1596failret:
1597 if (*arg == NULL)
1598 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001599 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001600 *arg = (char_u *)"";
1601 }
1602 dict_unref(d);
1603 return FAIL;
1604}
1605
1606/*
1607 * Compile "&option".
1608 */
1609 static int
1610compile_get_option(char_u **arg, cctx_T *cctx)
1611{
1612 typval_T rettv;
1613 char_u *start = *arg;
1614 int ret;
1615
1616 // parse the option and get the current value to get the type.
1617 rettv.v_type = VAR_UNKNOWN;
1618 ret = eval_option(arg, &rettv, TRUE);
1619 if (ret == OK)
1620 {
1621 // include the '&' in the name, eval_option() expects it.
1622 char_u *name = vim_strnsave(start, *arg - start);
1623 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1624 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1625
1626 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1627 vim_free(name);
1628 }
1629 clear_tv(&rettv);
1630
1631 return ret;
1632}
1633
1634/*
1635 * Compile "$VAR".
1636 */
1637 static int
1638compile_get_env(char_u **arg, cctx_T *cctx)
1639{
1640 char_u *start = *arg;
1641 int len;
1642 int ret;
1643 char_u *name;
1644
1645 ++*arg;
1646 len = get_env_len(arg);
1647 if (len == 0)
1648 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001649 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650 return FAIL;
1651 }
1652
1653 // include the '$' in the name, eval_env_var() expects it.
1654 name = vim_strnsave(start, len + 1);
1655 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1656 vim_free(name);
1657 return ret;
1658}
1659
1660/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001661 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001662 */
1663 static int
1664compile_interp_string(char_u **arg, cctx_T *cctx)
1665{
1666 typval_T tv;
1667 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001668 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001669 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001670 int count = 0;
1671 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001672
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001673 // *arg is on the '$' character, move it to the first string character.
1674 ++*arg;
1675 quote = **arg;
1676 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001677
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001678 for (;;)
1679 {
1680 // Get the string up to the matching quote or to a single '{'.
1681 // "arg" is advanced to either the quote or the '{'.
1682 if (quote == '"')
1683 ret = eval_string(arg, &tv, evaluate, TRUE);
1684 else
1685 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1686 if (ret == FAIL)
1687 break;
1688 if (evaluate)
1689 {
1690 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1691 || (**arg != '{' && count == 0))
1692 {
1693 // generate non-empty string or empty string if it's the only
1694 // one
1695 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1696 return FAIL;
1697 tv.vval.v_string = NULL; // don't free it now
1698 ++count;
1699 }
1700 clear_tv(&tv);
1701 }
1702
1703 if (**arg != '{')
1704 {
1705 // found terminating quote
1706 ++*arg;
1707 break;
1708 }
1709
1710 p = compile_one_expr_in_str(*arg, cctx);
1711 if (p == NULL)
1712 {
1713 ret = FAIL;
1714 break;
1715 }
1716 ++count;
1717 *arg = p;
1718 }
LemonBoy2eaef102022-05-06 13:14:50 +01001719
1720 if (ret == FAIL || !evaluate)
1721 return ret;
1722
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001723 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1724 if (count > 1)
1725 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001726
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001727 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001728}
1729
1730/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001731 * Compile "@r".
1732 */
1733 static int
1734compile_get_register(char_u **arg, cctx_T *cctx)
1735{
1736 int ret;
1737
1738 ++*arg;
1739 if (**arg == NUL)
1740 {
1741 semsg(_(e_syntax_error_at_str), *arg - 1);
1742 return FAIL;
1743 }
1744 if (!valid_yank_reg(**arg, FALSE))
1745 {
1746 emsg_invreg(**arg);
1747 return FAIL;
1748 }
1749 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1750 ++*arg;
1751 return ret;
1752}
1753
1754/*
1755 * Apply leading '!', '-' and '+' to constant "rettv".
1756 * When "numeric_only" is TRUE do not apply '!'.
1757 */
1758 static int
1759apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1760{
1761 char_u *p = *end;
1762
1763 // this works from end to start
1764 while (p > start)
1765 {
1766 --p;
1767 if (*p == '-' || *p == '+')
1768 {
1769 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001770 if (rettv->v_type == VAR_FLOAT)
1771 {
1772 if (*p == '-')
1773 rettv->vval.v_float = -rettv->vval.v_float;
1774 }
1775 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001776 {
1777 varnumber_T val;
1778 int error = FALSE;
1779
1780 // tv_get_number_chk() accepts a string, but we don't want that
1781 // here
1782 if (check_not_string(rettv) == FAIL)
1783 return FAIL;
1784 val = tv_get_number_chk(rettv, &error);
1785 clear_tv(rettv);
1786 if (error)
1787 return FAIL;
1788 if (*p == '-')
1789 val = -val;
1790 rettv->v_type = VAR_NUMBER;
1791 rettv->vval.v_number = val;
1792 }
1793 }
1794 else if (numeric_only)
1795 {
1796 ++p;
1797 break;
1798 }
1799 else if (*p == '!')
1800 {
1801 int v = tv2bool(rettv);
1802
1803 // '!' is permissive in the type.
1804 clear_tv(rettv);
1805 rettv->v_type = VAR_BOOL;
1806 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1807 }
1808 }
1809 *end = p;
1810 return OK;
1811}
1812
1813/*
1814 * Recognize v: variables that are constants and set "rettv".
1815 */
1816 static void
1817get_vim_constant(char_u **arg, typval_T *rettv)
1818{
1819 if (STRNCMP(*arg, "v:true", 6) == 0)
1820 {
1821 rettv->v_type = VAR_BOOL;
1822 rettv->vval.v_number = VVAL_TRUE;
1823 *arg += 6;
1824 }
1825 else if (STRNCMP(*arg, "v:false", 7) == 0)
1826 {
1827 rettv->v_type = VAR_BOOL;
1828 rettv->vval.v_number = VVAL_FALSE;
1829 *arg += 7;
1830 }
1831 else if (STRNCMP(*arg, "v:null", 6) == 0)
1832 {
1833 rettv->v_type = VAR_SPECIAL;
1834 rettv->vval.v_number = VVAL_NULL;
1835 *arg += 6;
1836 }
1837 else if (STRNCMP(*arg, "v:none", 6) == 0)
1838 {
1839 rettv->v_type = VAR_SPECIAL;
1840 rettv->vval.v_number = VVAL_NONE;
1841 *arg += 6;
1842 }
1843}
1844
1845 exprtype_T
1846get_compare_type(char_u *p, int *len, int *type_is)
1847{
1848 exprtype_T type = EXPR_UNKNOWN;
1849 int i;
1850
1851 switch (p[0])
1852 {
1853 case '=': if (p[1] == '=')
1854 type = EXPR_EQUAL;
1855 else if (p[1] == '~')
1856 type = EXPR_MATCH;
1857 break;
1858 case '!': if (p[1] == '=')
1859 type = EXPR_NEQUAL;
1860 else if (p[1] == '~')
1861 type = EXPR_NOMATCH;
1862 break;
1863 case '>': if (p[1] != '=')
1864 {
1865 type = EXPR_GREATER;
1866 *len = 1;
1867 }
1868 else
1869 type = EXPR_GEQUAL;
1870 break;
1871 case '<': if (p[1] != '=')
1872 {
1873 type = EXPR_SMALLER;
1874 *len = 1;
1875 }
1876 else
1877 type = EXPR_SEQUAL;
1878 break;
1879 case 'i': if (p[1] == 's')
1880 {
1881 // "is" and "isnot"; but not a prefix of a name
1882 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1883 *len = 5;
1884 i = p[*len];
1885 if (!isalnum(i) && i != '_')
1886 {
1887 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1888 *type_is = TRUE;
1889 }
1890 }
1891 break;
1892 }
1893 return type;
1894}
1895
1896/*
1897 * Skip over an expression, ignoring most errors.
1898 */
1899 void
1900skip_expr_cctx(char_u **arg, cctx_T *cctx)
1901{
1902 evalarg_T evalarg;
1903
1904 init_evalarg(&evalarg);
1905 evalarg.eval_cctx = cctx;
1906 skip_expr(arg, &evalarg);
1907 clear_evalarg(&evalarg, NULL);
1908}
1909
1910/*
1911 * Check that the top of the type stack has a type that can be used as a
1912 * condition. Give an error and return FAIL if not.
1913 */
1914 int
1915bool_on_stack(cctx_T *cctx)
1916{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001917 type_T *type;
1918
Bram Moolenaar078a4612022-01-04 15:17:03 +00001919 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920 if (type == &t_bool)
1921 return OK;
1922
Bram Moolenaar4913d422022-10-17 13:13:32 +01001923 if (type->tt_type == VAR_ANY
1924 || type->tt_type == VAR_UNKNOWN
1925 || type->tt_type == VAR_NUMBER
1926 || type == &t_number_bool
1927 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1929 // This requires a runtime type check.
1930 return generate_COND2BOOL(cctx);
1931
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001932 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001933}
1934
1935/*
1936 * Give the "white on both sides" error, taking the operator from "p[len]".
1937 */
1938 void
1939error_white_both(char_u *op, int len)
1940{
1941 char_u buf[10];
1942
1943 vim_strncpy(buf, op, len);
1944 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1945}
1946
1947/*
1948 * Compile code to apply '-', '+' and '!'.
1949 * When "numeric_only" is TRUE do not apply '!'.
1950 */
1951 static int
1952compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1953{
1954 char_u *p = *end;
1955
1956 // this works from end to start
1957 while (p > start)
1958 {
1959 --p;
1960 while (VIM_ISWHITE(*p))
1961 --p;
1962 if (*p == '-' || *p == '+')
1963 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001964 type_T *type = get_type_on_stack(cctx, 0);
1965 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001966 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001967 return FAIL;
1968
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001970 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1971 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001972 }
1973 else if (numeric_only)
1974 {
1975 ++p;
1976 break;
1977 }
1978 else
1979 {
1980 int invert = *p == '!';
1981
1982 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1983 {
1984 if (p[-1] == '!')
1985 invert = !invert;
1986 --p;
1987 }
1988 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1989 return FAIL;
1990 }
1991 }
1992 *end = p;
1993 return OK;
1994}
1995
1996/*
1997 * Compile "(expression)": recursive!
1998 * Return FAIL/OK.
1999 */
2000 static int
2001compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2002{
2003 int ret;
2004 char_u *p = *arg + 1;
2005
2006 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2007 return FAIL;
2008 if (ppconst->pp_used <= PPSIZE - 10)
2009 {
2010 ret = compile_expr1(arg, cctx, ppconst);
2011 }
2012 else
2013 {
2014 // Not enough space in ppconst, flush constants.
2015 if (generate_ppconst(cctx, ppconst) == FAIL)
2016 return FAIL;
2017 ret = compile_expr0(arg, cctx);
2018 }
2019 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2020 return FAIL;
2021 if (**arg == ')')
2022 ++*arg;
2023 else if (ret == OK)
2024 {
2025 emsg(_(e_missing_closing_paren));
2026 ret = FAIL;
2027 }
2028 return ret;
2029}
2030
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002031static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002032
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033/*
2034 * Compile whatever comes after "name" or "name()".
2035 * Advances "*arg" only when something was recognized.
2036 */
2037 static int
2038compile_subscript(
2039 char_u **arg,
2040 cctx_T *cctx,
2041 char_u *start_leader,
2042 char_u **end_leader,
2043 ppconst_T *ppconst)
2044{
2045 char_u *name_start = *end_leader;
2046 int keeping_dict = FALSE;
2047
2048 for (;;)
2049 {
2050 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002051 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002052
2053 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2054 {
2055 char_u *next = peek_next_line_from_context(cctx);
2056
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002057 // If a following line starts with "->{", "->(" or "->X" advance to
2058 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059 // Also if a following line starts with ".x".
2060 if (next != NULL &&
2061 ((next[0] == '-' && next[1] == '>'
2062 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002063 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 || ASCII_ISALPHA(*skipwhite(next + 2))))
2065 || (next[0] == '.' && eval_isdictc(next[1]))))
2066 {
2067 next = next_line_from_context(cctx, TRUE);
2068 if (next == NULL)
2069 return FAIL;
2070 *arg = next;
2071 p = skipwhite(*arg);
2072 }
2073 }
2074
2075 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2076 // is not a function call.
2077 if (**arg == '(')
2078 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002079 int argcount = 0;
2080
2081 if (generate_ppconst(cctx, ppconst) == FAIL)
2082 return FAIL;
2083 ppconst->pp_is_const = FALSE;
2084
2085 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002086 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002087
2088 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002089 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002090 return FAIL;
2091 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2092 return FAIL;
2093 if (keeping_dict)
2094 {
2095 keeping_dict = FALSE;
2096 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2097 return FAIL;
2098 }
2099 }
2100 else if (*p == '-' && p[1] == '>')
2101 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002102 char_u *pstart = p;
2103 int alt;
2104 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002105
Bram Moolenaarc7349932022-01-16 20:59:39 +00002106 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002107 if (generate_ppconst(cctx, ppconst) == FAIL)
2108 return FAIL;
2109 ppconst->pp_is_const = FALSE;
2110
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002111 // Apply the '!', '-' and '+' first:
2112 // -1.0->func() works like (-1.0)->func()
2113 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2114 return FAIL;
2115
2116 p += 2;
2117 *arg = skipwhite(p);
2118 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002119
2120 // Three alternatives handled here:
2121 // 1. "base->name(" only a name, use compile_call()
2122 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2123 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002125 alt = 2;
2126 else
2127 {
2128 // alternative 1 or 3
2129 p = *arg;
2130 if (!eval_isnamec1(*p))
2131 {
2132 semsg(_(e_trailing_characters_str), pstart);
2133 return FAIL;
2134 }
2135 if (ASCII_ISALPHA(*p) && p[1] == ':')
2136 p += 2;
2137 for ( ; eval_isnamec(*p); ++p)
2138 ;
2139 if (*p == '(')
2140 {
2141 // alternative 1
2142 alt = 1;
2143 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2144 return FAIL;
2145 }
2146 else
2147 {
2148 // Must be alternative 3, find the "(". Only works within
2149 // one line.
2150 alt = 3;
2151 paren = vim_strchr(p, '(');
2152 if (paren == NULL)
2153 {
2154 semsg(_(e_missing_parenthesis_str), *arg);
2155 return FAIL;
2156 }
2157 }
2158 }
2159
2160 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002161 {
2162 int argcount = 1;
2163 garray_T *stack = &cctx->ctx_type_stack;
2164 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002165 int expr_isn_start = cctx->ctx_instr.ga_len;
2166 int expr_isn_end;
2167 int arg_isn_count;
2168
Bram Moolenaarc7349932022-01-16 20:59:39 +00002169 if (alt == 2)
2170 {
2171 // Funcref call: list->(Refs[2])(arg)
2172 // or lambda: list->((arg) => expr)(arg)
2173 //
2174 // Fist compile the function expression.
2175 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2176 return FAIL;
2177 }
2178 else
2179 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002180 int fail;
2181 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002182 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002183
Bram Moolenaarc7349932022-01-16 20:59:39 +00002184 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002185
2186 // instead of using LOADG for "import.Func" use PUSHFUNC
2187 ++paren_follows_after_expr;
2188
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002189 // do not look in the next line
2190 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002191
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002192 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002193 || *skipwhite(*arg) != NUL;
2194 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002195 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002196 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002197
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002198 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002199 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002200 if (did_emsg == prev_did_emsg)
2201 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002202 return FAIL;
2203 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002204 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002205
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 // Compile the arguments.
2207 if (**arg != '(')
2208 {
2209 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002210 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002211 else
2212 semsg(_(e_missing_parenthesis_str), *arg);
2213 return FAIL;
2214 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002215
2216 // Remember the next instruction index, where the instructions
2217 // for arguments are being written.
2218 expr_isn_end = cctx->ctx_instr.ga_len;
2219
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002220 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002221 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2222 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002223 return FAIL;
2224
2225 // Move the instructions for the arguments to before the
2226 // instructions of the expression and move the type of the
2227 // expression after the argument types. This is what ISN_PCALL
2228 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2230 if (arg_isn_count > 0)
2231 {
2232 int expr_isn_count = expr_isn_end - expr_isn_start;
2233 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002234 type_T *decl_type;
2235 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002236
2237 if (isn == NULL)
2238 return FAIL;
2239 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2240 + expr_isn_start,
2241 sizeof(isn_T) * expr_isn_count);
2242 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2243 + expr_isn_start,
2244 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2245 sizeof(isn_T) * arg_isn_count);
2246 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2247 + expr_isn_start + arg_isn_count,
2248 isn, sizeof(isn_T) * expr_isn_count);
2249 vim_free(isn);
2250
Bram Moolenaar078a4612022-01-04 15:17:03 +00002251 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2252 type = typep->type_curr;
2253 decl_type = typep->type_decl;
2254 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2255 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2256 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002258 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2259 typep->type_curr = type;
2260 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261 }
2262
Bram Moolenaar078a4612022-01-04 15:17:03 +00002263 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002264 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2265 return FAIL;
2266 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002267
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002268 if (keeping_dict)
2269 {
2270 keeping_dict = FALSE;
2271 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2272 return FAIL;
2273 }
2274 }
2275 else if (**arg == '[')
2276 {
2277 int is_slice = FALSE;
2278
2279 // list index: list[123]
2280 // dict member: dict[key]
2281 // string index: text[123]
2282 // blob index: blob[123]
2283 if (generate_ppconst(cctx, ppconst) == FAIL)
2284 return FAIL;
2285 ppconst->pp_is_const = FALSE;
2286
2287 ++p;
2288 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2289 return FAIL;
2290 if (**arg == ':')
2291 {
2292 // missing first index is equal to zero
2293 generate_PUSHNR(cctx, 0);
2294 }
2295 else
2296 {
2297 if (compile_expr0(arg, cctx) == FAIL)
2298 return FAIL;
2299 if (**arg == ':')
2300 {
2301 semsg(_(e_white_space_required_before_and_after_str_at_str),
2302 ":", *arg);
2303 return FAIL;
2304 }
2305 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2306 return FAIL;
2307 *arg = skipwhite(*arg);
2308 }
2309 if (**arg == ':')
2310 {
2311 is_slice = TRUE;
2312 ++*arg;
2313 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2314 {
2315 semsg(_(e_white_space_required_before_and_after_str_at_str),
2316 ":", *arg);
2317 return FAIL;
2318 }
2319 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2320 return FAIL;
2321 if (**arg == ']')
2322 // missing second index is equal to end of string
2323 generate_PUSHNR(cctx, -1);
2324 else
2325 {
2326 if (compile_expr0(arg, cctx) == FAIL)
2327 return FAIL;
2328 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2329 return FAIL;
2330 *arg = skipwhite(*arg);
2331 }
2332 }
2333
2334 if (**arg != ']')
2335 {
2336 emsg(_(e_missing_closing_square_brace));
2337 return FAIL;
2338 }
2339 *arg = *arg + 1;
2340
2341 if (keeping_dict)
2342 {
2343 keeping_dict = FALSE;
2344 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2345 return FAIL;
2346 }
2347 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2348 return FAIL;
2349 }
2350 else if (*p == '.' && p[1] != '.')
2351 {
2352 // dictionary member: dict.name
2353 if (generate_ppconst(cctx, ppconst) == FAIL)
2354 return FAIL;
2355 ppconst->pp_is_const = FALSE;
2356
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002357 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2358 && (type->tt_type == VAR_CLASS
2359 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002360 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002361 // class member: SomeClass.varname
2362 // class method: SomeClass.SomeMethod()
2363 // class constructor: SomeClass.new()
2364 // object member: someObject.varname, this.varname
2365 // object method: someObject.SomeMethod(), this.SomeMethod()
2366 *arg = p;
2367 if (compile_class_object_index(cctx, arg, type) == FAIL)
2368 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002370 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002372 *arg = p + 1;
2373 if (IS_WHITE_OR_NUL(**arg))
2374 {
2375 emsg(_(e_missing_name_after_dot));
2376 return FAIL;
2377 }
2378 p = *arg;
2379 if (eval_isdictc(*p))
2380 while (eval_isnamec(*p))
2381 MB_PTR_ADV(p);
2382 if (p == *arg)
2383 {
2384 semsg(_(e_syntax_error_at_str), *arg);
2385 return FAIL;
2386 }
2387 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2388 return FAIL;
2389 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2390 return FAIL;
2391 keeping_dict = TRUE;
2392 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 }
2395 else
2396 break;
2397 }
2398
2399 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2400 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002401 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2402 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002403 return FAIL;
2404
2405 return OK;
2406}
2407
2408/*
2409 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2410 * "arg" is advanced until after the expression, skipping white space.
2411 *
2412 * If the value is a constant "ppconst->pp_used" will be non-zero.
2413 * Before instructions are generated, any values in "ppconst" will generated.
2414 *
2415 * This is the compiling equivalent of eval1(), eval2(), etc.
2416 */
2417
2418/*
2419 * number number constant
2420 * 0zFFFFFFFF Blob constant
2421 * "string" string constant
2422 * 'string' literal string constant
2423 * &option-name option value
2424 * @r register contents
2425 * identifier variable value
2426 * function() function call
2427 * $VAR environment variable
2428 * (expression) nested expression
2429 * [expr, expr] List
2430 * {key: val, [key]: val} Dictionary
2431 *
2432 * Also handle:
2433 * ! in front logical NOT
2434 * - in front unary minus
2435 * + in front unary plus (ignored)
2436 * trailing (arg) funcref/partial call
2437 * trailing [] subscript in String or List
2438 * trailing .name entry in Dictionary
2439 * trailing ->name() method call
2440 */
2441 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002442compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 char_u **arg,
2444 cctx_T *cctx,
2445 ppconst_T *ppconst)
2446{
2447 char_u *start_leader, *end_leader;
2448 int ret = OK;
2449 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2450 int used_before = ppconst->pp_used;
2451
2452 ppconst->pp_is_const = FALSE;
2453
2454 /*
2455 * Skip '!', '-' and '+' characters. They are handled later.
2456 */
2457 start_leader = *arg;
2458 if (eval_leader(arg, TRUE) == FAIL)
2459 return FAIL;
2460 end_leader = *arg;
2461
2462 rettv->v_type = VAR_UNKNOWN;
2463 switch (**arg)
2464 {
2465 /*
2466 * Number constant.
2467 */
2468 case '0': // also for blob starting with 0z
2469 case '1':
2470 case '2':
2471 case '3':
2472 case '4':
2473 case '5':
2474 case '6':
2475 case '7':
2476 case '8':
2477 case '9':
2478 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2479 return FAIL;
2480 // Apply "-" and "+" just before the number now, right to
2481 // left. Matters especially when "->" follows. Stops at
2482 // '!'.
2483 if (apply_leader(rettv, TRUE,
2484 start_leader, &end_leader) == FAIL)
2485 {
2486 clear_tv(rettv);
2487 return FAIL;
2488 }
2489 break;
2490
2491 /*
2492 * String constant: "string".
2493 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002494 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002495 return FAIL;
2496 break;
2497
2498 /*
2499 * Literal string constant: 'str''ing'.
2500 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002501 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002502 return FAIL;
2503 break;
2504
2505 /*
2506 * Constant Vim variable.
2507 */
2508 case 'v': get_vim_constant(arg, rettv);
2509 ret = NOTDONE;
2510 break;
2511
2512 /*
2513 * "true" constant
2514 */
2515 case 't': if (STRNCMP(*arg, "true", 4) == 0
2516 && !eval_isnamec((*arg)[4]))
2517 {
2518 *arg += 4;
2519 rettv->v_type = VAR_BOOL;
2520 rettv->vval.v_number = VVAL_TRUE;
2521 }
2522 else
2523 ret = NOTDONE;
2524 break;
2525
2526 /*
2527 * "false" constant
2528 */
2529 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2530 && !eval_isnamec((*arg)[5]))
2531 {
2532 *arg += 5;
2533 rettv->v_type = VAR_BOOL;
2534 rettv->vval.v_number = VVAL_FALSE;
2535 }
2536 else
2537 ret = NOTDONE;
2538 break;
2539
2540 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002541 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002543 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002544 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002545 char_u *p = *arg + 4;
2546 int len;
2547
2548 for (len = 0; eval_isnamec(p[len]); ++len)
2549 ;
2550 ret = handle_predefined(*arg, len + 4, rettv);
2551 if (ret == FAIL)
2552 ret = NOTDONE;
2553 else
2554 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002555 }
2556 else
2557 ret = NOTDONE;
2558 break;
2559
2560 /*
2561 * List: [expr, expr]
2562 */
2563 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2564 return FAIL;
2565 ret = compile_list(arg, cctx, ppconst);
2566 break;
2567
2568 /*
2569 * Dictionary: {'key': val, 'key': val}
2570 */
2571 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2572 return FAIL;
2573 ret = compile_dict(arg, cctx, ppconst);
2574 break;
2575
2576 /*
2577 * Option value: &name
2578 */
2579 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2580 return FAIL;
2581 ret = compile_get_option(arg, cctx);
2582 break;
2583
2584 /*
2585 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002586 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002587 */
2588 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2589 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002590 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2591 ret = compile_interp_string(arg, cctx);
2592 else
2593 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 break;
2595
2596 /*
2597 * Register contents: @r.
2598 */
2599 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2600 return FAIL;
2601 ret = compile_get_register(arg, cctx);
2602 break;
2603 /*
2604 * nested expression: (expression).
2605 * lambda: (arg, arg) => expr
2606 * funcref: (arg, arg) => { statement }
2607 */
2608 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2609 ret = compile_lambda(arg, cctx);
2610 if (ret == NOTDONE)
2611 ret = compile_parenthesis(arg, cctx, ppconst);
2612 break;
2613
2614 default: ret = NOTDONE;
2615 break;
2616 }
2617 if (ret == FAIL)
2618 return FAIL;
2619
2620 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2621 {
2622 if (cctx->ctx_skip == SKIP_YES)
2623 clear_tv(rettv);
2624 else
2625 // A constant expression can possibly be handled compile time,
2626 // return the value instead of generating code.
2627 ++ppconst->pp_used;
2628 }
2629 else if (ret == NOTDONE)
2630 {
2631 char_u *p;
2632 int r;
2633
2634 if (!eval_isnamec1(**arg))
2635 {
2636 if (!vim9_bad_comment(*arg))
2637 {
2638 if (ends_excmd(*skipwhite(*arg)))
2639 semsg(_(e_empty_expression_str), *arg);
2640 else
2641 semsg(_(e_name_expected_str), *arg);
2642 }
2643 return FAIL;
2644 }
2645
2646 // "name" or "name()"
2647 p = to_name_end(*arg, TRUE);
2648 if (p - *arg == (size_t)1 && **arg == '_')
2649 {
2650 emsg(_(e_cannot_use_underscore_here));
2651 return FAIL;
2652 }
2653
2654 if (*p == '(')
2655 {
2656 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2657 }
2658 else
2659 {
2660 if (cctx->ctx_skip != SKIP_YES
2661 && generate_ppconst(cctx, ppconst) == FAIL)
2662 return FAIL;
2663 r = compile_load(arg, p, cctx, TRUE, TRUE);
2664 }
2665 if (r == FAIL)
2666 return FAIL;
2667 }
2668
2669 // Handle following "[]", ".member", etc.
2670 // Then deal with prefixed '-', '+' and '!', if not done already.
2671 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2672 ppconst) == FAIL)
2673 return FAIL;
2674 if (ppconst->pp_used > 0)
2675 {
2676 // apply the '!', '-' and '+' before the constant
2677 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2678 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2679 return FAIL;
2680 return OK;
2681 }
2682 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2683 return FAIL;
2684 return OK;
2685}
2686
2687/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002688 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002689 */
2690 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002691compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002692{
2693 type_T *want_type = NULL;
2694
2695 // Recognize <type>
2696 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2697 {
2698 ++*arg;
2699 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2700 if (want_type == NULL)
2701 return FAIL;
2702
2703 if (**arg != '>')
2704 {
2705 if (*skipwhite(*arg) == '>')
2706 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2707 else
2708 emsg(_(e_missing_gt));
2709 return FAIL;
2710 }
2711 ++*arg;
2712 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2713 return FAIL;
2714 }
2715
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002716 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002717 return FAIL;
2718
2719 if (want_type != NULL)
2720 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002721 type_T *actual;
2722 where_T where = WHERE_INIT;
2723
2724 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002725 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002726 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002727 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002728 if (need_type(actual, want_type, FALSE,
2729 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002730 return FAIL;
2731 }
2732 }
2733
2734 return OK;
2735}
2736
2737/*
2738 * * number multiplication
2739 * / number division
2740 * % number modulo
2741 */
2742 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002743compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002744{
2745 char_u *op;
2746 char_u *next;
2747 int ppconst_used = ppconst->pp_used;
2748
2749 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002750 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002751 return FAIL;
2752
2753 /*
2754 * Repeat computing, until no "*", "/" or "%" is following.
2755 */
2756 for (;;)
2757 {
2758 op = may_peek_next_line(cctx, *arg, &next);
2759 if (*op != '*' && *op != '/' && *op != '%')
2760 break;
2761 if (next != NULL)
2762 {
2763 *arg = next_line_from_context(cctx, TRUE);
2764 op = skipwhite(*arg);
2765 }
2766
2767 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2768 {
2769 error_white_both(op, 1);
2770 return FAIL;
2771 }
2772 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2773 return FAIL;
2774
2775 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002776 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002777 return FAIL;
2778
2779 if (ppconst->pp_used == ppconst_used + 2
2780 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2781 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2782 {
2783 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2784 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2785 varnumber_T res = 0;
2786 int failed = FALSE;
2787
2788 // both are numbers: compute the result
2789 switch (*op)
2790 {
2791 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2792 break;
2793 case '/': res = num_divide(tv1->vval.v_number,
2794 tv2->vval.v_number, &failed);
2795 break;
2796 case '%': res = num_modulus(tv1->vval.v_number,
2797 tv2->vval.v_number, &failed);
2798 break;
2799 }
2800 if (failed)
2801 return FAIL;
2802 tv1->vval.v_number = res;
2803 --ppconst->pp_used;
2804 }
2805 else
2806 {
2807 generate_ppconst(cctx, ppconst);
2808 generate_two_op(cctx, op);
2809 }
2810 }
2811
2812 return OK;
2813}
2814
2815/*
2816 * + number addition or list/blobl concatenation
2817 * - number subtraction
2818 * .. string concatenation
2819 */
2820 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002821compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002822{
2823 char_u *op;
2824 char_u *next;
2825 int oplen;
2826 int ppconst_used = ppconst->pp_used;
2827
2828 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002829 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002830 return FAIL;
2831
2832 /*
2833 * Repeat computing, until no "+", "-" or ".." is following.
2834 */
2835 for (;;)
2836 {
2837 op = may_peek_next_line(cctx, *arg, &next);
2838 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2839 break;
2840 if (op[0] == op[1] && *op != '.' && next)
2841 // Finding "++" or "--" on the next line is a separate command.
2842 // But ".." is concatenation.
2843 break;
2844 oplen = (*op == '.' ? 2 : 1);
2845 if (next != NULL)
2846 {
2847 *arg = next_line_from_context(cctx, TRUE);
2848 op = skipwhite(*arg);
2849 }
2850
2851 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2852 {
2853 error_white_both(op, oplen);
2854 return FAIL;
2855 }
2856
2857 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2858 return FAIL;
2859
2860 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002861 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002862 return FAIL;
2863
2864 if (ppconst->pp_used == ppconst_used + 2
2865 && (*op == '.'
2866 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2867 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2868 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2869 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2870 {
2871 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2872 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2873
2874 // concat/subtract/add constant numbers
2875 if (*op == '+')
2876 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2877 else if (*op == '-')
2878 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2879 else
2880 {
2881 // concatenate constant strings
2882 char_u *s1 = tv1->vval.v_string;
2883 char_u *s2 = tv2->vval.v_string;
2884 size_t len1 = STRLEN(s1);
2885
2886 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2887 if (tv1->vval.v_string == NULL)
2888 {
2889 clear_ppconst(ppconst);
2890 return FAIL;
2891 }
2892 mch_memmove(tv1->vval.v_string, s1, len1);
2893 STRCPY(tv1->vval.v_string + len1, s2);
2894 vim_free(s1);
2895 vim_free(s2);
2896 }
2897 --ppconst->pp_used;
2898 }
2899 else
2900 {
2901 generate_ppconst(cctx, ppconst);
2902 ppconst->pp_is_const = FALSE;
2903 if (*op == '.')
2904 {
2905 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2906 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2907 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002908 if (generate_CONCAT(cctx, 2) == FAIL)
2909 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002910 }
2911 else
2912 generate_two_op(cctx, op);
2913 }
2914 }
2915
2916 return OK;
2917}
2918
2919/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002920 * expr6a >> expr6b
2921 * expr6a << expr6b
2922 *
2923 * Produces instructions:
2924 * OPNR bitwise left or right shift
2925 */
2926 static int
2927compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2928{
2929 exprtype_T type = EXPR_UNKNOWN;
2930 char_u *p;
2931 char_u *next;
2932 int len = 2;
2933 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002934 isn_T *isn;
2935
2936 // get the first variable
2937 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2938 return FAIL;
2939
2940 /*
2941 * Repeat computing, until no "+", "-" or ".." is following.
2942 */
2943 for (;;)
2944 {
2945 type = EXPR_UNKNOWN;
2946
2947 p = may_peek_next_line(cctx, *arg, &next);
2948 if (p[0] == '<' && p[1] == '<')
2949 type = EXPR_LSHIFT;
2950 else if (p[0] == '>' && p[1] == '>')
2951 type = EXPR_RSHIFT;
2952
2953 if (type == EXPR_UNKNOWN)
2954 return OK;
2955
2956 // Handle a bitwise left or right shift operator
2957 if (ppconst->pp_used == ppconst_used + 1)
2958 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002959 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002960 {
2961 // left operand should be a number
2962 emsg(_(e_bitshift_ops_must_be_number));
2963 return FAIL;
2964 }
2965 }
2966 else
2967 {
2968 type_T *t = get_type_on_stack(cctx, 0);
2969
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002970 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002971 {
2972 emsg(_(e_bitshift_ops_must_be_number));
2973 return FAIL;
2974 }
2975 }
2976
2977 if (next != NULL)
2978 {
2979 *arg = next_line_from_context(cctx, TRUE);
2980 p = skipwhite(*arg);
2981 }
2982
2983 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2984 {
2985 error_white_both(p, len);
2986 return FAIL;
2987 }
2988
2989 // get the second variable
2990 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2991 return FAIL;
2992
2993 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2994 return FAIL;
2995
2996 if (ppconst->pp_used == ppconst_used + 2)
2997 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002998 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2999 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3000
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003001 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003002 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3003 {
3004 // right operand should be a positive number
3005 if (tv2->v_type != VAR_NUMBER)
3006 emsg(_(e_bitshift_ops_must_be_number));
3007 else
dundargocc57b5bc2022-11-02 13:30:51 +00003008 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003009 return FAIL;
3010 }
3011
3012 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3013 tv1->vval.v_number = 0;
3014 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003015 tv1->vval.v_number =
3016 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003017 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003018 tv1->vval.v_number =
3019 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003020 clear_tv(tv2);
3021 --ppconst->pp_used;
3022 }
3023 else
3024 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003025 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3026 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003027 {
3028 emsg(_(e_bitshift_ops_must_be_number));
3029 return FAIL;
3030 }
3031
3032 generate_ppconst(cctx, ppconst);
3033
3034 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3035 if (isn == NULL)
3036 return FAIL;
3037
3038 if (isn != NULL)
3039 isn->isn_arg.op.op_type = type;
3040 }
3041 }
3042
3043 return OK;
3044}
3045
3046/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003047 * expr5a == expr5b
3048 * expr5a =~ expr5b
3049 * expr5a != expr5b
3050 * expr5a !~ expr5b
3051 * expr5a > expr5b
3052 * expr5a >= expr5b
3053 * expr5a < expr5b
3054 * expr5a <= expr5b
3055 * expr5a is expr5b
3056 * expr5a isnot expr5b
3057 *
3058 * Produces instructions:
3059 * EVAL expr5a Push result of "expr5a"
3060 * EVAL expr5b Push result of "expr5b"
3061 * COMPARE one of the compare instructions
3062 */
3063 static int
3064compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3065{
3066 exprtype_T type = EXPR_UNKNOWN;
3067 char_u *p;
3068 char_u *next;
3069 int len = 2;
3070 int type_is = FALSE;
3071 int ppconst_used = ppconst->pp_used;
3072
3073 // get the first variable
3074 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3075 return FAIL;
3076
3077 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003078
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003079 type = get_compare_type(p, &len, &type_is);
3080
3081 /*
3082 * If there is a comparative operator, use it.
3083 */
3084 if (type != EXPR_UNKNOWN)
3085 {
3086 int ic = FALSE; // Default: do not ignore case
3087
3088 if (next != NULL)
3089 {
3090 *arg = next_line_from_context(cctx, TRUE);
3091 p = skipwhite(*arg);
3092 }
3093 if (type_is && (p[len] == '?' || p[len] == '#'))
3094 {
3095 semsg(_(e_invalid_expression_str), *arg);
3096 return FAIL;
3097 }
3098 // extra question mark appended: ignore case
3099 if (p[len] == '?')
3100 {
3101 ic = TRUE;
3102 ++len;
3103 }
3104 // extra '#' appended: match case (ignored)
3105 else if (p[len] == '#')
3106 ++len;
3107 // nothing appended: match case
3108
3109 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3110 {
3111 error_white_both(p, len);
3112 return FAIL;
3113 }
3114
3115 // get the second variable
3116 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3117 return FAIL;
3118
3119 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3120 return FAIL;
3121
3122 if (ppconst->pp_used == ppconst_used + 2)
3123 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003124 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003125 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3126 int ret;
3127
3128 // Both sides are a constant, compute the result now.
3129 // First check for a valid combination of types, this is more
3130 // strict than typval_compare().
3131 if (check_compare_types(type, tv1, tv2) == FAIL)
3132 ret = FAIL;
3133 else
3134 {
3135 ret = typval_compare(tv1, tv2, type, ic);
3136 tv1->v_type = VAR_BOOL;
3137 tv1->vval.v_number = tv1->vval.v_number
3138 ? VVAL_TRUE : VVAL_FALSE;
3139 clear_tv(tv2);
3140 --ppconst->pp_used;
3141 }
3142 return ret;
3143 }
3144
3145 generate_ppconst(cctx, ppconst);
3146 return generate_COMPARE(cctx, type, ic);
3147 }
3148
3149 return OK;
3150}
3151
3152static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3153
3154/*
3155 * Compile || or &&.
3156 */
3157 static int
3158compile_and_or(
3159 char_u **arg,
3160 cctx_T *cctx,
3161 char *op,
3162 ppconst_T *ppconst,
3163 int ppconst_used UNUSED)
3164{
3165 char_u *next;
3166 char_u *p = may_peek_next_line(cctx, *arg, &next);
3167 int opchar = *op;
3168
3169 if (p[0] == opchar && p[1] == opchar)
3170 {
3171 garray_T *instr = &cctx->ctx_instr;
3172 garray_T end_ga;
3173 int save_skip = cctx->ctx_skip;
3174
3175 /*
3176 * Repeat until there is no following "||" or "&&"
3177 */
3178 ga_init2(&end_ga, sizeof(int), 10);
3179 while (p[0] == opchar && p[1] == opchar)
3180 {
3181 long start_lnum = SOURCING_LNUM;
3182 long save_sourcing_lnum;
3183 int start_ctx_lnum = cctx->ctx_lnum;
3184 int save_lnum;
3185 int const_used;
3186 int status;
3187 jumpwhen_T jump_when = opchar == '|'
3188 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3189
3190 if (next != NULL)
3191 {
3192 *arg = next_line_from_context(cctx, TRUE);
3193 p = skipwhite(*arg);
3194 }
3195
3196 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3197 {
3198 semsg(_(e_white_space_required_before_and_after_str_at_str),
3199 op, p);
3200 ga_clear(&end_ga);
3201 return FAIL;
3202 }
3203
3204 save_sourcing_lnum = SOURCING_LNUM;
3205 SOURCING_LNUM = start_lnum;
3206 save_lnum = cctx->ctx_lnum;
3207 cctx->ctx_lnum = start_ctx_lnum;
3208
3209 status = check_ppconst_bool(ppconst);
3210 if (status != FAIL)
3211 {
3212 // Use the last ppconst if possible.
3213 if (ppconst->pp_used > 0)
3214 {
3215 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3216 int is_true = tv2bool(tv);
3217
3218 if ((is_true && opchar == '|')
3219 || (!is_true && opchar == '&'))
3220 {
3221 // For "false && expr" and "true || expr" the "expr"
3222 // does not need to be evaluated.
3223 cctx->ctx_skip = SKIP_YES;
3224 clear_tv(tv);
3225 tv->v_type = VAR_BOOL;
3226 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3227 }
3228 else
3229 {
3230 // For "true && expr" and "false || expr" only "expr"
3231 // needs to be evaluated.
3232 --ppconst->pp_used;
3233 jump_when = JUMP_NEVER;
3234 }
3235 }
3236 else
3237 {
3238 // Every part must evaluate to a bool.
3239 status = bool_on_stack(cctx);
3240 }
3241 }
3242 if (status != FAIL)
3243 status = ga_grow(&end_ga, 1);
3244 cctx->ctx_lnum = save_lnum;
3245 if (status == FAIL)
3246 {
3247 ga_clear(&end_ga);
3248 return FAIL;
3249 }
3250
3251 if (jump_when != JUMP_NEVER)
3252 {
3253 if (cctx->ctx_skip != SKIP_YES)
3254 {
3255 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3256 ++end_ga.ga_len;
3257 }
3258 generate_JUMP(cctx, jump_when, 0);
3259 }
3260
3261 // eval the next expression
3262 SOURCING_LNUM = save_sourcing_lnum;
3263 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3264 {
3265 ga_clear(&end_ga);
3266 return FAIL;
3267 }
3268
3269 const_used = ppconst->pp_used;
3270 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3271 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3272 {
3273 ga_clear(&end_ga);
3274 return FAIL;
3275 }
3276
3277 // "0 || 1" results in true, "1 && 0" results in false.
3278 if (ppconst->pp_used == const_used + 1)
3279 {
3280 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3281
3282 if (tv->v_type == VAR_NUMBER
3283 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3284 {
3285 tv->vval.v_number = tv->vval.v_number == 1
3286 ? VVAL_TRUE : VVAL_FALSE;
3287 tv->v_type = VAR_BOOL;
3288 }
3289 }
3290
3291 p = may_peek_next_line(cctx, *arg, &next);
3292 }
3293
3294 if (check_ppconst_bool(ppconst) == FAIL)
3295 {
3296 ga_clear(&end_ga);
3297 return FAIL;
3298 }
3299
3300 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3301 // Every part must evaluate to a bool.
3302 if (bool_on_stack(cctx) == FAIL)
3303 {
3304 ga_clear(&end_ga);
3305 return FAIL;
3306 }
3307
3308 if (end_ga.ga_len > 0)
3309 {
3310 // Fill in the end label in all jumps.
3311 generate_ppconst(cctx, ppconst);
3312 while (end_ga.ga_len > 0)
3313 {
3314 isn_T *isn;
3315
3316 --end_ga.ga_len;
3317 isn = ((isn_T *)instr->ga_data)
3318 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3319 isn->isn_arg.jump.jump_where = instr->ga_len;
3320 }
3321 }
3322 ga_clear(&end_ga);
3323
3324 cctx->ctx_skip = save_skip;
3325 }
3326
3327 return OK;
3328}
3329
3330/*
3331 * expr4a && expr4a && expr4a logical AND
3332 *
3333 * Produces instructions:
3334 * EVAL expr4a Push result of "expr4a"
3335 * COND2BOOL convert to bool if needed
3336 * JUMP_IF_COND_FALSE end
3337 * EVAL expr4b Push result of "expr4b"
3338 * JUMP_IF_COND_FALSE end
3339 * EVAL expr4c Push result of "expr4c"
3340 * end:
3341 */
3342 static int
3343compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3344{
3345 int ppconst_used = ppconst->pp_used;
3346
3347 // get the first variable
3348 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3349 return FAIL;
3350
3351 // || and && work almost the same
3352 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3353}
3354
3355/*
3356 * expr3a || expr3b || expr3c logical OR
3357 *
3358 * Produces instructions:
3359 * EVAL expr3a Push result of "expr3a"
3360 * COND2BOOL convert to bool if needed
3361 * JUMP_IF_COND_TRUE end
3362 * EVAL expr3b Push result of "expr3b"
3363 * JUMP_IF_COND_TRUE end
3364 * EVAL expr3c Push result of "expr3c"
3365 * end:
3366 */
3367 static int
3368compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3369{
3370 int ppconst_used = ppconst->pp_used;
3371
3372 // eval the first expression
3373 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3374 return FAIL;
3375
3376 // || and && work almost the same
3377 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3378}
3379
3380/*
3381 * Toplevel expression: expr2 ? expr1a : expr1b
3382 * Produces instructions:
3383 * EVAL expr2 Push result of "expr2"
3384 * JUMP_IF_FALSE alt jump if false
3385 * EVAL expr1a
3386 * JUMP_ALWAYS end
3387 * alt: EVAL expr1b
3388 * end:
3389 *
3390 * Toplevel expression: expr2 ?? expr1
3391 * Produces instructions:
3392 * EVAL expr2 Push result of "expr2"
3393 * JUMP_AND_KEEP_IF_TRUE end jump if true
3394 * EVAL expr1
3395 * end:
3396 */
3397 int
3398compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3399{
3400 char_u *p;
3401 int ppconst_used = ppconst->pp_used;
3402 char_u *next;
3403
3404 // Ignore all kinds of errors when not producing code.
3405 if (cctx->ctx_skip == SKIP_YES)
3406 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003407 int prev_did_emsg = did_emsg;
3408
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003409 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003410 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003411 }
3412
3413 // Evaluate the first expression.
3414 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3415 return FAIL;
3416
3417 p = may_peek_next_line(cctx, *arg, &next);
3418 if (*p == '?')
3419 {
3420 int op_falsy = p[1] == '?';
3421 garray_T *instr = &cctx->ctx_instr;
3422 garray_T *stack = &cctx->ctx_type_stack;
3423 int alt_idx = instr->ga_len;
3424 int end_idx = 0;
3425 isn_T *isn;
3426 type_T *type1 = NULL;
3427 int has_const_expr = FALSE;
3428 int const_value = FALSE;
3429 int save_skip = cctx->ctx_skip;
3430
3431 if (next != NULL)
3432 {
3433 *arg = next_line_from_context(cctx, TRUE);
3434 p = skipwhite(*arg);
3435 }
3436
3437 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3438 {
3439 semsg(_(e_white_space_required_before_and_after_str_at_str),
3440 op_falsy ? "??" : "?", p);
3441 return FAIL;
3442 }
3443
3444 if (ppconst->pp_used == ppconst_used + 1)
3445 {
3446 // the condition is a constant, we know whether the ? or the :
3447 // expression is to be evaluated.
3448 has_const_expr = TRUE;
3449 if (op_falsy)
3450 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3451 else
3452 {
3453 int error = FALSE;
3454
3455 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3456 &error);
3457 if (error)
3458 return FAIL;
3459 }
3460 cctx->ctx_skip = save_skip == SKIP_YES ||
3461 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3462
3463 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3464 // "left ?? right" and "left" is truthy: produce "left"
3465 generate_ppconst(cctx, ppconst);
3466 else
3467 {
3468 clear_tv(&ppconst->pp_tv[ppconst_used]);
3469 --ppconst->pp_used;
3470 }
3471 }
3472 else
3473 {
3474 generate_ppconst(cctx, ppconst);
3475 if (op_falsy)
3476 end_idx = instr->ga_len;
3477 generate_JUMP(cctx, op_falsy
3478 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3479 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003480 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003481 }
3482
3483 // evaluate the second expression; any type is accepted
3484 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3485 return FAIL;
3486 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3487 return FAIL;
3488
3489 if (!has_const_expr)
3490 {
3491 generate_ppconst(cctx, ppconst);
3492
3493 if (!op_falsy)
3494 {
3495 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003496 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003497 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003498
3499 end_idx = instr->ga_len;
3500 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3501
3502 // jump here from JUMP_IF_FALSE
3503 isn = ((isn_T *)instr->ga_data) + alt_idx;
3504 isn->isn_arg.jump.jump_where = instr->ga_len;
3505 }
3506 }
3507
3508 if (!op_falsy)
3509 {
3510 // Check for the ":".
3511 p = may_peek_next_line(cctx, *arg, &next);
3512 if (*p != ':')
3513 {
3514 emsg(_(e_missing_colon_after_questionmark));
3515 return FAIL;
3516 }
3517 if (next != NULL)
3518 {
3519 *arg = next_line_from_context(cctx, TRUE);
3520 p = skipwhite(*arg);
3521 }
3522
3523 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3524 {
3525 semsg(_(e_white_space_required_before_and_after_str_at_str),
3526 ":", p);
3527 return FAIL;
3528 }
3529
3530 // evaluate the third expression
3531 if (has_const_expr)
3532 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3533 ? SKIP_YES : SKIP_NOT;
3534 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3535 return FAIL;
3536 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3537 return FAIL;
3538 }
3539
3540 if (!has_const_expr)
3541 {
3542 type_T **typep;
3543
3544 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003545 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003546
3547 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003548 typep = &((((type2_T *)stack->ga_data)
3549 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003550 common_type(type1, *typep, typep, cctx->ctx_type_list);
3551
3552 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3553 isn = ((isn_T *)instr->ga_data) + end_idx;
3554 isn->isn_arg.jump.jump_where = instr->ga_len;
3555 }
3556
3557 cctx->ctx_skip = save_skip;
3558 }
3559 return OK;
3560}
3561
3562/*
3563 * Toplevel expression.
3564 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3565 * Returns OK or FAIL.
3566 */
3567 int
3568compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3569{
3570 ppconst_T ppconst;
3571
3572 CLEAR_FIELD(ppconst);
3573 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3574 {
3575 clear_ppconst(&ppconst);
3576 return FAIL;
3577 }
3578 if (is_const != NULL)
3579 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3580 if (generate_ppconst(cctx, &ppconst) == FAIL)
3581 return FAIL;
3582 return OK;
3583}
3584
3585/*
3586 * Toplevel expression.
3587 */
3588 int
3589compile_expr0(char_u **arg, cctx_T *cctx)
3590{
3591 return compile_expr0_ext(arg, cctx, NULL);
3592}
3593
3594
3595#endif // defined(FEAT_EVAL)