blob: bcad79ca17b29c945a35f686e34ce8c953dcf091 [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 {
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000296 emsg(_(e_using_super_not_in_class_function));
297 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-east2261c892023-08-16 21:49:54 +0900398 return generate_CALL(cctx, ufunc, cl, fi, type, argcount);
399 return generate_CALL(cctx, ufunc, NULL, 0, type, 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 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200410 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
411 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))
416 return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type,
417 FALSE);
418 return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type, FALSE);
Ernie Rael18143d32023-09-04 22:30:41 +0200419 }
420
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000421 // Could be a function reference: "obj.Func".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200422 m_idx = object_method_idx(cl, name, len);
423 if (m_idx >= 0)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000424 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200425 ufunc_T *fp = cl->class_obj_methods[m_idx];
426 if (type->tt_type == VAR_OBJECT
427 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
428 return generate_FUNCREF(cctx, fp, cl, m_idx, NULL);
429 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000430 }
431
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200432 member_not_found_msg(cl, VAR_OBJECT, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000433 }
434 else
435 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000436 // load class member
437 int idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200438 ocmember_T *m = class_member_lookup(cl, name, len, &idx);
439 if (m != NULL)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000440 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200441 // Note: type->tt_type = VAR_CLASS
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200442 // A private class variable can be accessed only in the class where
443 // it is defined.
444 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200445 {
446 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
447 return FAIL;
448 }
449
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000450 *arg = name_end;
451 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
452 }
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200453 member_not_found_msg(cl, VAR_CLASS, name, len);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000454 }
455
456 return FAIL;
457}
458
459/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000460 * Generate an instruction to load script-local variable "name", without the
461 * leading "s:".
462 * Also finds imported variables.
463 */
464 int
465compile_load_scriptvar(
466 cctx_T *cctx,
467 char_u *name, // variable NUL terminated
468 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100469 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000470{
471 scriptitem_T *si;
472 int idx;
473 imported_T *import;
474
475 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
476 return FAIL;
477 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000478 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000479 if (idx >= 0)
480 {
481 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
482
483 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
484 current_sctx.sc_sid, idx, sv->sv_type);
485 return OK;
486 }
487
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000488 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000489 if (import != NULL)
490 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000491 char_u *p = skipwhite(*end);
492 char_u *exp_name;
493 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100494 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000495 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000496 int done = FALSE;
497 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000498
499 // Need to lookup the member.
500 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000501 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000502 semsg(_(e_expected_dot_after_name_str), start);
503 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000504 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000505 ++p;
506 if (VIM_ISWHITE(*p))
507 {
508 emsg(_(e_no_white_space_allowed_after_dot));
509 return FAIL;
510 }
511
512 // isolate one name
513 exp_name = p;
514 while (eval_isnamec(*p))
515 ++p;
516 cc = *p;
517 *p = NUL;
518
Bram Moolenaard041f422022-01-12 19:54:00 +0000519 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100520 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
521 // "import autoload './dir/script.vim'" or
522 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100523 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100524
525 if (res == OK)
526 {
527 if (si->sn_autoload_prefix != NULL
528 && si->sn_state == SN_STATE_NOT_LOADED)
529 {
530 char_u *auto_name =
531 concat_str(si->sn_autoload_prefix, exp_name);
532
533 // autoload script must be loaded later, access by the autoload
534 // name. If a '(' follows it must be a function. Otherwise we
535 // don't know, it can be "script.Func".
536 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100537 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100538 else
539 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
540 vim_free(auto_name);
541 done = TRUE;
542 }
543 else if (si->sn_import_autoload
544 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100545 {
546 // If a '(' follows it must be a function. Otherwise we don't
547 // know, it can be "script.Func".
548 if (cc == '(' || paren_follows_after_expr)
549 {
550 char_u sid_name[MAX_FUNC_NAME_LEN];
551
552 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100553 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100554 }
555 else
556 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
557 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100558 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100559 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100560 else
561 {
562 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
563 cctx, NULL, TRUE);
564 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100565 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100566
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000567 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000568 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000569 if (done)
570 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000571
572 if (idx < 0)
573 {
574 if (ufunc != NULL)
575 {
576 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100577 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000578 return OK;
579 }
580 return FAIL;
581 }
582
583 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
584 import->imp_sid,
585 idx,
586 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000587 return OK;
588 }
589
Bram Moolenaard0132f42022-05-12 11:05:40 +0100590 // Can only get here if we know "name" is a script variable and not in a
591 // Vim9 script (variable is not in sn_var_vals): old style script.
592 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000593 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000594}
595
596 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000597generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000598{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000599 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000600 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000602 // Reject a global non-autoload function found without the "g:" prefix.
603 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604 return FAIL;
605
606 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000607 compile_type = get_compile_type(ufunc);
608 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000609 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000610 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100611 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612}
613
614/*
615 * Compile a variable name into a load instruction.
616 * "end" points to just after the name.
617 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
618 * When "error" is FALSE do not give an error when not found.
619 */
620 int
621compile_load(
622 char_u **arg,
623 char_u *end_arg,
624 cctx_T *cctx,
625 int is_expr,
626 int error)
627{
628 type_T *type;
629 char_u *name = NULL;
630 char_u *end = end_arg;
631 int res = FAIL;
632 int prev_called_emsg = called_emsg;
633
634 if (*(*arg + 1) == ':')
635 {
636 if (end <= *arg + 2)
637 {
638 isntype_T isn_type;
639
640 // load dictionary of namespace
641 switch (**arg)
642 {
643 case 'g': isn_type = ISN_LOADGDICT; break;
644 case 'w': isn_type = ISN_LOADWDICT; break;
645 case 't': isn_type = ISN_LOADTDICT; break;
646 case 'b': isn_type = ISN_LOADBDICT; break;
647 default:
648 semsg(_(e_namespace_not_supported_str), *arg);
649 goto theend;
650 }
651 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
652 goto theend;
653 res = OK;
654 }
655 else
656 {
657 isntype_T isn_type = ISN_DROP;
658
659 // load namespaced variable
660 name = vim_strnsave(*arg + 2, end - (*arg + 2));
661 if (name == NULL)
662 return FAIL;
663
664 switch (**arg)
665 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100666 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000667 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000668 case 's': if (current_script_is_vim9())
669 {
670 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
671 *arg);
672 vim_free(name);
673 return FAIL;
674 }
Kota Kato948a3892022-08-16 16:09:59 +0100675 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000676 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000677 else
678 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100679 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000680 break;
681 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
682 {
683 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000684 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000685 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686 else
687 isn_type = ISN_LOADG;
688 }
689 else
690 {
691 isn_type = ISN_LOADAUTO;
692 vim_free(name);
693 name = vim_strnsave(*arg, end - *arg);
694 if (name == NULL)
695 return FAIL;
696 }
697 break;
698 case 'w': isn_type = ISN_LOADW; break;
699 case 't': isn_type = ISN_LOADT; break;
700 case 'b': isn_type = ISN_LOADB; break;
701 default: // cannot happen, just in case
702 semsg(_(e_namespace_not_supported_str), *arg);
703 goto theend;
704 }
705 if (isn_type != ISN_DROP)
706 {
707 // Global, Buffer-local, Window-local and Tabpage-local
708 // variables can be defined later, thus we don't check if it
709 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000710 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000711 }
712 }
713 }
714 else
715 {
716 size_t len = end - *arg;
717 int idx;
718 int gen_load = FALSE;
719 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100720 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100721 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722
723 name = vim_strnsave(*arg, end - *arg);
724 if (name == NULL)
725 return FAIL;
726
Bram Moolenaar58b40092023-01-11 15:59:05 +0000727 if (STRCMP(name, "super") == 0
728 && cctx->ctx_ufunc != NULL
729 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
730 {
731 // super.SomeFunc() in a class function: push &t_super type, this
732 // is recognized in compile_subscript().
733 res = push_type_stack(cctx, &t_super);
734 if (*end != '.')
735 emsg(_(e_super_must_be_followed_by_dot));
736 }
737 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000738 {
739 script_autoload(name, FALSE);
740 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
741 }
742 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
743 == OK)
744 {
745 if (gen_load_outer == 0)
746 gen_load = TRUE;
747 }
748 else
749 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000750 lvar_T lvar;
751 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752
753 if (lookup_local(*arg, len, &lvar, cctx) == OK)
754 {
755 type = lvar.lv_type;
756 idx = lvar.lv_idx;
757 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100758 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100760 outer_loop_depth = lvar.lv_loop_depth;
761 outer_loop_idx = lvar.lv_loop_idx;
762 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000763 else
764 gen_load = TRUE;
765 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200766 else if ((idx = cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000767 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200768 // Referencing a class variable without the class name.
769 // A class variable can be referenced without the class name
770 // only in the class where the function is defined.
771 if (cctx->ctx_ufunc->uf_defclass == cl)
772 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
773 else
774 {
775 semsg(_(e_class_member_str_accessible_only_inside_class_str),
776 name, cl->class_name);
777 res = FAIL;
778 }
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000779 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780 else
781 {
782 // "var" can be script-local even without using "s:" if it
783 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000784 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000785 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100786 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000787
788 // When evaluating an expression and the name starts with an
789 // uppercase letter it can be a user defined function.
790 // generate_funcref() will fail if the function can't be found.
791 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000792 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 }
794 }
795 if (gen_load)
796 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
797 if (gen_load_outer > 0)
798 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100799 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
800 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 cctx->ctx_outer_used = TRUE;
802 }
803 }
804
805 *arg = end;
806
807theend:
808 if (res == FAIL && error && called_emsg == prev_called_emsg)
809 semsg(_(e_variable_not_found_str), name);
810 vim_free(name);
811 return res;
812}
813
814/*
815 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100816 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 * Returns FAIL if compilation fails.
818 */
819 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100820compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821{
LemonBoyf3b48952022-05-05 13:53:03 +0100822 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823 garray_T save_ga = cctx->ctx_instr;
824 int expr_res;
825 int trailing_error;
826 int instr_count;
827 isn_T *instr = NULL;
828
829 // Remove the string type from the stack.
830 --cctx->ctx_type_stack.ga_len;
831
832 // Temporarily reset the list of instructions so that the jump labels are
833 // correct.
834 cctx->ctx_instr.ga_len = 0;
835 cctx->ctx_instr.ga_maxlen = 0;
836 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000837
838 // avoid peeking a next line
839 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
840 cctx->ctx_ufunc->uf_lines.ga_len = 0;
841
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000843
844 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
845
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 s = skipwhite(s);
847 trailing_error = *s != NUL;
848
849 if (expr_res == FAIL || trailing_error
850 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
851 {
852 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000853 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000854 clear_instr_ga(&cctx->ctx_instr);
855 cctx->ctx_instr = save_ga;
856 ++cctx->ctx_type_stack.ga_len;
857 return FAIL;
858 }
859
860 // Move the generated instructions into the ISN_INSTR instruction, then
861 // restore the list of instructions.
862 instr_count = cctx->ctx_instr.ga_len;
863 instr = cctx->ctx_instr.ga_data;
864 instr[instr_count].isn_type = ISN_FINISH;
865
866 cctx->ctx_instr = save_ga;
867 vim_free(isn->isn_arg.string);
868 isn->isn_type = ISN_INSTR;
869 isn->isn_arg.instr = instr;
870 return OK;
871}
872
873/*
874 * Compile the argument expressions.
875 * "arg" points to just after the "(" and is advanced to after the ")"
876 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100877 int
LemonBoyf3b48952022-05-05 13:53:03 +0100878compile_arguments(
879 char_u **arg,
880 cctx_T *cctx,
881 int *argcount,
882 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883{
884 char_u *p = *arg;
885 char_u *whitep = *arg;
886 int must_end = FALSE;
887 int instr_count;
888
889 for (;;)
890 {
891 if (may_get_next_line(whitep, &p, cctx) == FAIL)
892 goto failret;
893 if (*p == ')')
894 {
895 *arg = p + 1;
896 return OK;
897 }
898 if (must_end)
899 {
900 semsg(_(e_missing_comma_before_argument_str), p);
901 return FAIL;
902 }
903
904 instr_count = cctx->ctx_instr.ga_len;
905 if (compile_expr0(&p, cctx) == FAIL)
906 return FAIL;
907 ++*argcount;
908
LemonBoyf3b48952022-05-05 13:53:03 +0100909 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 && cctx->ctx_instr.ga_len == instr_count + 1)
911 {
912 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
913
914 // {skip} argument of searchpair() can be compiled if not empty
915 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100916 compile_string(isn, cctx, 0);
917 }
918 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
919 && cctx->ctx_instr.ga_len == instr_count + 1)
920 {
921 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
922
923 // {sub} argument of substitute() can be compiled if it starts
924 // with \=
925 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100926 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100927 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 }
929
930 if (*p != ',' && *skipwhite(p) == ',')
931 {
932 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
933 p = skipwhite(p);
934 }
935 if (*p == ',')
936 {
937 ++p;
938 if (*p != NUL && !VIM_ISWHITE(*p))
939 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
940 }
941 else
942 must_end = TRUE;
943 whitep = p;
944 p = skipwhite(p);
945 }
946failret:
947 emsg(_(e_missing_closing_paren));
948 return FAIL;
949}
950
951/*
952 * Compile a function call: name(arg1, arg2)
953 * "arg" points to "name", "arg + varlen" to the "(".
954 * "argcount_init" is 1 for "value->method()"
955 * Instructions:
956 * EVAL arg1
957 * EVAL arg2
958 * BCALL / DCALL / UCALL
959 */
960 static int
961compile_call(
962 char_u **arg,
963 size_t varlen,
964 cctx_T *cctx,
965 ppconst_T *ppconst,
966 int argcount_init)
967{
968 char_u *name = *arg;
969 char_u *p;
970 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100971 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972 char_u fname_buf[FLEN_FIXED + 1];
973 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 ufunc_T *ufunc = NULL;
975 int res = FAIL;
976 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000977 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100978 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000979 imported_T *import;
h-east2261c892023-08-16 21:49:54 +0900980 type_T *type;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000981
982 if (varlen >= sizeof(namebuf))
983 {
984 semsg(_(e_name_too_long_str), name);
985 return FAIL;
986 }
987 vim_strncpy(namebuf, *arg, varlen);
988
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000989 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000990 if (import != NULL)
991 {
992 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
993 return FAIL;
994 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000995
996 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100997 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000998 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100999 if ((varlen == 3
1000 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1002 {
1003 char_u *s = skipwhite(*arg + varlen + 1);
1004 typval_T argvars[2];
1005 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001006 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007
1008 argvars[0].v_type = VAR_UNKNOWN;
1009 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001010 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001012 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001013 s = skipwhite(s);
1014 if (*s == ')' && argvars[0].v_type == VAR_STRING
1015 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1016 || !is_has))
1017 {
1018 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1019
1020 *arg = s + 1;
1021 argvars[1].v_type = VAR_UNKNOWN;
1022 tv->v_type = VAR_NUMBER;
1023 tv->vval.v_number = 0;
1024 if (is_has)
1025 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001026 else if (is_len)
1027 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001028 else
1029 f_exists(argvars, tv);
1030 clear_tv(&argvars[0]);
1031 ++ppconst->pp_used;
1032 return OK;
1033 }
1034 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001035 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 {
1037 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1038 return FAIL;
1039 }
1040 }
1041
1042 if (generate_ppconst(cctx, ppconst) == FAIL)
1043 return FAIL;
1044
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001045 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001046 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1047
1048 // We handle the "skip" argument of searchpair() and searchpairpos()
1049 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001050 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1051 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1052 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1053 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1054 special_fn = CA_SEARCHPAIR;
1055 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1056 special_fn = CA_SUBSTITUTE;
1057 else
1058 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059
1060 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001061 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062 goto theend;
1063
h-east2261c892023-08-16 21:49:54 +09001064 type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001065 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1066 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1067 {
1068 int idx;
1069
1070 // builtin function
1071 idx = find_internal_func(name);
1072 if (idx >= 0)
1073 {
1074 if (STRCMP(name, "flatten") == 0)
1075 {
1076 emsg(_(e_cannot_use_flatten_in_vim9_script));
1077 goto theend;
1078 }
1079
1080 if (STRCMP(name, "add") == 0 && argcount == 2)
1081 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 // add() can be compiled to instructions if we know the type
1083 if (type->tt_type == VAR_LIST)
1084 {
1085 // inline "add(list, item)" so that the type can be checked
1086 res = generate_LISTAPPEND(cctx);
1087 idx = -1;
1088 }
1089 else if (type->tt_type == VAR_BLOB)
1090 {
1091 // inline "add(blob, nr)" so that the type can be checked
1092 res = generate_BLOBAPPEND(cctx);
1093 idx = -1;
1094 }
1095 }
1096
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001097 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1098 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001099 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001100 // May have the "D" or "R" flag, reserve a variable for a
1101 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001102 if (get_defer_var_idx(cctx) == 0)
1103 idx = -1;
1104 }
1105
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 if (idx >= 0)
1107 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1108 }
1109 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001110 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001111 goto theend;
1112 }
1113
Bram Moolenaar62aec932022-01-29 21:45:34 +00001114 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1115
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116 // An argument or local variable can be a function reference, this
1117 // overrules a function name.
1118 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1119 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1120 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001121 class_T *cl = NULL;
1122 int mi = 0;
1123
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124 // If we can find the function by name generate the right call.
1125 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001126 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001127 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001129 if (!func_is_global(ufunc))
1130 {
h-east2261c892023-08-16 21:49:54 +09001131 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001132 goto theend;
1133 }
1134 if (!has_g_namespace
1135 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1136 {
1137 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001138 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001139 goto theend;
1140 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001142 else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001143 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001144 // Class method invocation without the class name.
1145 // A class method can be referenced without the class name only in
1146 // the class where the function is defined.
1147 if (cctx->ctx_ufunc->uf_defclass == cl)
1148 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001149 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL,
1150 0, type, argcount);
1151 }
1152 else
1153 {
1154 semsg(_(e_class_member_str_accessible_only_inside_class_str),
1155 name, cl->class_name);
1156 res = FAIL;
1157 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001158 goto theend;
1159 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160 }
1161
1162 // If the name is a variable, load it and use PCALL.
1163 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001164 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001166 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1168 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001169 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170
Christian Brabandtd5475e82023-08-17 23:34:09 +02001171 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001172 goto theend;
1173 }
1174
1175 // If we can find a global function by name generate the right call.
1176 if (ufunc != NULL)
1177 {
h-east2261c892023-08-16 21:49:54 +09001178 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 goto theend;
1180 }
1181
1182 // A global function may be defined only later. Need to figure out at
1183 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001184 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185 res = generate_UCALL(cctx, name, argcount);
1186 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001187 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188
1189theend:
1190 vim_free(tofree);
1191 return res;
1192}
1193
1194// like NAMESPACE_CHAR but with 'a' and 'l'.
1195#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1196
1197/*
1198 * Find the end of a variable or function name. Unlike find_name_end() this
1199 * does not recognize magic braces.
1200 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1201 * Return a pointer to just after the name. Equal to "arg" if there is no
1202 * valid name.
1203 */
1204 char_u *
1205to_name_end(char_u *arg, int use_namespace)
1206{
1207 char_u *p;
1208
1209 // Quick check for valid starting character.
1210 if (!eval_isnamec1(*arg))
1211 return arg;
1212
1213 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1214 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1215 // and can be used in slice "[n:]".
1216 if (*p == ':' && (p != arg + 1
1217 || !use_namespace
1218 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1219 break;
1220 return p;
1221}
1222
1223/*
1224 * Like to_name_end() but also skip over a list or dict constant.
1225 * Also accept "<SNR>123_Func".
1226 * This intentionally does not handle line continuation.
1227 */
1228 char_u *
1229to_name_const_end(char_u *arg)
1230{
1231 char_u *p = arg;
1232 typval_T rettv;
1233
1234 if (STRNCMP(p, "<SNR>", 5) == 0)
1235 p = skipdigits(p + 5);
1236 p = to_name_end(p, TRUE);
1237 if (p == arg && *arg == '[')
1238 {
1239
1240 // Can be "[1, 2, 3]->Func()".
1241 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1242 p = arg;
1243 }
1244 return p;
1245}
1246
1247/*
1248 * parse a list: [expr, expr]
1249 * "*arg" points to the '['.
1250 * ppconst->pp_is_const is set if all items are a constant.
1251 */
1252 static int
1253compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1254{
1255 char_u *p = skipwhite(*arg + 1);
1256 char_u *whitep = *arg + 1;
1257 int count = 0;
1258 int is_const;
1259 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001260 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001261
1262 for (;;)
1263 {
1264 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1265 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001266 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267 return FAIL;
1268 }
1269 if (*p == ',')
1270 {
1271 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1272 return FAIL;
1273 }
1274 if (*p == ']')
1275 {
1276 ++p;
1277 break;
1278 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001279 if (must_end)
1280 {
1281 semsg(_(e_missing_comma_in_list_str), p);
1282 return FAIL;
1283 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001284 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1285 return FAIL;
1286 if (!is_const)
1287 is_all_const = FALSE;
1288 ++count;
1289 if (*p == ',')
1290 {
1291 ++p;
1292 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1293 {
1294 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1295 return FAIL;
1296 }
1297 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001298 else
1299 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 whitep = p;
1301 p = skipwhite(p);
1302 }
1303 *arg = p;
1304
1305 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001306 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307}
1308
1309/*
1310 * Parse a lambda: "(arg, arg) => expr"
1311 * "*arg" points to the '('.
1312 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1313 */
1314 static int
1315compile_lambda(char_u **arg, cctx_T *cctx)
1316{
1317 int r;
1318 typval_T rettv;
1319 ufunc_T *ufunc;
1320 evalarg_T evalarg;
1321
1322 init_evalarg(&evalarg);
1323 evalarg.eval_flags = EVAL_EVALUATE;
1324 evalarg.eval_cctx = cctx;
1325
1326 // Get the funcref in "rettv".
1327 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1328 if (r != OK)
1329 {
1330 clear_evalarg(&evalarg, NULL);
1331 return r;
1332 }
1333
1334 // "rettv" will now be a partial referencing the function.
1335 ufunc = rettv.vval.v_partial->pt_func;
1336 ++ufunc->uf_refcount;
1337 clear_tv(&rettv);
1338
1339 // Compile it here to get the return type. The return type is optional,
1340 // when it's missing use t_unknown. This is recognized in
1341 // compile_return().
1342 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1343 ufunc->uf_ret_type = &t_unknown;
1344 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1345
1346 // When the outer function is compiled for profiling or debugging, the
1347 // lambda may be called without profiling or debugging. Compile it here in
1348 // the right context.
1349 if (cctx->ctx_compile_type == CT_DEBUG
1350#ifdef FEAT_PROFILE
1351 || cctx->ctx_compile_type == CT_PROFILE
1352#endif
1353 )
1354 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1355
Bram Moolenaar139575d2022-03-15 19:29:30 +00001356 // if the outer function is not compiled for debugging or profiling, this
1357 // one might be
1358 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001359 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001360 compiletype_T compile_type = get_compile_type(ufunc);
1361
1362 if (compile_type != CT_NONE)
1363 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001364 }
1365
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1367 // "*arg" may point into it. Point into the original line to avoid a
1368 // dangling pointer.
1369 if (evalarg.eval_using_cmdline)
1370 {
1371 garray_T *gap = &evalarg.eval_tofree_ga;
1372 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1373
1374 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1375 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001376 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 }
1378
1379 clear_evalarg(&evalarg, NULL);
1380
1381 if (ufunc->uf_def_status == UF_COMPILED)
1382 {
1383 // The return type will now be known.
1384 set_function_type(ufunc);
1385
1386 // The function reference count will be 1. When the ISN_FUNCREF
1387 // instruction is deleted the reference count is decremented and the
1388 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001389 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390 }
1391
1392 func_ptr_unref(ufunc);
1393 return FAIL;
1394}
1395
1396/*
1397 * Get a lambda and compile it. Uses Vim9 syntax.
1398 */
1399 int
1400get_lambda_tv_and_compile(
1401 char_u **arg,
1402 typval_T *rettv,
1403 int types_optional,
1404 evalarg_T *evalarg)
1405{
1406 int r;
1407 ufunc_T *ufunc;
1408 int save_sc_version = current_sctx.sc_version;
1409
1410 // Get the funcref in "rettv".
1411 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1412 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1413 current_sctx.sc_version = save_sc_version;
1414 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001415 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001416
1417 // "rettv" will now be a partial referencing the function.
1418 ufunc = rettv->vval.v_partial->pt_func;
1419
1420 // Compile it here to get the return type. The return type is optional,
1421 // when it's missing use t_unknown. This is recognized in
1422 // compile_return().
1423 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1424 ufunc->uf_ret_type = &t_unknown;
1425 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1426
1427 if (ufunc->uf_def_status == UF_COMPILED)
1428 {
1429 // The return type will now be known.
1430 set_function_type(ufunc);
1431 return OK;
1432 }
1433 clear_tv(rettv);
1434 return FAIL;
1435}
1436
1437/*
1438 * parse a dict: {key: val, [key]: val}
1439 * "*arg" points to the '{'.
1440 * ppconst->pp_is_const is set if all item values are a constant.
1441 */
1442 static int
1443compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1444{
1445 garray_T *instr = &cctx->ctx_instr;
1446 int count = 0;
1447 dict_T *d = dict_alloc();
1448 dictitem_T *item;
1449 char_u *whitep = *arg + 1;
1450 char_u *p;
1451 int is_const;
1452 int is_all_const = TRUE; // reset when non-const encountered
1453
1454 if (d == NULL)
1455 return FAIL;
1456 if (generate_ppconst(cctx, ppconst) == FAIL)
1457 return FAIL;
1458 for (;;)
1459 {
1460 char_u *key = NULL;
1461
1462 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1463 {
1464 *arg = NULL;
1465 goto failret;
1466 }
1467
1468 if (**arg == '}')
1469 break;
1470
1471 if (**arg == '[')
1472 {
1473 isn_T *isn;
1474
1475 // {[expr]: value} uses an evaluated key.
1476 *arg = skipwhite(*arg + 1);
1477 if (compile_expr0(arg, cctx) == FAIL)
1478 return FAIL;
1479 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1480 if (isn->isn_type == ISN_PUSHNR)
1481 {
1482 char buf[NUMBUFLEN];
1483
1484 // Convert to string at compile time.
1485 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1486 isn->isn_type = ISN_PUSHS;
1487 isn->isn_arg.string = vim_strsave((char_u *)buf);
1488 }
1489 if (isn->isn_type == ISN_PUSHS)
1490 key = isn->isn_arg.string;
1491 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1492 return FAIL;
1493 *arg = skipwhite(*arg);
1494 if (**arg != ']')
1495 {
1496 emsg(_(e_missing_matching_bracket_after_dict_key));
1497 return FAIL;
1498 }
1499 ++*arg;
1500 }
1501 else
1502 {
1503 // {"name": value},
1504 // {'name': value},
1505 // {name: value} use "name" as a literal key
1506 key = get_literal_key(arg);
1507 if (key == NULL)
1508 return FAIL;
1509 if (generate_PUSHS(cctx, &key) == FAIL)
1510 return FAIL;
1511 }
1512
1513 // Check for duplicate keys, if using string keys.
1514 if (key != NULL)
1515 {
1516 item = dict_find(d, key, -1);
1517 if (item != NULL)
1518 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001519 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001520 goto failret;
1521 }
1522 item = dictitem_alloc(key);
1523 if (item != NULL)
1524 {
1525 item->di_tv.v_type = VAR_UNKNOWN;
1526 item->di_tv.v_lock = 0;
1527 if (dict_add(d, item) == FAIL)
1528 dictitem_free(item);
1529 }
1530 }
1531
1532 if (**arg != ':')
1533 {
1534 if (*skipwhite(*arg) == ':')
1535 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1536 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001537 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001538 return FAIL;
1539 }
1540 whitep = *arg + 1;
1541 if (!IS_WHITE_OR_NUL(*whitep))
1542 {
1543 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1544 return FAIL;
1545 }
1546
1547 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1548 {
1549 *arg = NULL;
1550 goto failret;
1551 }
1552
1553 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1554 return FAIL;
1555 if (!is_const)
1556 is_all_const = FALSE;
1557 ++count;
1558
1559 whitep = *arg;
1560 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1561 {
1562 *arg = NULL;
1563 goto failret;
1564 }
1565 if (**arg == '}')
1566 break;
1567 if (**arg != ',')
1568 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001569 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570 goto failret;
1571 }
1572 if (IS_WHITE_OR_NUL(*whitep))
1573 {
1574 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1575 return FAIL;
1576 }
1577 whitep = *arg + 1;
1578 if (!IS_WHITE_OR_NUL(*whitep))
1579 {
1580 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1581 return FAIL;
1582 }
1583 *arg = skipwhite(whitep);
1584 }
1585
1586 *arg = *arg + 1;
1587
1588 // Allow for following comment, after at least one space.
1589 p = skipwhite(*arg);
1590 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1591 *arg += STRLEN(*arg);
1592
1593 dict_unref(d);
1594 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001595 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001596
1597failret:
1598 if (*arg == NULL)
1599 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001600 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601 *arg = (char_u *)"";
1602 }
1603 dict_unref(d);
1604 return FAIL;
1605}
1606
1607/*
1608 * Compile "&option".
1609 */
1610 static int
1611compile_get_option(char_u **arg, cctx_T *cctx)
1612{
1613 typval_T rettv;
1614 char_u *start = *arg;
1615 int ret;
1616
1617 // parse the option and get the current value to get the type.
1618 rettv.v_type = VAR_UNKNOWN;
1619 ret = eval_option(arg, &rettv, TRUE);
1620 if (ret == OK)
1621 {
1622 // include the '&' in the name, eval_option() expects it.
1623 char_u *name = vim_strnsave(start, *arg - start);
1624 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1625 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1626
1627 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1628 vim_free(name);
1629 }
1630 clear_tv(&rettv);
1631
1632 return ret;
1633}
1634
1635/*
1636 * Compile "$VAR".
1637 */
1638 static int
1639compile_get_env(char_u **arg, cctx_T *cctx)
1640{
1641 char_u *start = *arg;
1642 int len;
1643 int ret;
1644 char_u *name;
1645
1646 ++*arg;
1647 len = get_env_len(arg);
1648 if (len == 0)
1649 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001650 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651 return FAIL;
1652 }
1653
1654 // include the '$' in the name, eval_env_var() expects it.
1655 name = vim_strnsave(start, len + 1);
1656 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1657 vim_free(name);
1658 return ret;
1659}
1660
1661/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001662 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001663 */
1664 static int
1665compile_interp_string(char_u **arg, cctx_T *cctx)
1666{
1667 typval_T tv;
1668 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001669 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001670 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001671 int count = 0;
1672 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001673
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001674 // *arg is on the '$' character, move it to the first string character.
1675 ++*arg;
1676 quote = **arg;
1677 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001678
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001679 for (;;)
1680 {
1681 // Get the string up to the matching quote or to a single '{'.
1682 // "arg" is advanced to either the quote or the '{'.
1683 if (quote == '"')
1684 ret = eval_string(arg, &tv, evaluate, TRUE);
1685 else
1686 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1687 if (ret == FAIL)
1688 break;
1689 if (evaluate)
1690 {
1691 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1692 || (**arg != '{' && count == 0))
1693 {
1694 // generate non-empty string or empty string if it's the only
1695 // one
1696 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1697 return FAIL;
1698 tv.vval.v_string = NULL; // don't free it now
1699 ++count;
1700 }
1701 clear_tv(&tv);
1702 }
1703
1704 if (**arg != '{')
1705 {
1706 // found terminating quote
1707 ++*arg;
1708 break;
1709 }
1710
1711 p = compile_one_expr_in_str(*arg, cctx);
1712 if (p == NULL)
1713 {
1714 ret = FAIL;
1715 break;
1716 }
1717 ++count;
1718 *arg = p;
1719 }
LemonBoy2eaef102022-05-06 13:14:50 +01001720
1721 if (ret == FAIL || !evaluate)
1722 return ret;
1723
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001724 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1725 if (count > 1)
1726 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001727
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001728 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001729}
1730
1731/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 * Compile "@r".
1733 */
1734 static int
1735compile_get_register(char_u **arg, cctx_T *cctx)
1736{
1737 int ret;
1738
1739 ++*arg;
1740 if (**arg == NUL)
1741 {
1742 semsg(_(e_syntax_error_at_str), *arg - 1);
1743 return FAIL;
1744 }
1745 if (!valid_yank_reg(**arg, FALSE))
1746 {
1747 emsg_invreg(**arg);
1748 return FAIL;
1749 }
1750 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1751 ++*arg;
1752 return ret;
1753}
1754
1755/*
1756 * Apply leading '!', '-' and '+' to constant "rettv".
1757 * When "numeric_only" is TRUE do not apply '!'.
1758 */
1759 static int
1760apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1761{
1762 char_u *p = *end;
1763
1764 // this works from end to start
1765 while (p > start)
1766 {
1767 --p;
1768 if (*p == '-' || *p == '+')
1769 {
1770 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771 if (rettv->v_type == VAR_FLOAT)
1772 {
1773 if (*p == '-')
1774 rettv->vval.v_float = -rettv->vval.v_float;
1775 }
1776 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 {
1778 varnumber_T val;
1779 int error = FALSE;
1780
1781 // tv_get_number_chk() accepts a string, but we don't want that
1782 // here
1783 if (check_not_string(rettv) == FAIL)
1784 return FAIL;
1785 val = tv_get_number_chk(rettv, &error);
1786 clear_tv(rettv);
1787 if (error)
1788 return FAIL;
1789 if (*p == '-')
1790 val = -val;
1791 rettv->v_type = VAR_NUMBER;
1792 rettv->vval.v_number = val;
1793 }
1794 }
1795 else if (numeric_only)
1796 {
1797 ++p;
1798 break;
1799 }
1800 else if (*p == '!')
1801 {
1802 int v = tv2bool(rettv);
1803
1804 // '!' is permissive in the type.
1805 clear_tv(rettv);
1806 rettv->v_type = VAR_BOOL;
1807 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1808 }
1809 }
1810 *end = p;
1811 return OK;
1812}
1813
1814/*
1815 * Recognize v: variables that are constants and set "rettv".
1816 */
1817 static void
1818get_vim_constant(char_u **arg, typval_T *rettv)
1819{
1820 if (STRNCMP(*arg, "v:true", 6) == 0)
1821 {
1822 rettv->v_type = VAR_BOOL;
1823 rettv->vval.v_number = VVAL_TRUE;
1824 *arg += 6;
1825 }
1826 else if (STRNCMP(*arg, "v:false", 7) == 0)
1827 {
1828 rettv->v_type = VAR_BOOL;
1829 rettv->vval.v_number = VVAL_FALSE;
1830 *arg += 7;
1831 }
1832 else if (STRNCMP(*arg, "v:null", 6) == 0)
1833 {
1834 rettv->v_type = VAR_SPECIAL;
1835 rettv->vval.v_number = VVAL_NULL;
1836 *arg += 6;
1837 }
1838 else if (STRNCMP(*arg, "v:none", 6) == 0)
1839 {
1840 rettv->v_type = VAR_SPECIAL;
1841 rettv->vval.v_number = VVAL_NONE;
1842 *arg += 6;
1843 }
1844}
1845
1846 exprtype_T
1847get_compare_type(char_u *p, int *len, int *type_is)
1848{
1849 exprtype_T type = EXPR_UNKNOWN;
1850 int i;
1851
1852 switch (p[0])
1853 {
1854 case '=': if (p[1] == '=')
1855 type = EXPR_EQUAL;
1856 else if (p[1] == '~')
1857 type = EXPR_MATCH;
1858 break;
1859 case '!': if (p[1] == '=')
1860 type = EXPR_NEQUAL;
1861 else if (p[1] == '~')
1862 type = EXPR_NOMATCH;
1863 break;
1864 case '>': if (p[1] != '=')
1865 {
1866 type = EXPR_GREATER;
1867 *len = 1;
1868 }
1869 else
1870 type = EXPR_GEQUAL;
1871 break;
1872 case '<': if (p[1] != '=')
1873 {
1874 type = EXPR_SMALLER;
1875 *len = 1;
1876 }
1877 else
1878 type = EXPR_SEQUAL;
1879 break;
1880 case 'i': if (p[1] == 's')
1881 {
1882 // "is" and "isnot"; but not a prefix of a name
1883 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1884 *len = 5;
1885 i = p[*len];
1886 if (!isalnum(i) && i != '_')
1887 {
1888 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1889 *type_is = TRUE;
1890 }
1891 }
1892 break;
1893 }
1894 return type;
1895}
1896
1897/*
1898 * Skip over an expression, ignoring most errors.
1899 */
1900 void
1901skip_expr_cctx(char_u **arg, cctx_T *cctx)
1902{
1903 evalarg_T evalarg;
1904
1905 init_evalarg(&evalarg);
1906 evalarg.eval_cctx = cctx;
1907 skip_expr(arg, &evalarg);
1908 clear_evalarg(&evalarg, NULL);
1909}
1910
1911/*
1912 * Check that the top of the type stack has a type that can be used as a
1913 * condition. Give an error and return FAIL if not.
1914 */
1915 int
1916bool_on_stack(cctx_T *cctx)
1917{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001918 type_T *type;
1919
Bram Moolenaar078a4612022-01-04 15:17:03 +00001920 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001921 if (type == &t_bool)
1922 return OK;
1923
Bram Moolenaar4913d422022-10-17 13:13:32 +01001924 if (type->tt_type == VAR_ANY
1925 || type->tt_type == VAR_UNKNOWN
1926 || type->tt_type == VAR_NUMBER
1927 || type == &t_number_bool
1928 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001929 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1930 // This requires a runtime type check.
1931 return generate_COND2BOOL(cctx);
1932
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001933 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001934}
1935
1936/*
1937 * Give the "white on both sides" error, taking the operator from "p[len]".
1938 */
1939 void
1940error_white_both(char_u *op, int len)
1941{
1942 char_u buf[10];
1943
1944 vim_strncpy(buf, op, len);
1945 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1946}
1947
1948/*
1949 * Compile code to apply '-', '+' and '!'.
1950 * When "numeric_only" is TRUE do not apply '!'.
1951 */
1952 static int
1953compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1954{
1955 char_u *p = *end;
1956
1957 // this works from end to start
1958 while (p > start)
1959 {
1960 --p;
1961 while (VIM_ISWHITE(*p))
1962 --p;
1963 if (*p == '-' || *p == '+')
1964 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001965 type_T *type = get_type_on_stack(cctx, 0);
1966 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001967 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001968 return FAIL;
1969
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001970 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001971 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1972 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001973 }
1974 else if (numeric_only)
1975 {
1976 ++p;
1977 break;
1978 }
1979 else
1980 {
1981 int invert = *p == '!';
1982
1983 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1984 {
1985 if (p[-1] == '!')
1986 invert = !invert;
1987 --p;
1988 }
1989 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1990 return FAIL;
1991 }
1992 }
1993 *end = p;
1994 return OK;
1995}
1996
1997/*
1998 * Compile "(expression)": recursive!
1999 * Return FAIL/OK.
2000 */
2001 static int
2002compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2003{
2004 int ret;
2005 char_u *p = *arg + 1;
2006
2007 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2008 return FAIL;
2009 if (ppconst->pp_used <= PPSIZE - 10)
2010 {
2011 ret = compile_expr1(arg, cctx, ppconst);
2012 }
2013 else
2014 {
2015 // Not enough space in ppconst, flush constants.
2016 if (generate_ppconst(cctx, ppconst) == FAIL)
2017 return FAIL;
2018 ret = compile_expr0(arg, cctx);
2019 }
2020 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2021 return FAIL;
2022 if (**arg == ')')
2023 ++*arg;
2024 else if (ret == OK)
2025 {
2026 emsg(_(e_missing_closing_paren));
2027 ret = FAIL;
2028 }
2029 return ret;
2030}
2031
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002032static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002033
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034/*
2035 * Compile whatever comes after "name" or "name()".
2036 * Advances "*arg" only when something was recognized.
2037 */
2038 static int
2039compile_subscript(
2040 char_u **arg,
2041 cctx_T *cctx,
2042 char_u *start_leader,
2043 char_u **end_leader,
2044 ppconst_T *ppconst)
2045{
2046 char_u *name_start = *end_leader;
2047 int keeping_dict = FALSE;
2048
2049 for (;;)
2050 {
2051 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002052 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002053
2054 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2055 {
2056 char_u *next = peek_next_line_from_context(cctx);
2057
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002058 // If a following line starts with "->{", "->(" or "->X" advance to
2059 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060 // Also if a following line starts with ".x".
2061 if (next != NULL &&
2062 ((next[0] == '-' && next[1] == '>'
2063 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002064 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 || ASCII_ISALPHA(*skipwhite(next + 2))))
2066 || (next[0] == '.' && eval_isdictc(next[1]))))
2067 {
2068 next = next_line_from_context(cctx, TRUE);
2069 if (next == NULL)
2070 return FAIL;
2071 *arg = next;
2072 p = skipwhite(*arg);
2073 }
2074 }
2075
2076 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2077 // is not a function call.
2078 if (**arg == '(')
2079 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002080 int argcount = 0;
2081
2082 if (generate_ppconst(cctx, ppconst) == FAIL)
2083 return FAIL;
2084 ppconst->pp_is_const = FALSE;
2085
2086 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002087 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002088
2089 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002090 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 return FAIL;
2092 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2093 return FAIL;
2094 if (keeping_dict)
2095 {
2096 keeping_dict = FALSE;
2097 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2098 return FAIL;
2099 }
2100 }
2101 else if (*p == '-' && p[1] == '>')
2102 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002103 char_u *pstart = p;
2104 int alt;
2105 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106
Bram Moolenaarc7349932022-01-16 20:59:39 +00002107 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002108 if (generate_ppconst(cctx, ppconst) == FAIL)
2109 return FAIL;
2110 ppconst->pp_is_const = FALSE;
2111
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002112 // Apply the '!', '-' and '+' first:
2113 // -1.0->func() works like (-1.0)->func()
2114 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2115 return FAIL;
2116
2117 p += 2;
2118 *arg = skipwhite(p);
2119 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002120
2121 // Three alternatives handled here:
2122 // 1. "base->name(" only a name, use compile_call()
2123 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2124 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002126 alt = 2;
2127 else
2128 {
2129 // alternative 1 or 3
2130 p = *arg;
2131 if (!eval_isnamec1(*p))
2132 {
2133 semsg(_(e_trailing_characters_str), pstart);
2134 return FAIL;
2135 }
2136 if (ASCII_ISALPHA(*p) && p[1] == ':')
2137 p += 2;
2138 for ( ; eval_isnamec(*p); ++p)
2139 ;
2140 if (*p == '(')
2141 {
2142 // alternative 1
2143 alt = 1;
2144 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2145 return FAIL;
2146 }
2147 else
2148 {
2149 // Must be alternative 3, find the "(". Only works within
2150 // one line.
2151 alt = 3;
2152 paren = vim_strchr(p, '(');
2153 if (paren == NULL)
2154 {
2155 semsg(_(e_missing_parenthesis_str), *arg);
2156 return FAIL;
2157 }
2158 }
2159 }
2160
2161 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002162 {
2163 int argcount = 1;
2164 garray_T *stack = &cctx->ctx_type_stack;
2165 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 int expr_isn_start = cctx->ctx_instr.ga_len;
2167 int expr_isn_end;
2168 int arg_isn_count;
2169
Bram Moolenaarc7349932022-01-16 20:59:39 +00002170 if (alt == 2)
2171 {
2172 // Funcref call: list->(Refs[2])(arg)
2173 // or lambda: list->((arg) => expr)(arg)
2174 //
2175 // Fist compile the function expression.
2176 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2177 return FAIL;
2178 }
2179 else
2180 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002181 int fail;
2182 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002183 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002184
Bram Moolenaarc7349932022-01-16 20:59:39 +00002185 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002186
2187 // instead of using LOADG for "import.Func" use PUSHFUNC
2188 ++paren_follows_after_expr;
2189
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002190 // do not look in the next line
2191 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002192
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002193 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002194 || *skipwhite(*arg) != NUL;
2195 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002196 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002197 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002198
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002199 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002200 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002201 if (did_emsg == prev_did_emsg)
2202 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002203 return FAIL;
2204 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002205 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002207 // Compile the arguments.
2208 if (**arg != '(')
2209 {
2210 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002211 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002212 else
2213 semsg(_(e_missing_parenthesis_str), *arg);
2214 return FAIL;
2215 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002216
2217 // Remember the next instruction index, where the instructions
2218 // for arguments are being written.
2219 expr_isn_end = cctx->ctx_instr.ga_len;
2220
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002221 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002222 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2223 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 return FAIL;
2225
2226 // Move the instructions for the arguments to before the
2227 // instructions of the expression and move the type of the
2228 // expression after the argument types. This is what ISN_PCALL
2229 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002230 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2231 if (arg_isn_count > 0)
2232 {
2233 int expr_isn_count = expr_isn_end - expr_isn_start;
2234 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002235 type_T *decl_type;
2236 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237
2238 if (isn == NULL)
2239 return FAIL;
2240 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2241 + expr_isn_start,
2242 sizeof(isn_T) * expr_isn_count);
2243 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2244 + expr_isn_start,
2245 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2246 sizeof(isn_T) * arg_isn_count);
2247 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2248 + expr_isn_start + arg_isn_count,
2249 isn, sizeof(isn_T) * expr_isn_count);
2250 vim_free(isn);
2251
Bram Moolenaar078a4612022-01-04 15:17:03 +00002252 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2253 type = typep->type_curr;
2254 decl_type = typep->type_decl;
2255 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2256 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2257 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002258 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002259 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2260 typep->type_curr = type;
2261 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002262 }
2263
Bram Moolenaar078a4612022-01-04 15:17:03 +00002264 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002265 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2266 return FAIL;
2267 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002268
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002269 if (keeping_dict)
2270 {
2271 keeping_dict = FALSE;
2272 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2273 return FAIL;
2274 }
2275 }
2276 else if (**arg == '[')
2277 {
2278 int is_slice = FALSE;
2279
2280 // list index: list[123]
2281 // dict member: dict[key]
2282 // string index: text[123]
2283 // blob index: blob[123]
2284 if (generate_ppconst(cctx, ppconst) == FAIL)
2285 return FAIL;
2286 ppconst->pp_is_const = FALSE;
2287
2288 ++p;
2289 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2290 return FAIL;
2291 if (**arg == ':')
2292 {
2293 // missing first index is equal to zero
2294 generate_PUSHNR(cctx, 0);
2295 }
2296 else
2297 {
2298 if (compile_expr0(arg, cctx) == FAIL)
2299 return FAIL;
2300 if (**arg == ':')
2301 {
2302 semsg(_(e_white_space_required_before_and_after_str_at_str),
2303 ":", *arg);
2304 return FAIL;
2305 }
2306 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2307 return FAIL;
2308 *arg = skipwhite(*arg);
2309 }
2310 if (**arg == ':')
2311 {
2312 is_slice = TRUE;
2313 ++*arg;
2314 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2315 {
2316 semsg(_(e_white_space_required_before_and_after_str_at_str),
2317 ":", *arg);
2318 return FAIL;
2319 }
2320 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2321 return FAIL;
2322 if (**arg == ']')
2323 // missing second index is equal to end of string
2324 generate_PUSHNR(cctx, -1);
2325 else
2326 {
2327 if (compile_expr0(arg, cctx) == FAIL)
2328 return FAIL;
2329 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2330 return FAIL;
2331 *arg = skipwhite(*arg);
2332 }
2333 }
2334
2335 if (**arg != ']')
2336 {
2337 emsg(_(e_missing_closing_square_brace));
2338 return FAIL;
2339 }
2340 *arg = *arg + 1;
2341
2342 if (keeping_dict)
2343 {
2344 keeping_dict = FALSE;
2345 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2346 return FAIL;
2347 }
2348 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2349 return FAIL;
2350 }
2351 else if (*p == '.' && p[1] != '.')
2352 {
2353 // dictionary member: dict.name
2354 if (generate_ppconst(cctx, ppconst) == FAIL)
2355 return FAIL;
2356 ppconst->pp_is_const = FALSE;
2357
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002358 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2359 && (type->tt_type == VAR_CLASS
2360 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002361 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002362 // class member: SomeClass.varname
2363 // class method: SomeClass.SomeMethod()
2364 // class constructor: SomeClass.new()
2365 // object member: someObject.varname, this.varname
2366 // object method: someObject.SomeMethod(), this.SomeMethod()
2367 *arg = p;
2368 if (compile_class_object_index(cctx, arg, type) == FAIL)
2369 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002371 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002372 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002373 *arg = p + 1;
2374 if (IS_WHITE_OR_NUL(**arg))
2375 {
2376 emsg(_(e_missing_name_after_dot));
2377 return FAIL;
2378 }
2379 p = *arg;
2380 if (eval_isdictc(*p))
2381 while (eval_isnamec(*p))
2382 MB_PTR_ADV(p);
2383 if (p == *arg)
2384 {
2385 semsg(_(e_syntax_error_at_str), *arg);
2386 return FAIL;
2387 }
2388 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2389 return FAIL;
2390 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2391 return FAIL;
2392 keeping_dict = TRUE;
2393 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002395 }
2396 else
2397 break;
2398 }
2399
2400 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2401 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002402 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2403 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002404 return FAIL;
2405
2406 return OK;
2407}
2408
2409/*
2410 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2411 * "arg" is advanced until after the expression, skipping white space.
2412 *
2413 * If the value is a constant "ppconst->pp_used" will be non-zero.
2414 * Before instructions are generated, any values in "ppconst" will generated.
2415 *
2416 * This is the compiling equivalent of eval1(), eval2(), etc.
2417 */
2418
2419/*
2420 * number number constant
2421 * 0zFFFFFFFF Blob constant
2422 * "string" string constant
2423 * 'string' literal string constant
2424 * &option-name option value
2425 * @r register contents
2426 * identifier variable value
2427 * function() function call
2428 * $VAR environment variable
2429 * (expression) nested expression
2430 * [expr, expr] List
2431 * {key: val, [key]: val} Dictionary
2432 *
2433 * Also handle:
2434 * ! in front logical NOT
2435 * - in front unary minus
2436 * + in front unary plus (ignored)
2437 * trailing (arg) funcref/partial call
2438 * trailing [] subscript in String or List
2439 * trailing .name entry in Dictionary
2440 * trailing ->name() method call
2441 */
2442 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002443compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 char_u **arg,
2445 cctx_T *cctx,
2446 ppconst_T *ppconst)
2447{
2448 char_u *start_leader, *end_leader;
2449 int ret = OK;
2450 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2451 int used_before = ppconst->pp_used;
2452
2453 ppconst->pp_is_const = FALSE;
2454
2455 /*
2456 * Skip '!', '-' and '+' characters. They are handled later.
2457 */
2458 start_leader = *arg;
2459 if (eval_leader(arg, TRUE) == FAIL)
2460 return FAIL;
2461 end_leader = *arg;
2462
2463 rettv->v_type = VAR_UNKNOWN;
2464 switch (**arg)
2465 {
2466 /*
2467 * Number constant.
2468 */
2469 case '0': // also for blob starting with 0z
2470 case '1':
2471 case '2':
2472 case '3':
2473 case '4':
2474 case '5':
2475 case '6':
2476 case '7':
2477 case '8':
2478 case '9':
2479 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2480 return FAIL;
2481 // Apply "-" and "+" just before the number now, right to
2482 // left. Matters especially when "->" follows. Stops at
2483 // '!'.
2484 if (apply_leader(rettv, TRUE,
2485 start_leader, &end_leader) == FAIL)
2486 {
2487 clear_tv(rettv);
2488 return FAIL;
2489 }
2490 break;
2491
2492 /*
2493 * String constant: "string".
2494 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002495 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 return FAIL;
2497 break;
2498
2499 /*
2500 * Literal string constant: 'str''ing'.
2501 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002502 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002503 return FAIL;
2504 break;
2505
2506 /*
2507 * Constant Vim variable.
2508 */
2509 case 'v': get_vim_constant(arg, rettv);
2510 ret = NOTDONE;
2511 break;
2512
2513 /*
2514 * "true" constant
2515 */
2516 case 't': if (STRNCMP(*arg, "true", 4) == 0
2517 && !eval_isnamec((*arg)[4]))
2518 {
2519 *arg += 4;
2520 rettv->v_type = VAR_BOOL;
2521 rettv->vval.v_number = VVAL_TRUE;
2522 }
2523 else
2524 ret = NOTDONE;
2525 break;
2526
2527 /*
2528 * "false" constant
2529 */
2530 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2531 && !eval_isnamec((*arg)[5]))
2532 {
2533 *arg += 5;
2534 rettv->v_type = VAR_BOOL;
2535 rettv->vval.v_number = VVAL_FALSE;
2536 }
2537 else
2538 ret = NOTDONE;
2539 break;
2540
2541 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002542 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002544 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002546 char_u *p = *arg + 4;
2547 int len;
2548
2549 for (len = 0; eval_isnamec(p[len]); ++len)
2550 ;
2551 ret = handle_predefined(*arg, len + 4, rettv);
2552 if (ret == FAIL)
2553 ret = NOTDONE;
2554 else
2555 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556 }
2557 else
2558 ret = NOTDONE;
2559 break;
2560
2561 /*
2562 * List: [expr, expr]
2563 */
2564 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2565 return FAIL;
2566 ret = compile_list(arg, cctx, ppconst);
2567 break;
2568
2569 /*
2570 * Dictionary: {'key': val, 'key': val}
2571 */
2572 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2573 return FAIL;
2574 ret = compile_dict(arg, cctx, ppconst);
2575 break;
2576
2577 /*
2578 * Option value: &name
2579 */
2580 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2581 return FAIL;
2582 ret = compile_get_option(arg, cctx);
2583 break;
2584
2585 /*
2586 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002587 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002588 */
2589 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2590 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002591 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2592 ret = compile_interp_string(arg, cctx);
2593 else
2594 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002595 break;
2596
2597 /*
2598 * Register contents: @r.
2599 */
2600 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2601 return FAIL;
2602 ret = compile_get_register(arg, cctx);
2603 break;
2604 /*
2605 * nested expression: (expression).
2606 * lambda: (arg, arg) => expr
2607 * funcref: (arg, arg) => { statement }
2608 */
2609 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2610 ret = compile_lambda(arg, cctx);
2611 if (ret == NOTDONE)
2612 ret = compile_parenthesis(arg, cctx, ppconst);
2613 break;
2614
2615 default: ret = NOTDONE;
2616 break;
2617 }
2618 if (ret == FAIL)
2619 return FAIL;
2620
2621 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2622 {
2623 if (cctx->ctx_skip == SKIP_YES)
2624 clear_tv(rettv);
2625 else
2626 // A constant expression can possibly be handled compile time,
2627 // return the value instead of generating code.
2628 ++ppconst->pp_used;
2629 }
2630 else if (ret == NOTDONE)
2631 {
2632 char_u *p;
2633 int r;
2634
2635 if (!eval_isnamec1(**arg))
2636 {
2637 if (!vim9_bad_comment(*arg))
2638 {
2639 if (ends_excmd(*skipwhite(*arg)))
2640 semsg(_(e_empty_expression_str), *arg);
2641 else
2642 semsg(_(e_name_expected_str), *arg);
2643 }
2644 return FAIL;
2645 }
2646
2647 // "name" or "name()"
2648 p = to_name_end(*arg, TRUE);
2649 if (p - *arg == (size_t)1 && **arg == '_')
2650 {
2651 emsg(_(e_cannot_use_underscore_here));
2652 return FAIL;
2653 }
2654
2655 if (*p == '(')
2656 {
2657 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2658 }
2659 else
2660 {
2661 if (cctx->ctx_skip != SKIP_YES
2662 && generate_ppconst(cctx, ppconst) == FAIL)
2663 return FAIL;
2664 r = compile_load(arg, p, cctx, TRUE, TRUE);
2665 }
2666 if (r == FAIL)
2667 return FAIL;
2668 }
2669
2670 // Handle following "[]", ".member", etc.
2671 // Then deal with prefixed '-', '+' and '!', if not done already.
2672 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2673 ppconst) == FAIL)
2674 return FAIL;
2675 if (ppconst->pp_used > 0)
2676 {
2677 // apply the '!', '-' and '+' before the constant
2678 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2679 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2680 return FAIL;
2681 return OK;
2682 }
2683 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2684 return FAIL;
2685 return OK;
2686}
2687
2688/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002689 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002690 */
2691 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002692compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002693{
2694 type_T *want_type = NULL;
2695
2696 // Recognize <type>
2697 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2698 {
2699 ++*arg;
2700 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2701 if (want_type == NULL)
2702 return FAIL;
2703
2704 if (**arg != '>')
2705 {
2706 if (*skipwhite(*arg) == '>')
2707 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2708 else
2709 emsg(_(e_missing_gt));
2710 return FAIL;
2711 }
2712 ++*arg;
2713 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2714 return FAIL;
2715 }
2716
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002717 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002718 return FAIL;
2719
2720 if (want_type != NULL)
2721 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 type_T *actual;
2723 where_T where = WHERE_INIT;
2724
2725 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002726 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002727 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002728 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002729 if (need_type(actual, want_type, FALSE,
2730 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 return FAIL;
2732 }
2733 }
2734
2735 return OK;
2736}
2737
2738/*
2739 * * number multiplication
2740 * / number division
2741 * % number modulo
2742 */
2743 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002744compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002745{
2746 char_u *op;
2747 char_u *next;
2748 int ppconst_used = ppconst->pp_used;
2749
2750 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002751 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002752 return FAIL;
2753
2754 /*
2755 * Repeat computing, until no "*", "/" or "%" is following.
2756 */
2757 for (;;)
2758 {
2759 op = may_peek_next_line(cctx, *arg, &next);
2760 if (*op != '*' && *op != '/' && *op != '%')
2761 break;
2762 if (next != NULL)
2763 {
2764 *arg = next_line_from_context(cctx, TRUE);
2765 op = skipwhite(*arg);
2766 }
2767
2768 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2769 {
2770 error_white_both(op, 1);
2771 return FAIL;
2772 }
2773 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2774 return FAIL;
2775
2776 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002777 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002778 return FAIL;
2779
2780 if (ppconst->pp_used == ppconst_used + 2
2781 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2782 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2783 {
2784 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2785 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2786 varnumber_T res = 0;
2787 int failed = FALSE;
2788
2789 // both are numbers: compute the result
2790 switch (*op)
2791 {
2792 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2793 break;
2794 case '/': res = num_divide(tv1->vval.v_number,
2795 tv2->vval.v_number, &failed);
2796 break;
2797 case '%': res = num_modulus(tv1->vval.v_number,
2798 tv2->vval.v_number, &failed);
2799 break;
2800 }
2801 if (failed)
2802 return FAIL;
2803 tv1->vval.v_number = res;
2804 --ppconst->pp_used;
2805 }
2806 else
2807 {
2808 generate_ppconst(cctx, ppconst);
2809 generate_two_op(cctx, op);
2810 }
2811 }
2812
2813 return OK;
2814}
2815
2816/*
2817 * + number addition or list/blobl concatenation
2818 * - number subtraction
2819 * .. string concatenation
2820 */
2821 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002822compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002823{
2824 char_u *op;
2825 char_u *next;
2826 int oplen;
2827 int ppconst_used = ppconst->pp_used;
2828
2829 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002830 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002831 return FAIL;
2832
2833 /*
2834 * Repeat computing, until no "+", "-" or ".." is following.
2835 */
2836 for (;;)
2837 {
2838 op = may_peek_next_line(cctx, *arg, &next);
2839 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2840 break;
2841 if (op[0] == op[1] && *op != '.' && next)
2842 // Finding "++" or "--" on the next line is a separate command.
2843 // But ".." is concatenation.
2844 break;
2845 oplen = (*op == '.' ? 2 : 1);
2846 if (next != NULL)
2847 {
2848 *arg = next_line_from_context(cctx, TRUE);
2849 op = skipwhite(*arg);
2850 }
2851
2852 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2853 {
2854 error_white_both(op, oplen);
2855 return FAIL;
2856 }
2857
2858 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2859 return FAIL;
2860
2861 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002862 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002863 return FAIL;
2864
2865 if (ppconst->pp_used == ppconst_used + 2
2866 && (*op == '.'
2867 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2868 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2869 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2870 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2871 {
2872 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2873 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2874
2875 // concat/subtract/add constant numbers
2876 if (*op == '+')
2877 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2878 else if (*op == '-')
2879 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2880 else
2881 {
2882 // concatenate constant strings
2883 char_u *s1 = tv1->vval.v_string;
2884 char_u *s2 = tv2->vval.v_string;
2885 size_t len1 = STRLEN(s1);
2886
2887 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2888 if (tv1->vval.v_string == NULL)
2889 {
2890 clear_ppconst(ppconst);
2891 return FAIL;
2892 }
2893 mch_memmove(tv1->vval.v_string, s1, len1);
2894 STRCPY(tv1->vval.v_string + len1, s2);
2895 vim_free(s1);
2896 vim_free(s2);
2897 }
2898 --ppconst->pp_used;
2899 }
2900 else
2901 {
2902 generate_ppconst(cctx, ppconst);
2903 ppconst->pp_is_const = FALSE;
2904 if (*op == '.')
2905 {
2906 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2907 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2908 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002909 if (generate_CONCAT(cctx, 2) == FAIL)
2910 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002911 }
2912 else
2913 generate_two_op(cctx, op);
2914 }
2915 }
2916
2917 return OK;
2918}
2919
2920/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002921 * expr6a >> expr6b
2922 * expr6a << expr6b
2923 *
2924 * Produces instructions:
2925 * OPNR bitwise left or right shift
2926 */
2927 static int
2928compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2929{
2930 exprtype_T type = EXPR_UNKNOWN;
2931 char_u *p;
2932 char_u *next;
2933 int len = 2;
2934 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002935 isn_T *isn;
2936
2937 // get the first variable
2938 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2939 return FAIL;
2940
2941 /*
2942 * Repeat computing, until no "+", "-" or ".." is following.
2943 */
2944 for (;;)
2945 {
2946 type = EXPR_UNKNOWN;
2947
2948 p = may_peek_next_line(cctx, *arg, &next);
2949 if (p[0] == '<' && p[1] == '<')
2950 type = EXPR_LSHIFT;
2951 else if (p[0] == '>' && p[1] == '>')
2952 type = EXPR_RSHIFT;
2953
2954 if (type == EXPR_UNKNOWN)
2955 return OK;
2956
2957 // Handle a bitwise left or right shift operator
2958 if (ppconst->pp_used == ppconst_used + 1)
2959 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002960 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002961 {
2962 // left operand should be a number
2963 emsg(_(e_bitshift_ops_must_be_number));
2964 return FAIL;
2965 }
2966 }
2967 else
2968 {
2969 type_T *t = get_type_on_stack(cctx, 0);
2970
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002971 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002972 {
2973 emsg(_(e_bitshift_ops_must_be_number));
2974 return FAIL;
2975 }
2976 }
2977
2978 if (next != NULL)
2979 {
2980 *arg = next_line_from_context(cctx, TRUE);
2981 p = skipwhite(*arg);
2982 }
2983
2984 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2985 {
2986 error_white_both(p, len);
2987 return FAIL;
2988 }
2989
2990 // get the second variable
2991 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2992 return FAIL;
2993
2994 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2995 return FAIL;
2996
2997 if (ppconst->pp_used == ppconst_used + 2)
2998 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002999 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3000 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3001
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003002 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003003 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
3004 {
3005 // right operand should be a positive number
3006 if (tv2->v_type != VAR_NUMBER)
3007 emsg(_(e_bitshift_ops_must_be_number));
3008 else
dundargocc57b5bc2022-11-02 13:30:51 +00003009 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003010 return FAIL;
3011 }
3012
3013 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3014 tv1->vval.v_number = 0;
3015 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003016 tv1->vval.v_number =
3017 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003018 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003019 tv1->vval.v_number =
3020 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003021 clear_tv(tv2);
3022 --ppconst->pp_used;
3023 }
3024 else
3025 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003026 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3027 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003028 {
3029 emsg(_(e_bitshift_ops_must_be_number));
3030 return FAIL;
3031 }
3032
3033 generate_ppconst(cctx, ppconst);
3034
3035 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3036 if (isn == NULL)
3037 return FAIL;
3038
3039 if (isn != NULL)
3040 isn->isn_arg.op.op_type = type;
3041 }
3042 }
3043
3044 return OK;
3045}
3046
3047/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003048 * expr5a == expr5b
3049 * expr5a =~ expr5b
3050 * expr5a != expr5b
3051 * expr5a !~ expr5b
3052 * expr5a > expr5b
3053 * expr5a >= expr5b
3054 * expr5a < expr5b
3055 * expr5a <= expr5b
3056 * expr5a is expr5b
3057 * expr5a isnot expr5b
3058 *
3059 * Produces instructions:
3060 * EVAL expr5a Push result of "expr5a"
3061 * EVAL expr5b Push result of "expr5b"
3062 * COMPARE one of the compare instructions
3063 */
3064 static int
3065compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3066{
3067 exprtype_T type = EXPR_UNKNOWN;
3068 char_u *p;
3069 char_u *next;
3070 int len = 2;
3071 int type_is = FALSE;
3072 int ppconst_used = ppconst->pp_used;
3073
3074 // get the first variable
3075 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3076 return FAIL;
3077
3078 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003079
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003080 type = get_compare_type(p, &len, &type_is);
3081
3082 /*
3083 * If there is a comparative operator, use it.
3084 */
3085 if (type != EXPR_UNKNOWN)
3086 {
3087 int ic = FALSE; // Default: do not ignore case
3088
3089 if (next != NULL)
3090 {
3091 *arg = next_line_from_context(cctx, TRUE);
3092 p = skipwhite(*arg);
3093 }
3094 if (type_is && (p[len] == '?' || p[len] == '#'))
3095 {
3096 semsg(_(e_invalid_expression_str), *arg);
3097 return FAIL;
3098 }
3099 // extra question mark appended: ignore case
3100 if (p[len] == '?')
3101 {
3102 ic = TRUE;
3103 ++len;
3104 }
3105 // extra '#' appended: match case (ignored)
3106 else if (p[len] == '#')
3107 ++len;
3108 // nothing appended: match case
3109
3110 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3111 {
3112 error_white_both(p, len);
3113 return FAIL;
3114 }
3115
3116 // get the second variable
3117 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3118 return FAIL;
3119
3120 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3121 return FAIL;
3122
3123 if (ppconst->pp_used == ppconst_used + 2)
3124 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003125 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003126 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3127 int ret;
3128
3129 // Both sides are a constant, compute the result now.
3130 // First check for a valid combination of types, this is more
3131 // strict than typval_compare().
3132 if (check_compare_types(type, tv1, tv2) == FAIL)
3133 ret = FAIL;
3134 else
3135 {
3136 ret = typval_compare(tv1, tv2, type, ic);
3137 tv1->v_type = VAR_BOOL;
3138 tv1->vval.v_number = tv1->vval.v_number
3139 ? VVAL_TRUE : VVAL_FALSE;
3140 clear_tv(tv2);
3141 --ppconst->pp_used;
3142 }
3143 return ret;
3144 }
3145
3146 generate_ppconst(cctx, ppconst);
3147 return generate_COMPARE(cctx, type, ic);
3148 }
3149
3150 return OK;
3151}
3152
3153static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3154
3155/*
3156 * Compile || or &&.
3157 */
3158 static int
3159compile_and_or(
3160 char_u **arg,
3161 cctx_T *cctx,
3162 char *op,
3163 ppconst_T *ppconst,
3164 int ppconst_used UNUSED)
3165{
3166 char_u *next;
3167 char_u *p = may_peek_next_line(cctx, *arg, &next);
3168 int opchar = *op;
3169
3170 if (p[0] == opchar && p[1] == opchar)
3171 {
3172 garray_T *instr = &cctx->ctx_instr;
3173 garray_T end_ga;
3174 int save_skip = cctx->ctx_skip;
3175
3176 /*
3177 * Repeat until there is no following "||" or "&&"
3178 */
3179 ga_init2(&end_ga, sizeof(int), 10);
3180 while (p[0] == opchar && p[1] == opchar)
3181 {
3182 long start_lnum = SOURCING_LNUM;
3183 long save_sourcing_lnum;
3184 int start_ctx_lnum = cctx->ctx_lnum;
3185 int save_lnum;
3186 int const_used;
3187 int status;
3188 jumpwhen_T jump_when = opchar == '|'
3189 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3190
3191 if (next != NULL)
3192 {
3193 *arg = next_line_from_context(cctx, TRUE);
3194 p = skipwhite(*arg);
3195 }
3196
3197 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3198 {
3199 semsg(_(e_white_space_required_before_and_after_str_at_str),
3200 op, p);
3201 ga_clear(&end_ga);
3202 return FAIL;
3203 }
3204
3205 save_sourcing_lnum = SOURCING_LNUM;
3206 SOURCING_LNUM = start_lnum;
3207 save_lnum = cctx->ctx_lnum;
3208 cctx->ctx_lnum = start_ctx_lnum;
3209
3210 status = check_ppconst_bool(ppconst);
3211 if (status != FAIL)
3212 {
3213 // Use the last ppconst if possible.
3214 if (ppconst->pp_used > 0)
3215 {
3216 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3217 int is_true = tv2bool(tv);
3218
3219 if ((is_true && opchar == '|')
3220 || (!is_true && opchar == '&'))
3221 {
3222 // For "false && expr" and "true || expr" the "expr"
3223 // does not need to be evaluated.
3224 cctx->ctx_skip = SKIP_YES;
3225 clear_tv(tv);
3226 tv->v_type = VAR_BOOL;
3227 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3228 }
3229 else
3230 {
3231 // For "true && expr" and "false || expr" only "expr"
3232 // needs to be evaluated.
3233 --ppconst->pp_used;
3234 jump_when = JUMP_NEVER;
3235 }
3236 }
3237 else
3238 {
3239 // Every part must evaluate to a bool.
3240 status = bool_on_stack(cctx);
3241 }
3242 }
3243 if (status != FAIL)
3244 status = ga_grow(&end_ga, 1);
3245 cctx->ctx_lnum = save_lnum;
3246 if (status == FAIL)
3247 {
3248 ga_clear(&end_ga);
3249 return FAIL;
3250 }
3251
3252 if (jump_when != JUMP_NEVER)
3253 {
3254 if (cctx->ctx_skip != SKIP_YES)
3255 {
3256 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3257 ++end_ga.ga_len;
3258 }
3259 generate_JUMP(cctx, jump_when, 0);
3260 }
3261
3262 // eval the next expression
3263 SOURCING_LNUM = save_sourcing_lnum;
3264 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3265 {
3266 ga_clear(&end_ga);
3267 return FAIL;
3268 }
3269
3270 const_used = ppconst->pp_used;
3271 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3272 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3273 {
3274 ga_clear(&end_ga);
3275 return FAIL;
3276 }
3277
3278 // "0 || 1" results in true, "1 && 0" results in false.
3279 if (ppconst->pp_used == const_used + 1)
3280 {
3281 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3282
3283 if (tv->v_type == VAR_NUMBER
3284 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3285 {
3286 tv->vval.v_number = tv->vval.v_number == 1
3287 ? VVAL_TRUE : VVAL_FALSE;
3288 tv->v_type = VAR_BOOL;
3289 }
3290 }
3291
3292 p = may_peek_next_line(cctx, *arg, &next);
3293 }
3294
3295 if (check_ppconst_bool(ppconst) == FAIL)
3296 {
3297 ga_clear(&end_ga);
3298 return FAIL;
3299 }
3300
3301 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3302 // Every part must evaluate to a bool.
3303 if (bool_on_stack(cctx) == FAIL)
3304 {
3305 ga_clear(&end_ga);
3306 return FAIL;
3307 }
3308
3309 if (end_ga.ga_len > 0)
3310 {
3311 // Fill in the end label in all jumps.
3312 generate_ppconst(cctx, ppconst);
3313 while (end_ga.ga_len > 0)
3314 {
3315 isn_T *isn;
3316
3317 --end_ga.ga_len;
3318 isn = ((isn_T *)instr->ga_data)
3319 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3320 isn->isn_arg.jump.jump_where = instr->ga_len;
3321 }
3322 }
3323 ga_clear(&end_ga);
3324
3325 cctx->ctx_skip = save_skip;
3326 }
3327
3328 return OK;
3329}
3330
3331/*
3332 * expr4a && expr4a && expr4a logical AND
3333 *
3334 * Produces instructions:
3335 * EVAL expr4a Push result of "expr4a"
3336 * COND2BOOL convert to bool if needed
3337 * JUMP_IF_COND_FALSE end
3338 * EVAL expr4b Push result of "expr4b"
3339 * JUMP_IF_COND_FALSE end
3340 * EVAL expr4c Push result of "expr4c"
3341 * end:
3342 */
3343 static int
3344compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3345{
3346 int ppconst_used = ppconst->pp_used;
3347
3348 // get the first variable
3349 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3350 return FAIL;
3351
3352 // || and && work almost the same
3353 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3354}
3355
3356/*
3357 * expr3a || expr3b || expr3c logical OR
3358 *
3359 * Produces instructions:
3360 * EVAL expr3a Push result of "expr3a"
3361 * COND2BOOL convert to bool if needed
3362 * JUMP_IF_COND_TRUE end
3363 * EVAL expr3b Push result of "expr3b"
3364 * JUMP_IF_COND_TRUE end
3365 * EVAL expr3c Push result of "expr3c"
3366 * end:
3367 */
3368 static int
3369compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3370{
3371 int ppconst_used = ppconst->pp_used;
3372
3373 // eval the first expression
3374 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3375 return FAIL;
3376
3377 // || and && work almost the same
3378 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3379}
3380
3381/*
3382 * Toplevel expression: expr2 ? expr1a : expr1b
3383 * Produces instructions:
3384 * EVAL expr2 Push result of "expr2"
3385 * JUMP_IF_FALSE alt jump if false
3386 * EVAL expr1a
3387 * JUMP_ALWAYS end
3388 * alt: EVAL expr1b
3389 * end:
3390 *
3391 * Toplevel expression: expr2 ?? expr1
3392 * Produces instructions:
3393 * EVAL expr2 Push result of "expr2"
3394 * JUMP_AND_KEEP_IF_TRUE end jump if true
3395 * EVAL expr1
3396 * end:
3397 */
3398 int
3399compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3400{
3401 char_u *p;
3402 int ppconst_used = ppconst->pp_used;
3403 char_u *next;
3404
3405 // Ignore all kinds of errors when not producing code.
3406 if (cctx->ctx_skip == SKIP_YES)
3407 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003408 int prev_did_emsg = did_emsg;
3409
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003410 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003411 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003412 }
3413
3414 // Evaluate the first expression.
3415 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3416 return FAIL;
3417
3418 p = may_peek_next_line(cctx, *arg, &next);
3419 if (*p == '?')
3420 {
3421 int op_falsy = p[1] == '?';
3422 garray_T *instr = &cctx->ctx_instr;
3423 garray_T *stack = &cctx->ctx_type_stack;
3424 int alt_idx = instr->ga_len;
3425 int end_idx = 0;
3426 isn_T *isn;
3427 type_T *type1 = NULL;
3428 int has_const_expr = FALSE;
3429 int const_value = FALSE;
3430 int save_skip = cctx->ctx_skip;
3431
3432 if (next != NULL)
3433 {
3434 *arg = next_line_from_context(cctx, TRUE);
3435 p = skipwhite(*arg);
3436 }
3437
3438 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3439 {
3440 semsg(_(e_white_space_required_before_and_after_str_at_str),
3441 op_falsy ? "??" : "?", p);
3442 return FAIL;
3443 }
3444
3445 if (ppconst->pp_used == ppconst_used + 1)
3446 {
3447 // the condition is a constant, we know whether the ? or the :
3448 // expression is to be evaluated.
3449 has_const_expr = TRUE;
3450 if (op_falsy)
3451 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3452 else
3453 {
3454 int error = FALSE;
3455
3456 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3457 &error);
3458 if (error)
3459 return FAIL;
3460 }
3461 cctx->ctx_skip = save_skip == SKIP_YES ||
3462 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3463
3464 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3465 // "left ?? right" and "left" is truthy: produce "left"
3466 generate_ppconst(cctx, ppconst);
3467 else
3468 {
3469 clear_tv(&ppconst->pp_tv[ppconst_used]);
3470 --ppconst->pp_used;
3471 }
3472 }
3473 else
3474 {
3475 generate_ppconst(cctx, ppconst);
3476 if (op_falsy)
3477 end_idx = instr->ga_len;
3478 generate_JUMP(cctx, op_falsy
3479 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3480 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003481 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003482 }
3483
3484 // evaluate the second expression; any type is accepted
3485 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3486 return FAIL;
3487 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3488 return FAIL;
3489
3490 if (!has_const_expr)
3491 {
3492 generate_ppconst(cctx, ppconst);
3493
3494 if (!op_falsy)
3495 {
3496 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003497 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003498 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003499
3500 end_idx = instr->ga_len;
3501 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3502
3503 // jump here from JUMP_IF_FALSE
3504 isn = ((isn_T *)instr->ga_data) + alt_idx;
3505 isn->isn_arg.jump.jump_where = instr->ga_len;
3506 }
3507 }
3508
3509 if (!op_falsy)
3510 {
3511 // Check for the ":".
3512 p = may_peek_next_line(cctx, *arg, &next);
3513 if (*p != ':')
3514 {
3515 emsg(_(e_missing_colon_after_questionmark));
3516 return FAIL;
3517 }
3518 if (next != NULL)
3519 {
3520 *arg = next_line_from_context(cctx, TRUE);
3521 p = skipwhite(*arg);
3522 }
3523
3524 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3525 {
3526 semsg(_(e_white_space_required_before_and_after_str_at_str),
3527 ":", p);
3528 return FAIL;
3529 }
3530
3531 // evaluate the third expression
3532 if (has_const_expr)
3533 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3534 ? SKIP_YES : SKIP_NOT;
3535 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3536 return FAIL;
3537 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3538 return FAIL;
3539 }
3540
3541 if (!has_const_expr)
3542 {
3543 type_T **typep;
3544
3545 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003546 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003547
3548 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003549 typep = &((((type2_T *)stack->ga_data)
3550 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003551 common_type(type1, *typep, typep, cctx->ctx_type_list);
3552
3553 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3554 isn = ((isn_T *)instr->ga_data) + end_idx;
3555 isn->isn_arg.jump.jump_where = instr->ga_len;
3556 }
3557
3558 cctx->ctx_skip = save_skip;
3559 }
3560 return OK;
3561}
3562
3563/*
3564 * Toplevel expression.
3565 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3566 * Returns OK or FAIL.
3567 */
3568 int
3569compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3570{
3571 ppconst_T ppconst;
3572
3573 CLEAR_FIELD(ppconst);
3574 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3575 {
3576 clear_ppconst(&ppconst);
3577 return FAIL;
3578 }
3579 if (is_const != NULL)
3580 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3581 if (generate_ppconst(cctx, &ppconst) == FAIL)
3582 return FAIL;
3583 return OK;
3584}
3585
3586/*
3587 * Toplevel expression.
3588 */
3589 int
3590compile_expr0(char_u **arg, cctx_T *cctx)
3591{
3592 return compile_expr0_ext(arg, cctx, NULL);
3593}
3594
3595
3596#endif // defined(FEAT_EVAL)