blob: e8949693841da2984adcbff8130cbd54871e9449 [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 {
370 // TODO: different error for object method?
371 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
372 return FAIL;
373 }
374
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200375 if (*ufunc->uf_name == '_' && !inside_class_hierarchy(cctx, cl))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200376 {
377 semsg(_(e_cannot_access_private_method_str), name);
378 return FAIL;
379 }
380
Bram Moolenaar574950d2023-01-03 19:08:50 +0000381 // Compile the arguments and call the class function or object method.
382 // The object method will know that the object is on the stack, just
383 // before the arguments.
384 *arg = skipwhite(name_end + 1);
385 int argcount = 0;
386 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
387 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000388
389 if (type->tt_type == VAR_OBJECT
390 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-east2261c892023-08-16 21:49:54 +0900391 return generate_CALL(cctx, ufunc, cl, fi, type, argcount);
392 return generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000393 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000394
395 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000396 {
397 for (int i = 0; i < cl->class_obj_member_count; ++i)
398 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000399 ocmember_T *m = &cl->class_obj_members[i];
400 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000401 {
Bram Moolenaar62a69232023-01-24 15:07:04 +0000402 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000403 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000404 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000405 return FAIL;
406 }
407
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000408 *arg = name_end;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000409 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000410 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000411 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000412 }
413 }
414
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000415 // Could be a function reference: "obj.Func".
416 for (int i = 0; i < cl->class_obj_method_count; ++i)
417 {
418 ufunc_T *fp = cl->class_obj_methods[i];
419 // Use a separate pointer to avoid that ASAN complains about
420 // uf_name[] only being 4 characters.
421 char_u *ufname = (char_u *)fp->uf_name;
422 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar313e4722023-02-08 20:55:27 +0000423 {
424 if (type->tt_type == VAR_OBJECT
425 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
426 return generate_FUNCREF(cctx, fp, cl, i, NULL);
427 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
428 }
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000429 }
430
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000431 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
432 }
433 else
434 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000435 // load class member
436 int idx;
437 for (idx = 0; idx < cl->class_class_member_count; ++idx)
438 {
439 ocmember_T *m = &cl->class_class_members[idx];
440 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200441 {
442 if (*name == '_' && !inside_class(cctx, cl))
443 {
444 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
445 return FAIL;
446 }
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000447 break;
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200448 }
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000449 }
450 if (idx < cl->class_class_member_count)
451 {
452 *arg = name_end;
453 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
454 }
455 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000456 }
457
458 return FAIL;
459}
460
461/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462 * Generate an instruction to load script-local variable "name", without the
463 * leading "s:".
464 * Also finds imported variables.
465 */
466 int
467compile_load_scriptvar(
468 cctx_T *cctx,
469 char_u *name, // variable NUL terminated
470 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100471 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000472{
473 scriptitem_T *si;
474 int idx;
475 imported_T *import;
476
477 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
478 return FAIL;
479 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000480 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000481 if (idx >= 0)
482 {
483 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
484
485 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
486 current_sctx.sc_sid, idx, sv->sv_type);
487 return OK;
488 }
489
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000490 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 if (import != NULL)
492 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000493 char_u *p = skipwhite(*end);
494 char_u *exp_name;
495 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100496 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000497 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000498 int done = FALSE;
499 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000500
501 // Need to lookup the member.
502 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000504 semsg(_(e_expected_dot_after_name_str), start);
505 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000506 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000507 ++p;
508 if (VIM_ISWHITE(*p))
509 {
510 emsg(_(e_no_white_space_allowed_after_dot));
511 return FAIL;
512 }
513
514 // isolate one name
515 exp_name = p;
516 while (eval_isnamec(*p))
517 ++p;
518 cc = *p;
519 *p = NUL;
520
Bram Moolenaard041f422022-01-12 19:54:00 +0000521 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100522 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
523 // "import autoload './dir/script.vim'" or
524 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100525 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100526
527 if (res == OK)
528 {
529 if (si->sn_autoload_prefix != NULL
530 && si->sn_state == SN_STATE_NOT_LOADED)
531 {
532 char_u *auto_name =
533 concat_str(si->sn_autoload_prefix, exp_name);
534
535 // autoload script must be loaded later, access by the autoload
536 // name. If a '(' follows it must be a function. Otherwise we
537 // don't know, it can be "script.Func".
538 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100539 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100540 else
541 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
542 vim_free(auto_name);
543 done = TRUE;
544 }
545 else if (si->sn_import_autoload
546 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100547 {
548 // If a '(' follows it must be a function. Otherwise we don't
549 // know, it can be "script.Func".
550 if (cc == '(' || paren_follows_after_expr)
551 {
552 char_u sid_name[MAX_FUNC_NAME_LEN];
553
554 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100555 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100556 }
557 else
558 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
559 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100560 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100561 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100562 else
563 {
564 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
565 cctx, NULL, TRUE);
566 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100567 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100568
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000569 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000570 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000571 if (done)
572 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000573
574 if (idx < 0)
575 {
576 if (ufunc != NULL)
577 {
578 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100579 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000580 return OK;
581 }
582 return FAIL;
583 }
584
585 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
586 import->imp_sid,
587 idx,
588 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589 return OK;
590 }
591
Bram Moolenaard0132f42022-05-12 11:05:40 +0100592 // Can only get here if we know "name" is a script variable and not in a
593 // Vim9 script (variable is not in sn_var_vals): old style script.
594 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000595 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000596}
597
598 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000599generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000601 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000602 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000603
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000604 // Reject a global non-autoload function found without the "g:" prefix.
605 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000606 return FAIL;
607
608 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000609 compile_type = get_compile_type(ufunc);
610 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000611 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100613 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614}
615
616/*
617 * Compile a variable name into a load instruction.
618 * "end" points to just after the name.
619 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
620 * When "error" is FALSE do not give an error when not found.
621 */
622 int
623compile_load(
624 char_u **arg,
625 char_u *end_arg,
626 cctx_T *cctx,
627 int is_expr,
628 int error)
629{
630 type_T *type;
631 char_u *name = NULL;
632 char_u *end = end_arg;
633 int res = FAIL;
634 int prev_called_emsg = called_emsg;
635
636 if (*(*arg + 1) == ':')
637 {
638 if (end <= *arg + 2)
639 {
640 isntype_T isn_type;
641
642 // load dictionary of namespace
643 switch (**arg)
644 {
645 case 'g': isn_type = ISN_LOADGDICT; break;
646 case 'w': isn_type = ISN_LOADWDICT; break;
647 case 't': isn_type = ISN_LOADTDICT; break;
648 case 'b': isn_type = ISN_LOADBDICT; break;
649 default:
650 semsg(_(e_namespace_not_supported_str), *arg);
651 goto theend;
652 }
653 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
654 goto theend;
655 res = OK;
656 }
657 else
658 {
659 isntype_T isn_type = ISN_DROP;
660
661 // load namespaced variable
662 name = vim_strnsave(*arg + 2, end - (*arg + 2));
663 if (name == NULL)
664 return FAIL;
665
666 switch (**arg)
667 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100668 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000670 case 's': if (current_script_is_vim9())
671 {
672 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
673 *arg);
674 vim_free(name);
675 return FAIL;
676 }
Kota Kato948a3892022-08-16 16:09:59 +0100677 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000678 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000679 else
680 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100681 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000682 break;
683 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
684 {
685 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000686 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000687 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000688 else
689 isn_type = ISN_LOADG;
690 }
691 else
692 {
693 isn_type = ISN_LOADAUTO;
694 vim_free(name);
695 name = vim_strnsave(*arg, end - *arg);
696 if (name == NULL)
697 return FAIL;
698 }
699 break;
700 case 'w': isn_type = ISN_LOADW; break;
701 case 't': isn_type = ISN_LOADT; break;
702 case 'b': isn_type = ISN_LOADB; break;
703 default: // cannot happen, just in case
704 semsg(_(e_namespace_not_supported_str), *arg);
705 goto theend;
706 }
707 if (isn_type != ISN_DROP)
708 {
709 // Global, Buffer-local, Window-local and Tabpage-local
710 // variables can be defined later, thus we don't check if it
711 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000712 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000713 }
714 }
715 }
716 else
717 {
718 size_t len = end - *arg;
719 int idx;
720 int gen_load = FALSE;
721 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100722 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100723 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000724
725 name = vim_strnsave(*arg, end - *arg);
726 if (name == NULL)
727 return FAIL;
728
Bram Moolenaar58b40092023-01-11 15:59:05 +0000729 if (STRCMP(name, "super") == 0
730 && cctx->ctx_ufunc != NULL
731 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
732 {
733 // super.SomeFunc() in a class function: push &t_super type, this
734 // is recognized in compile_subscript().
735 res = push_type_stack(cctx, &t_super);
736 if (*end != '.')
737 emsg(_(e_super_must_be_followed_by_dot));
738 }
739 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000740 {
741 script_autoload(name, FALSE);
742 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
743 }
744 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
745 == OK)
746 {
747 if (gen_load_outer == 0)
748 gen_load = TRUE;
749 }
750 else
751 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000752 lvar_T lvar;
753 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000754
755 if (lookup_local(*arg, len, &lvar, cctx) == OK)
756 {
757 type = lvar.lv_type;
758 idx = lvar.lv_idx;
759 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100760 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100762 outer_loop_depth = lvar.lv_loop_depth;
763 outer_loop_idx = lvar.lv_loop_idx;
764 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765 else
766 gen_load = TRUE;
767 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000768 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000769 {
770 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
771 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 else
773 {
774 // "var" can be script-local even without using "s:" if it
775 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000776 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000777 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100778 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779
780 // When evaluating an expression and the name starts with an
781 // uppercase letter it can be a user defined function.
782 // generate_funcref() will fail if the function can't be found.
783 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000784 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785 }
786 }
787 if (gen_load)
788 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
789 if (gen_load_outer > 0)
790 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100791 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
792 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 cctx->ctx_outer_used = TRUE;
794 }
795 }
796
797 *arg = end;
798
799theend:
800 if (res == FAIL && error && called_emsg == prev_called_emsg)
801 semsg(_(e_variable_not_found_str), name);
802 vim_free(name);
803 return res;
804}
805
806/*
807 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100808 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809 * Returns FAIL if compilation fails.
810 */
811 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100812compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000813{
LemonBoyf3b48952022-05-05 13:53:03 +0100814 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000815 garray_T save_ga = cctx->ctx_instr;
816 int expr_res;
817 int trailing_error;
818 int instr_count;
819 isn_T *instr = NULL;
820
821 // Remove the string type from the stack.
822 --cctx->ctx_type_stack.ga_len;
823
824 // Temporarily reset the list of instructions so that the jump labels are
825 // correct.
826 cctx->ctx_instr.ga_len = 0;
827 cctx->ctx_instr.ga_maxlen = 0;
828 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000829
830 // avoid peeking a next line
831 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
832 cctx->ctx_ufunc->uf_lines.ga_len = 0;
833
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000834 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000835
836 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
837
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000838 s = skipwhite(s);
839 trailing_error = *s != NUL;
840
841 if (expr_res == FAIL || trailing_error
842 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
843 {
844 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000845 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 clear_instr_ga(&cctx->ctx_instr);
847 cctx->ctx_instr = save_ga;
848 ++cctx->ctx_type_stack.ga_len;
849 return FAIL;
850 }
851
852 // Move the generated instructions into the ISN_INSTR instruction, then
853 // restore the list of instructions.
854 instr_count = cctx->ctx_instr.ga_len;
855 instr = cctx->ctx_instr.ga_data;
856 instr[instr_count].isn_type = ISN_FINISH;
857
858 cctx->ctx_instr = save_ga;
859 vim_free(isn->isn_arg.string);
860 isn->isn_type = ISN_INSTR;
861 isn->isn_arg.instr = instr;
862 return OK;
863}
864
865/*
866 * Compile the argument expressions.
867 * "arg" points to just after the "(" and is advanced to after the ")"
868 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100869 int
LemonBoyf3b48952022-05-05 13:53:03 +0100870compile_arguments(
871 char_u **arg,
872 cctx_T *cctx,
873 int *argcount,
874 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875{
876 char_u *p = *arg;
877 char_u *whitep = *arg;
878 int must_end = FALSE;
879 int instr_count;
880
881 for (;;)
882 {
883 if (may_get_next_line(whitep, &p, cctx) == FAIL)
884 goto failret;
885 if (*p == ')')
886 {
887 *arg = p + 1;
888 return OK;
889 }
890 if (must_end)
891 {
892 semsg(_(e_missing_comma_before_argument_str), p);
893 return FAIL;
894 }
895
896 instr_count = cctx->ctx_instr.ga_len;
897 if (compile_expr0(&p, cctx) == FAIL)
898 return FAIL;
899 ++*argcount;
900
LemonBoyf3b48952022-05-05 13:53:03 +0100901 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000902 && cctx->ctx_instr.ga_len == instr_count + 1)
903 {
904 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
905
906 // {skip} argument of searchpair() can be compiled if not empty
907 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100908 compile_string(isn, cctx, 0);
909 }
910 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
911 && cctx->ctx_instr.ga_len == instr_count + 1)
912 {
913 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
914
915 // {sub} argument of substitute() can be compiled if it starts
916 // with \=
917 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100918 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100919 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920 }
921
922 if (*p != ',' && *skipwhite(p) == ',')
923 {
924 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
925 p = skipwhite(p);
926 }
927 if (*p == ',')
928 {
929 ++p;
930 if (*p != NUL && !VIM_ISWHITE(*p))
931 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
932 }
933 else
934 must_end = TRUE;
935 whitep = p;
936 p = skipwhite(p);
937 }
938failret:
939 emsg(_(e_missing_closing_paren));
940 return FAIL;
941}
942
943/*
944 * Compile a function call: name(arg1, arg2)
945 * "arg" points to "name", "arg + varlen" to the "(".
946 * "argcount_init" is 1 for "value->method()"
947 * Instructions:
948 * EVAL arg1
949 * EVAL arg2
950 * BCALL / DCALL / UCALL
951 */
952 static int
953compile_call(
954 char_u **arg,
955 size_t varlen,
956 cctx_T *cctx,
957 ppconst_T *ppconst,
958 int argcount_init)
959{
960 char_u *name = *arg;
961 char_u *p;
962 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100963 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000964 char_u fname_buf[FLEN_FIXED + 1];
965 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966 ufunc_T *ufunc = NULL;
967 int res = FAIL;
968 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000969 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100970 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000971 imported_T *import;
h-east2261c892023-08-16 21:49:54 +0900972 type_T *type;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000973
974 if (varlen >= sizeof(namebuf))
975 {
976 semsg(_(e_name_too_long_str), name);
977 return FAIL;
978 }
979 vim_strncpy(namebuf, *arg, varlen);
980
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000981 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000982 if (import != NULL)
983 {
984 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
985 return FAIL;
986 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987
988 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100989 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100991 if ((varlen == 3
992 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
994 {
995 char_u *s = skipwhite(*arg + varlen + 1);
996 typval_T argvars[2];
997 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100998 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000999
1000 argvars[0].v_type = VAR_UNKNOWN;
1001 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001002 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001003 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001004 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001005 s = skipwhite(s);
1006 if (*s == ')' && argvars[0].v_type == VAR_STRING
1007 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1008 || !is_has))
1009 {
1010 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1011
1012 *arg = s + 1;
1013 argvars[1].v_type = VAR_UNKNOWN;
1014 tv->v_type = VAR_NUMBER;
1015 tv->vval.v_number = 0;
1016 if (is_has)
1017 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001018 else if (is_len)
1019 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020 else
1021 f_exists(argvars, tv);
1022 clear_tv(&argvars[0]);
1023 ++ppconst->pp_used;
1024 return OK;
1025 }
1026 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001027 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001028 {
1029 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1030 return FAIL;
1031 }
1032 }
1033
1034 if (generate_ppconst(cctx, ppconst) == FAIL)
1035 return FAIL;
1036
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001037 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001038 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1039
1040 // We handle the "skip" argument of searchpair() and searchpairpos()
1041 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001042 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1043 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1044 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1045 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1046 special_fn = CA_SEARCHPAIR;
1047 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1048 special_fn = CA_SUBSTITUTE;
1049 else
1050 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001051
1052 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001053 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001054 goto theend;
1055
h-east2261c892023-08-16 21:49:54 +09001056 type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001057 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1058 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1059 {
1060 int idx;
1061
1062 // builtin function
1063 idx = find_internal_func(name);
1064 if (idx >= 0)
1065 {
1066 if (STRCMP(name, "flatten") == 0)
1067 {
1068 emsg(_(e_cannot_use_flatten_in_vim9_script));
1069 goto theend;
1070 }
1071
1072 if (STRCMP(name, "add") == 0 && argcount == 2)
1073 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001074 // add() can be compiled to instructions if we know the type
1075 if (type->tt_type == VAR_LIST)
1076 {
1077 // inline "add(list, item)" so that the type can be checked
1078 res = generate_LISTAPPEND(cctx);
1079 idx = -1;
1080 }
1081 else if (type->tt_type == VAR_BLOB)
1082 {
1083 // inline "add(blob, nr)" so that the type can be checked
1084 res = generate_BLOBAPPEND(cctx);
1085 idx = -1;
1086 }
1087 }
1088
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001089 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1090 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001091 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001092 // May have the "D" or "R" flag, reserve a variable for a
1093 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001094 if (get_defer_var_idx(cctx) == 0)
1095 idx = -1;
1096 }
1097
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001098 if (idx >= 0)
1099 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1100 }
1101 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001102 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103 goto theend;
1104 }
1105
Bram Moolenaar62aec932022-01-29 21:45:34 +00001106 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1107
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001108 // An argument or local variable can be a function reference, this
1109 // overrules a function name.
1110 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1111 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1112 {
1113 // If we can find the function by name generate the right call.
1114 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001115 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001116 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001118 if (!func_is_global(ufunc))
1119 {
h-east2261c892023-08-16 21:49:54 +09001120 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001121 goto theend;
1122 }
1123 if (!has_g_namespace
1124 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1125 {
1126 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001127 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001128 goto theend;
1129 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130 }
1131 }
1132
1133 // If the name is a variable, load it and use PCALL.
1134 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001135 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001136 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001137 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001138 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1139 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001140 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141
Christian Brabandtd5475e82023-08-17 23:34:09 +02001142 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143 goto theend;
1144 }
1145
1146 // If we can find a global function by name generate the right call.
1147 if (ufunc != NULL)
1148 {
h-east2261c892023-08-16 21:49:54 +09001149 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001150 goto theend;
1151 }
1152
1153 // A global function may be defined only later. Need to figure out at
1154 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001155 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156 res = generate_UCALL(cctx, name, argcount);
1157 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001158 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001159
1160theend:
1161 vim_free(tofree);
1162 return res;
1163}
1164
1165// like NAMESPACE_CHAR but with 'a' and 'l'.
1166#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1167
1168/*
1169 * Find the end of a variable or function name. Unlike find_name_end() this
1170 * does not recognize magic braces.
1171 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1172 * Return a pointer to just after the name. Equal to "arg" if there is no
1173 * valid name.
1174 */
1175 char_u *
1176to_name_end(char_u *arg, int use_namespace)
1177{
1178 char_u *p;
1179
1180 // Quick check for valid starting character.
1181 if (!eval_isnamec1(*arg))
1182 return arg;
1183
1184 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1185 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1186 // and can be used in slice "[n:]".
1187 if (*p == ':' && (p != arg + 1
1188 || !use_namespace
1189 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1190 break;
1191 return p;
1192}
1193
1194/*
1195 * Like to_name_end() but also skip over a list or dict constant.
1196 * Also accept "<SNR>123_Func".
1197 * This intentionally does not handle line continuation.
1198 */
1199 char_u *
1200to_name_const_end(char_u *arg)
1201{
1202 char_u *p = arg;
1203 typval_T rettv;
1204
1205 if (STRNCMP(p, "<SNR>", 5) == 0)
1206 p = skipdigits(p + 5);
1207 p = to_name_end(p, TRUE);
1208 if (p == arg && *arg == '[')
1209 {
1210
1211 // Can be "[1, 2, 3]->Func()".
1212 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1213 p = arg;
1214 }
1215 return p;
1216}
1217
1218/*
1219 * parse a list: [expr, expr]
1220 * "*arg" points to the '['.
1221 * ppconst->pp_is_const is set if all items are a constant.
1222 */
1223 static int
1224compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1225{
1226 char_u *p = skipwhite(*arg + 1);
1227 char_u *whitep = *arg + 1;
1228 int count = 0;
1229 int is_const;
1230 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001231 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001232
1233 for (;;)
1234 {
1235 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1236 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001237 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001238 return FAIL;
1239 }
1240 if (*p == ',')
1241 {
1242 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1243 return FAIL;
1244 }
1245 if (*p == ']')
1246 {
1247 ++p;
1248 break;
1249 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001250 if (must_end)
1251 {
1252 semsg(_(e_missing_comma_in_list_str), p);
1253 return FAIL;
1254 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1256 return FAIL;
1257 if (!is_const)
1258 is_all_const = FALSE;
1259 ++count;
1260 if (*p == ',')
1261 {
1262 ++p;
1263 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1264 {
1265 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1266 return FAIL;
1267 }
1268 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001269 else
1270 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001271 whitep = p;
1272 p = skipwhite(p);
1273 }
1274 *arg = p;
1275
1276 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001277 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278}
1279
1280/*
1281 * Parse a lambda: "(arg, arg) => expr"
1282 * "*arg" points to the '('.
1283 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1284 */
1285 static int
1286compile_lambda(char_u **arg, cctx_T *cctx)
1287{
1288 int r;
1289 typval_T rettv;
1290 ufunc_T *ufunc;
1291 evalarg_T evalarg;
1292
1293 init_evalarg(&evalarg);
1294 evalarg.eval_flags = EVAL_EVALUATE;
1295 evalarg.eval_cctx = cctx;
1296
1297 // Get the funcref in "rettv".
1298 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1299 if (r != OK)
1300 {
1301 clear_evalarg(&evalarg, NULL);
1302 return r;
1303 }
1304
1305 // "rettv" will now be a partial referencing the function.
1306 ufunc = rettv.vval.v_partial->pt_func;
1307 ++ufunc->uf_refcount;
1308 clear_tv(&rettv);
1309
1310 // Compile it here to get the return type. The return type is optional,
1311 // when it's missing use t_unknown. This is recognized in
1312 // compile_return().
1313 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1314 ufunc->uf_ret_type = &t_unknown;
1315 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1316
1317 // When the outer function is compiled for profiling or debugging, the
1318 // lambda may be called without profiling or debugging. Compile it here in
1319 // the right context.
1320 if (cctx->ctx_compile_type == CT_DEBUG
1321#ifdef FEAT_PROFILE
1322 || cctx->ctx_compile_type == CT_PROFILE
1323#endif
1324 )
1325 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1326
Bram Moolenaar139575d2022-03-15 19:29:30 +00001327 // if the outer function is not compiled for debugging or profiling, this
1328 // one might be
1329 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001330 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001331 compiletype_T compile_type = get_compile_type(ufunc);
1332
1333 if (compile_type != CT_NONE)
1334 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001335 }
1336
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001337 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1338 // "*arg" may point into it. Point into the original line to avoid a
1339 // dangling pointer.
1340 if (evalarg.eval_using_cmdline)
1341 {
1342 garray_T *gap = &evalarg.eval_tofree_ga;
1343 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1344
1345 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1346 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001347 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348 }
1349
1350 clear_evalarg(&evalarg, NULL);
1351
1352 if (ufunc->uf_def_status == UF_COMPILED)
1353 {
1354 // The return type will now be known.
1355 set_function_type(ufunc);
1356
1357 // The function reference count will be 1. When the ISN_FUNCREF
1358 // instruction is deleted the reference count is decremented and the
1359 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001360 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361 }
1362
1363 func_ptr_unref(ufunc);
1364 return FAIL;
1365}
1366
1367/*
1368 * Get a lambda and compile it. Uses Vim9 syntax.
1369 */
1370 int
1371get_lambda_tv_and_compile(
1372 char_u **arg,
1373 typval_T *rettv,
1374 int types_optional,
1375 evalarg_T *evalarg)
1376{
1377 int r;
1378 ufunc_T *ufunc;
1379 int save_sc_version = current_sctx.sc_version;
1380
1381 // Get the funcref in "rettv".
1382 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1383 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1384 current_sctx.sc_version = save_sc_version;
1385 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001386 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001387
1388 // "rettv" will now be a partial referencing the function.
1389 ufunc = rettv->vval.v_partial->pt_func;
1390
1391 // Compile it here to get the return type. The return type is optional,
1392 // when it's missing use t_unknown. This is recognized in
1393 // compile_return().
1394 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1395 ufunc->uf_ret_type = &t_unknown;
1396 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1397
1398 if (ufunc->uf_def_status == UF_COMPILED)
1399 {
1400 // The return type will now be known.
1401 set_function_type(ufunc);
1402 return OK;
1403 }
1404 clear_tv(rettv);
1405 return FAIL;
1406}
1407
1408/*
1409 * parse a dict: {key: val, [key]: val}
1410 * "*arg" points to the '{'.
1411 * ppconst->pp_is_const is set if all item values are a constant.
1412 */
1413 static int
1414compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1415{
1416 garray_T *instr = &cctx->ctx_instr;
1417 int count = 0;
1418 dict_T *d = dict_alloc();
1419 dictitem_T *item;
1420 char_u *whitep = *arg + 1;
1421 char_u *p;
1422 int is_const;
1423 int is_all_const = TRUE; // reset when non-const encountered
1424
1425 if (d == NULL)
1426 return FAIL;
1427 if (generate_ppconst(cctx, ppconst) == FAIL)
1428 return FAIL;
1429 for (;;)
1430 {
1431 char_u *key = NULL;
1432
1433 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1434 {
1435 *arg = NULL;
1436 goto failret;
1437 }
1438
1439 if (**arg == '}')
1440 break;
1441
1442 if (**arg == '[')
1443 {
1444 isn_T *isn;
1445
1446 // {[expr]: value} uses an evaluated key.
1447 *arg = skipwhite(*arg + 1);
1448 if (compile_expr0(arg, cctx) == FAIL)
1449 return FAIL;
1450 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1451 if (isn->isn_type == ISN_PUSHNR)
1452 {
1453 char buf[NUMBUFLEN];
1454
1455 // Convert to string at compile time.
1456 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1457 isn->isn_type = ISN_PUSHS;
1458 isn->isn_arg.string = vim_strsave((char_u *)buf);
1459 }
1460 if (isn->isn_type == ISN_PUSHS)
1461 key = isn->isn_arg.string;
1462 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1463 return FAIL;
1464 *arg = skipwhite(*arg);
1465 if (**arg != ']')
1466 {
1467 emsg(_(e_missing_matching_bracket_after_dict_key));
1468 return FAIL;
1469 }
1470 ++*arg;
1471 }
1472 else
1473 {
1474 // {"name": value},
1475 // {'name': value},
1476 // {name: value} use "name" as a literal key
1477 key = get_literal_key(arg);
1478 if (key == NULL)
1479 return FAIL;
1480 if (generate_PUSHS(cctx, &key) == FAIL)
1481 return FAIL;
1482 }
1483
1484 // Check for duplicate keys, if using string keys.
1485 if (key != NULL)
1486 {
1487 item = dict_find(d, key, -1);
1488 if (item != NULL)
1489 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001490 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001491 goto failret;
1492 }
1493 item = dictitem_alloc(key);
1494 if (item != NULL)
1495 {
1496 item->di_tv.v_type = VAR_UNKNOWN;
1497 item->di_tv.v_lock = 0;
1498 if (dict_add(d, item) == FAIL)
1499 dictitem_free(item);
1500 }
1501 }
1502
1503 if (**arg != ':')
1504 {
1505 if (*skipwhite(*arg) == ':')
1506 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1507 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001508 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509 return FAIL;
1510 }
1511 whitep = *arg + 1;
1512 if (!IS_WHITE_OR_NUL(*whitep))
1513 {
1514 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1515 return FAIL;
1516 }
1517
1518 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1519 {
1520 *arg = NULL;
1521 goto failret;
1522 }
1523
1524 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1525 return FAIL;
1526 if (!is_const)
1527 is_all_const = FALSE;
1528 ++count;
1529
1530 whitep = *arg;
1531 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1532 {
1533 *arg = NULL;
1534 goto failret;
1535 }
1536 if (**arg == '}')
1537 break;
1538 if (**arg != ',')
1539 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001540 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001541 goto failret;
1542 }
1543 if (IS_WHITE_OR_NUL(*whitep))
1544 {
1545 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1546 return FAIL;
1547 }
1548 whitep = *arg + 1;
1549 if (!IS_WHITE_OR_NUL(*whitep))
1550 {
1551 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1552 return FAIL;
1553 }
1554 *arg = skipwhite(whitep);
1555 }
1556
1557 *arg = *arg + 1;
1558
1559 // Allow for following comment, after at least one space.
1560 p = skipwhite(*arg);
1561 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1562 *arg += STRLEN(*arg);
1563
1564 dict_unref(d);
1565 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001566 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001567
1568failret:
1569 if (*arg == NULL)
1570 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001571 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001572 *arg = (char_u *)"";
1573 }
1574 dict_unref(d);
1575 return FAIL;
1576}
1577
1578/*
1579 * Compile "&option".
1580 */
1581 static int
1582compile_get_option(char_u **arg, cctx_T *cctx)
1583{
1584 typval_T rettv;
1585 char_u *start = *arg;
1586 int ret;
1587
1588 // parse the option and get the current value to get the type.
1589 rettv.v_type = VAR_UNKNOWN;
1590 ret = eval_option(arg, &rettv, TRUE);
1591 if (ret == OK)
1592 {
1593 // include the '&' in the name, eval_option() expects it.
1594 char_u *name = vim_strnsave(start, *arg - start);
1595 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1596 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1597
1598 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1599 vim_free(name);
1600 }
1601 clear_tv(&rettv);
1602
1603 return ret;
1604}
1605
1606/*
1607 * Compile "$VAR".
1608 */
1609 static int
1610compile_get_env(char_u **arg, cctx_T *cctx)
1611{
1612 char_u *start = *arg;
1613 int len;
1614 int ret;
1615 char_u *name;
1616
1617 ++*arg;
1618 len = get_env_len(arg);
1619 if (len == 0)
1620 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001621 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001622 return FAIL;
1623 }
1624
1625 // include the '$' in the name, eval_env_var() expects it.
1626 name = vim_strnsave(start, len + 1);
1627 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1628 vim_free(name);
1629 return ret;
1630}
1631
1632/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001633 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001634 */
1635 static int
1636compile_interp_string(char_u **arg, cctx_T *cctx)
1637{
1638 typval_T tv;
1639 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001640 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001641 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001642 int count = 0;
1643 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001644
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001645 // *arg is on the '$' character, move it to the first string character.
1646 ++*arg;
1647 quote = **arg;
1648 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001649
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001650 for (;;)
1651 {
1652 // Get the string up to the matching quote or to a single '{'.
1653 // "arg" is advanced to either the quote or the '{'.
1654 if (quote == '"')
1655 ret = eval_string(arg, &tv, evaluate, TRUE);
1656 else
1657 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1658 if (ret == FAIL)
1659 break;
1660 if (evaluate)
1661 {
1662 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1663 || (**arg != '{' && count == 0))
1664 {
1665 // generate non-empty string or empty string if it's the only
1666 // one
1667 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1668 return FAIL;
1669 tv.vval.v_string = NULL; // don't free it now
1670 ++count;
1671 }
1672 clear_tv(&tv);
1673 }
1674
1675 if (**arg != '{')
1676 {
1677 // found terminating quote
1678 ++*arg;
1679 break;
1680 }
1681
1682 p = compile_one_expr_in_str(*arg, cctx);
1683 if (p == NULL)
1684 {
1685 ret = FAIL;
1686 break;
1687 }
1688 ++count;
1689 *arg = p;
1690 }
LemonBoy2eaef102022-05-06 13:14:50 +01001691
1692 if (ret == FAIL || !evaluate)
1693 return ret;
1694
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001695 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1696 if (count > 1)
1697 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001698
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001699 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001700}
1701
1702/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001703 * Compile "@r".
1704 */
1705 static int
1706compile_get_register(char_u **arg, cctx_T *cctx)
1707{
1708 int ret;
1709
1710 ++*arg;
1711 if (**arg == NUL)
1712 {
1713 semsg(_(e_syntax_error_at_str), *arg - 1);
1714 return FAIL;
1715 }
1716 if (!valid_yank_reg(**arg, FALSE))
1717 {
1718 emsg_invreg(**arg);
1719 return FAIL;
1720 }
1721 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1722 ++*arg;
1723 return ret;
1724}
1725
1726/*
1727 * Apply leading '!', '-' and '+' to constant "rettv".
1728 * When "numeric_only" is TRUE do not apply '!'.
1729 */
1730 static int
1731apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1732{
1733 char_u *p = *end;
1734
1735 // this works from end to start
1736 while (p > start)
1737 {
1738 --p;
1739 if (*p == '-' || *p == '+')
1740 {
1741 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742 if (rettv->v_type == VAR_FLOAT)
1743 {
1744 if (*p == '-')
1745 rettv->vval.v_float = -rettv->vval.v_float;
1746 }
1747 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748 {
1749 varnumber_T val;
1750 int error = FALSE;
1751
1752 // tv_get_number_chk() accepts a string, but we don't want that
1753 // here
1754 if (check_not_string(rettv) == FAIL)
1755 return FAIL;
1756 val = tv_get_number_chk(rettv, &error);
1757 clear_tv(rettv);
1758 if (error)
1759 return FAIL;
1760 if (*p == '-')
1761 val = -val;
1762 rettv->v_type = VAR_NUMBER;
1763 rettv->vval.v_number = val;
1764 }
1765 }
1766 else if (numeric_only)
1767 {
1768 ++p;
1769 break;
1770 }
1771 else if (*p == '!')
1772 {
1773 int v = tv2bool(rettv);
1774
1775 // '!' is permissive in the type.
1776 clear_tv(rettv);
1777 rettv->v_type = VAR_BOOL;
1778 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1779 }
1780 }
1781 *end = p;
1782 return OK;
1783}
1784
1785/*
1786 * Recognize v: variables that are constants and set "rettv".
1787 */
1788 static void
1789get_vim_constant(char_u **arg, typval_T *rettv)
1790{
1791 if (STRNCMP(*arg, "v:true", 6) == 0)
1792 {
1793 rettv->v_type = VAR_BOOL;
1794 rettv->vval.v_number = VVAL_TRUE;
1795 *arg += 6;
1796 }
1797 else if (STRNCMP(*arg, "v:false", 7) == 0)
1798 {
1799 rettv->v_type = VAR_BOOL;
1800 rettv->vval.v_number = VVAL_FALSE;
1801 *arg += 7;
1802 }
1803 else if (STRNCMP(*arg, "v:null", 6) == 0)
1804 {
1805 rettv->v_type = VAR_SPECIAL;
1806 rettv->vval.v_number = VVAL_NULL;
1807 *arg += 6;
1808 }
1809 else if (STRNCMP(*arg, "v:none", 6) == 0)
1810 {
1811 rettv->v_type = VAR_SPECIAL;
1812 rettv->vval.v_number = VVAL_NONE;
1813 *arg += 6;
1814 }
1815}
1816
1817 exprtype_T
1818get_compare_type(char_u *p, int *len, int *type_is)
1819{
1820 exprtype_T type = EXPR_UNKNOWN;
1821 int i;
1822
1823 switch (p[0])
1824 {
1825 case '=': if (p[1] == '=')
1826 type = EXPR_EQUAL;
1827 else if (p[1] == '~')
1828 type = EXPR_MATCH;
1829 break;
1830 case '!': if (p[1] == '=')
1831 type = EXPR_NEQUAL;
1832 else if (p[1] == '~')
1833 type = EXPR_NOMATCH;
1834 break;
1835 case '>': if (p[1] != '=')
1836 {
1837 type = EXPR_GREATER;
1838 *len = 1;
1839 }
1840 else
1841 type = EXPR_GEQUAL;
1842 break;
1843 case '<': if (p[1] != '=')
1844 {
1845 type = EXPR_SMALLER;
1846 *len = 1;
1847 }
1848 else
1849 type = EXPR_SEQUAL;
1850 break;
1851 case 'i': if (p[1] == 's')
1852 {
1853 // "is" and "isnot"; but not a prefix of a name
1854 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1855 *len = 5;
1856 i = p[*len];
1857 if (!isalnum(i) && i != '_')
1858 {
1859 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1860 *type_is = TRUE;
1861 }
1862 }
1863 break;
1864 }
1865 return type;
1866}
1867
1868/*
1869 * Skip over an expression, ignoring most errors.
1870 */
1871 void
1872skip_expr_cctx(char_u **arg, cctx_T *cctx)
1873{
1874 evalarg_T evalarg;
1875
1876 init_evalarg(&evalarg);
1877 evalarg.eval_cctx = cctx;
1878 skip_expr(arg, &evalarg);
1879 clear_evalarg(&evalarg, NULL);
1880}
1881
1882/*
1883 * Check that the top of the type stack has a type that can be used as a
1884 * condition. Give an error and return FAIL if not.
1885 */
1886 int
1887bool_on_stack(cctx_T *cctx)
1888{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889 type_T *type;
1890
Bram Moolenaar078a4612022-01-04 15:17:03 +00001891 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001892 if (type == &t_bool)
1893 return OK;
1894
Bram Moolenaar4913d422022-10-17 13:13:32 +01001895 if (type->tt_type == VAR_ANY
1896 || type->tt_type == VAR_UNKNOWN
1897 || type->tt_type == VAR_NUMBER
1898 || type == &t_number_bool
1899 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001900 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1901 // This requires a runtime type check.
1902 return generate_COND2BOOL(cctx);
1903
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001904 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001905}
1906
1907/*
1908 * Give the "white on both sides" error, taking the operator from "p[len]".
1909 */
1910 void
1911error_white_both(char_u *op, int len)
1912{
1913 char_u buf[10];
1914
1915 vim_strncpy(buf, op, len);
1916 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1917}
1918
1919/*
1920 * Compile code to apply '-', '+' and '!'.
1921 * When "numeric_only" is TRUE do not apply '!'.
1922 */
1923 static int
1924compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1925{
1926 char_u *p = *end;
1927
1928 // this works from end to start
1929 while (p > start)
1930 {
1931 --p;
1932 while (VIM_ISWHITE(*p))
1933 --p;
1934 if (*p == '-' || *p == '+')
1935 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001936 type_T *type = get_type_on_stack(cctx, 0);
1937 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001938 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001939 return FAIL;
1940
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001942 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1943 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001944 }
1945 else if (numeric_only)
1946 {
1947 ++p;
1948 break;
1949 }
1950 else
1951 {
1952 int invert = *p == '!';
1953
1954 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1955 {
1956 if (p[-1] == '!')
1957 invert = !invert;
1958 --p;
1959 }
1960 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1961 return FAIL;
1962 }
1963 }
1964 *end = p;
1965 return OK;
1966}
1967
1968/*
1969 * Compile "(expression)": recursive!
1970 * Return FAIL/OK.
1971 */
1972 static int
1973compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1974{
1975 int ret;
1976 char_u *p = *arg + 1;
1977
1978 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1979 return FAIL;
1980 if (ppconst->pp_used <= PPSIZE - 10)
1981 {
1982 ret = compile_expr1(arg, cctx, ppconst);
1983 }
1984 else
1985 {
1986 // Not enough space in ppconst, flush constants.
1987 if (generate_ppconst(cctx, ppconst) == FAIL)
1988 return FAIL;
1989 ret = compile_expr0(arg, cctx);
1990 }
1991 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1992 return FAIL;
1993 if (**arg == ')')
1994 ++*arg;
1995 else if (ret == OK)
1996 {
1997 emsg(_(e_missing_closing_paren));
1998 ret = FAIL;
1999 }
2000 return ret;
2001}
2002
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002003static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002004
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005/*
2006 * Compile whatever comes after "name" or "name()".
2007 * Advances "*arg" only when something was recognized.
2008 */
2009 static int
2010compile_subscript(
2011 char_u **arg,
2012 cctx_T *cctx,
2013 char_u *start_leader,
2014 char_u **end_leader,
2015 ppconst_T *ppconst)
2016{
2017 char_u *name_start = *end_leader;
2018 int keeping_dict = FALSE;
2019
2020 for (;;)
2021 {
2022 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002023 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024
2025 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2026 {
2027 char_u *next = peek_next_line_from_context(cctx);
2028
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002029 // If a following line starts with "->{", "->(" or "->X" advance to
2030 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002031 // Also if a following line starts with ".x".
2032 if (next != NULL &&
2033 ((next[0] == '-' && next[1] == '>'
2034 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002035 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002036 || ASCII_ISALPHA(*skipwhite(next + 2))))
2037 || (next[0] == '.' && eval_isdictc(next[1]))))
2038 {
2039 next = next_line_from_context(cctx, TRUE);
2040 if (next == NULL)
2041 return FAIL;
2042 *arg = next;
2043 p = skipwhite(*arg);
2044 }
2045 }
2046
2047 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2048 // is not a function call.
2049 if (**arg == '(')
2050 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002051 int argcount = 0;
2052
2053 if (generate_ppconst(cctx, ppconst) == FAIL)
2054 return FAIL;
2055 ppconst->pp_is_const = FALSE;
2056
2057 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002058 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059
2060 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002061 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002062 return FAIL;
2063 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2064 return FAIL;
2065 if (keeping_dict)
2066 {
2067 keeping_dict = FALSE;
2068 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2069 return FAIL;
2070 }
2071 }
2072 else if (*p == '-' && p[1] == '>')
2073 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002074 char_u *pstart = p;
2075 int alt;
2076 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002077
Bram Moolenaarc7349932022-01-16 20:59:39 +00002078 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002079 if (generate_ppconst(cctx, ppconst) == FAIL)
2080 return FAIL;
2081 ppconst->pp_is_const = FALSE;
2082
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002083 // Apply the '!', '-' and '+' first:
2084 // -1.0->func() works like (-1.0)->func()
2085 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2086 return FAIL;
2087
2088 p += 2;
2089 *arg = skipwhite(p);
2090 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002091
2092 // Three alternatives handled here:
2093 // 1. "base->name(" only a name, use compile_call()
2094 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2095 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002097 alt = 2;
2098 else
2099 {
2100 // alternative 1 or 3
2101 p = *arg;
2102 if (!eval_isnamec1(*p))
2103 {
2104 semsg(_(e_trailing_characters_str), pstart);
2105 return FAIL;
2106 }
2107 if (ASCII_ISALPHA(*p) && p[1] == ':')
2108 p += 2;
2109 for ( ; eval_isnamec(*p); ++p)
2110 ;
2111 if (*p == '(')
2112 {
2113 // alternative 1
2114 alt = 1;
2115 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2116 return FAIL;
2117 }
2118 else
2119 {
2120 // Must be alternative 3, find the "(". Only works within
2121 // one line.
2122 alt = 3;
2123 paren = vim_strchr(p, '(');
2124 if (paren == NULL)
2125 {
2126 semsg(_(e_missing_parenthesis_str), *arg);
2127 return FAIL;
2128 }
2129 }
2130 }
2131
2132 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002133 {
2134 int argcount = 1;
2135 garray_T *stack = &cctx->ctx_type_stack;
2136 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 int expr_isn_start = cctx->ctx_instr.ga_len;
2138 int expr_isn_end;
2139 int arg_isn_count;
2140
Bram Moolenaarc7349932022-01-16 20:59:39 +00002141 if (alt == 2)
2142 {
2143 // Funcref call: list->(Refs[2])(arg)
2144 // or lambda: list->((arg) => expr)(arg)
2145 //
2146 // Fist compile the function expression.
2147 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2148 return FAIL;
2149 }
2150 else
2151 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002152 int fail;
2153 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002154 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002155
Bram Moolenaarc7349932022-01-16 20:59:39 +00002156 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002157
2158 // instead of using LOADG for "import.Func" use PUSHFUNC
2159 ++paren_follows_after_expr;
2160
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002161 // do not look in the next line
2162 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002163
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002164 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002165 || *skipwhite(*arg) != NUL;
2166 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002167 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002168 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002169
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002170 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002171 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002172 if (did_emsg == prev_did_emsg)
2173 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002174 return FAIL;
2175 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002176 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002177
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178 // Compile the arguments.
2179 if (**arg != '(')
2180 {
2181 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002182 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002183 else
2184 semsg(_(e_missing_parenthesis_str), *arg);
2185 return FAIL;
2186 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002187
2188 // Remember the next instruction index, where the instructions
2189 // for arguments are being written.
2190 expr_isn_end = cctx->ctx_instr.ga_len;
2191
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002192 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002193 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2194 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002195 return FAIL;
2196
2197 // Move the instructions for the arguments to before the
2198 // instructions of the expression and move the type of the
2199 // expression after the argument types. This is what ISN_PCALL
2200 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2202 if (arg_isn_count > 0)
2203 {
2204 int expr_isn_count = expr_isn_end - expr_isn_start;
2205 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002206 type_T *decl_type;
2207 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002208
2209 if (isn == NULL)
2210 return FAIL;
2211 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2212 + expr_isn_start,
2213 sizeof(isn_T) * expr_isn_count);
2214 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2215 + expr_isn_start,
2216 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2217 sizeof(isn_T) * arg_isn_count);
2218 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2219 + expr_isn_start + arg_isn_count,
2220 isn, sizeof(isn_T) * expr_isn_count);
2221 vim_free(isn);
2222
Bram Moolenaar078a4612022-01-04 15:17:03 +00002223 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2224 type = typep->type_curr;
2225 decl_type = typep->type_decl;
2226 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2227 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2228 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002230 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2231 typep->type_curr = type;
2232 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233 }
2234
Bram Moolenaar078a4612022-01-04 15:17:03 +00002235 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002236 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2237 return FAIL;
2238 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002239
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002240 if (keeping_dict)
2241 {
2242 keeping_dict = FALSE;
2243 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2244 return FAIL;
2245 }
2246 }
2247 else if (**arg == '[')
2248 {
2249 int is_slice = FALSE;
2250
2251 // list index: list[123]
2252 // dict member: dict[key]
2253 // string index: text[123]
2254 // blob index: blob[123]
2255 if (generate_ppconst(cctx, ppconst) == FAIL)
2256 return FAIL;
2257 ppconst->pp_is_const = FALSE;
2258
2259 ++p;
2260 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2261 return FAIL;
2262 if (**arg == ':')
2263 {
2264 // missing first index is equal to zero
2265 generate_PUSHNR(cctx, 0);
2266 }
2267 else
2268 {
2269 if (compile_expr0(arg, cctx) == FAIL)
2270 return FAIL;
2271 if (**arg == ':')
2272 {
2273 semsg(_(e_white_space_required_before_and_after_str_at_str),
2274 ":", *arg);
2275 return FAIL;
2276 }
2277 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2278 return FAIL;
2279 *arg = skipwhite(*arg);
2280 }
2281 if (**arg == ':')
2282 {
2283 is_slice = TRUE;
2284 ++*arg;
2285 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2286 {
2287 semsg(_(e_white_space_required_before_and_after_str_at_str),
2288 ":", *arg);
2289 return FAIL;
2290 }
2291 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2292 return FAIL;
2293 if (**arg == ']')
2294 // missing second index is equal to end of string
2295 generate_PUSHNR(cctx, -1);
2296 else
2297 {
2298 if (compile_expr0(arg, cctx) == FAIL)
2299 return FAIL;
2300 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2301 return FAIL;
2302 *arg = skipwhite(*arg);
2303 }
2304 }
2305
2306 if (**arg != ']')
2307 {
2308 emsg(_(e_missing_closing_square_brace));
2309 return FAIL;
2310 }
2311 *arg = *arg + 1;
2312
2313 if (keeping_dict)
2314 {
2315 keeping_dict = FALSE;
2316 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2317 return FAIL;
2318 }
2319 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2320 return FAIL;
2321 }
2322 else if (*p == '.' && p[1] != '.')
2323 {
2324 // dictionary member: dict.name
2325 if (generate_ppconst(cctx, ppconst) == FAIL)
2326 return FAIL;
2327 ppconst->pp_is_const = FALSE;
2328
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002329 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2330 && (type->tt_type == VAR_CLASS
2331 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002332 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002333 // class member: SomeClass.varname
2334 // class method: SomeClass.SomeMethod()
2335 // class constructor: SomeClass.new()
2336 // object member: someObject.varname, this.varname
2337 // object method: someObject.SomeMethod(), this.SomeMethod()
2338 *arg = p;
2339 if (compile_class_object_index(cctx, arg, type) == FAIL)
2340 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002341 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002342 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002343 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002344 *arg = p + 1;
2345 if (IS_WHITE_OR_NUL(**arg))
2346 {
2347 emsg(_(e_missing_name_after_dot));
2348 return FAIL;
2349 }
2350 p = *arg;
2351 if (eval_isdictc(*p))
2352 while (eval_isnamec(*p))
2353 MB_PTR_ADV(p);
2354 if (p == *arg)
2355 {
2356 semsg(_(e_syntax_error_at_str), *arg);
2357 return FAIL;
2358 }
2359 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2360 return FAIL;
2361 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2362 return FAIL;
2363 keeping_dict = TRUE;
2364 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002365 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002366 }
2367 else
2368 break;
2369 }
2370
2371 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2372 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002373 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2374 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002375 return FAIL;
2376
2377 return OK;
2378}
2379
2380/*
2381 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2382 * "arg" is advanced until after the expression, skipping white space.
2383 *
2384 * If the value is a constant "ppconst->pp_used" will be non-zero.
2385 * Before instructions are generated, any values in "ppconst" will generated.
2386 *
2387 * This is the compiling equivalent of eval1(), eval2(), etc.
2388 */
2389
2390/*
2391 * number number constant
2392 * 0zFFFFFFFF Blob constant
2393 * "string" string constant
2394 * 'string' literal string constant
2395 * &option-name option value
2396 * @r register contents
2397 * identifier variable value
2398 * function() function call
2399 * $VAR environment variable
2400 * (expression) nested expression
2401 * [expr, expr] List
2402 * {key: val, [key]: val} Dictionary
2403 *
2404 * Also handle:
2405 * ! in front logical NOT
2406 * - in front unary minus
2407 * + in front unary plus (ignored)
2408 * trailing (arg) funcref/partial call
2409 * trailing [] subscript in String or List
2410 * trailing .name entry in Dictionary
2411 * trailing ->name() method call
2412 */
2413 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002414compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002415 char_u **arg,
2416 cctx_T *cctx,
2417 ppconst_T *ppconst)
2418{
2419 char_u *start_leader, *end_leader;
2420 int ret = OK;
2421 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2422 int used_before = ppconst->pp_used;
2423
2424 ppconst->pp_is_const = FALSE;
2425
2426 /*
2427 * Skip '!', '-' and '+' characters. They are handled later.
2428 */
2429 start_leader = *arg;
2430 if (eval_leader(arg, TRUE) == FAIL)
2431 return FAIL;
2432 end_leader = *arg;
2433
2434 rettv->v_type = VAR_UNKNOWN;
2435 switch (**arg)
2436 {
2437 /*
2438 * Number constant.
2439 */
2440 case '0': // also for blob starting with 0z
2441 case '1':
2442 case '2':
2443 case '3':
2444 case '4':
2445 case '5':
2446 case '6':
2447 case '7':
2448 case '8':
2449 case '9':
2450 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2451 return FAIL;
2452 // Apply "-" and "+" just before the number now, right to
2453 // left. Matters especially when "->" follows. Stops at
2454 // '!'.
2455 if (apply_leader(rettv, TRUE,
2456 start_leader, &end_leader) == FAIL)
2457 {
2458 clear_tv(rettv);
2459 return FAIL;
2460 }
2461 break;
2462
2463 /*
2464 * String constant: "string".
2465 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002466 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 return FAIL;
2468 break;
2469
2470 /*
2471 * Literal string constant: 'str''ing'.
2472 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002473 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002474 return FAIL;
2475 break;
2476
2477 /*
2478 * Constant Vim variable.
2479 */
2480 case 'v': get_vim_constant(arg, rettv);
2481 ret = NOTDONE;
2482 break;
2483
2484 /*
2485 * "true" constant
2486 */
2487 case 't': if (STRNCMP(*arg, "true", 4) == 0
2488 && !eval_isnamec((*arg)[4]))
2489 {
2490 *arg += 4;
2491 rettv->v_type = VAR_BOOL;
2492 rettv->vval.v_number = VVAL_TRUE;
2493 }
2494 else
2495 ret = NOTDONE;
2496 break;
2497
2498 /*
2499 * "false" constant
2500 */
2501 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2502 && !eval_isnamec((*arg)[5]))
2503 {
2504 *arg += 5;
2505 rettv->v_type = VAR_BOOL;
2506 rettv->vval.v_number = VVAL_FALSE;
2507 }
2508 else
2509 ret = NOTDONE;
2510 break;
2511
2512 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002513 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002514 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002515 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002516 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002517 char_u *p = *arg + 4;
2518 int len;
2519
2520 for (len = 0; eval_isnamec(p[len]); ++len)
2521 ;
2522 ret = handle_predefined(*arg, len + 4, rettv);
2523 if (ret == FAIL)
2524 ret = NOTDONE;
2525 else
2526 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002527 }
2528 else
2529 ret = NOTDONE;
2530 break;
2531
2532 /*
2533 * List: [expr, expr]
2534 */
2535 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2536 return FAIL;
2537 ret = compile_list(arg, cctx, ppconst);
2538 break;
2539
2540 /*
2541 * Dictionary: {'key': val, 'key': val}
2542 */
2543 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2544 return FAIL;
2545 ret = compile_dict(arg, cctx, ppconst);
2546 break;
2547
2548 /*
2549 * Option value: &name
2550 */
2551 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2552 return FAIL;
2553 ret = compile_get_option(arg, cctx);
2554 break;
2555
2556 /*
2557 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002558 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559 */
2560 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2561 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002562 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2563 ret = compile_interp_string(arg, cctx);
2564 else
2565 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002566 break;
2567
2568 /*
2569 * Register contents: @r.
2570 */
2571 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2572 return FAIL;
2573 ret = compile_get_register(arg, cctx);
2574 break;
2575 /*
2576 * nested expression: (expression).
2577 * lambda: (arg, arg) => expr
2578 * funcref: (arg, arg) => { statement }
2579 */
2580 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2581 ret = compile_lambda(arg, cctx);
2582 if (ret == NOTDONE)
2583 ret = compile_parenthesis(arg, cctx, ppconst);
2584 break;
2585
2586 default: ret = NOTDONE;
2587 break;
2588 }
2589 if (ret == FAIL)
2590 return FAIL;
2591
2592 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2593 {
2594 if (cctx->ctx_skip == SKIP_YES)
2595 clear_tv(rettv);
2596 else
2597 // A constant expression can possibly be handled compile time,
2598 // return the value instead of generating code.
2599 ++ppconst->pp_used;
2600 }
2601 else if (ret == NOTDONE)
2602 {
2603 char_u *p;
2604 int r;
2605
2606 if (!eval_isnamec1(**arg))
2607 {
2608 if (!vim9_bad_comment(*arg))
2609 {
2610 if (ends_excmd(*skipwhite(*arg)))
2611 semsg(_(e_empty_expression_str), *arg);
2612 else
2613 semsg(_(e_name_expected_str), *arg);
2614 }
2615 return FAIL;
2616 }
2617
2618 // "name" or "name()"
2619 p = to_name_end(*arg, TRUE);
2620 if (p - *arg == (size_t)1 && **arg == '_')
2621 {
2622 emsg(_(e_cannot_use_underscore_here));
2623 return FAIL;
2624 }
2625
2626 if (*p == '(')
2627 {
2628 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2629 }
2630 else
2631 {
2632 if (cctx->ctx_skip != SKIP_YES
2633 && generate_ppconst(cctx, ppconst) == FAIL)
2634 return FAIL;
2635 r = compile_load(arg, p, cctx, TRUE, TRUE);
2636 }
2637 if (r == FAIL)
2638 return FAIL;
2639 }
2640
2641 // Handle following "[]", ".member", etc.
2642 // Then deal with prefixed '-', '+' and '!', if not done already.
2643 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2644 ppconst) == FAIL)
2645 return FAIL;
2646 if (ppconst->pp_used > 0)
2647 {
2648 // apply the '!', '-' and '+' before the constant
2649 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2650 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2651 return FAIL;
2652 return OK;
2653 }
2654 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2655 return FAIL;
2656 return OK;
2657}
2658
2659/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002660 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002661 */
2662 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002663compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002664{
2665 type_T *want_type = NULL;
2666
2667 // Recognize <type>
2668 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2669 {
2670 ++*arg;
2671 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2672 if (want_type == NULL)
2673 return FAIL;
2674
2675 if (**arg != '>')
2676 {
2677 if (*skipwhite(*arg) == '>')
2678 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2679 else
2680 emsg(_(e_missing_gt));
2681 return FAIL;
2682 }
2683 ++*arg;
2684 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2685 return FAIL;
2686 }
2687
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002688 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002689 return FAIL;
2690
2691 if (want_type != NULL)
2692 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002693 type_T *actual;
2694 where_T where = WHERE_INIT;
2695
2696 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002697 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002698 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002699 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002700 if (need_type(actual, want_type, FALSE,
2701 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002702 return FAIL;
2703 }
2704 }
2705
2706 return OK;
2707}
2708
2709/*
2710 * * number multiplication
2711 * / number division
2712 * % number modulo
2713 */
2714 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002715compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716{
2717 char_u *op;
2718 char_u *next;
2719 int ppconst_used = ppconst->pp_used;
2720
2721 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002722 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002723 return FAIL;
2724
2725 /*
2726 * Repeat computing, until no "*", "/" or "%" is following.
2727 */
2728 for (;;)
2729 {
2730 op = may_peek_next_line(cctx, *arg, &next);
2731 if (*op != '*' && *op != '/' && *op != '%')
2732 break;
2733 if (next != NULL)
2734 {
2735 *arg = next_line_from_context(cctx, TRUE);
2736 op = skipwhite(*arg);
2737 }
2738
2739 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2740 {
2741 error_white_both(op, 1);
2742 return FAIL;
2743 }
2744 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2745 return FAIL;
2746
2747 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002748 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002749 return FAIL;
2750
2751 if (ppconst->pp_used == ppconst_used + 2
2752 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2753 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2754 {
2755 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2756 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2757 varnumber_T res = 0;
2758 int failed = FALSE;
2759
2760 // both are numbers: compute the result
2761 switch (*op)
2762 {
2763 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2764 break;
2765 case '/': res = num_divide(tv1->vval.v_number,
2766 tv2->vval.v_number, &failed);
2767 break;
2768 case '%': res = num_modulus(tv1->vval.v_number,
2769 tv2->vval.v_number, &failed);
2770 break;
2771 }
2772 if (failed)
2773 return FAIL;
2774 tv1->vval.v_number = res;
2775 --ppconst->pp_used;
2776 }
2777 else
2778 {
2779 generate_ppconst(cctx, ppconst);
2780 generate_two_op(cctx, op);
2781 }
2782 }
2783
2784 return OK;
2785}
2786
2787/*
2788 * + number addition or list/blobl concatenation
2789 * - number subtraction
2790 * .. string concatenation
2791 */
2792 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002793compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002794{
2795 char_u *op;
2796 char_u *next;
2797 int oplen;
2798 int ppconst_used = ppconst->pp_used;
2799
2800 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002801 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002802 return FAIL;
2803
2804 /*
2805 * Repeat computing, until no "+", "-" or ".." is following.
2806 */
2807 for (;;)
2808 {
2809 op = may_peek_next_line(cctx, *arg, &next);
2810 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2811 break;
2812 if (op[0] == op[1] && *op != '.' && next)
2813 // Finding "++" or "--" on the next line is a separate command.
2814 // But ".." is concatenation.
2815 break;
2816 oplen = (*op == '.' ? 2 : 1);
2817 if (next != NULL)
2818 {
2819 *arg = next_line_from_context(cctx, TRUE);
2820 op = skipwhite(*arg);
2821 }
2822
2823 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2824 {
2825 error_white_both(op, oplen);
2826 return FAIL;
2827 }
2828
2829 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2830 return FAIL;
2831
2832 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002833 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002834 return FAIL;
2835
2836 if (ppconst->pp_used == ppconst_used + 2
2837 && (*op == '.'
2838 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2839 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2840 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2841 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2842 {
2843 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2844 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2845
2846 // concat/subtract/add constant numbers
2847 if (*op == '+')
2848 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2849 else if (*op == '-')
2850 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2851 else
2852 {
2853 // concatenate constant strings
2854 char_u *s1 = tv1->vval.v_string;
2855 char_u *s2 = tv2->vval.v_string;
2856 size_t len1 = STRLEN(s1);
2857
2858 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2859 if (tv1->vval.v_string == NULL)
2860 {
2861 clear_ppconst(ppconst);
2862 return FAIL;
2863 }
2864 mch_memmove(tv1->vval.v_string, s1, len1);
2865 STRCPY(tv1->vval.v_string + len1, s2);
2866 vim_free(s1);
2867 vim_free(s2);
2868 }
2869 --ppconst->pp_used;
2870 }
2871 else
2872 {
2873 generate_ppconst(cctx, ppconst);
2874 ppconst->pp_is_const = FALSE;
2875 if (*op == '.')
2876 {
2877 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2878 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2879 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002880 if (generate_CONCAT(cctx, 2) == FAIL)
2881 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002882 }
2883 else
2884 generate_two_op(cctx, op);
2885 }
2886 }
2887
2888 return OK;
2889}
2890
2891/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002892 * expr6a >> expr6b
2893 * expr6a << expr6b
2894 *
2895 * Produces instructions:
2896 * OPNR bitwise left or right shift
2897 */
2898 static int
2899compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2900{
2901 exprtype_T type = EXPR_UNKNOWN;
2902 char_u *p;
2903 char_u *next;
2904 int len = 2;
2905 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002906 isn_T *isn;
2907
2908 // get the first variable
2909 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2910 return FAIL;
2911
2912 /*
2913 * Repeat computing, until no "+", "-" or ".." is following.
2914 */
2915 for (;;)
2916 {
2917 type = EXPR_UNKNOWN;
2918
2919 p = may_peek_next_line(cctx, *arg, &next);
2920 if (p[0] == '<' && p[1] == '<')
2921 type = EXPR_LSHIFT;
2922 else if (p[0] == '>' && p[1] == '>')
2923 type = EXPR_RSHIFT;
2924
2925 if (type == EXPR_UNKNOWN)
2926 return OK;
2927
2928 // Handle a bitwise left or right shift operator
2929 if (ppconst->pp_used == ppconst_used + 1)
2930 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002931 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002932 {
2933 // left operand should be a number
2934 emsg(_(e_bitshift_ops_must_be_number));
2935 return FAIL;
2936 }
2937 }
2938 else
2939 {
2940 type_T *t = get_type_on_stack(cctx, 0);
2941
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002942 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002943 {
2944 emsg(_(e_bitshift_ops_must_be_number));
2945 return FAIL;
2946 }
2947 }
2948
2949 if (next != NULL)
2950 {
2951 *arg = next_line_from_context(cctx, TRUE);
2952 p = skipwhite(*arg);
2953 }
2954
2955 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2956 {
2957 error_white_both(p, len);
2958 return FAIL;
2959 }
2960
2961 // get the second variable
2962 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2963 return FAIL;
2964
2965 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2966 return FAIL;
2967
2968 if (ppconst->pp_used == ppconst_used + 2)
2969 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002970 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2971 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2972
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002973 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002974 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2975 {
2976 // right operand should be a positive number
2977 if (tv2->v_type != VAR_NUMBER)
2978 emsg(_(e_bitshift_ops_must_be_number));
2979 else
dundargocc57b5bc2022-11-02 13:30:51 +00002980 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002981 return FAIL;
2982 }
2983
2984 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2985 tv1->vval.v_number = 0;
2986 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002987 tv1->vval.v_number =
2988 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002989 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002990 tv1->vval.v_number =
2991 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002992 clear_tv(tv2);
2993 --ppconst->pp_used;
2994 }
2995 else
2996 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002997 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2998 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002999 {
3000 emsg(_(e_bitshift_ops_must_be_number));
3001 return FAIL;
3002 }
3003
3004 generate_ppconst(cctx, ppconst);
3005
3006 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3007 if (isn == NULL)
3008 return FAIL;
3009
3010 if (isn != NULL)
3011 isn->isn_arg.op.op_type = type;
3012 }
3013 }
3014
3015 return OK;
3016}
3017
3018/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003019 * expr5a == expr5b
3020 * expr5a =~ expr5b
3021 * expr5a != expr5b
3022 * expr5a !~ expr5b
3023 * expr5a > expr5b
3024 * expr5a >= expr5b
3025 * expr5a < expr5b
3026 * expr5a <= expr5b
3027 * expr5a is expr5b
3028 * expr5a isnot expr5b
3029 *
3030 * Produces instructions:
3031 * EVAL expr5a Push result of "expr5a"
3032 * EVAL expr5b Push result of "expr5b"
3033 * COMPARE one of the compare instructions
3034 */
3035 static int
3036compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3037{
3038 exprtype_T type = EXPR_UNKNOWN;
3039 char_u *p;
3040 char_u *next;
3041 int len = 2;
3042 int type_is = FALSE;
3043 int ppconst_used = ppconst->pp_used;
3044
3045 // get the first variable
3046 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3047 return FAIL;
3048
3049 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003050
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003051 type = get_compare_type(p, &len, &type_is);
3052
3053 /*
3054 * If there is a comparative operator, use it.
3055 */
3056 if (type != EXPR_UNKNOWN)
3057 {
3058 int ic = FALSE; // Default: do not ignore case
3059
3060 if (next != NULL)
3061 {
3062 *arg = next_line_from_context(cctx, TRUE);
3063 p = skipwhite(*arg);
3064 }
3065 if (type_is && (p[len] == '?' || p[len] == '#'))
3066 {
3067 semsg(_(e_invalid_expression_str), *arg);
3068 return FAIL;
3069 }
3070 // extra question mark appended: ignore case
3071 if (p[len] == '?')
3072 {
3073 ic = TRUE;
3074 ++len;
3075 }
3076 // extra '#' appended: match case (ignored)
3077 else if (p[len] == '#')
3078 ++len;
3079 // nothing appended: match case
3080
3081 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3082 {
3083 error_white_both(p, len);
3084 return FAIL;
3085 }
3086
3087 // get the second variable
3088 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3089 return FAIL;
3090
3091 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3092 return FAIL;
3093
3094 if (ppconst->pp_used == ppconst_used + 2)
3095 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003096 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003097 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3098 int ret;
3099
3100 // Both sides are a constant, compute the result now.
3101 // First check for a valid combination of types, this is more
3102 // strict than typval_compare().
3103 if (check_compare_types(type, tv1, tv2) == FAIL)
3104 ret = FAIL;
3105 else
3106 {
3107 ret = typval_compare(tv1, tv2, type, ic);
3108 tv1->v_type = VAR_BOOL;
3109 tv1->vval.v_number = tv1->vval.v_number
3110 ? VVAL_TRUE : VVAL_FALSE;
3111 clear_tv(tv2);
3112 --ppconst->pp_used;
3113 }
3114 return ret;
3115 }
3116
3117 generate_ppconst(cctx, ppconst);
3118 return generate_COMPARE(cctx, type, ic);
3119 }
3120
3121 return OK;
3122}
3123
3124static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3125
3126/*
3127 * Compile || or &&.
3128 */
3129 static int
3130compile_and_or(
3131 char_u **arg,
3132 cctx_T *cctx,
3133 char *op,
3134 ppconst_T *ppconst,
3135 int ppconst_used UNUSED)
3136{
3137 char_u *next;
3138 char_u *p = may_peek_next_line(cctx, *arg, &next);
3139 int opchar = *op;
3140
3141 if (p[0] == opchar && p[1] == opchar)
3142 {
3143 garray_T *instr = &cctx->ctx_instr;
3144 garray_T end_ga;
3145 int save_skip = cctx->ctx_skip;
3146
3147 /*
3148 * Repeat until there is no following "||" or "&&"
3149 */
3150 ga_init2(&end_ga, sizeof(int), 10);
3151 while (p[0] == opchar && p[1] == opchar)
3152 {
3153 long start_lnum = SOURCING_LNUM;
3154 long save_sourcing_lnum;
3155 int start_ctx_lnum = cctx->ctx_lnum;
3156 int save_lnum;
3157 int const_used;
3158 int status;
3159 jumpwhen_T jump_when = opchar == '|'
3160 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3161
3162 if (next != NULL)
3163 {
3164 *arg = next_line_from_context(cctx, TRUE);
3165 p = skipwhite(*arg);
3166 }
3167
3168 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3169 {
3170 semsg(_(e_white_space_required_before_and_after_str_at_str),
3171 op, p);
3172 ga_clear(&end_ga);
3173 return FAIL;
3174 }
3175
3176 save_sourcing_lnum = SOURCING_LNUM;
3177 SOURCING_LNUM = start_lnum;
3178 save_lnum = cctx->ctx_lnum;
3179 cctx->ctx_lnum = start_ctx_lnum;
3180
3181 status = check_ppconst_bool(ppconst);
3182 if (status != FAIL)
3183 {
3184 // Use the last ppconst if possible.
3185 if (ppconst->pp_used > 0)
3186 {
3187 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3188 int is_true = tv2bool(tv);
3189
3190 if ((is_true && opchar == '|')
3191 || (!is_true && opchar == '&'))
3192 {
3193 // For "false && expr" and "true || expr" the "expr"
3194 // does not need to be evaluated.
3195 cctx->ctx_skip = SKIP_YES;
3196 clear_tv(tv);
3197 tv->v_type = VAR_BOOL;
3198 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3199 }
3200 else
3201 {
3202 // For "true && expr" and "false || expr" only "expr"
3203 // needs to be evaluated.
3204 --ppconst->pp_used;
3205 jump_when = JUMP_NEVER;
3206 }
3207 }
3208 else
3209 {
3210 // Every part must evaluate to a bool.
3211 status = bool_on_stack(cctx);
3212 }
3213 }
3214 if (status != FAIL)
3215 status = ga_grow(&end_ga, 1);
3216 cctx->ctx_lnum = save_lnum;
3217 if (status == FAIL)
3218 {
3219 ga_clear(&end_ga);
3220 return FAIL;
3221 }
3222
3223 if (jump_when != JUMP_NEVER)
3224 {
3225 if (cctx->ctx_skip != SKIP_YES)
3226 {
3227 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3228 ++end_ga.ga_len;
3229 }
3230 generate_JUMP(cctx, jump_when, 0);
3231 }
3232
3233 // eval the next expression
3234 SOURCING_LNUM = save_sourcing_lnum;
3235 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3236 {
3237 ga_clear(&end_ga);
3238 return FAIL;
3239 }
3240
3241 const_used = ppconst->pp_used;
3242 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3243 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3244 {
3245 ga_clear(&end_ga);
3246 return FAIL;
3247 }
3248
3249 // "0 || 1" results in true, "1 && 0" results in false.
3250 if (ppconst->pp_used == const_used + 1)
3251 {
3252 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3253
3254 if (tv->v_type == VAR_NUMBER
3255 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3256 {
3257 tv->vval.v_number = tv->vval.v_number == 1
3258 ? VVAL_TRUE : VVAL_FALSE;
3259 tv->v_type = VAR_BOOL;
3260 }
3261 }
3262
3263 p = may_peek_next_line(cctx, *arg, &next);
3264 }
3265
3266 if (check_ppconst_bool(ppconst) == FAIL)
3267 {
3268 ga_clear(&end_ga);
3269 return FAIL;
3270 }
3271
3272 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3273 // Every part must evaluate to a bool.
3274 if (bool_on_stack(cctx) == FAIL)
3275 {
3276 ga_clear(&end_ga);
3277 return FAIL;
3278 }
3279
3280 if (end_ga.ga_len > 0)
3281 {
3282 // Fill in the end label in all jumps.
3283 generate_ppconst(cctx, ppconst);
3284 while (end_ga.ga_len > 0)
3285 {
3286 isn_T *isn;
3287
3288 --end_ga.ga_len;
3289 isn = ((isn_T *)instr->ga_data)
3290 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3291 isn->isn_arg.jump.jump_where = instr->ga_len;
3292 }
3293 }
3294 ga_clear(&end_ga);
3295
3296 cctx->ctx_skip = save_skip;
3297 }
3298
3299 return OK;
3300}
3301
3302/*
3303 * expr4a && expr4a && expr4a logical AND
3304 *
3305 * Produces instructions:
3306 * EVAL expr4a Push result of "expr4a"
3307 * COND2BOOL convert to bool if needed
3308 * JUMP_IF_COND_FALSE end
3309 * EVAL expr4b Push result of "expr4b"
3310 * JUMP_IF_COND_FALSE end
3311 * EVAL expr4c Push result of "expr4c"
3312 * end:
3313 */
3314 static int
3315compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3316{
3317 int ppconst_used = ppconst->pp_used;
3318
3319 // get the first variable
3320 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3321 return FAIL;
3322
3323 // || and && work almost the same
3324 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3325}
3326
3327/*
3328 * expr3a || expr3b || expr3c logical OR
3329 *
3330 * Produces instructions:
3331 * EVAL expr3a Push result of "expr3a"
3332 * COND2BOOL convert to bool if needed
3333 * JUMP_IF_COND_TRUE end
3334 * EVAL expr3b Push result of "expr3b"
3335 * JUMP_IF_COND_TRUE end
3336 * EVAL expr3c Push result of "expr3c"
3337 * end:
3338 */
3339 static int
3340compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3341{
3342 int ppconst_used = ppconst->pp_used;
3343
3344 // eval the first expression
3345 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3346 return FAIL;
3347
3348 // || and && work almost the same
3349 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3350}
3351
3352/*
3353 * Toplevel expression: expr2 ? expr1a : expr1b
3354 * Produces instructions:
3355 * EVAL expr2 Push result of "expr2"
3356 * JUMP_IF_FALSE alt jump if false
3357 * EVAL expr1a
3358 * JUMP_ALWAYS end
3359 * alt: EVAL expr1b
3360 * end:
3361 *
3362 * Toplevel expression: expr2 ?? expr1
3363 * Produces instructions:
3364 * EVAL expr2 Push result of "expr2"
3365 * JUMP_AND_KEEP_IF_TRUE end jump if true
3366 * EVAL expr1
3367 * end:
3368 */
3369 int
3370compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3371{
3372 char_u *p;
3373 int ppconst_used = ppconst->pp_used;
3374 char_u *next;
3375
3376 // Ignore all kinds of errors when not producing code.
3377 if (cctx->ctx_skip == SKIP_YES)
3378 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003379 int prev_did_emsg = did_emsg;
3380
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003381 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003382 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003383 }
3384
3385 // Evaluate the first expression.
3386 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3387 return FAIL;
3388
3389 p = may_peek_next_line(cctx, *arg, &next);
3390 if (*p == '?')
3391 {
3392 int op_falsy = p[1] == '?';
3393 garray_T *instr = &cctx->ctx_instr;
3394 garray_T *stack = &cctx->ctx_type_stack;
3395 int alt_idx = instr->ga_len;
3396 int end_idx = 0;
3397 isn_T *isn;
3398 type_T *type1 = NULL;
3399 int has_const_expr = FALSE;
3400 int const_value = FALSE;
3401 int save_skip = cctx->ctx_skip;
3402
3403 if (next != NULL)
3404 {
3405 *arg = next_line_from_context(cctx, TRUE);
3406 p = skipwhite(*arg);
3407 }
3408
3409 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3410 {
3411 semsg(_(e_white_space_required_before_and_after_str_at_str),
3412 op_falsy ? "??" : "?", p);
3413 return FAIL;
3414 }
3415
3416 if (ppconst->pp_used == ppconst_used + 1)
3417 {
3418 // the condition is a constant, we know whether the ? or the :
3419 // expression is to be evaluated.
3420 has_const_expr = TRUE;
3421 if (op_falsy)
3422 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3423 else
3424 {
3425 int error = FALSE;
3426
3427 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3428 &error);
3429 if (error)
3430 return FAIL;
3431 }
3432 cctx->ctx_skip = save_skip == SKIP_YES ||
3433 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3434
3435 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3436 // "left ?? right" and "left" is truthy: produce "left"
3437 generate_ppconst(cctx, ppconst);
3438 else
3439 {
3440 clear_tv(&ppconst->pp_tv[ppconst_used]);
3441 --ppconst->pp_used;
3442 }
3443 }
3444 else
3445 {
3446 generate_ppconst(cctx, ppconst);
3447 if (op_falsy)
3448 end_idx = instr->ga_len;
3449 generate_JUMP(cctx, op_falsy
3450 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3451 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003452 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003453 }
3454
3455 // evaluate the second expression; any type is accepted
3456 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3457 return FAIL;
3458 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3459 return FAIL;
3460
3461 if (!has_const_expr)
3462 {
3463 generate_ppconst(cctx, ppconst);
3464
3465 if (!op_falsy)
3466 {
3467 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003468 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003469 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003470
3471 end_idx = instr->ga_len;
3472 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3473
3474 // jump here from JUMP_IF_FALSE
3475 isn = ((isn_T *)instr->ga_data) + alt_idx;
3476 isn->isn_arg.jump.jump_where = instr->ga_len;
3477 }
3478 }
3479
3480 if (!op_falsy)
3481 {
3482 // Check for the ":".
3483 p = may_peek_next_line(cctx, *arg, &next);
3484 if (*p != ':')
3485 {
3486 emsg(_(e_missing_colon_after_questionmark));
3487 return FAIL;
3488 }
3489 if (next != NULL)
3490 {
3491 *arg = next_line_from_context(cctx, TRUE);
3492 p = skipwhite(*arg);
3493 }
3494
3495 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3496 {
3497 semsg(_(e_white_space_required_before_and_after_str_at_str),
3498 ":", p);
3499 return FAIL;
3500 }
3501
3502 // evaluate the third expression
3503 if (has_const_expr)
3504 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3505 ? SKIP_YES : SKIP_NOT;
3506 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3507 return FAIL;
3508 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3509 return FAIL;
3510 }
3511
3512 if (!has_const_expr)
3513 {
3514 type_T **typep;
3515
3516 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003517 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003518
3519 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003520 typep = &((((type2_T *)stack->ga_data)
3521 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003522 common_type(type1, *typep, typep, cctx->ctx_type_list);
3523
3524 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3525 isn = ((isn_T *)instr->ga_data) + end_idx;
3526 isn->isn_arg.jump.jump_where = instr->ga_len;
3527 }
3528
3529 cctx->ctx_skip = save_skip;
3530 }
3531 return OK;
3532}
3533
3534/*
3535 * Toplevel expression.
3536 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3537 * Returns OK or FAIL.
3538 */
3539 int
3540compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3541{
3542 ppconst_T ppconst;
3543
3544 CLEAR_FIELD(ppconst);
3545 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3546 {
3547 clear_ppconst(&ppconst);
3548 return FAIL;
3549 }
3550 if (is_const != NULL)
3551 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3552 if (generate_ppconst(cctx, &ppconst) == FAIL)
3553 return FAIL;
3554 return OK;
3555}
3556
3557/*
3558 * Toplevel expression.
3559 */
3560 int
3561compile_expr0(char_u **arg, cctx_T *cctx)
3562{
3563 return compile_expr0_ext(arg, cctx, NULL);
3564}
3565
3566
3567#endif // defined(FEAT_EVAL)