blob: 3817e38c569959167655640d2e8d8f1caa311bcd [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))
Ernie Rael18143d32023-09-04 22:30:41 +0200410 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type,
411 FALSE);
412 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type, FALSE);
413 }
414 }
415
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000416 // Could be a function reference: "obj.Func".
417 for (int i = 0; i < cl->class_obj_method_count; ++i)
418 {
419 ufunc_T *fp = cl->class_obj_methods[i];
420 // Use a separate pointer to avoid that ASAN complains about
421 // uf_name[] only being 4 characters.
422 char_u *ufname = (char_u *)fp->uf_name;
423 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar313e4722023-02-08 20:55:27 +0000424 {
425 if (type->tt_type == VAR_OBJECT
426 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
427 return generate_FUNCREF(cctx, fp, cl, i, NULL);
428 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
429 }
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000430 }
431
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000432 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
433 }
434 else
435 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000436 // load class member
437 int idx;
438 for (idx = 0; idx < cl->class_class_member_count; ++idx)
439 {
440 ocmember_T *m = &cl->class_class_members[idx];
441 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200442 {
Ernie Rael18143d32023-09-04 22:30:41 +0200443 // Note: type->tt_type = VAR_CLASS
444 if ((cl->class_flags & CLASS_INTERFACE) != 0)
445 {
446 semsg(_(e_interface_static_direct_access_str),
447 cl->class_name, m->ocm_name);
448 return FAIL;
449 }
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200450 if (*name == '_' && !inside_class(cctx, cl))
451 {
452 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
453 return FAIL;
454 }
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000455 break;
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200456 }
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000457 }
458 if (idx < cl->class_class_member_count)
459 {
460 *arg = name_end;
461 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
462 }
463 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000464 }
465
466 return FAIL;
467}
468
469/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000470 * Generate an instruction to load script-local variable "name", without the
471 * leading "s:".
472 * Also finds imported variables.
473 */
474 int
475compile_load_scriptvar(
476 cctx_T *cctx,
477 char_u *name, // variable NUL terminated
478 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100479 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000480{
481 scriptitem_T *si;
482 int idx;
483 imported_T *import;
484
485 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
486 return FAIL;
487 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000488 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000489 if (idx >= 0)
490 {
491 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
492
493 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
494 current_sctx.sc_sid, idx, sv->sv_type);
495 return OK;
496 }
497
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000498 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 if (import != NULL)
500 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000501 char_u *p = skipwhite(*end);
502 char_u *exp_name;
503 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100504 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000505 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000506 int done = FALSE;
507 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000508
509 // Need to lookup the member.
510 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000511 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000512 semsg(_(e_expected_dot_after_name_str), start);
513 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000514 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000515 ++p;
516 if (VIM_ISWHITE(*p))
517 {
518 emsg(_(e_no_white_space_allowed_after_dot));
519 return FAIL;
520 }
521
522 // isolate one name
523 exp_name = p;
524 while (eval_isnamec(*p))
525 ++p;
526 cc = *p;
527 *p = NUL;
528
Bram Moolenaard041f422022-01-12 19:54:00 +0000529 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100530 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
531 // "import autoload './dir/script.vim'" or
532 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100533 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100534
535 if (res == OK)
536 {
537 if (si->sn_autoload_prefix != NULL
538 && si->sn_state == SN_STATE_NOT_LOADED)
539 {
540 char_u *auto_name =
541 concat_str(si->sn_autoload_prefix, exp_name);
542
543 // autoload script must be loaded later, access by the autoload
544 // name. If a '(' follows it must be a function. Otherwise we
545 // don't know, it can be "script.Func".
546 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100547 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100548 else
549 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
550 vim_free(auto_name);
551 done = TRUE;
552 }
553 else if (si->sn_import_autoload
554 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100555 {
556 // If a '(' follows it must be a function. Otherwise we don't
557 // know, it can be "script.Func".
558 if (cc == '(' || paren_follows_after_expr)
559 {
560 char_u sid_name[MAX_FUNC_NAME_LEN];
561
562 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100563 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100564 }
565 else
566 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
567 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100568 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100569 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100570 else
571 {
572 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
573 cctx, NULL, TRUE);
574 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100575 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100576
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000577 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000578 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000579 if (done)
580 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000581
582 if (idx < 0)
583 {
584 if (ufunc != NULL)
585 {
586 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100587 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000588 return OK;
589 }
590 return FAIL;
591 }
592
593 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
594 import->imp_sid,
595 idx,
596 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000597 return OK;
598 }
599
Bram Moolenaard0132f42022-05-12 11:05:40 +0100600 // Can only get here if we know "name" is a script variable and not in a
601 // Vim9 script (variable is not in sn_var_vals): old style script.
602 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000603 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604}
605
606 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000607generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000608{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000609 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000610 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000612 // Reject a global non-autoload function found without the "g:" prefix.
613 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614 return FAIL;
615
616 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000617 compile_type = get_compile_type(ufunc);
618 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000619 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100621 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622}
623
624/*
625 * Compile a variable name into a load instruction.
626 * "end" points to just after the name.
627 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
628 * When "error" is FALSE do not give an error when not found.
629 */
630 int
631compile_load(
632 char_u **arg,
633 char_u *end_arg,
634 cctx_T *cctx,
635 int is_expr,
636 int error)
637{
638 type_T *type;
639 char_u *name = NULL;
640 char_u *end = end_arg;
641 int res = FAIL;
642 int prev_called_emsg = called_emsg;
643
644 if (*(*arg + 1) == ':')
645 {
646 if (end <= *arg + 2)
647 {
648 isntype_T isn_type;
649
650 // load dictionary of namespace
651 switch (**arg)
652 {
653 case 'g': isn_type = ISN_LOADGDICT; break;
654 case 'w': isn_type = ISN_LOADWDICT; break;
655 case 't': isn_type = ISN_LOADTDICT; break;
656 case 'b': isn_type = ISN_LOADBDICT; break;
657 default:
658 semsg(_(e_namespace_not_supported_str), *arg);
659 goto theend;
660 }
661 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
662 goto theend;
663 res = OK;
664 }
665 else
666 {
667 isntype_T isn_type = ISN_DROP;
668
669 // load namespaced variable
670 name = vim_strnsave(*arg + 2, end - (*arg + 2));
671 if (name == NULL)
672 return FAIL;
673
674 switch (**arg)
675 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100676 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000677 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000678 case 's': if (current_script_is_vim9())
679 {
680 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
681 *arg);
682 vim_free(name);
683 return FAIL;
684 }
Kota Kato948a3892022-08-16 16:09:59 +0100685 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000686 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 else
688 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100689 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000690 break;
691 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
692 {
693 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000694 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000695 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 else
697 isn_type = ISN_LOADG;
698 }
699 else
700 {
701 isn_type = ISN_LOADAUTO;
702 vim_free(name);
703 name = vim_strnsave(*arg, end - *arg);
704 if (name == NULL)
705 return FAIL;
706 }
707 break;
708 case 'w': isn_type = ISN_LOADW; break;
709 case 't': isn_type = ISN_LOADT; break;
710 case 'b': isn_type = ISN_LOADB; break;
711 default: // cannot happen, just in case
712 semsg(_(e_namespace_not_supported_str), *arg);
713 goto theend;
714 }
715 if (isn_type != ISN_DROP)
716 {
717 // Global, Buffer-local, Window-local and Tabpage-local
718 // variables can be defined later, thus we don't check if it
719 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000720 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721 }
722 }
723 }
724 else
725 {
726 size_t len = end - *arg;
727 int idx;
728 int gen_load = FALSE;
729 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100730 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100731 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000732
733 name = vim_strnsave(*arg, end - *arg);
734 if (name == NULL)
735 return FAIL;
736
Bram Moolenaar58b40092023-01-11 15:59:05 +0000737 if (STRCMP(name, "super") == 0
738 && cctx->ctx_ufunc != NULL
739 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
740 {
741 // super.SomeFunc() in a class function: push &t_super type, this
742 // is recognized in compile_subscript().
743 res = push_type_stack(cctx, &t_super);
744 if (*end != '.')
745 emsg(_(e_super_must_be_followed_by_dot));
746 }
747 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 {
749 script_autoload(name, FALSE);
750 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
751 }
752 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
753 == OK)
754 {
755 if (gen_load_outer == 0)
756 gen_load = TRUE;
757 }
758 else
759 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000760 lvar_T lvar;
761 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762
763 if (lookup_local(*arg, len, &lvar, cctx) == OK)
764 {
765 type = lvar.lv_type;
766 idx = lvar.lv_idx;
767 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100768 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100770 outer_loop_depth = lvar.lv_loop_depth;
771 outer_loop_idx = lvar.lv_loop_idx;
772 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 else
774 gen_load = TRUE;
775 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000776 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000777 {
778 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
779 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780 else
781 {
782 // "var" can be script-local even without using "s:" if it
783 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000784 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000785 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100786 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000787
788 // When evaluating an expression and the name starts with an
789 // uppercase letter it can be a user defined function.
790 // generate_funcref() will fail if the function can't be found.
791 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000792 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 }
794 }
795 if (gen_load)
796 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
797 if (gen_load_outer > 0)
798 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100799 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
800 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 cctx->ctx_outer_used = TRUE;
802 }
803 }
804
805 *arg = end;
806
807theend:
808 if (res == FAIL && error && called_emsg == prev_called_emsg)
809 semsg(_(e_variable_not_found_str), name);
810 vim_free(name);
811 return res;
812}
813
814/*
815 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100816 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 * Returns FAIL if compilation fails.
818 */
819 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100820compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821{
LemonBoyf3b48952022-05-05 13:53:03 +0100822 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823 garray_T save_ga = cctx->ctx_instr;
824 int expr_res;
825 int trailing_error;
826 int instr_count;
827 isn_T *instr = NULL;
828
829 // Remove the string type from the stack.
830 --cctx->ctx_type_stack.ga_len;
831
832 // Temporarily reset the list of instructions so that the jump labels are
833 // correct.
834 cctx->ctx_instr.ga_len = 0;
835 cctx->ctx_instr.ga_maxlen = 0;
836 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000837
838 // avoid peeking a next line
839 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
840 cctx->ctx_ufunc->uf_lines.ga_len = 0;
841
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000843
844 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
845
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 s = skipwhite(s);
847 trailing_error = *s != NUL;
848
849 if (expr_res == FAIL || trailing_error
850 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
851 {
852 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000853 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000854 clear_instr_ga(&cctx->ctx_instr);
855 cctx->ctx_instr = save_ga;
856 ++cctx->ctx_type_stack.ga_len;
857 return FAIL;
858 }
859
860 // Move the generated instructions into the ISN_INSTR instruction, then
861 // restore the list of instructions.
862 instr_count = cctx->ctx_instr.ga_len;
863 instr = cctx->ctx_instr.ga_data;
864 instr[instr_count].isn_type = ISN_FINISH;
865
866 cctx->ctx_instr = save_ga;
867 vim_free(isn->isn_arg.string);
868 isn->isn_type = ISN_INSTR;
869 isn->isn_arg.instr = instr;
870 return OK;
871}
872
873/*
874 * Compile the argument expressions.
875 * "arg" points to just after the "(" and is advanced to after the ")"
876 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100877 int
LemonBoyf3b48952022-05-05 13:53:03 +0100878compile_arguments(
879 char_u **arg,
880 cctx_T *cctx,
881 int *argcount,
882 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883{
884 char_u *p = *arg;
885 char_u *whitep = *arg;
886 int must_end = FALSE;
887 int instr_count;
888
889 for (;;)
890 {
891 if (may_get_next_line(whitep, &p, cctx) == FAIL)
892 goto failret;
893 if (*p == ')')
894 {
895 *arg = p + 1;
896 return OK;
897 }
898 if (must_end)
899 {
900 semsg(_(e_missing_comma_before_argument_str), p);
901 return FAIL;
902 }
903
904 instr_count = cctx->ctx_instr.ga_len;
905 if (compile_expr0(&p, cctx) == FAIL)
906 return FAIL;
907 ++*argcount;
908
LemonBoyf3b48952022-05-05 13:53:03 +0100909 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 && cctx->ctx_instr.ga_len == instr_count + 1)
911 {
912 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
913
914 // {skip} argument of searchpair() can be compiled if not empty
915 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100916 compile_string(isn, cctx, 0);
917 }
918 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
919 && cctx->ctx_instr.ga_len == instr_count + 1)
920 {
921 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
922
923 // {sub} argument of substitute() can be compiled if it starts
924 // with \=
925 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100926 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100927 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 }
929
930 if (*p != ',' && *skipwhite(p) == ',')
931 {
932 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
933 p = skipwhite(p);
934 }
935 if (*p == ',')
936 {
937 ++p;
938 if (*p != NUL && !VIM_ISWHITE(*p))
939 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
940 }
941 else
942 must_end = TRUE;
943 whitep = p;
944 p = skipwhite(p);
945 }
946failret:
947 emsg(_(e_missing_closing_paren));
948 return FAIL;
949}
950
951/*
952 * Compile a function call: name(arg1, arg2)
953 * "arg" points to "name", "arg + varlen" to the "(".
954 * "argcount_init" is 1 for "value->method()"
955 * Instructions:
956 * EVAL arg1
957 * EVAL arg2
958 * BCALL / DCALL / UCALL
959 */
960 static int
961compile_call(
962 char_u **arg,
963 size_t varlen,
964 cctx_T *cctx,
965 ppconst_T *ppconst,
966 int argcount_init)
967{
968 char_u *name = *arg;
969 char_u *p;
970 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100971 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972 char_u fname_buf[FLEN_FIXED + 1];
973 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 ufunc_T *ufunc = NULL;
975 int res = FAIL;
976 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000977 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100978 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000979 imported_T *import;
h-east2261c892023-08-16 21:49:54 +0900980 type_T *type;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000981
982 if (varlen >= sizeof(namebuf))
983 {
984 semsg(_(e_name_too_long_str), name);
985 return FAIL;
986 }
987 vim_strncpy(namebuf, *arg, varlen);
988
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000989 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000990 if (import != NULL)
991 {
992 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
993 return FAIL;
994 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000995
996 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100997 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000998 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100999 if ((varlen == 3
1000 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1002 {
1003 char_u *s = skipwhite(*arg + varlen + 1);
1004 typval_T argvars[2];
1005 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001006 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007
1008 argvars[0].v_type = VAR_UNKNOWN;
1009 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001010 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001012 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001013 s = skipwhite(s);
1014 if (*s == ')' && argvars[0].v_type == VAR_STRING
1015 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1016 || !is_has))
1017 {
1018 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1019
1020 *arg = s + 1;
1021 argvars[1].v_type = VAR_UNKNOWN;
1022 tv->v_type = VAR_NUMBER;
1023 tv->vval.v_number = 0;
1024 if (is_has)
1025 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001026 else if (is_len)
1027 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001028 else
1029 f_exists(argvars, tv);
1030 clear_tv(&argvars[0]);
1031 ++ppconst->pp_used;
1032 return OK;
1033 }
1034 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001035 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 {
1037 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1038 return FAIL;
1039 }
1040 }
1041
1042 if (generate_ppconst(cctx, ppconst) == FAIL)
1043 return FAIL;
1044
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001045 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001046 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1047
1048 // We handle the "skip" argument of searchpair() and searchpairpos()
1049 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001050 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1051 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1052 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1053 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1054 special_fn = CA_SEARCHPAIR;
1055 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1056 special_fn = CA_SUBSTITUTE;
1057 else
1058 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059
1060 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001061 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062 goto theend;
1063
h-east2261c892023-08-16 21:49:54 +09001064 type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001065 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1066 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1067 {
1068 int idx;
1069
1070 // builtin function
1071 idx = find_internal_func(name);
1072 if (idx >= 0)
1073 {
1074 if (STRCMP(name, "flatten") == 0)
1075 {
1076 emsg(_(e_cannot_use_flatten_in_vim9_script));
1077 goto theend;
1078 }
1079
1080 if (STRCMP(name, "add") == 0 && argcount == 2)
1081 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 // add() can be compiled to instructions if we know the type
1083 if (type->tt_type == VAR_LIST)
1084 {
1085 // inline "add(list, item)" so that the type can be checked
1086 res = generate_LISTAPPEND(cctx);
1087 idx = -1;
1088 }
1089 else if (type->tt_type == VAR_BLOB)
1090 {
1091 // inline "add(blob, nr)" so that the type can be checked
1092 res = generate_BLOBAPPEND(cctx);
1093 idx = -1;
1094 }
1095 }
1096
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001097 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1098 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001099 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001100 // May have the "D" or "R" flag, reserve a variable for a
1101 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001102 if (get_defer_var_idx(cctx) == 0)
1103 idx = -1;
1104 }
1105
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 if (idx >= 0)
1107 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1108 }
1109 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001110 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001111 goto theend;
1112 }
1113
Bram Moolenaar62aec932022-01-29 21:45:34 +00001114 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1115
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116 // An argument or local variable can be a function reference, this
1117 // overrules a function name.
1118 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1119 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1120 {
1121 // If we can find the function by name generate the right call.
1122 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001123 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001124 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001126 if (!func_is_global(ufunc))
1127 {
h-east2261c892023-08-16 21:49:54 +09001128 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001129 goto theend;
1130 }
1131 if (!has_g_namespace
1132 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1133 {
1134 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001135 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001136 goto theend;
1137 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001138 }
1139 }
1140
1141 // If the name is a variable, load it and use PCALL.
1142 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001143 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001145 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001146 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1147 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001148 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149
Christian Brabandtd5475e82023-08-17 23:34:09 +02001150 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001151 goto theend;
1152 }
1153
1154 // If we can find a global function by name generate the right call.
1155 if (ufunc != NULL)
1156 {
h-east2261c892023-08-16 21:49:54 +09001157 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001158 goto theend;
1159 }
1160
1161 // A global function may be defined only later. Need to figure out at
1162 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001163 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164 res = generate_UCALL(cctx, name, argcount);
1165 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001166 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167
1168theend:
1169 vim_free(tofree);
1170 return res;
1171}
1172
1173// like NAMESPACE_CHAR but with 'a' and 'l'.
1174#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1175
1176/*
1177 * Find the end of a variable or function name. Unlike find_name_end() this
1178 * does not recognize magic braces.
1179 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1180 * Return a pointer to just after the name. Equal to "arg" if there is no
1181 * valid name.
1182 */
1183 char_u *
1184to_name_end(char_u *arg, int use_namespace)
1185{
1186 char_u *p;
1187
1188 // Quick check for valid starting character.
1189 if (!eval_isnamec1(*arg))
1190 return arg;
1191
1192 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1193 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1194 // and can be used in slice "[n:]".
1195 if (*p == ':' && (p != arg + 1
1196 || !use_namespace
1197 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1198 break;
1199 return p;
1200}
1201
1202/*
1203 * Like to_name_end() but also skip over a list or dict constant.
1204 * Also accept "<SNR>123_Func".
1205 * This intentionally does not handle line continuation.
1206 */
1207 char_u *
1208to_name_const_end(char_u *arg)
1209{
1210 char_u *p = arg;
1211 typval_T rettv;
1212
1213 if (STRNCMP(p, "<SNR>", 5) == 0)
1214 p = skipdigits(p + 5);
1215 p = to_name_end(p, TRUE);
1216 if (p == arg && *arg == '[')
1217 {
1218
1219 // Can be "[1, 2, 3]->Func()".
1220 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1221 p = arg;
1222 }
1223 return p;
1224}
1225
1226/*
1227 * parse a list: [expr, expr]
1228 * "*arg" points to the '['.
1229 * ppconst->pp_is_const is set if all items are a constant.
1230 */
1231 static int
1232compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1233{
1234 char_u *p = skipwhite(*arg + 1);
1235 char_u *whitep = *arg + 1;
1236 int count = 0;
1237 int is_const;
1238 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001239 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001240
1241 for (;;)
1242 {
1243 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1244 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001245 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 return FAIL;
1247 }
1248 if (*p == ',')
1249 {
1250 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1251 return FAIL;
1252 }
1253 if (*p == ']')
1254 {
1255 ++p;
1256 break;
1257 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001258 if (must_end)
1259 {
1260 semsg(_(e_missing_comma_in_list_str), p);
1261 return FAIL;
1262 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1264 return FAIL;
1265 if (!is_const)
1266 is_all_const = FALSE;
1267 ++count;
1268 if (*p == ',')
1269 {
1270 ++p;
1271 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1272 {
1273 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1274 return FAIL;
1275 }
1276 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001277 else
1278 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001279 whitep = p;
1280 p = skipwhite(p);
1281 }
1282 *arg = p;
1283
1284 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001285 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001286}
1287
1288/*
1289 * Parse a lambda: "(arg, arg) => expr"
1290 * "*arg" points to the '('.
1291 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1292 */
1293 static int
1294compile_lambda(char_u **arg, cctx_T *cctx)
1295{
1296 int r;
1297 typval_T rettv;
1298 ufunc_T *ufunc;
1299 evalarg_T evalarg;
1300
1301 init_evalarg(&evalarg);
1302 evalarg.eval_flags = EVAL_EVALUATE;
1303 evalarg.eval_cctx = cctx;
1304
1305 // Get the funcref in "rettv".
1306 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1307 if (r != OK)
1308 {
1309 clear_evalarg(&evalarg, NULL);
1310 return r;
1311 }
1312
1313 // "rettv" will now be a partial referencing the function.
1314 ufunc = rettv.vval.v_partial->pt_func;
1315 ++ufunc->uf_refcount;
1316 clear_tv(&rettv);
1317
1318 // Compile it here to get the return type. The return type is optional,
1319 // when it's missing use t_unknown. This is recognized in
1320 // compile_return().
1321 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1322 ufunc->uf_ret_type = &t_unknown;
1323 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1324
1325 // When the outer function is compiled for profiling or debugging, the
1326 // lambda may be called without profiling or debugging. Compile it here in
1327 // the right context.
1328 if (cctx->ctx_compile_type == CT_DEBUG
1329#ifdef FEAT_PROFILE
1330 || cctx->ctx_compile_type == CT_PROFILE
1331#endif
1332 )
1333 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1334
Bram Moolenaar139575d2022-03-15 19:29:30 +00001335 // if the outer function is not compiled for debugging or profiling, this
1336 // one might be
1337 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001338 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001339 compiletype_T compile_type = get_compile_type(ufunc);
1340
1341 if (compile_type != CT_NONE)
1342 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001343 }
1344
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001345 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1346 // "*arg" may point into it. Point into the original line to avoid a
1347 // dangling pointer.
1348 if (evalarg.eval_using_cmdline)
1349 {
1350 garray_T *gap = &evalarg.eval_tofree_ga;
1351 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1352
1353 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1354 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001355 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 }
1357
1358 clear_evalarg(&evalarg, NULL);
1359
1360 if (ufunc->uf_def_status == UF_COMPILED)
1361 {
1362 // The return type will now be known.
1363 set_function_type(ufunc);
1364
1365 // The function reference count will be 1. When the ISN_FUNCREF
1366 // instruction is deleted the reference count is decremented and the
1367 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001368 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369 }
1370
1371 func_ptr_unref(ufunc);
1372 return FAIL;
1373}
1374
1375/*
1376 * Get a lambda and compile it. Uses Vim9 syntax.
1377 */
1378 int
1379get_lambda_tv_and_compile(
1380 char_u **arg,
1381 typval_T *rettv,
1382 int types_optional,
1383 evalarg_T *evalarg)
1384{
1385 int r;
1386 ufunc_T *ufunc;
1387 int save_sc_version = current_sctx.sc_version;
1388
1389 // Get the funcref in "rettv".
1390 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1391 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1392 current_sctx.sc_version = save_sc_version;
1393 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001394 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395
1396 // "rettv" will now be a partial referencing the function.
1397 ufunc = rettv->vval.v_partial->pt_func;
1398
1399 // Compile it here to get the return type. The return type is optional,
1400 // when it's missing use t_unknown. This is recognized in
1401 // compile_return().
1402 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1403 ufunc->uf_ret_type = &t_unknown;
1404 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1405
1406 if (ufunc->uf_def_status == UF_COMPILED)
1407 {
1408 // The return type will now be known.
1409 set_function_type(ufunc);
1410 return OK;
1411 }
1412 clear_tv(rettv);
1413 return FAIL;
1414}
1415
1416/*
1417 * parse a dict: {key: val, [key]: val}
1418 * "*arg" points to the '{'.
1419 * ppconst->pp_is_const is set if all item values are a constant.
1420 */
1421 static int
1422compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1423{
1424 garray_T *instr = &cctx->ctx_instr;
1425 int count = 0;
1426 dict_T *d = dict_alloc();
1427 dictitem_T *item;
1428 char_u *whitep = *arg + 1;
1429 char_u *p;
1430 int is_const;
1431 int is_all_const = TRUE; // reset when non-const encountered
1432
1433 if (d == NULL)
1434 return FAIL;
1435 if (generate_ppconst(cctx, ppconst) == FAIL)
1436 return FAIL;
1437 for (;;)
1438 {
1439 char_u *key = NULL;
1440
1441 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1442 {
1443 *arg = NULL;
1444 goto failret;
1445 }
1446
1447 if (**arg == '}')
1448 break;
1449
1450 if (**arg == '[')
1451 {
1452 isn_T *isn;
1453
1454 // {[expr]: value} uses an evaluated key.
1455 *arg = skipwhite(*arg + 1);
1456 if (compile_expr0(arg, cctx) == FAIL)
1457 return FAIL;
1458 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1459 if (isn->isn_type == ISN_PUSHNR)
1460 {
1461 char buf[NUMBUFLEN];
1462
1463 // Convert to string at compile time.
1464 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1465 isn->isn_type = ISN_PUSHS;
1466 isn->isn_arg.string = vim_strsave((char_u *)buf);
1467 }
1468 if (isn->isn_type == ISN_PUSHS)
1469 key = isn->isn_arg.string;
1470 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1471 return FAIL;
1472 *arg = skipwhite(*arg);
1473 if (**arg != ']')
1474 {
1475 emsg(_(e_missing_matching_bracket_after_dict_key));
1476 return FAIL;
1477 }
1478 ++*arg;
1479 }
1480 else
1481 {
1482 // {"name": value},
1483 // {'name': value},
1484 // {name: value} use "name" as a literal key
1485 key = get_literal_key(arg);
1486 if (key == NULL)
1487 return FAIL;
1488 if (generate_PUSHS(cctx, &key) == FAIL)
1489 return FAIL;
1490 }
1491
1492 // Check for duplicate keys, if using string keys.
1493 if (key != NULL)
1494 {
1495 item = dict_find(d, key, -1);
1496 if (item != NULL)
1497 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001498 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001499 goto failret;
1500 }
1501 item = dictitem_alloc(key);
1502 if (item != NULL)
1503 {
1504 item->di_tv.v_type = VAR_UNKNOWN;
1505 item->di_tv.v_lock = 0;
1506 if (dict_add(d, item) == FAIL)
1507 dictitem_free(item);
1508 }
1509 }
1510
1511 if (**arg != ':')
1512 {
1513 if (*skipwhite(*arg) == ':')
1514 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1515 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001516 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001517 return FAIL;
1518 }
1519 whitep = *arg + 1;
1520 if (!IS_WHITE_OR_NUL(*whitep))
1521 {
1522 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1523 return FAIL;
1524 }
1525
1526 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1527 {
1528 *arg = NULL;
1529 goto failret;
1530 }
1531
1532 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1533 return FAIL;
1534 if (!is_const)
1535 is_all_const = FALSE;
1536 ++count;
1537
1538 whitep = *arg;
1539 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1540 {
1541 *arg = NULL;
1542 goto failret;
1543 }
1544 if (**arg == '}')
1545 break;
1546 if (**arg != ',')
1547 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001548 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001549 goto failret;
1550 }
1551 if (IS_WHITE_OR_NUL(*whitep))
1552 {
1553 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1554 return FAIL;
1555 }
1556 whitep = *arg + 1;
1557 if (!IS_WHITE_OR_NUL(*whitep))
1558 {
1559 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1560 return FAIL;
1561 }
1562 *arg = skipwhite(whitep);
1563 }
1564
1565 *arg = *arg + 1;
1566
1567 // Allow for following comment, after at least one space.
1568 p = skipwhite(*arg);
1569 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1570 *arg += STRLEN(*arg);
1571
1572 dict_unref(d);
1573 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001574 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001575
1576failret:
1577 if (*arg == NULL)
1578 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001579 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001580 *arg = (char_u *)"";
1581 }
1582 dict_unref(d);
1583 return FAIL;
1584}
1585
1586/*
1587 * Compile "&option".
1588 */
1589 static int
1590compile_get_option(char_u **arg, cctx_T *cctx)
1591{
1592 typval_T rettv;
1593 char_u *start = *arg;
1594 int ret;
1595
1596 // parse the option and get the current value to get the type.
1597 rettv.v_type = VAR_UNKNOWN;
1598 ret = eval_option(arg, &rettv, TRUE);
1599 if (ret == OK)
1600 {
1601 // include the '&' in the name, eval_option() expects it.
1602 char_u *name = vim_strnsave(start, *arg - start);
1603 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1604 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1605
1606 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1607 vim_free(name);
1608 }
1609 clear_tv(&rettv);
1610
1611 return ret;
1612}
1613
1614/*
1615 * Compile "$VAR".
1616 */
1617 static int
1618compile_get_env(char_u **arg, cctx_T *cctx)
1619{
1620 char_u *start = *arg;
1621 int len;
1622 int ret;
1623 char_u *name;
1624
1625 ++*arg;
1626 len = get_env_len(arg);
1627 if (len == 0)
1628 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001629 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001630 return FAIL;
1631 }
1632
1633 // include the '$' in the name, eval_env_var() expects it.
1634 name = vim_strnsave(start, len + 1);
1635 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1636 vim_free(name);
1637 return ret;
1638}
1639
1640/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001641 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001642 */
1643 static int
1644compile_interp_string(char_u **arg, cctx_T *cctx)
1645{
1646 typval_T tv;
1647 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001648 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001649 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001650 int count = 0;
1651 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001652
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001653 // *arg is on the '$' character, move it to the first string character.
1654 ++*arg;
1655 quote = **arg;
1656 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001657
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001658 for (;;)
1659 {
1660 // Get the string up to the matching quote or to a single '{'.
1661 // "arg" is advanced to either the quote or the '{'.
1662 if (quote == '"')
1663 ret = eval_string(arg, &tv, evaluate, TRUE);
1664 else
1665 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1666 if (ret == FAIL)
1667 break;
1668 if (evaluate)
1669 {
1670 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1671 || (**arg != '{' && count == 0))
1672 {
1673 // generate non-empty string or empty string if it's the only
1674 // one
1675 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1676 return FAIL;
1677 tv.vval.v_string = NULL; // don't free it now
1678 ++count;
1679 }
1680 clear_tv(&tv);
1681 }
1682
1683 if (**arg != '{')
1684 {
1685 // found terminating quote
1686 ++*arg;
1687 break;
1688 }
1689
1690 p = compile_one_expr_in_str(*arg, cctx);
1691 if (p == NULL)
1692 {
1693 ret = FAIL;
1694 break;
1695 }
1696 ++count;
1697 *arg = p;
1698 }
LemonBoy2eaef102022-05-06 13:14:50 +01001699
1700 if (ret == FAIL || !evaluate)
1701 return ret;
1702
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001703 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1704 if (count > 1)
1705 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001706
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001707 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001708}
1709
1710/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001711 * Compile "@r".
1712 */
1713 static int
1714compile_get_register(char_u **arg, cctx_T *cctx)
1715{
1716 int ret;
1717
1718 ++*arg;
1719 if (**arg == NUL)
1720 {
1721 semsg(_(e_syntax_error_at_str), *arg - 1);
1722 return FAIL;
1723 }
1724 if (!valid_yank_reg(**arg, FALSE))
1725 {
1726 emsg_invreg(**arg);
1727 return FAIL;
1728 }
1729 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1730 ++*arg;
1731 return ret;
1732}
1733
1734/*
1735 * Apply leading '!', '-' and '+' to constant "rettv".
1736 * When "numeric_only" is TRUE do not apply '!'.
1737 */
1738 static int
1739apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1740{
1741 char_u *p = *end;
1742
1743 // this works from end to start
1744 while (p > start)
1745 {
1746 --p;
1747 if (*p == '-' || *p == '+')
1748 {
1749 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001750 if (rettv->v_type == VAR_FLOAT)
1751 {
1752 if (*p == '-')
1753 rettv->vval.v_float = -rettv->vval.v_float;
1754 }
1755 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001756 {
1757 varnumber_T val;
1758 int error = FALSE;
1759
1760 // tv_get_number_chk() accepts a string, but we don't want that
1761 // here
1762 if (check_not_string(rettv) == FAIL)
1763 return FAIL;
1764 val = tv_get_number_chk(rettv, &error);
1765 clear_tv(rettv);
1766 if (error)
1767 return FAIL;
1768 if (*p == '-')
1769 val = -val;
1770 rettv->v_type = VAR_NUMBER;
1771 rettv->vval.v_number = val;
1772 }
1773 }
1774 else if (numeric_only)
1775 {
1776 ++p;
1777 break;
1778 }
1779 else if (*p == '!')
1780 {
1781 int v = tv2bool(rettv);
1782
1783 // '!' is permissive in the type.
1784 clear_tv(rettv);
1785 rettv->v_type = VAR_BOOL;
1786 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1787 }
1788 }
1789 *end = p;
1790 return OK;
1791}
1792
1793/*
1794 * Recognize v: variables that are constants and set "rettv".
1795 */
1796 static void
1797get_vim_constant(char_u **arg, typval_T *rettv)
1798{
1799 if (STRNCMP(*arg, "v:true", 6) == 0)
1800 {
1801 rettv->v_type = VAR_BOOL;
1802 rettv->vval.v_number = VVAL_TRUE;
1803 *arg += 6;
1804 }
1805 else if (STRNCMP(*arg, "v:false", 7) == 0)
1806 {
1807 rettv->v_type = VAR_BOOL;
1808 rettv->vval.v_number = VVAL_FALSE;
1809 *arg += 7;
1810 }
1811 else if (STRNCMP(*arg, "v:null", 6) == 0)
1812 {
1813 rettv->v_type = VAR_SPECIAL;
1814 rettv->vval.v_number = VVAL_NULL;
1815 *arg += 6;
1816 }
1817 else if (STRNCMP(*arg, "v:none", 6) == 0)
1818 {
1819 rettv->v_type = VAR_SPECIAL;
1820 rettv->vval.v_number = VVAL_NONE;
1821 *arg += 6;
1822 }
1823}
1824
1825 exprtype_T
1826get_compare_type(char_u *p, int *len, int *type_is)
1827{
1828 exprtype_T type = EXPR_UNKNOWN;
1829 int i;
1830
1831 switch (p[0])
1832 {
1833 case '=': if (p[1] == '=')
1834 type = EXPR_EQUAL;
1835 else if (p[1] == '~')
1836 type = EXPR_MATCH;
1837 break;
1838 case '!': if (p[1] == '=')
1839 type = EXPR_NEQUAL;
1840 else if (p[1] == '~')
1841 type = EXPR_NOMATCH;
1842 break;
1843 case '>': if (p[1] != '=')
1844 {
1845 type = EXPR_GREATER;
1846 *len = 1;
1847 }
1848 else
1849 type = EXPR_GEQUAL;
1850 break;
1851 case '<': if (p[1] != '=')
1852 {
1853 type = EXPR_SMALLER;
1854 *len = 1;
1855 }
1856 else
1857 type = EXPR_SEQUAL;
1858 break;
1859 case 'i': if (p[1] == 's')
1860 {
1861 // "is" and "isnot"; but not a prefix of a name
1862 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1863 *len = 5;
1864 i = p[*len];
1865 if (!isalnum(i) && i != '_')
1866 {
1867 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1868 *type_is = TRUE;
1869 }
1870 }
1871 break;
1872 }
1873 return type;
1874}
1875
1876/*
1877 * Skip over an expression, ignoring most errors.
1878 */
1879 void
1880skip_expr_cctx(char_u **arg, cctx_T *cctx)
1881{
1882 evalarg_T evalarg;
1883
1884 init_evalarg(&evalarg);
1885 evalarg.eval_cctx = cctx;
1886 skip_expr(arg, &evalarg);
1887 clear_evalarg(&evalarg, NULL);
1888}
1889
1890/*
1891 * Check that the top of the type stack has a type that can be used as a
1892 * condition. Give an error and return FAIL if not.
1893 */
1894 int
1895bool_on_stack(cctx_T *cctx)
1896{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001897 type_T *type;
1898
Bram Moolenaar078a4612022-01-04 15:17:03 +00001899 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001900 if (type == &t_bool)
1901 return OK;
1902
Bram Moolenaar4913d422022-10-17 13:13:32 +01001903 if (type->tt_type == VAR_ANY
1904 || type->tt_type == VAR_UNKNOWN
1905 || type->tt_type == VAR_NUMBER
1906 || type == &t_number_bool
1907 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001908 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1909 // This requires a runtime type check.
1910 return generate_COND2BOOL(cctx);
1911
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001912 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001913}
1914
1915/*
1916 * Give the "white on both sides" error, taking the operator from "p[len]".
1917 */
1918 void
1919error_white_both(char_u *op, int len)
1920{
1921 char_u buf[10];
1922
1923 vim_strncpy(buf, op, len);
1924 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1925}
1926
1927/*
1928 * Compile code to apply '-', '+' and '!'.
1929 * When "numeric_only" is TRUE do not apply '!'.
1930 */
1931 static int
1932compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1933{
1934 char_u *p = *end;
1935
1936 // this works from end to start
1937 while (p > start)
1938 {
1939 --p;
1940 while (VIM_ISWHITE(*p))
1941 --p;
1942 if (*p == '-' || *p == '+')
1943 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001944 type_T *type = get_type_on_stack(cctx, 0);
1945 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001946 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001947 return FAIL;
1948
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001949 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001950 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1951 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001952 }
1953 else if (numeric_only)
1954 {
1955 ++p;
1956 break;
1957 }
1958 else
1959 {
1960 int invert = *p == '!';
1961
1962 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1963 {
1964 if (p[-1] == '!')
1965 invert = !invert;
1966 --p;
1967 }
1968 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1969 return FAIL;
1970 }
1971 }
1972 *end = p;
1973 return OK;
1974}
1975
1976/*
1977 * Compile "(expression)": recursive!
1978 * Return FAIL/OK.
1979 */
1980 static int
1981compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1982{
1983 int ret;
1984 char_u *p = *arg + 1;
1985
1986 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1987 return FAIL;
1988 if (ppconst->pp_used <= PPSIZE - 10)
1989 {
1990 ret = compile_expr1(arg, cctx, ppconst);
1991 }
1992 else
1993 {
1994 // Not enough space in ppconst, flush constants.
1995 if (generate_ppconst(cctx, ppconst) == FAIL)
1996 return FAIL;
1997 ret = compile_expr0(arg, cctx);
1998 }
1999 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2000 return FAIL;
2001 if (**arg == ')')
2002 ++*arg;
2003 else if (ret == OK)
2004 {
2005 emsg(_(e_missing_closing_paren));
2006 ret = FAIL;
2007 }
2008 return ret;
2009}
2010
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002011static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002012
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013/*
2014 * Compile whatever comes after "name" or "name()".
2015 * Advances "*arg" only when something was recognized.
2016 */
2017 static int
2018compile_subscript(
2019 char_u **arg,
2020 cctx_T *cctx,
2021 char_u *start_leader,
2022 char_u **end_leader,
2023 ppconst_T *ppconst)
2024{
2025 char_u *name_start = *end_leader;
2026 int keeping_dict = FALSE;
2027
2028 for (;;)
2029 {
2030 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002031 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032
2033 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2034 {
2035 char_u *next = peek_next_line_from_context(cctx);
2036
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002037 // If a following line starts with "->{", "->(" or "->X" advance to
2038 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002039 // Also if a following line starts with ".x".
2040 if (next != NULL &&
2041 ((next[0] == '-' && next[1] == '>'
2042 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002043 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002044 || ASCII_ISALPHA(*skipwhite(next + 2))))
2045 || (next[0] == '.' && eval_isdictc(next[1]))))
2046 {
2047 next = next_line_from_context(cctx, TRUE);
2048 if (next == NULL)
2049 return FAIL;
2050 *arg = next;
2051 p = skipwhite(*arg);
2052 }
2053 }
2054
2055 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2056 // is not a function call.
2057 if (**arg == '(')
2058 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059 int argcount = 0;
2060
2061 if (generate_ppconst(cctx, ppconst) == FAIL)
2062 return FAIL;
2063 ppconst->pp_is_const = FALSE;
2064
2065 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002066 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002067
2068 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002069 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002070 return FAIL;
2071 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2072 return FAIL;
2073 if (keeping_dict)
2074 {
2075 keeping_dict = FALSE;
2076 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2077 return FAIL;
2078 }
2079 }
2080 else if (*p == '-' && p[1] == '>')
2081 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002082 char_u *pstart = p;
2083 int alt;
2084 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085
Bram Moolenaarc7349932022-01-16 20:59:39 +00002086 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002087 if (generate_ppconst(cctx, ppconst) == FAIL)
2088 return FAIL;
2089 ppconst->pp_is_const = FALSE;
2090
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 // Apply the '!', '-' and '+' first:
2092 // -1.0->func() works like (-1.0)->func()
2093 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2094 return FAIL;
2095
2096 p += 2;
2097 *arg = skipwhite(p);
2098 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002099
2100 // Three alternatives handled here:
2101 // 1. "base->name(" only a name, use compile_call()
2102 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2103 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002104 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002105 alt = 2;
2106 else
2107 {
2108 // alternative 1 or 3
2109 p = *arg;
2110 if (!eval_isnamec1(*p))
2111 {
2112 semsg(_(e_trailing_characters_str), pstart);
2113 return FAIL;
2114 }
2115 if (ASCII_ISALPHA(*p) && p[1] == ':')
2116 p += 2;
2117 for ( ; eval_isnamec(*p); ++p)
2118 ;
2119 if (*p == '(')
2120 {
2121 // alternative 1
2122 alt = 1;
2123 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2124 return FAIL;
2125 }
2126 else
2127 {
2128 // Must be alternative 3, find the "(". Only works within
2129 // one line.
2130 alt = 3;
2131 paren = vim_strchr(p, '(');
2132 if (paren == NULL)
2133 {
2134 semsg(_(e_missing_parenthesis_str), *arg);
2135 return FAIL;
2136 }
2137 }
2138 }
2139
2140 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 {
2142 int argcount = 1;
2143 garray_T *stack = &cctx->ctx_type_stack;
2144 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002145 int expr_isn_start = cctx->ctx_instr.ga_len;
2146 int expr_isn_end;
2147 int arg_isn_count;
2148
Bram Moolenaarc7349932022-01-16 20:59:39 +00002149 if (alt == 2)
2150 {
2151 // Funcref call: list->(Refs[2])(arg)
2152 // or lambda: list->((arg) => expr)(arg)
2153 //
2154 // Fist compile the function expression.
2155 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2156 return FAIL;
2157 }
2158 else
2159 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002160 int fail;
2161 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002162 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002163
Bram Moolenaarc7349932022-01-16 20:59:39 +00002164 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002165
2166 // instead of using LOADG for "import.Func" use PUSHFUNC
2167 ++paren_follows_after_expr;
2168
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002169 // do not look in the next line
2170 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002171
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002172 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002173 || *skipwhite(*arg) != NUL;
2174 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002175 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002176 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002177
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002178 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002179 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002180 if (did_emsg == prev_did_emsg)
2181 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002182 return FAIL;
2183 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002184 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002185
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002186 // Compile the arguments.
2187 if (**arg != '(')
2188 {
2189 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002190 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002191 else
2192 semsg(_(e_missing_parenthesis_str), *arg);
2193 return FAIL;
2194 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002195
2196 // Remember the next instruction index, where the instructions
2197 // for arguments are being written.
2198 expr_isn_end = cctx->ctx_instr.ga_len;
2199
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002200 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002201 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2202 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002203 return FAIL;
2204
2205 // Move the instructions for the arguments to before the
2206 // instructions of the expression and move the type of the
2207 // expression after the argument types. This is what ISN_PCALL
2208 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002209 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2210 if (arg_isn_count > 0)
2211 {
2212 int expr_isn_count = expr_isn_end - expr_isn_start;
2213 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002214 type_T *decl_type;
2215 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002216
2217 if (isn == NULL)
2218 return FAIL;
2219 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2220 + expr_isn_start,
2221 sizeof(isn_T) * expr_isn_count);
2222 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2223 + expr_isn_start,
2224 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2225 sizeof(isn_T) * arg_isn_count);
2226 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2227 + expr_isn_start + arg_isn_count,
2228 isn, sizeof(isn_T) * expr_isn_count);
2229 vim_free(isn);
2230
Bram Moolenaar078a4612022-01-04 15:17:03 +00002231 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2232 type = typep->type_curr;
2233 decl_type = typep->type_decl;
2234 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2235 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2236 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002238 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2239 typep->type_curr = type;
2240 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241 }
2242
Bram Moolenaar078a4612022-01-04 15:17:03 +00002243 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002244 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2245 return FAIL;
2246 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002247
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248 if (keeping_dict)
2249 {
2250 keeping_dict = FALSE;
2251 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2252 return FAIL;
2253 }
2254 }
2255 else if (**arg == '[')
2256 {
2257 int is_slice = FALSE;
2258
2259 // list index: list[123]
2260 // dict member: dict[key]
2261 // string index: text[123]
2262 // blob index: blob[123]
2263 if (generate_ppconst(cctx, ppconst) == FAIL)
2264 return FAIL;
2265 ppconst->pp_is_const = FALSE;
2266
2267 ++p;
2268 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2269 return FAIL;
2270 if (**arg == ':')
2271 {
2272 // missing first index is equal to zero
2273 generate_PUSHNR(cctx, 0);
2274 }
2275 else
2276 {
2277 if (compile_expr0(arg, cctx) == FAIL)
2278 return FAIL;
2279 if (**arg == ':')
2280 {
2281 semsg(_(e_white_space_required_before_and_after_str_at_str),
2282 ":", *arg);
2283 return FAIL;
2284 }
2285 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2286 return FAIL;
2287 *arg = skipwhite(*arg);
2288 }
2289 if (**arg == ':')
2290 {
2291 is_slice = TRUE;
2292 ++*arg;
2293 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2294 {
2295 semsg(_(e_white_space_required_before_and_after_str_at_str),
2296 ":", *arg);
2297 return FAIL;
2298 }
2299 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2300 return FAIL;
2301 if (**arg == ']')
2302 // missing second index is equal to end of string
2303 generate_PUSHNR(cctx, -1);
2304 else
2305 {
2306 if (compile_expr0(arg, cctx) == FAIL)
2307 return FAIL;
2308 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2309 return FAIL;
2310 *arg = skipwhite(*arg);
2311 }
2312 }
2313
2314 if (**arg != ']')
2315 {
2316 emsg(_(e_missing_closing_square_brace));
2317 return FAIL;
2318 }
2319 *arg = *arg + 1;
2320
2321 if (keeping_dict)
2322 {
2323 keeping_dict = FALSE;
2324 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2325 return FAIL;
2326 }
2327 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2328 return FAIL;
2329 }
2330 else if (*p == '.' && p[1] != '.')
2331 {
2332 // dictionary member: dict.name
2333 if (generate_ppconst(cctx, ppconst) == FAIL)
2334 return FAIL;
2335 ppconst->pp_is_const = FALSE;
2336
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002337 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2338 && (type->tt_type == VAR_CLASS
2339 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002340 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002341 // class member: SomeClass.varname
2342 // class method: SomeClass.SomeMethod()
2343 // class constructor: SomeClass.new()
2344 // object member: someObject.varname, this.varname
2345 // object method: someObject.SomeMethod(), this.SomeMethod()
2346 *arg = p;
2347 if (compile_class_object_index(cctx, arg, type) == FAIL)
2348 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002350 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002351 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002352 *arg = p + 1;
2353 if (IS_WHITE_OR_NUL(**arg))
2354 {
2355 emsg(_(e_missing_name_after_dot));
2356 return FAIL;
2357 }
2358 p = *arg;
2359 if (eval_isdictc(*p))
2360 while (eval_isnamec(*p))
2361 MB_PTR_ADV(p);
2362 if (p == *arg)
2363 {
2364 semsg(_(e_syntax_error_at_str), *arg);
2365 return FAIL;
2366 }
2367 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2368 return FAIL;
2369 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2370 return FAIL;
2371 keeping_dict = TRUE;
2372 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002373 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002374 }
2375 else
2376 break;
2377 }
2378
2379 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2380 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002381 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2382 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 return FAIL;
2384
2385 return OK;
2386}
2387
2388/*
2389 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2390 * "arg" is advanced until after the expression, skipping white space.
2391 *
2392 * If the value is a constant "ppconst->pp_used" will be non-zero.
2393 * Before instructions are generated, any values in "ppconst" will generated.
2394 *
2395 * This is the compiling equivalent of eval1(), eval2(), etc.
2396 */
2397
2398/*
2399 * number number constant
2400 * 0zFFFFFFFF Blob constant
2401 * "string" string constant
2402 * 'string' literal string constant
2403 * &option-name option value
2404 * @r register contents
2405 * identifier variable value
2406 * function() function call
2407 * $VAR environment variable
2408 * (expression) nested expression
2409 * [expr, expr] List
2410 * {key: val, [key]: val} Dictionary
2411 *
2412 * Also handle:
2413 * ! in front logical NOT
2414 * - in front unary minus
2415 * + in front unary plus (ignored)
2416 * trailing (arg) funcref/partial call
2417 * trailing [] subscript in String or List
2418 * trailing .name entry in Dictionary
2419 * trailing ->name() method call
2420 */
2421 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002422compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 char_u **arg,
2424 cctx_T *cctx,
2425 ppconst_T *ppconst)
2426{
2427 char_u *start_leader, *end_leader;
2428 int ret = OK;
2429 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2430 int used_before = ppconst->pp_used;
2431
2432 ppconst->pp_is_const = FALSE;
2433
2434 /*
2435 * Skip '!', '-' and '+' characters. They are handled later.
2436 */
2437 start_leader = *arg;
2438 if (eval_leader(arg, TRUE) == FAIL)
2439 return FAIL;
2440 end_leader = *arg;
2441
2442 rettv->v_type = VAR_UNKNOWN;
2443 switch (**arg)
2444 {
2445 /*
2446 * Number constant.
2447 */
2448 case '0': // also for blob starting with 0z
2449 case '1':
2450 case '2':
2451 case '3':
2452 case '4':
2453 case '5':
2454 case '6':
2455 case '7':
2456 case '8':
2457 case '9':
2458 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2459 return FAIL;
2460 // Apply "-" and "+" just before the number now, right to
2461 // left. Matters especially when "->" follows. Stops at
2462 // '!'.
2463 if (apply_leader(rettv, TRUE,
2464 start_leader, &end_leader) == FAIL)
2465 {
2466 clear_tv(rettv);
2467 return FAIL;
2468 }
2469 break;
2470
2471 /*
2472 * String constant: "string".
2473 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002474 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002475 return FAIL;
2476 break;
2477
2478 /*
2479 * Literal string constant: 'str''ing'.
2480 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002481 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002482 return FAIL;
2483 break;
2484
2485 /*
2486 * Constant Vim variable.
2487 */
2488 case 'v': get_vim_constant(arg, rettv);
2489 ret = NOTDONE;
2490 break;
2491
2492 /*
2493 * "true" constant
2494 */
2495 case 't': if (STRNCMP(*arg, "true", 4) == 0
2496 && !eval_isnamec((*arg)[4]))
2497 {
2498 *arg += 4;
2499 rettv->v_type = VAR_BOOL;
2500 rettv->vval.v_number = VVAL_TRUE;
2501 }
2502 else
2503 ret = NOTDONE;
2504 break;
2505
2506 /*
2507 * "false" constant
2508 */
2509 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2510 && !eval_isnamec((*arg)[5]))
2511 {
2512 *arg += 5;
2513 rettv->v_type = VAR_BOOL;
2514 rettv->vval.v_number = VVAL_FALSE;
2515 }
2516 else
2517 ret = NOTDONE;
2518 break;
2519
2520 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002521 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002523 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002525 char_u *p = *arg + 4;
2526 int len;
2527
2528 for (len = 0; eval_isnamec(p[len]); ++len)
2529 ;
2530 ret = handle_predefined(*arg, len + 4, rettv);
2531 if (ret == FAIL)
2532 ret = NOTDONE;
2533 else
2534 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 }
2536 else
2537 ret = NOTDONE;
2538 break;
2539
2540 /*
2541 * List: [expr, expr]
2542 */
2543 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2544 return FAIL;
2545 ret = compile_list(arg, cctx, ppconst);
2546 break;
2547
2548 /*
2549 * Dictionary: {'key': val, 'key': val}
2550 */
2551 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2552 return FAIL;
2553 ret = compile_dict(arg, cctx, ppconst);
2554 break;
2555
2556 /*
2557 * Option value: &name
2558 */
2559 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2560 return FAIL;
2561 ret = compile_get_option(arg, cctx);
2562 break;
2563
2564 /*
2565 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002566 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002567 */
2568 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2569 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002570 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2571 ret = compile_interp_string(arg, cctx);
2572 else
2573 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002574 break;
2575
2576 /*
2577 * Register contents: @r.
2578 */
2579 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2580 return FAIL;
2581 ret = compile_get_register(arg, cctx);
2582 break;
2583 /*
2584 * nested expression: (expression).
2585 * lambda: (arg, arg) => expr
2586 * funcref: (arg, arg) => { statement }
2587 */
2588 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2589 ret = compile_lambda(arg, cctx);
2590 if (ret == NOTDONE)
2591 ret = compile_parenthesis(arg, cctx, ppconst);
2592 break;
2593
2594 default: ret = NOTDONE;
2595 break;
2596 }
2597 if (ret == FAIL)
2598 return FAIL;
2599
2600 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2601 {
2602 if (cctx->ctx_skip == SKIP_YES)
2603 clear_tv(rettv);
2604 else
2605 // A constant expression can possibly be handled compile time,
2606 // return the value instead of generating code.
2607 ++ppconst->pp_used;
2608 }
2609 else if (ret == NOTDONE)
2610 {
2611 char_u *p;
2612 int r;
2613
2614 if (!eval_isnamec1(**arg))
2615 {
2616 if (!vim9_bad_comment(*arg))
2617 {
2618 if (ends_excmd(*skipwhite(*arg)))
2619 semsg(_(e_empty_expression_str), *arg);
2620 else
2621 semsg(_(e_name_expected_str), *arg);
2622 }
2623 return FAIL;
2624 }
2625
2626 // "name" or "name()"
2627 p = to_name_end(*arg, TRUE);
2628 if (p - *arg == (size_t)1 && **arg == '_')
2629 {
2630 emsg(_(e_cannot_use_underscore_here));
2631 return FAIL;
2632 }
2633
2634 if (*p == '(')
2635 {
2636 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2637 }
2638 else
2639 {
2640 if (cctx->ctx_skip != SKIP_YES
2641 && generate_ppconst(cctx, ppconst) == FAIL)
2642 return FAIL;
2643 r = compile_load(arg, p, cctx, TRUE, TRUE);
2644 }
2645 if (r == FAIL)
2646 return FAIL;
2647 }
2648
2649 // Handle following "[]", ".member", etc.
2650 // Then deal with prefixed '-', '+' and '!', if not done already.
2651 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2652 ppconst) == FAIL)
2653 return FAIL;
2654 if (ppconst->pp_used > 0)
2655 {
2656 // apply the '!', '-' and '+' before the constant
2657 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2658 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2659 return FAIL;
2660 return OK;
2661 }
2662 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2663 return FAIL;
2664 return OK;
2665}
2666
2667/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002668 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002669 */
2670 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002671compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002672{
2673 type_T *want_type = NULL;
2674
2675 // Recognize <type>
2676 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2677 {
2678 ++*arg;
2679 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2680 if (want_type == NULL)
2681 return FAIL;
2682
2683 if (**arg != '>')
2684 {
2685 if (*skipwhite(*arg) == '>')
2686 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2687 else
2688 emsg(_(e_missing_gt));
2689 return FAIL;
2690 }
2691 ++*arg;
2692 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2693 return FAIL;
2694 }
2695
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002696 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 return FAIL;
2698
2699 if (want_type != NULL)
2700 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002701 type_T *actual;
2702 where_T where = WHERE_INIT;
2703
2704 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002705 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002706 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002707 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002708 if (need_type(actual, want_type, FALSE,
2709 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002710 return FAIL;
2711 }
2712 }
2713
2714 return OK;
2715}
2716
2717/*
2718 * * number multiplication
2719 * / number division
2720 * % number modulo
2721 */
2722 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002723compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002724{
2725 char_u *op;
2726 char_u *next;
2727 int ppconst_used = ppconst->pp_used;
2728
2729 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002730 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 return FAIL;
2732
2733 /*
2734 * Repeat computing, until no "*", "/" or "%" is following.
2735 */
2736 for (;;)
2737 {
2738 op = may_peek_next_line(cctx, *arg, &next);
2739 if (*op != '*' && *op != '/' && *op != '%')
2740 break;
2741 if (next != NULL)
2742 {
2743 *arg = next_line_from_context(cctx, TRUE);
2744 op = skipwhite(*arg);
2745 }
2746
2747 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2748 {
2749 error_white_both(op, 1);
2750 return FAIL;
2751 }
2752 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2753 return FAIL;
2754
2755 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002756 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002757 return FAIL;
2758
2759 if (ppconst->pp_used == ppconst_used + 2
2760 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2761 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2762 {
2763 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2764 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2765 varnumber_T res = 0;
2766 int failed = FALSE;
2767
2768 // both are numbers: compute the result
2769 switch (*op)
2770 {
2771 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2772 break;
2773 case '/': res = num_divide(tv1->vval.v_number,
2774 tv2->vval.v_number, &failed);
2775 break;
2776 case '%': res = num_modulus(tv1->vval.v_number,
2777 tv2->vval.v_number, &failed);
2778 break;
2779 }
2780 if (failed)
2781 return FAIL;
2782 tv1->vval.v_number = res;
2783 --ppconst->pp_used;
2784 }
2785 else
2786 {
2787 generate_ppconst(cctx, ppconst);
2788 generate_two_op(cctx, op);
2789 }
2790 }
2791
2792 return OK;
2793}
2794
2795/*
2796 * + number addition or list/blobl concatenation
2797 * - number subtraction
2798 * .. string concatenation
2799 */
2800 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002801compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002802{
2803 char_u *op;
2804 char_u *next;
2805 int oplen;
2806 int ppconst_used = ppconst->pp_used;
2807
2808 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002809 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002810 return FAIL;
2811
2812 /*
2813 * Repeat computing, until no "+", "-" or ".." is following.
2814 */
2815 for (;;)
2816 {
2817 op = may_peek_next_line(cctx, *arg, &next);
2818 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2819 break;
2820 if (op[0] == op[1] && *op != '.' && next)
2821 // Finding "++" or "--" on the next line is a separate command.
2822 // But ".." is concatenation.
2823 break;
2824 oplen = (*op == '.' ? 2 : 1);
2825 if (next != NULL)
2826 {
2827 *arg = next_line_from_context(cctx, TRUE);
2828 op = skipwhite(*arg);
2829 }
2830
2831 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2832 {
2833 error_white_both(op, oplen);
2834 return FAIL;
2835 }
2836
2837 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2838 return FAIL;
2839
2840 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002841 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002842 return FAIL;
2843
2844 if (ppconst->pp_used == ppconst_used + 2
2845 && (*op == '.'
2846 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2847 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2848 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2849 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2850 {
2851 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2852 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2853
2854 // concat/subtract/add constant numbers
2855 if (*op == '+')
2856 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2857 else if (*op == '-')
2858 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2859 else
2860 {
2861 // concatenate constant strings
2862 char_u *s1 = tv1->vval.v_string;
2863 char_u *s2 = tv2->vval.v_string;
2864 size_t len1 = STRLEN(s1);
2865
2866 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2867 if (tv1->vval.v_string == NULL)
2868 {
2869 clear_ppconst(ppconst);
2870 return FAIL;
2871 }
2872 mch_memmove(tv1->vval.v_string, s1, len1);
2873 STRCPY(tv1->vval.v_string + len1, s2);
2874 vim_free(s1);
2875 vim_free(s2);
2876 }
2877 --ppconst->pp_used;
2878 }
2879 else
2880 {
2881 generate_ppconst(cctx, ppconst);
2882 ppconst->pp_is_const = FALSE;
2883 if (*op == '.')
2884 {
2885 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2886 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2887 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002888 if (generate_CONCAT(cctx, 2) == FAIL)
2889 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002890 }
2891 else
2892 generate_two_op(cctx, op);
2893 }
2894 }
2895
2896 return OK;
2897}
2898
2899/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002900 * expr6a >> expr6b
2901 * expr6a << expr6b
2902 *
2903 * Produces instructions:
2904 * OPNR bitwise left or right shift
2905 */
2906 static int
2907compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2908{
2909 exprtype_T type = EXPR_UNKNOWN;
2910 char_u *p;
2911 char_u *next;
2912 int len = 2;
2913 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002914 isn_T *isn;
2915
2916 // get the first variable
2917 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2918 return FAIL;
2919
2920 /*
2921 * Repeat computing, until no "+", "-" or ".." is following.
2922 */
2923 for (;;)
2924 {
2925 type = EXPR_UNKNOWN;
2926
2927 p = may_peek_next_line(cctx, *arg, &next);
2928 if (p[0] == '<' && p[1] == '<')
2929 type = EXPR_LSHIFT;
2930 else if (p[0] == '>' && p[1] == '>')
2931 type = EXPR_RSHIFT;
2932
2933 if (type == EXPR_UNKNOWN)
2934 return OK;
2935
2936 // Handle a bitwise left or right shift operator
2937 if (ppconst->pp_used == ppconst_used + 1)
2938 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002939 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002940 {
2941 // left operand should be a number
2942 emsg(_(e_bitshift_ops_must_be_number));
2943 return FAIL;
2944 }
2945 }
2946 else
2947 {
2948 type_T *t = get_type_on_stack(cctx, 0);
2949
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002950 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002951 {
2952 emsg(_(e_bitshift_ops_must_be_number));
2953 return FAIL;
2954 }
2955 }
2956
2957 if (next != NULL)
2958 {
2959 *arg = next_line_from_context(cctx, TRUE);
2960 p = skipwhite(*arg);
2961 }
2962
2963 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2964 {
2965 error_white_both(p, len);
2966 return FAIL;
2967 }
2968
2969 // get the second variable
2970 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2971 return FAIL;
2972
2973 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2974 return FAIL;
2975
2976 if (ppconst->pp_used == ppconst_used + 2)
2977 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002978 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2979 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2980
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002981 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002982 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2983 {
2984 // right operand should be a positive number
2985 if (tv2->v_type != VAR_NUMBER)
2986 emsg(_(e_bitshift_ops_must_be_number));
2987 else
dundargocc57b5bc2022-11-02 13:30:51 +00002988 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002989 return FAIL;
2990 }
2991
2992 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2993 tv1->vval.v_number = 0;
2994 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002995 tv1->vval.v_number =
2996 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002997 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002998 tv1->vval.v_number =
2999 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003000 clear_tv(tv2);
3001 --ppconst->pp_used;
3002 }
3003 else
3004 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003005 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3006 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003007 {
3008 emsg(_(e_bitshift_ops_must_be_number));
3009 return FAIL;
3010 }
3011
3012 generate_ppconst(cctx, ppconst);
3013
3014 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3015 if (isn == NULL)
3016 return FAIL;
3017
3018 if (isn != NULL)
3019 isn->isn_arg.op.op_type = type;
3020 }
3021 }
3022
3023 return OK;
3024}
3025
3026/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003027 * expr5a == expr5b
3028 * expr5a =~ expr5b
3029 * expr5a != expr5b
3030 * expr5a !~ expr5b
3031 * expr5a > expr5b
3032 * expr5a >= expr5b
3033 * expr5a < expr5b
3034 * expr5a <= expr5b
3035 * expr5a is expr5b
3036 * expr5a isnot expr5b
3037 *
3038 * Produces instructions:
3039 * EVAL expr5a Push result of "expr5a"
3040 * EVAL expr5b Push result of "expr5b"
3041 * COMPARE one of the compare instructions
3042 */
3043 static int
3044compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3045{
3046 exprtype_T type = EXPR_UNKNOWN;
3047 char_u *p;
3048 char_u *next;
3049 int len = 2;
3050 int type_is = FALSE;
3051 int ppconst_used = ppconst->pp_used;
3052
3053 // get the first variable
3054 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3055 return FAIL;
3056
3057 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003058
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003059 type = get_compare_type(p, &len, &type_is);
3060
3061 /*
3062 * If there is a comparative operator, use it.
3063 */
3064 if (type != EXPR_UNKNOWN)
3065 {
3066 int ic = FALSE; // Default: do not ignore case
3067
3068 if (next != NULL)
3069 {
3070 *arg = next_line_from_context(cctx, TRUE);
3071 p = skipwhite(*arg);
3072 }
3073 if (type_is && (p[len] == '?' || p[len] == '#'))
3074 {
3075 semsg(_(e_invalid_expression_str), *arg);
3076 return FAIL;
3077 }
3078 // extra question mark appended: ignore case
3079 if (p[len] == '?')
3080 {
3081 ic = TRUE;
3082 ++len;
3083 }
3084 // extra '#' appended: match case (ignored)
3085 else if (p[len] == '#')
3086 ++len;
3087 // nothing appended: match case
3088
3089 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3090 {
3091 error_white_both(p, len);
3092 return FAIL;
3093 }
3094
3095 // get the second variable
3096 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3097 return FAIL;
3098
3099 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3100 return FAIL;
3101
3102 if (ppconst->pp_used == ppconst_used + 2)
3103 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003104 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003105 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3106 int ret;
3107
3108 // Both sides are a constant, compute the result now.
3109 // First check for a valid combination of types, this is more
3110 // strict than typval_compare().
3111 if (check_compare_types(type, tv1, tv2) == FAIL)
3112 ret = FAIL;
3113 else
3114 {
3115 ret = typval_compare(tv1, tv2, type, ic);
3116 tv1->v_type = VAR_BOOL;
3117 tv1->vval.v_number = tv1->vval.v_number
3118 ? VVAL_TRUE : VVAL_FALSE;
3119 clear_tv(tv2);
3120 --ppconst->pp_used;
3121 }
3122 return ret;
3123 }
3124
3125 generate_ppconst(cctx, ppconst);
3126 return generate_COMPARE(cctx, type, ic);
3127 }
3128
3129 return OK;
3130}
3131
3132static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3133
3134/*
3135 * Compile || or &&.
3136 */
3137 static int
3138compile_and_or(
3139 char_u **arg,
3140 cctx_T *cctx,
3141 char *op,
3142 ppconst_T *ppconst,
3143 int ppconst_used UNUSED)
3144{
3145 char_u *next;
3146 char_u *p = may_peek_next_line(cctx, *arg, &next);
3147 int opchar = *op;
3148
3149 if (p[0] == opchar && p[1] == opchar)
3150 {
3151 garray_T *instr = &cctx->ctx_instr;
3152 garray_T end_ga;
3153 int save_skip = cctx->ctx_skip;
3154
3155 /*
3156 * Repeat until there is no following "||" or "&&"
3157 */
3158 ga_init2(&end_ga, sizeof(int), 10);
3159 while (p[0] == opchar && p[1] == opchar)
3160 {
3161 long start_lnum = SOURCING_LNUM;
3162 long save_sourcing_lnum;
3163 int start_ctx_lnum = cctx->ctx_lnum;
3164 int save_lnum;
3165 int const_used;
3166 int status;
3167 jumpwhen_T jump_when = opchar == '|'
3168 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3169
3170 if (next != NULL)
3171 {
3172 *arg = next_line_from_context(cctx, TRUE);
3173 p = skipwhite(*arg);
3174 }
3175
3176 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3177 {
3178 semsg(_(e_white_space_required_before_and_after_str_at_str),
3179 op, p);
3180 ga_clear(&end_ga);
3181 return FAIL;
3182 }
3183
3184 save_sourcing_lnum = SOURCING_LNUM;
3185 SOURCING_LNUM = start_lnum;
3186 save_lnum = cctx->ctx_lnum;
3187 cctx->ctx_lnum = start_ctx_lnum;
3188
3189 status = check_ppconst_bool(ppconst);
3190 if (status != FAIL)
3191 {
3192 // Use the last ppconst if possible.
3193 if (ppconst->pp_used > 0)
3194 {
3195 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3196 int is_true = tv2bool(tv);
3197
3198 if ((is_true && opchar == '|')
3199 || (!is_true && opchar == '&'))
3200 {
3201 // For "false && expr" and "true || expr" the "expr"
3202 // does not need to be evaluated.
3203 cctx->ctx_skip = SKIP_YES;
3204 clear_tv(tv);
3205 tv->v_type = VAR_BOOL;
3206 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3207 }
3208 else
3209 {
3210 // For "true && expr" and "false || expr" only "expr"
3211 // needs to be evaluated.
3212 --ppconst->pp_used;
3213 jump_when = JUMP_NEVER;
3214 }
3215 }
3216 else
3217 {
3218 // Every part must evaluate to a bool.
3219 status = bool_on_stack(cctx);
3220 }
3221 }
3222 if (status != FAIL)
3223 status = ga_grow(&end_ga, 1);
3224 cctx->ctx_lnum = save_lnum;
3225 if (status == FAIL)
3226 {
3227 ga_clear(&end_ga);
3228 return FAIL;
3229 }
3230
3231 if (jump_when != JUMP_NEVER)
3232 {
3233 if (cctx->ctx_skip != SKIP_YES)
3234 {
3235 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3236 ++end_ga.ga_len;
3237 }
3238 generate_JUMP(cctx, jump_when, 0);
3239 }
3240
3241 // eval the next expression
3242 SOURCING_LNUM = save_sourcing_lnum;
3243 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3244 {
3245 ga_clear(&end_ga);
3246 return FAIL;
3247 }
3248
3249 const_used = ppconst->pp_used;
3250 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3251 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3252 {
3253 ga_clear(&end_ga);
3254 return FAIL;
3255 }
3256
3257 // "0 || 1" results in true, "1 && 0" results in false.
3258 if (ppconst->pp_used == const_used + 1)
3259 {
3260 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3261
3262 if (tv->v_type == VAR_NUMBER
3263 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3264 {
3265 tv->vval.v_number = tv->vval.v_number == 1
3266 ? VVAL_TRUE : VVAL_FALSE;
3267 tv->v_type = VAR_BOOL;
3268 }
3269 }
3270
3271 p = may_peek_next_line(cctx, *arg, &next);
3272 }
3273
3274 if (check_ppconst_bool(ppconst) == FAIL)
3275 {
3276 ga_clear(&end_ga);
3277 return FAIL;
3278 }
3279
3280 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3281 // Every part must evaluate to a bool.
3282 if (bool_on_stack(cctx) == FAIL)
3283 {
3284 ga_clear(&end_ga);
3285 return FAIL;
3286 }
3287
3288 if (end_ga.ga_len > 0)
3289 {
3290 // Fill in the end label in all jumps.
3291 generate_ppconst(cctx, ppconst);
3292 while (end_ga.ga_len > 0)
3293 {
3294 isn_T *isn;
3295
3296 --end_ga.ga_len;
3297 isn = ((isn_T *)instr->ga_data)
3298 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3299 isn->isn_arg.jump.jump_where = instr->ga_len;
3300 }
3301 }
3302 ga_clear(&end_ga);
3303
3304 cctx->ctx_skip = save_skip;
3305 }
3306
3307 return OK;
3308}
3309
3310/*
3311 * expr4a && expr4a && expr4a logical AND
3312 *
3313 * Produces instructions:
3314 * EVAL expr4a Push result of "expr4a"
3315 * COND2BOOL convert to bool if needed
3316 * JUMP_IF_COND_FALSE end
3317 * EVAL expr4b Push result of "expr4b"
3318 * JUMP_IF_COND_FALSE end
3319 * EVAL expr4c Push result of "expr4c"
3320 * end:
3321 */
3322 static int
3323compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3324{
3325 int ppconst_used = ppconst->pp_used;
3326
3327 // get the first variable
3328 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3329 return FAIL;
3330
3331 // || and && work almost the same
3332 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3333}
3334
3335/*
3336 * expr3a || expr3b || expr3c logical OR
3337 *
3338 * Produces instructions:
3339 * EVAL expr3a Push result of "expr3a"
3340 * COND2BOOL convert to bool if needed
3341 * JUMP_IF_COND_TRUE end
3342 * EVAL expr3b Push result of "expr3b"
3343 * JUMP_IF_COND_TRUE end
3344 * EVAL expr3c Push result of "expr3c"
3345 * end:
3346 */
3347 static int
3348compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3349{
3350 int ppconst_used = ppconst->pp_used;
3351
3352 // eval the first expression
3353 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3354 return FAIL;
3355
3356 // || and && work almost the same
3357 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3358}
3359
3360/*
3361 * Toplevel expression: expr2 ? expr1a : expr1b
3362 * Produces instructions:
3363 * EVAL expr2 Push result of "expr2"
3364 * JUMP_IF_FALSE alt jump if false
3365 * EVAL expr1a
3366 * JUMP_ALWAYS end
3367 * alt: EVAL expr1b
3368 * end:
3369 *
3370 * Toplevel expression: expr2 ?? expr1
3371 * Produces instructions:
3372 * EVAL expr2 Push result of "expr2"
3373 * JUMP_AND_KEEP_IF_TRUE end jump if true
3374 * EVAL expr1
3375 * end:
3376 */
3377 int
3378compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3379{
3380 char_u *p;
3381 int ppconst_used = ppconst->pp_used;
3382 char_u *next;
3383
3384 // Ignore all kinds of errors when not producing code.
3385 if (cctx->ctx_skip == SKIP_YES)
3386 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003387 int prev_did_emsg = did_emsg;
3388
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003389 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003390 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003391 }
3392
3393 // Evaluate the first expression.
3394 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3395 return FAIL;
3396
3397 p = may_peek_next_line(cctx, *arg, &next);
3398 if (*p == '?')
3399 {
3400 int op_falsy = p[1] == '?';
3401 garray_T *instr = &cctx->ctx_instr;
3402 garray_T *stack = &cctx->ctx_type_stack;
3403 int alt_idx = instr->ga_len;
3404 int end_idx = 0;
3405 isn_T *isn;
3406 type_T *type1 = NULL;
3407 int has_const_expr = FALSE;
3408 int const_value = FALSE;
3409 int save_skip = cctx->ctx_skip;
3410
3411 if (next != NULL)
3412 {
3413 *arg = next_line_from_context(cctx, TRUE);
3414 p = skipwhite(*arg);
3415 }
3416
3417 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3418 {
3419 semsg(_(e_white_space_required_before_and_after_str_at_str),
3420 op_falsy ? "??" : "?", p);
3421 return FAIL;
3422 }
3423
3424 if (ppconst->pp_used == ppconst_used + 1)
3425 {
3426 // the condition is a constant, we know whether the ? or the :
3427 // expression is to be evaluated.
3428 has_const_expr = TRUE;
3429 if (op_falsy)
3430 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3431 else
3432 {
3433 int error = FALSE;
3434
3435 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3436 &error);
3437 if (error)
3438 return FAIL;
3439 }
3440 cctx->ctx_skip = save_skip == SKIP_YES ||
3441 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3442
3443 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3444 // "left ?? right" and "left" is truthy: produce "left"
3445 generate_ppconst(cctx, ppconst);
3446 else
3447 {
3448 clear_tv(&ppconst->pp_tv[ppconst_used]);
3449 --ppconst->pp_used;
3450 }
3451 }
3452 else
3453 {
3454 generate_ppconst(cctx, ppconst);
3455 if (op_falsy)
3456 end_idx = instr->ga_len;
3457 generate_JUMP(cctx, op_falsy
3458 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3459 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003460 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003461 }
3462
3463 // evaluate the second expression; any type is accepted
3464 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3465 return FAIL;
3466 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3467 return FAIL;
3468
3469 if (!has_const_expr)
3470 {
3471 generate_ppconst(cctx, ppconst);
3472
3473 if (!op_falsy)
3474 {
3475 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003476 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003477 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003478
3479 end_idx = instr->ga_len;
3480 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3481
3482 // jump here from JUMP_IF_FALSE
3483 isn = ((isn_T *)instr->ga_data) + alt_idx;
3484 isn->isn_arg.jump.jump_where = instr->ga_len;
3485 }
3486 }
3487
3488 if (!op_falsy)
3489 {
3490 // Check for the ":".
3491 p = may_peek_next_line(cctx, *arg, &next);
3492 if (*p != ':')
3493 {
3494 emsg(_(e_missing_colon_after_questionmark));
3495 return FAIL;
3496 }
3497 if (next != NULL)
3498 {
3499 *arg = next_line_from_context(cctx, TRUE);
3500 p = skipwhite(*arg);
3501 }
3502
3503 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3504 {
3505 semsg(_(e_white_space_required_before_and_after_str_at_str),
3506 ":", p);
3507 return FAIL;
3508 }
3509
3510 // evaluate the third expression
3511 if (has_const_expr)
3512 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3513 ? SKIP_YES : SKIP_NOT;
3514 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3515 return FAIL;
3516 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3517 return FAIL;
3518 }
3519
3520 if (!has_const_expr)
3521 {
3522 type_T **typep;
3523
3524 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003525 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003526
3527 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003528 typep = &((((type2_T *)stack->ga_data)
3529 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003530 common_type(type1, *typep, typep, cctx->ctx_type_list);
3531
3532 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3533 isn = ((isn_T *)instr->ga_data) + end_idx;
3534 isn->isn_arg.jump.jump_where = instr->ga_len;
3535 }
3536
3537 cctx->ctx_skip = save_skip;
3538 }
3539 return OK;
3540}
3541
3542/*
3543 * Toplevel expression.
3544 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3545 * Returns OK or FAIL.
3546 */
3547 int
3548compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3549{
3550 ppconst_T ppconst;
3551
3552 CLEAR_FIELD(ppconst);
3553 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3554 {
3555 clear_ppconst(&ppconst);
3556 return FAIL;
3557 }
3558 if (is_const != NULL)
3559 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3560 if (generate_ppconst(cctx, &ppconst) == FAIL)
3561 return FAIL;
3562 return OK;
3563}
3564
3565/*
3566 * Toplevel expression.
3567 */
3568 int
3569compile_expr0(char_u **arg, cctx_T *cctx)
3570{
3571 return compile_expr0_ext(arg, cctx, NULL);
3572}
3573
3574
3575#endif // defined(FEAT_EVAL)