blob: 1b23b7f1991238286dc4dc14e88b3fdad0a3ef39 [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 Lakshmanancd7293b2023-08-27 19:18:23 +0200375 if (ufunc->uf_private && !inside_class_hierarchy(cctx, cl))
376 {
377 semsg(_(e_cannot_access_private_method_str), name);
378 return FAIL;
379 }
380
Bram Moolenaar574950d2023-01-03 19:08:50 +0000381 // Compile the arguments and call the class function or object method.
382 // The object method will know that the object is on the stack, just
383 // before the arguments.
384 *arg = skipwhite(name_end + 1);
385 int argcount = 0;
386 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
387 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000388
389 if (type->tt_type == VAR_OBJECT
390 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-east2261c892023-08-16 21:49:54 +0900391 return generate_CALL(cctx, ufunc, cl, fi, type, argcount);
392 return generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000393 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000394
395 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000396 {
397 for (int i = 0; i < cl->class_obj_member_count; ++i)
398 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000399 ocmember_T *m = &cl->class_obj_members[i];
400 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000401 {
Bram Moolenaar62a69232023-01-24 15:07:04 +0000402 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000403 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000404 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000405 return FAIL;
406 }
407
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000408 *arg = name_end;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000409 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000410 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000411 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000412 }
413 }
414
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000415 // Could be a function reference: "obj.Func".
416 for (int i = 0; i < cl->class_obj_method_count; ++i)
417 {
418 ufunc_T *fp = cl->class_obj_methods[i];
419 // Use a separate pointer to avoid that ASAN complains about
420 // uf_name[] only being 4 characters.
421 char_u *ufname = (char_u *)fp->uf_name;
422 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar313e4722023-02-08 20:55:27 +0000423 {
424 if (type->tt_type == VAR_OBJECT
425 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
426 return generate_FUNCREF(cctx, fp, cl, i, NULL);
427 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
428 }
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000429 }
430
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000431 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
432 }
433 else
434 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000435 // load class member
436 int idx;
437 for (idx = 0; idx < cl->class_class_member_count; ++idx)
438 {
439 ocmember_T *m = &cl->class_class_members[idx];
440 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
441 break;
442 }
443 if (idx < cl->class_class_member_count)
444 {
445 *arg = name_end;
446 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
447 }
448 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000449 }
450
451 return FAIL;
452}
453
454/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000455 * Generate an instruction to load script-local variable "name", without the
456 * leading "s:".
457 * Also finds imported variables.
458 */
459 int
460compile_load_scriptvar(
461 cctx_T *cctx,
462 char_u *name, // variable NUL terminated
463 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100464 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000465{
466 scriptitem_T *si;
467 int idx;
468 imported_T *import;
469
470 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
471 return FAIL;
472 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000473 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000474 if (idx >= 0)
475 {
476 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
477
478 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
479 current_sctx.sc_sid, idx, sv->sv_type);
480 return OK;
481 }
482
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000483 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000484 if (import != NULL)
485 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000486 char_u *p = skipwhite(*end);
487 char_u *exp_name;
488 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100489 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000490 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000491 int done = FALSE;
492 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000493
494 // Need to lookup the member.
495 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000496 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000497 semsg(_(e_expected_dot_after_name_str), start);
498 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000500 ++p;
501 if (VIM_ISWHITE(*p))
502 {
503 emsg(_(e_no_white_space_allowed_after_dot));
504 return FAIL;
505 }
506
507 // isolate one name
508 exp_name = p;
509 while (eval_isnamec(*p))
510 ++p;
511 cc = *p;
512 *p = NUL;
513
Bram Moolenaard041f422022-01-12 19:54:00 +0000514 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100515 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
516 // "import autoload './dir/script.vim'" or
517 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100518 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100519
520 if (res == OK)
521 {
522 if (si->sn_autoload_prefix != NULL
523 && si->sn_state == SN_STATE_NOT_LOADED)
524 {
525 char_u *auto_name =
526 concat_str(si->sn_autoload_prefix, exp_name);
527
528 // autoload script must be loaded later, access by the autoload
529 // name. If a '(' follows it must be a function. Otherwise we
530 // don't know, it can be "script.Func".
531 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100532 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100533 else
534 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
535 vim_free(auto_name);
536 done = TRUE;
537 }
538 else if (si->sn_import_autoload
539 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100540 {
541 // If a '(' follows it must be a function. Otherwise we don't
542 // know, it can be "script.Func".
543 if (cc == '(' || paren_follows_after_expr)
544 {
545 char_u sid_name[MAX_FUNC_NAME_LEN];
546
547 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100548 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100549 }
550 else
551 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
552 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100553 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100554 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100555 else
556 {
557 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
558 cctx, NULL, TRUE);
559 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100560 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100561
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000562 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000563 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000564 if (done)
565 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000566
567 if (idx < 0)
568 {
569 if (ufunc != NULL)
570 {
571 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100572 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000573 return OK;
574 }
575 return FAIL;
576 }
577
578 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
579 import->imp_sid,
580 idx,
581 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000582 return OK;
583 }
584
Bram Moolenaard0132f42022-05-12 11:05:40 +0100585 // Can only get here if we know "name" is a script variable and not in a
586 // Vim9 script (variable is not in sn_var_vals): old style script.
587 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000588 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589}
590
591 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000592generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000593{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000594 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000595 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000596
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000597 // Reject a global non-autoload function found without the "g:" prefix.
598 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000599 return FAIL;
600
601 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000602 compile_type = get_compile_type(ufunc);
603 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000604 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100606 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000607}
608
609/*
610 * Compile a variable name into a load instruction.
611 * "end" points to just after the name.
612 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
613 * When "error" is FALSE do not give an error when not found.
614 */
615 int
616compile_load(
617 char_u **arg,
618 char_u *end_arg,
619 cctx_T *cctx,
620 int is_expr,
621 int error)
622{
623 type_T *type;
624 char_u *name = NULL;
625 char_u *end = end_arg;
626 int res = FAIL;
627 int prev_called_emsg = called_emsg;
628
629 if (*(*arg + 1) == ':')
630 {
631 if (end <= *arg + 2)
632 {
633 isntype_T isn_type;
634
635 // load dictionary of namespace
636 switch (**arg)
637 {
638 case 'g': isn_type = ISN_LOADGDICT; break;
639 case 'w': isn_type = ISN_LOADWDICT; break;
640 case 't': isn_type = ISN_LOADTDICT; break;
641 case 'b': isn_type = ISN_LOADBDICT; break;
642 default:
643 semsg(_(e_namespace_not_supported_str), *arg);
644 goto theend;
645 }
646 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
647 goto theend;
648 res = OK;
649 }
650 else
651 {
652 isntype_T isn_type = ISN_DROP;
653
654 // load namespaced variable
655 name = vim_strnsave(*arg + 2, end - (*arg + 2));
656 if (name == NULL)
657 return FAIL;
658
659 switch (**arg)
660 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100661 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000663 case 's': if (current_script_is_vim9())
664 {
665 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
666 *arg);
667 vim_free(name);
668 return FAIL;
669 }
Kota Kato948a3892022-08-16 16:09:59 +0100670 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000671 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000672 else
673 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100674 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000675 break;
676 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
677 {
678 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000679 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000680 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000681 else
682 isn_type = ISN_LOADG;
683 }
684 else
685 {
686 isn_type = ISN_LOADAUTO;
687 vim_free(name);
688 name = vim_strnsave(*arg, end - *arg);
689 if (name == NULL)
690 return FAIL;
691 }
692 break;
693 case 'w': isn_type = ISN_LOADW; break;
694 case 't': isn_type = ISN_LOADT; break;
695 case 'b': isn_type = ISN_LOADB; break;
696 default: // cannot happen, just in case
697 semsg(_(e_namespace_not_supported_str), *arg);
698 goto theend;
699 }
700 if (isn_type != ISN_DROP)
701 {
702 // Global, Buffer-local, Window-local and Tabpage-local
703 // variables can be defined later, thus we don't check if it
704 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000705 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000706 }
707 }
708 }
709 else
710 {
711 size_t len = end - *arg;
712 int idx;
713 int gen_load = FALSE;
714 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100715 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100716 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717
718 name = vim_strnsave(*arg, end - *arg);
719 if (name == NULL)
720 return FAIL;
721
Bram Moolenaar58b40092023-01-11 15:59:05 +0000722 if (STRCMP(name, "super") == 0
723 && cctx->ctx_ufunc != NULL
724 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
725 {
726 // super.SomeFunc() in a class function: push &t_super type, this
727 // is recognized in compile_subscript().
728 res = push_type_stack(cctx, &t_super);
729 if (*end != '.')
730 emsg(_(e_super_must_be_followed_by_dot));
731 }
732 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733 {
734 script_autoload(name, FALSE);
735 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
736 }
737 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
738 == OK)
739 {
740 if (gen_load_outer == 0)
741 gen_load = TRUE;
742 }
743 else
744 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000745 lvar_T lvar;
746 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747
748 if (lookup_local(*arg, len, &lvar, cctx) == OK)
749 {
750 type = lvar.lv_type;
751 idx = lvar.lv_idx;
752 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100753 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000754 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100755 outer_loop_depth = lvar.lv_loop_depth;
756 outer_loop_idx = lvar.lv_loop_idx;
757 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758 else
759 gen_load = TRUE;
760 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000761 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000762 {
763 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
764 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765 else
766 {
767 // "var" can be script-local even without using "s:" if it
768 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000769 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000770 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100771 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772
773 // When evaluating an expression and the name starts with an
774 // uppercase letter it can be a user defined function.
775 // generate_funcref() will fail if the function can't be found.
776 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000777 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000778 }
779 }
780 if (gen_load)
781 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
782 if (gen_load_outer > 0)
783 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100784 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
785 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786 cctx->ctx_outer_used = TRUE;
787 }
788 }
789
790 *arg = end;
791
792theend:
793 if (res == FAIL && error && called_emsg == prev_called_emsg)
794 semsg(_(e_variable_not_found_str), name);
795 vim_free(name);
796 return res;
797}
798
799/*
800 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100801 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802 * Returns FAIL if compilation fails.
803 */
804 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100805compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806{
LemonBoyf3b48952022-05-05 13:53:03 +0100807 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000808 garray_T save_ga = cctx->ctx_instr;
809 int expr_res;
810 int trailing_error;
811 int instr_count;
812 isn_T *instr = NULL;
813
814 // Remove the string type from the stack.
815 --cctx->ctx_type_stack.ga_len;
816
817 // Temporarily reset the list of instructions so that the jump labels are
818 // correct.
819 cctx->ctx_instr.ga_len = 0;
820 cctx->ctx_instr.ga_maxlen = 0;
821 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000822
823 // avoid peeking a next line
824 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
825 cctx->ctx_ufunc->uf_lines.ga_len = 0;
826
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000828
829 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
830
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 s = skipwhite(s);
832 trailing_error = *s != NUL;
833
834 if (expr_res == FAIL || trailing_error
835 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
836 {
837 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000838 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000839 clear_instr_ga(&cctx->ctx_instr);
840 cctx->ctx_instr = save_ga;
841 ++cctx->ctx_type_stack.ga_len;
842 return FAIL;
843 }
844
845 // Move the generated instructions into the ISN_INSTR instruction, then
846 // restore the list of instructions.
847 instr_count = cctx->ctx_instr.ga_len;
848 instr = cctx->ctx_instr.ga_data;
849 instr[instr_count].isn_type = ISN_FINISH;
850
851 cctx->ctx_instr = save_ga;
852 vim_free(isn->isn_arg.string);
853 isn->isn_type = ISN_INSTR;
854 isn->isn_arg.instr = instr;
855 return OK;
856}
857
858/*
859 * Compile the argument expressions.
860 * "arg" points to just after the "(" and is advanced to after the ")"
861 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100862 int
LemonBoyf3b48952022-05-05 13:53:03 +0100863compile_arguments(
864 char_u **arg,
865 cctx_T *cctx,
866 int *argcount,
867 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868{
869 char_u *p = *arg;
870 char_u *whitep = *arg;
871 int must_end = FALSE;
872 int instr_count;
873
874 for (;;)
875 {
876 if (may_get_next_line(whitep, &p, cctx) == FAIL)
877 goto failret;
878 if (*p == ')')
879 {
880 *arg = p + 1;
881 return OK;
882 }
883 if (must_end)
884 {
885 semsg(_(e_missing_comma_before_argument_str), p);
886 return FAIL;
887 }
888
889 instr_count = cctx->ctx_instr.ga_len;
890 if (compile_expr0(&p, cctx) == FAIL)
891 return FAIL;
892 ++*argcount;
893
LemonBoyf3b48952022-05-05 13:53:03 +0100894 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 && cctx->ctx_instr.ga_len == instr_count + 1)
896 {
897 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
898
899 // {skip} argument of searchpair() can be compiled if not empty
900 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100901 compile_string(isn, cctx, 0);
902 }
903 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
904 && cctx->ctx_instr.ga_len == instr_count + 1)
905 {
906 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
907
908 // {sub} argument of substitute() can be compiled if it starts
909 // with \=
910 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100911 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100912 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000913 }
914
915 if (*p != ',' && *skipwhite(p) == ',')
916 {
917 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
918 p = skipwhite(p);
919 }
920 if (*p == ',')
921 {
922 ++p;
923 if (*p != NUL && !VIM_ISWHITE(*p))
924 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
925 }
926 else
927 must_end = TRUE;
928 whitep = p;
929 p = skipwhite(p);
930 }
931failret:
932 emsg(_(e_missing_closing_paren));
933 return FAIL;
934}
935
936/*
937 * Compile a function call: name(arg1, arg2)
938 * "arg" points to "name", "arg + varlen" to the "(".
939 * "argcount_init" is 1 for "value->method()"
940 * Instructions:
941 * EVAL arg1
942 * EVAL arg2
943 * BCALL / DCALL / UCALL
944 */
945 static int
946compile_call(
947 char_u **arg,
948 size_t varlen,
949 cctx_T *cctx,
950 ppconst_T *ppconst,
951 int argcount_init)
952{
953 char_u *name = *arg;
954 char_u *p;
955 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100956 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000957 char_u fname_buf[FLEN_FIXED + 1];
958 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000959 ufunc_T *ufunc = NULL;
960 int res = FAIL;
961 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000962 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100963 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000964 imported_T *import;
h-east2261c892023-08-16 21:49:54 +0900965 type_T *type;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000966
967 if (varlen >= sizeof(namebuf))
968 {
969 semsg(_(e_name_too_long_str), name);
970 return FAIL;
971 }
972 vim_strncpy(namebuf, *arg, varlen);
973
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000974 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000975 if (import != NULL)
976 {
977 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
978 return FAIL;
979 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000980
981 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100982 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100984 if ((varlen == 3
985 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
987 {
988 char_u *s = skipwhite(*arg + varlen + 1);
989 typval_T argvars[2];
990 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100991 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000992
993 argvars[0].v_type = VAR_UNKNOWN;
994 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100995 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100997 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000998 s = skipwhite(s);
999 if (*s == ')' && argvars[0].v_type == VAR_STRING
1000 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1001 || !is_has))
1002 {
1003 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1004
1005 *arg = s + 1;
1006 argvars[1].v_type = VAR_UNKNOWN;
1007 tv->v_type = VAR_NUMBER;
1008 tv->vval.v_number = 0;
1009 if (is_has)
1010 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001011 else if (is_len)
1012 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001013 else
1014 f_exists(argvars, tv);
1015 clear_tv(&argvars[0]);
1016 ++ppconst->pp_used;
1017 return OK;
1018 }
1019 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001020 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001021 {
1022 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1023 return FAIL;
1024 }
1025 }
1026
1027 if (generate_ppconst(cctx, ppconst) == FAIL)
1028 return FAIL;
1029
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001030 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001031 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1032
1033 // We handle the "skip" argument of searchpair() and searchpairpos()
1034 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001035 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1036 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1037 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1038 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1039 special_fn = CA_SEARCHPAIR;
1040 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1041 special_fn = CA_SUBSTITUTE;
1042 else
1043 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044
1045 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001046 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001047 goto theend;
1048
h-east2261c892023-08-16 21:49:54 +09001049 type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001050 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1051 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1052 {
1053 int idx;
1054
1055 // builtin function
1056 idx = find_internal_func(name);
1057 if (idx >= 0)
1058 {
1059 if (STRCMP(name, "flatten") == 0)
1060 {
1061 emsg(_(e_cannot_use_flatten_in_vim9_script));
1062 goto theend;
1063 }
1064
1065 if (STRCMP(name, "add") == 0 && argcount == 2)
1066 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001067 // add() can be compiled to instructions if we know the type
1068 if (type->tt_type == VAR_LIST)
1069 {
1070 // inline "add(list, item)" so that the type can be checked
1071 res = generate_LISTAPPEND(cctx);
1072 idx = -1;
1073 }
1074 else if (type->tt_type == VAR_BLOB)
1075 {
1076 // inline "add(blob, nr)" so that the type can be checked
1077 res = generate_BLOBAPPEND(cctx);
1078 idx = -1;
1079 }
1080 }
1081
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001082 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1083 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001084 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001085 // May have the "D" or "R" flag, reserve a variable for a
1086 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001087 if (get_defer_var_idx(cctx) == 0)
1088 idx = -1;
1089 }
1090
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001091 if (idx >= 0)
1092 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1093 }
1094 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001095 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 goto theend;
1097 }
1098
Bram Moolenaar62aec932022-01-29 21:45:34 +00001099 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1100
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101 // An argument or local variable can be a function reference, this
1102 // overrules a function name.
1103 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1104 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1105 {
1106 // If we can find the function by name generate the right call.
1107 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001108 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001109 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001110 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001111 if (!func_is_global(ufunc))
1112 {
h-east2261c892023-08-16 21:49:54 +09001113 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001114 goto theend;
1115 }
1116 if (!has_g_namespace
1117 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1118 {
1119 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001120 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001121 goto theend;
1122 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001123 }
1124 }
1125
1126 // If the name is a variable, load it and use PCALL.
1127 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001128 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001130 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1132 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001133 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001134
Christian Brabandtd5475e82023-08-17 23:34:09 +02001135 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001136 goto theend;
1137 }
1138
1139 // If we can find a global function by name generate the right call.
1140 if (ufunc != NULL)
1141 {
h-east2261c892023-08-16 21:49:54 +09001142 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143 goto theend;
1144 }
1145
1146 // A global function may be defined only later. Need to figure out at
1147 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001148 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149 res = generate_UCALL(cctx, name, argcount);
1150 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001151 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001152
1153theend:
1154 vim_free(tofree);
1155 return res;
1156}
1157
1158// like NAMESPACE_CHAR but with 'a' and 'l'.
1159#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1160
1161/*
1162 * Find the end of a variable or function name. Unlike find_name_end() this
1163 * does not recognize magic braces.
1164 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1165 * Return a pointer to just after the name. Equal to "arg" if there is no
1166 * valid name.
1167 */
1168 char_u *
1169to_name_end(char_u *arg, int use_namespace)
1170{
1171 char_u *p;
1172
1173 // Quick check for valid starting character.
1174 if (!eval_isnamec1(*arg))
1175 return arg;
1176
1177 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1178 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1179 // and can be used in slice "[n:]".
1180 if (*p == ':' && (p != arg + 1
1181 || !use_namespace
1182 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1183 break;
1184 return p;
1185}
1186
1187/*
1188 * Like to_name_end() but also skip over a list or dict constant.
1189 * Also accept "<SNR>123_Func".
1190 * This intentionally does not handle line continuation.
1191 */
1192 char_u *
1193to_name_const_end(char_u *arg)
1194{
1195 char_u *p = arg;
1196 typval_T rettv;
1197
1198 if (STRNCMP(p, "<SNR>", 5) == 0)
1199 p = skipdigits(p + 5);
1200 p = to_name_end(p, TRUE);
1201 if (p == arg && *arg == '[')
1202 {
1203
1204 // Can be "[1, 2, 3]->Func()".
1205 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1206 p = arg;
1207 }
1208 return p;
1209}
1210
1211/*
1212 * parse a list: [expr, expr]
1213 * "*arg" points to the '['.
1214 * ppconst->pp_is_const is set if all items are a constant.
1215 */
1216 static int
1217compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1218{
1219 char_u *p = skipwhite(*arg + 1);
1220 char_u *whitep = *arg + 1;
1221 int count = 0;
1222 int is_const;
1223 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001224 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001225
1226 for (;;)
1227 {
1228 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1229 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001230 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231 return FAIL;
1232 }
1233 if (*p == ',')
1234 {
1235 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1236 return FAIL;
1237 }
1238 if (*p == ']')
1239 {
1240 ++p;
1241 break;
1242 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001243 if (must_end)
1244 {
1245 semsg(_(e_missing_comma_in_list_str), p);
1246 return FAIL;
1247 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001248 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1249 return FAIL;
1250 if (!is_const)
1251 is_all_const = FALSE;
1252 ++count;
1253 if (*p == ',')
1254 {
1255 ++p;
1256 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1257 {
1258 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1259 return FAIL;
1260 }
1261 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001262 else
1263 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264 whitep = p;
1265 p = skipwhite(p);
1266 }
1267 *arg = p;
1268
1269 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001270 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001271}
1272
1273/*
1274 * Parse a lambda: "(arg, arg) => expr"
1275 * "*arg" points to the '('.
1276 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1277 */
1278 static int
1279compile_lambda(char_u **arg, cctx_T *cctx)
1280{
1281 int r;
1282 typval_T rettv;
1283 ufunc_T *ufunc;
1284 evalarg_T evalarg;
1285
1286 init_evalarg(&evalarg);
1287 evalarg.eval_flags = EVAL_EVALUATE;
1288 evalarg.eval_cctx = cctx;
1289
1290 // Get the funcref in "rettv".
1291 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1292 if (r != OK)
1293 {
1294 clear_evalarg(&evalarg, NULL);
1295 return r;
1296 }
1297
1298 // "rettv" will now be a partial referencing the function.
1299 ufunc = rettv.vval.v_partial->pt_func;
1300 ++ufunc->uf_refcount;
1301 clear_tv(&rettv);
1302
1303 // Compile it here to get the return type. The return type is optional,
1304 // when it's missing use t_unknown. This is recognized in
1305 // compile_return().
1306 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1307 ufunc->uf_ret_type = &t_unknown;
1308 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1309
1310 // When the outer function is compiled for profiling or debugging, the
1311 // lambda may be called without profiling or debugging. Compile it here in
1312 // the right context.
1313 if (cctx->ctx_compile_type == CT_DEBUG
1314#ifdef FEAT_PROFILE
1315 || cctx->ctx_compile_type == CT_PROFILE
1316#endif
1317 )
1318 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1319
Bram Moolenaar139575d2022-03-15 19:29:30 +00001320 // if the outer function is not compiled for debugging or profiling, this
1321 // one might be
1322 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001323 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001324 compiletype_T compile_type = get_compile_type(ufunc);
1325
1326 if (compile_type != CT_NONE)
1327 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001328 }
1329
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001330 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1331 // "*arg" may point into it. Point into the original line to avoid a
1332 // dangling pointer.
1333 if (evalarg.eval_using_cmdline)
1334 {
1335 garray_T *gap = &evalarg.eval_tofree_ga;
1336 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1337
1338 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1339 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001340 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341 }
1342
1343 clear_evalarg(&evalarg, NULL);
1344
1345 if (ufunc->uf_def_status == UF_COMPILED)
1346 {
1347 // The return type will now be known.
1348 set_function_type(ufunc);
1349
1350 // The function reference count will be 1. When the ISN_FUNCREF
1351 // instruction is deleted the reference count is decremented and the
1352 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001353 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001354 }
1355
1356 func_ptr_unref(ufunc);
1357 return FAIL;
1358}
1359
1360/*
1361 * Get a lambda and compile it. Uses Vim9 syntax.
1362 */
1363 int
1364get_lambda_tv_and_compile(
1365 char_u **arg,
1366 typval_T *rettv,
1367 int types_optional,
1368 evalarg_T *evalarg)
1369{
1370 int r;
1371 ufunc_T *ufunc;
1372 int save_sc_version = current_sctx.sc_version;
1373
1374 // Get the funcref in "rettv".
1375 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1376 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1377 current_sctx.sc_version = save_sc_version;
1378 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001379 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001380
1381 // "rettv" will now be a partial referencing the function.
1382 ufunc = rettv->vval.v_partial->pt_func;
1383
1384 // Compile it here to get the return type. The return type is optional,
1385 // when it's missing use t_unknown. This is recognized in
1386 // compile_return().
1387 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1388 ufunc->uf_ret_type = &t_unknown;
1389 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1390
1391 if (ufunc->uf_def_status == UF_COMPILED)
1392 {
1393 // The return type will now be known.
1394 set_function_type(ufunc);
1395 return OK;
1396 }
1397 clear_tv(rettv);
1398 return FAIL;
1399}
1400
1401/*
1402 * parse a dict: {key: val, [key]: val}
1403 * "*arg" points to the '{'.
1404 * ppconst->pp_is_const is set if all item values are a constant.
1405 */
1406 static int
1407compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1408{
1409 garray_T *instr = &cctx->ctx_instr;
1410 int count = 0;
1411 dict_T *d = dict_alloc();
1412 dictitem_T *item;
1413 char_u *whitep = *arg + 1;
1414 char_u *p;
1415 int is_const;
1416 int is_all_const = TRUE; // reset when non-const encountered
1417
1418 if (d == NULL)
1419 return FAIL;
1420 if (generate_ppconst(cctx, ppconst) == FAIL)
1421 return FAIL;
1422 for (;;)
1423 {
1424 char_u *key = NULL;
1425
1426 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1427 {
1428 *arg = NULL;
1429 goto failret;
1430 }
1431
1432 if (**arg == '}')
1433 break;
1434
1435 if (**arg == '[')
1436 {
1437 isn_T *isn;
1438
1439 // {[expr]: value} uses an evaluated key.
1440 *arg = skipwhite(*arg + 1);
1441 if (compile_expr0(arg, cctx) == FAIL)
1442 return FAIL;
1443 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1444 if (isn->isn_type == ISN_PUSHNR)
1445 {
1446 char buf[NUMBUFLEN];
1447
1448 // Convert to string at compile time.
1449 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1450 isn->isn_type = ISN_PUSHS;
1451 isn->isn_arg.string = vim_strsave((char_u *)buf);
1452 }
1453 if (isn->isn_type == ISN_PUSHS)
1454 key = isn->isn_arg.string;
1455 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1456 return FAIL;
1457 *arg = skipwhite(*arg);
1458 if (**arg != ']')
1459 {
1460 emsg(_(e_missing_matching_bracket_after_dict_key));
1461 return FAIL;
1462 }
1463 ++*arg;
1464 }
1465 else
1466 {
1467 // {"name": value},
1468 // {'name': value},
1469 // {name: value} use "name" as a literal key
1470 key = get_literal_key(arg);
1471 if (key == NULL)
1472 return FAIL;
1473 if (generate_PUSHS(cctx, &key) == FAIL)
1474 return FAIL;
1475 }
1476
1477 // Check for duplicate keys, if using string keys.
1478 if (key != NULL)
1479 {
1480 item = dict_find(d, key, -1);
1481 if (item != NULL)
1482 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001483 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001484 goto failret;
1485 }
1486 item = dictitem_alloc(key);
1487 if (item != NULL)
1488 {
1489 item->di_tv.v_type = VAR_UNKNOWN;
1490 item->di_tv.v_lock = 0;
1491 if (dict_add(d, item) == FAIL)
1492 dictitem_free(item);
1493 }
1494 }
1495
1496 if (**arg != ':')
1497 {
1498 if (*skipwhite(*arg) == ':')
1499 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1500 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001501 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502 return FAIL;
1503 }
1504 whitep = *arg + 1;
1505 if (!IS_WHITE_OR_NUL(*whitep))
1506 {
1507 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1508 return FAIL;
1509 }
1510
1511 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1512 {
1513 *arg = NULL;
1514 goto failret;
1515 }
1516
1517 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1518 return FAIL;
1519 if (!is_const)
1520 is_all_const = FALSE;
1521 ++count;
1522
1523 whitep = *arg;
1524 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1525 {
1526 *arg = NULL;
1527 goto failret;
1528 }
1529 if (**arg == '}')
1530 break;
1531 if (**arg != ',')
1532 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001533 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001534 goto failret;
1535 }
1536 if (IS_WHITE_OR_NUL(*whitep))
1537 {
1538 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1539 return FAIL;
1540 }
1541 whitep = *arg + 1;
1542 if (!IS_WHITE_OR_NUL(*whitep))
1543 {
1544 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1545 return FAIL;
1546 }
1547 *arg = skipwhite(whitep);
1548 }
1549
1550 *arg = *arg + 1;
1551
1552 // Allow for following comment, after at least one space.
1553 p = skipwhite(*arg);
1554 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1555 *arg += STRLEN(*arg);
1556
1557 dict_unref(d);
1558 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001559 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001560
1561failret:
1562 if (*arg == NULL)
1563 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001564 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001565 *arg = (char_u *)"";
1566 }
1567 dict_unref(d);
1568 return FAIL;
1569}
1570
1571/*
1572 * Compile "&option".
1573 */
1574 static int
1575compile_get_option(char_u **arg, cctx_T *cctx)
1576{
1577 typval_T rettv;
1578 char_u *start = *arg;
1579 int ret;
1580
1581 // parse the option and get the current value to get the type.
1582 rettv.v_type = VAR_UNKNOWN;
1583 ret = eval_option(arg, &rettv, TRUE);
1584 if (ret == OK)
1585 {
1586 // include the '&' in the name, eval_option() expects it.
1587 char_u *name = vim_strnsave(start, *arg - start);
1588 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1589 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1590
1591 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1592 vim_free(name);
1593 }
1594 clear_tv(&rettv);
1595
1596 return ret;
1597}
1598
1599/*
1600 * Compile "$VAR".
1601 */
1602 static int
1603compile_get_env(char_u **arg, cctx_T *cctx)
1604{
1605 char_u *start = *arg;
1606 int len;
1607 int ret;
1608 char_u *name;
1609
1610 ++*arg;
1611 len = get_env_len(arg);
1612 if (len == 0)
1613 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001614 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001615 return FAIL;
1616 }
1617
1618 // include the '$' in the name, eval_env_var() expects it.
1619 name = vim_strnsave(start, len + 1);
1620 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1621 vim_free(name);
1622 return ret;
1623}
1624
1625/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001626 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001627 */
1628 static int
1629compile_interp_string(char_u **arg, cctx_T *cctx)
1630{
1631 typval_T tv;
1632 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001633 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001634 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001635 int count = 0;
1636 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001637
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001638 // *arg is on the '$' character, move it to the first string character.
1639 ++*arg;
1640 quote = **arg;
1641 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001642
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001643 for (;;)
1644 {
1645 // Get the string up to the matching quote or to a single '{'.
1646 // "arg" is advanced to either the quote or the '{'.
1647 if (quote == '"')
1648 ret = eval_string(arg, &tv, evaluate, TRUE);
1649 else
1650 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1651 if (ret == FAIL)
1652 break;
1653 if (evaluate)
1654 {
1655 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1656 || (**arg != '{' && count == 0))
1657 {
1658 // generate non-empty string or empty string if it's the only
1659 // one
1660 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1661 return FAIL;
1662 tv.vval.v_string = NULL; // don't free it now
1663 ++count;
1664 }
1665 clear_tv(&tv);
1666 }
1667
1668 if (**arg != '{')
1669 {
1670 // found terminating quote
1671 ++*arg;
1672 break;
1673 }
1674
1675 p = compile_one_expr_in_str(*arg, cctx);
1676 if (p == NULL)
1677 {
1678 ret = FAIL;
1679 break;
1680 }
1681 ++count;
1682 *arg = p;
1683 }
LemonBoy2eaef102022-05-06 13:14:50 +01001684
1685 if (ret == FAIL || !evaluate)
1686 return ret;
1687
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001688 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1689 if (count > 1)
1690 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001691
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001692 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001693}
1694
1695/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001696 * Compile "@r".
1697 */
1698 static int
1699compile_get_register(char_u **arg, cctx_T *cctx)
1700{
1701 int ret;
1702
1703 ++*arg;
1704 if (**arg == NUL)
1705 {
1706 semsg(_(e_syntax_error_at_str), *arg - 1);
1707 return FAIL;
1708 }
1709 if (!valid_yank_reg(**arg, FALSE))
1710 {
1711 emsg_invreg(**arg);
1712 return FAIL;
1713 }
1714 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1715 ++*arg;
1716 return ret;
1717}
1718
1719/*
1720 * Apply leading '!', '-' and '+' to constant "rettv".
1721 * When "numeric_only" is TRUE do not apply '!'.
1722 */
1723 static int
1724apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1725{
1726 char_u *p = *end;
1727
1728 // this works from end to start
1729 while (p > start)
1730 {
1731 --p;
1732 if (*p == '-' || *p == '+')
1733 {
1734 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001735 if (rettv->v_type == VAR_FLOAT)
1736 {
1737 if (*p == '-')
1738 rettv->vval.v_float = -rettv->vval.v_float;
1739 }
1740 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001741 {
1742 varnumber_T val;
1743 int error = FALSE;
1744
1745 // tv_get_number_chk() accepts a string, but we don't want that
1746 // here
1747 if (check_not_string(rettv) == FAIL)
1748 return FAIL;
1749 val = tv_get_number_chk(rettv, &error);
1750 clear_tv(rettv);
1751 if (error)
1752 return FAIL;
1753 if (*p == '-')
1754 val = -val;
1755 rettv->v_type = VAR_NUMBER;
1756 rettv->vval.v_number = val;
1757 }
1758 }
1759 else if (numeric_only)
1760 {
1761 ++p;
1762 break;
1763 }
1764 else if (*p == '!')
1765 {
1766 int v = tv2bool(rettv);
1767
1768 // '!' is permissive in the type.
1769 clear_tv(rettv);
1770 rettv->v_type = VAR_BOOL;
1771 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1772 }
1773 }
1774 *end = p;
1775 return OK;
1776}
1777
1778/*
1779 * Recognize v: variables that are constants and set "rettv".
1780 */
1781 static void
1782get_vim_constant(char_u **arg, typval_T *rettv)
1783{
1784 if (STRNCMP(*arg, "v:true", 6) == 0)
1785 {
1786 rettv->v_type = VAR_BOOL;
1787 rettv->vval.v_number = VVAL_TRUE;
1788 *arg += 6;
1789 }
1790 else if (STRNCMP(*arg, "v:false", 7) == 0)
1791 {
1792 rettv->v_type = VAR_BOOL;
1793 rettv->vval.v_number = VVAL_FALSE;
1794 *arg += 7;
1795 }
1796 else if (STRNCMP(*arg, "v:null", 6) == 0)
1797 {
1798 rettv->v_type = VAR_SPECIAL;
1799 rettv->vval.v_number = VVAL_NULL;
1800 *arg += 6;
1801 }
1802 else if (STRNCMP(*arg, "v:none", 6) == 0)
1803 {
1804 rettv->v_type = VAR_SPECIAL;
1805 rettv->vval.v_number = VVAL_NONE;
1806 *arg += 6;
1807 }
1808}
1809
1810 exprtype_T
1811get_compare_type(char_u *p, int *len, int *type_is)
1812{
1813 exprtype_T type = EXPR_UNKNOWN;
1814 int i;
1815
1816 switch (p[0])
1817 {
1818 case '=': if (p[1] == '=')
1819 type = EXPR_EQUAL;
1820 else if (p[1] == '~')
1821 type = EXPR_MATCH;
1822 break;
1823 case '!': if (p[1] == '=')
1824 type = EXPR_NEQUAL;
1825 else if (p[1] == '~')
1826 type = EXPR_NOMATCH;
1827 break;
1828 case '>': if (p[1] != '=')
1829 {
1830 type = EXPR_GREATER;
1831 *len = 1;
1832 }
1833 else
1834 type = EXPR_GEQUAL;
1835 break;
1836 case '<': if (p[1] != '=')
1837 {
1838 type = EXPR_SMALLER;
1839 *len = 1;
1840 }
1841 else
1842 type = EXPR_SEQUAL;
1843 break;
1844 case 'i': if (p[1] == 's')
1845 {
1846 // "is" and "isnot"; but not a prefix of a name
1847 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1848 *len = 5;
1849 i = p[*len];
1850 if (!isalnum(i) && i != '_')
1851 {
1852 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1853 *type_is = TRUE;
1854 }
1855 }
1856 break;
1857 }
1858 return type;
1859}
1860
1861/*
1862 * Skip over an expression, ignoring most errors.
1863 */
1864 void
1865skip_expr_cctx(char_u **arg, cctx_T *cctx)
1866{
1867 evalarg_T evalarg;
1868
1869 init_evalarg(&evalarg);
1870 evalarg.eval_cctx = cctx;
1871 skip_expr(arg, &evalarg);
1872 clear_evalarg(&evalarg, NULL);
1873}
1874
1875/*
1876 * Check that the top of the type stack has a type that can be used as a
1877 * condition. Give an error and return FAIL if not.
1878 */
1879 int
1880bool_on_stack(cctx_T *cctx)
1881{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001882 type_T *type;
1883
Bram Moolenaar078a4612022-01-04 15:17:03 +00001884 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001885 if (type == &t_bool)
1886 return OK;
1887
Bram Moolenaar4913d422022-10-17 13:13:32 +01001888 if (type->tt_type == VAR_ANY
1889 || type->tt_type == VAR_UNKNOWN
1890 || type->tt_type == VAR_NUMBER
1891 || type == &t_number_bool
1892 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001893 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1894 // This requires a runtime type check.
1895 return generate_COND2BOOL(cctx);
1896
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001897 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001898}
1899
1900/*
1901 * Give the "white on both sides" error, taking the operator from "p[len]".
1902 */
1903 void
1904error_white_both(char_u *op, int len)
1905{
1906 char_u buf[10];
1907
1908 vim_strncpy(buf, op, len);
1909 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1910}
1911
1912/*
1913 * Compile code to apply '-', '+' and '!'.
1914 * When "numeric_only" is TRUE do not apply '!'.
1915 */
1916 static int
1917compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1918{
1919 char_u *p = *end;
1920
1921 // this works from end to start
1922 while (p > start)
1923 {
1924 --p;
1925 while (VIM_ISWHITE(*p))
1926 --p;
1927 if (*p == '-' || *p == '+')
1928 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001929 type_T *type = get_type_on_stack(cctx, 0);
1930 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001931 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001932 return FAIL;
1933
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001934 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001935 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1936 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001937 }
1938 else if (numeric_only)
1939 {
1940 ++p;
1941 break;
1942 }
1943 else
1944 {
1945 int invert = *p == '!';
1946
1947 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1948 {
1949 if (p[-1] == '!')
1950 invert = !invert;
1951 --p;
1952 }
1953 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1954 return FAIL;
1955 }
1956 }
1957 *end = p;
1958 return OK;
1959}
1960
1961/*
1962 * Compile "(expression)": recursive!
1963 * Return FAIL/OK.
1964 */
1965 static int
1966compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1967{
1968 int ret;
1969 char_u *p = *arg + 1;
1970
1971 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1972 return FAIL;
1973 if (ppconst->pp_used <= PPSIZE - 10)
1974 {
1975 ret = compile_expr1(arg, cctx, ppconst);
1976 }
1977 else
1978 {
1979 // Not enough space in ppconst, flush constants.
1980 if (generate_ppconst(cctx, ppconst) == FAIL)
1981 return FAIL;
1982 ret = compile_expr0(arg, cctx);
1983 }
1984 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1985 return FAIL;
1986 if (**arg == ')')
1987 ++*arg;
1988 else if (ret == OK)
1989 {
1990 emsg(_(e_missing_closing_paren));
1991 ret = FAIL;
1992 }
1993 return ret;
1994}
1995
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001996static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001997
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001998/*
1999 * Compile whatever comes after "name" or "name()".
2000 * Advances "*arg" only when something was recognized.
2001 */
2002 static int
2003compile_subscript(
2004 char_u **arg,
2005 cctx_T *cctx,
2006 char_u *start_leader,
2007 char_u **end_leader,
2008 ppconst_T *ppconst)
2009{
2010 char_u *name_start = *end_leader;
2011 int keeping_dict = FALSE;
2012
2013 for (;;)
2014 {
2015 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002016 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002017
2018 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2019 {
2020 char_u *next = peek_next_line_from_context(cctx);
2021
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002022 // If a following line starts with "->{", "->(" or "->X" advance to
2023 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024 // Also if a following line starts with ".x".
2025 if (next != NULL &&
2026 ((next[0] == '-' && next[1] == '>'
2027 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002028 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029 || ASCII_ISALPHA(*skipwhite(next + 2))))
2030 || (next[0] == '.' && eval_isdictc(next[1]))))
2031 {
2032 next = next_line_from_context(cctx, TRUE);
2033 if (next == NULL)
2034 return FAIL;
2035 *arg = next;
2036 p = skipwhite(*arg);
2037 }
2038 }
2039
2040 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2041 // is not a function call.
2042 if (**arg == '(')
2043 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002044 int argcount = 0;
2045
2046 if (generate_ppconst(cctx, ppconst) == FAIL)
2047 return FAIL;
2048 ppconst->pp_is_const = FALSE;
2049
2050 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002051 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002052
2053 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002054 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002055 return FAIL;
2056 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2057 return FAIL;
2058 if (keeping_dict)
2059 {
2060 keeping_dict = FALSE;
2061 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2062 return FAIL;
2063 }
2064 }
2065 else if (*p == '-' && p[1] == '>')
2066 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002067 char_u *pstart = p;
2068 int alt;
2069 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002070
Bram Moolenaarc7349932022-01-16 20:59:39 +00002071 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072 if (generate_ppconst(cctx, ppconst) == FAIL)
2073 return FAIL;
2074 ppconst->pp_is_const = FALSE;
2075
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002076 // Apply the '!', '-' and '+' first:
2077 // -1.0->func() works like (-1.0)->func()
2078 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2079 return FAIL;
2080
2081 p += 2;
2082 *arg = skipwhite(p);
2083 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002084
2085 // Three alternatives handled here:
2086 // 1. "base->name(" only a name, use compile_call()
2087 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2088 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002089 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002090 alt = 2;
2091 else
2092 {
2093 // alternative 1 or 3
2094 p = *arg;
2095 if (!eval_isnamec1(*p))
2096 {
2097 semsg(_(e_trailing_characters_str), pstart);
2098 return FAIL;
2099 }
2100 if (ASCII_ISALPHA(*p) && p[1] == ':')
2101 p += 2;
2102 for ( ; eval_isnamec(*p); ++p)
2103 ;
2104 if (*p == '(')
2105 {
2106 // alternative 1
2107 alt = 1;
2108 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2109 return FAIL;
2110 }
2111 else
2112 {
2113 // Must be alternative 3, find the "(". Only works within
2114 // one line.
2115 alt = 3;
2116 paren = vim_strchr(p, '(');
2117 if (paren == NULL)
2118 {
2119 semsg(_(e_missing_parenthesis_str), *arg);
2120 return FAIL;
2121 }
2122 }
2123 }
2124
2125 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002126 {
2127 int argcount = 1;
2128 garray_T *stack = &cctx->ctx_type_stack;
2129 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002130 int expr_isn_start = cctx->ctx_instr.ga_len;
2131 int expr_isn_end;
2132 int arg_isn_count;
2133
Bram Moolenaarc7349932022-01-16 20:59:39 +00002134 if (alt == 2)
2135 {
2136 // Funcref call: list->(Refs[2])(arg)
2137 // or lambda: list->((arg) => expr)(arg)
2138 //
2139 // Fist compile the function expression.
2140 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2141 return FAIL;
2142 }
2143 else
2144 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002145 int fail;
2146 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002147 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002148
Bram Moolenaarc7349932022-01-16 20:59:39 +00002149 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002150
2151 // instead of using LOADG for "import.Func" use PUSHFUNC
2152 ++paren_follows_after_expr;
2153
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002154 // do not look in the next line
2155 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002156
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002157 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002158 || *skipwhite(*arg) != NUL;
2159 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002160 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002161 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002162
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002163 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002164 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002165 if (did_emsg == prev_did_emsg)
2166 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002167 return FAIL;
2168 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002169 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002170
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002171 // Compile the arguments.
2172 if (**arg != '(')
2173 {
2174 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002175 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002176 else
2177 semsg(_(e_missing_parenthesis_str), *arg);
2178 return FAIL;
2179 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002180
2181 // Remember the next instruction index, where the instructions
2182 // for arguments are being written.
2183 expr_isn_end = cctx->ctx_instr.ga_len;
2184
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002185 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002186 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2187 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002188 return FAIL;
2189
2190 // Move the instructions for the arguments to before the
2191 // instructions of the expression and move the type of the
2192 // expression after the argument types. This is what ISN_PCALL
2193 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002194 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2195 if (arg_isn_count > 0)
2196 {
2197 int expr_isn_count = expr_isn_end - expr_isn_start;
2198 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002199 type_T *decl_type;
2200 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201
2202 if (isn == NULL)
2203 return FAIL;
2204 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2205 + expr_isn_start,
2206 sizeof(isn_T) * expr_isn_count);
2207 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2208 + expr_isn_start,
2209 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2210 sizeof(isn_T) * arg_isn_count);
2211 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2212 + expr_isn_start + arg_isn_count,
2213 isn, sizeof(isn_T) * expr_isn_count);
2214 vim_free(isn);
2215
Bram Moolenaar078a4612022-01-04 15:17:03 +00002216 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2217 type = typep->type_curr;
2218 decl_type = typep->type_decl;
2219 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2220 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2221 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002222 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002223 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2224 typep->type_curr = type;
2225 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 }
2227
Bram Moolenaar078a4612022-01-04 15:17:03 +00002228 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2230 return FAIL;
2231 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002232
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233 if (keeping_dict)
2234 {
2235 keeping_dict = FALSE;
2236 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2237 return FAIL;
2238 }
2239 }
2240 else if (**arg == '[')
2241 {
2242 int is_slice = FALSE;
2243
2244 // list index: list[123]
2245 // dict member: dict[key]
2246 // string index: text[123]
2247 // blob index: blob[123]
2248 if (generate_ppconst(cctx, ppconst) == FAIL)
2249 return FAIL;
2250 ppconst->pp_is_const = FALSE;
2251
2252 ++p;
2253 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2254 return FAIL;
2255 if (**arg == ':')
2256 {
2257 // missing first index is equal to zero
2258 generate_PUSHNR(cctx, 0);
2259 }
2260 else
2261 {
2262 if (compile_expr0(arg, cctx) == FAIL)
2263 return FAIL;
2264 if (**arg == ':')
2265 {
2266 semsg(_(e_white_space_required_before_and_after_str_at_str),
2267 ":", *arg);
2268 return FAIL;
2269 }
2270 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2271 return FAIL;
2272 *arg = skipwhite(*arg);
2273 }
2274 if (**arg == ':')
2275 {
2276 is_slice = TRUE;
2277 ++*arg;
2278 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2279 {
2280 semsg(_(e_white_space_required_before_and_after_str_at_str),
2281 ":", *arg);
2282 return FAIL;
2283 }
2284 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2285 return FAIL;
2286 if (**arg == ']')
2287 // missing second index is equal to end of string
2288 generate_PUSHNR(cctx, -1);
2289 else
2290 {
2291 if (compile_expr0(arg, cctx) == FAIL)
2292 return FAIL;
2293 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2294 return FAIL;
2295 *arg = skipwhite(*arg);
2296 }
2297 }
2298
2299 if (**arg != ']')
2300 {
2301 emsg(_(e_missing_closing_square_brace));
2302 return FAIL;
2303 }
2304 *arg = *arg + 1;
2305
2306 if (keeping_dict)
2307 {
2308 keeping_dict = FALSE;
2309 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2310 return FAIL;
2311 }
2312 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2313 return FAIL;
2314 }
2315 else if (*p == '.' && p[1] != '.')
2316 {
2317 // dictionary member: dict.name
2318 if (generate_ppconst(cctx, ppconst) == FAIL)
2319 return FAIL;
2320 ppconst->pp_is_const = FALSE;
2321
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002322 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2323 && (type->tt_type == VAR_CLASS
2324 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002326 // class member: SomeClass.varname
2327 // class method: SomeClass.SomeMethod()
2328 // class constructor: SomeClass.new()
2329 // object member: someObject.varname, this.varname
2330 // object method: someObject.SomeMethod(), this.SomeMethod()
2331 *arg = p;
2332 if (compile_class_object_index(cctx, arg, type) == FAIL)
2333 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002335 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002337 *arg = p + 1;
2338 if (IS_WHITE_OR_NUL(**arg))
2339 {
2340 emsg(_(e_missing_name_after_dot));
2341 return FAIL;
2342 }
2343 p = *arg;
2344 if (eval_isdictc(*p))
2345 while (eval_isnamec(*p))
2346 MB_PTR_ADV(p);
2347 if (p == *arg)
2348 {
2349 semsg(_(e_syntax_error_at_str), *arg);
2350 return FAIL;
2351 }
2352 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2353 return FAIL;
2354 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2355 return FAIL;
2356 keeping_dict = TRUE;
2357 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002358 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359 }
2360 else
2361 break;
2362 }
2363
2364 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2365 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002366 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2367 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002368 return FAIL;
2369
2370 return OK;
2371}
2372
2373/*
2374 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2375 * "arg" is advanced until after the expression, skipping white space.
2376 *
2377 * If the value is a constant "ppconst->pp_used" will be non-zero.
2378 * Before instructions are generated, any values in "ppconst" will generated.
2379 *
2380 * This is the compiling equivalent of eval1(), eval2(), etc.
2381 */
2382
2383/*
2384 * number number constant
2385 * 0zFFFFFFFF Blob constant
2386 * "string" string constant
2387 * 'string' literal string constant
2388 * &option-name option value
2389 * @r register contents
2390 * identifier variable value
2391 * function() function call
2392 * $VAR environment variable
2393 * (expression) nested expression
2394 * [expr, expr] List
2395 * {key: val, [key]: val} Dictionary
2396 *
2397 * Also handle:
2398 * ! in front logical NOT
2399 * - in front unary minus
2400 * + in front unary plus (ignored)
2401 * trailing (arg) funcref/partial call
2402 * trailing [] subscript in String or List
2403 * trailing .name entry in Dictionary
2404 * trailing ->name() method call
2405 */
2406 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002407compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002408 char_u **arg,
2409 cctx_T *cctx,
2410 ppconst_T *ppconst)
2411{
2412 char_u *start_leader, *end_leader;
2413 int ret = OK;
2414 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2415 int used_before = ppconst->pp_used;
2416
2417 ppconst->pp_is_const = FALSE;
2418
2419 /*
2420 * Skip '!', '-' and '+' characters. They are handled later.
2421 */
2422 start_leader = *arg;
2423 if (eval_leader(arg, TRUE) == FAIL)
2424 return FAIL;
2425 end_leader = *arg;
2426
2427 rettv->v_type = VAR_UNKNOWN;
2428 switch (**arg)
2429 {
2430 /*
2431 * Number constant.
2432 */
2433 case '0': // also for blob starting with 0z
2434 case '1':
2435 case '2':
2436 case '3':
2437 case '4':
2438 case '5':
2439 case '6':
2440 case '7':
2441 case '8':
2442 case '9':
2443 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2444 return FAIL;
2445 // Apply "-" and "+" just before the number now, right to
2446 // left. Matters especially when "->" follows. Stops at
2447 // '!'.
2448 if (apply_leader(rettv, TRUE,
2449 start_leader, &end_leader) == FAIL)
2450 {
2451 clear_tv(rettv);
2452 return FAIL;
2453 }
2454 break;
2455
2456 /*
2457 * String constant: "string".
2458 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002459 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002460 return FAIL;
2461 break;
2462
2463 /*
2464 * Literal string constant: 'str''ing'.
2465 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002466 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 return FAIL;
2468 break;
2469
2470 /*
2471 * Constant Vim variable.
2472 */
2473 case 'v': get_vim_constant(arg, rettv);
2474 ret = NOTDONE;
2475 break;
2476
2477 /*
2478 * "true" constant
2479 */
2480 case 't': if (STRNCMP(*arg, "true", 4) == 0
2481 && !eval_isnamec((*arg)[4]))
2482 {
2483 *arg += 4;
2484 rettv->v_type = VAR_BOOL;
2485 rettv->vval.v_number = VVAL_TRUE;
2486 }
2487 else
2488 ret = NOTDONE;
2489 break;
2490
2491 /*
2492 * "false" constant
2493 */
2494 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2495 && !eval_isnamec((*arg)[5]))
2496 {
2497 *arg += 5;
2498 rettv->v_type = VAR_BOOL;
2499 rettv->vval.v_number = VVAL_FALSE;
2500 }
2501 else
2502 ret = NOTDONE;
2503 break;
2504
2505 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002506 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002507 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002508 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002509 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002510 char_u *p = *arg + 4;
2511 int len;
2512
2513 for (len = 0; eval_isnamec(p[len]); ++len)
2514 ;
2515 ret = handle_predefined(*arg, len + 4, rettv);
2516 if (ret == FAIL)
2517 ret = NOTDONE;
2518 else
2519 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 }
2521 else
2522 ret = NOTDONE;
2523 break;
2524
2525 /*
2526 * List: [expr, expr]
2527 */
2528 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2529 return FAIL;
2530 ret = compile_list(arg, cctx, ppconst);
2531 break;
2532
2533 /*
2534 * Dictionary: {'key': val, 'key': val}
2535 */
2536 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2537 return FAIL;
2538 ret = compile_dict(arg, cctx, ppconst);
2539 break;
2540
2541 /*
2542 * Option value: &name
2543 */
2544 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2545 return FAIL;
2546 ret = compile_get_option(arg, cctx);
2547 break;
2548
2549 /*
2550 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002551 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002552 */
2553 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2554 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002555 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2556 ret = compile_interp_string(arg, cctx);
2557 else
2558 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559 break;
2560
2561 /*
2562 * Register contents: @r.
2563 */
2564 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2565 return FAIL;
2566 ret = compile_get_register(arg, cctx);
2567 break;
2568 /*
2569 * nested expression: (expression).
2570 * lambda: (arg, arg) => expr
2571 * funcref: (arg, arg) => { statement }
2572 */
2573 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2574 ret = compile_lambda(arg, cctx);
2575 if (ret == NOTDONE)
2576 ret = compile_parenthesis(arg, cctx, ppconst);
2577 break;
2578
2579 default: ret = NOTDONE;
2580 break;
2581 }
2582 if (ret == FAIL)
2583 return FAIL;
2584
2585 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2586 {
2587 if (cctx->ctx_skip == SKIP_YES)
2588 clear_tv(rettv);
2589 else
2590 // A constant expression can possibly be handled compile time,
2591 // return the value instead of generating code.
2592 ++ppconst->pp_used;
2593 }
2594 else if (ret == NOTDONE)
2595 {
2596 char_u *p;
2597 int r;
2598
2599 if (!eval_isnamec1(**arg))
2600 {
2601 if (!vim9_bad_comment(*arg))
2602 {
2603 if (ends_excmd(*skipwhite(*arg)))
2604 semsg(_(e_empty_expression_str), *arg);
2605 else
2606 semsg(_(e_name_expected_str), *arg);
2607 }
2608 return FAIL;
2609 }
2610
2611 // "name" or "name()"
2612 p = to_name_end(*arg, TRUE);
2613 if (p - *arg == (size_t)1 && **arg == '_')
2614 {
2615 emsg(_(e_cannot_use_underscore_here));
2616 return FAIL;
2617 }
2618
2619 if (*p == '(')
2620 {
2621 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2622 }
2623 else
2624 {
2625 if (cctx->ctx_skip != SKIP_YES
2626 && generate_ppconst(cctx, ppconst) == FAIL)
2627 return FAIL;
2628 r = compile_load(arg, p, cctx, TRUE, TRUE);
2629 }
2630 if (r == FAIL)
2631 return FAIL;
2632 }
2633
2634 // Handle following "[]", ".member", etc.
2635 // Then deal with prefixed '-', '+' and '!', if not done already.
2636 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2637 ppconst) == FAIL)
2638 return FAIL;
2639 if (ppconst->pp_used > 0)
2640 {
2641 // apply the '!', '-' and '+' before the constant
2642 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2643 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2644 return FAIL;
2645 return OK;
2646 }
2647 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2648 return FAIL;
2649 return OK;
2650}
2651
2652/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002653 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002654 */
2655 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002656compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002657{
2658 type_T *want_type = NULL;
2659
2660 // Recognize <type>
2661 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2662 {
2663 ++*arg;
2664 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2665 if (want_type == NULL)
2666 return FAIL;
2667
2668 if (**arg != '>')
2669 {
2670 if (*skipwhite(*arg) == '>')
2671 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2672 else
2673 emsg(_(e_missing_gt));
2674 return FAIL;
2675 }
2676 ++*arg;
2677 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2678 return FAIL;
2679 }
2680
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002681 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002682 return FAIL;
2683
2684 if (want_type != NULL)
2685 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002686 type_T *actual;
2687 where_T where = WHERE_INIT;
2688
2689 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002690 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002691 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002692 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002693 if (need_type(actual, want_type, FALSE,
2694 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002695 return FAIL;
2696 }
2697 }
2698
2699 return OK;
2700}
2701
2702/*
2703 * * number multiplication
2704 * / number division
2705 * % number modulo
2706 */
2707 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002708compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002709{
2710 char_u *op;
2711 char_u *next;
2712 int ppconst_used = ppconst->pp_used;
2713
2714 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002715 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716 return FAIL;
2717
2718 /*
2719 * Repeat computing, until no "*", "/" or "%" is following.
2720 */
2721 for (;;)
2722 {
2723 op = may_peek_next_line(cctx, *arg, &next);
2724 if (*op != '*' && *op != '/' && *op != '%')
2725 break;
2726 if (next != NULL)
2727 {
2728 *arg = next_line_from_context(cctx, TRUE);
2729 op = skipwhite(*arg);
2730 }
2731
2732 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2733 {
2734 error_white_both(op, 1);
2735 return FAIL;
2736 }
2737 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2738 return FAIL;
2739
2740 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002741 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002742 return FAIL;
2743
2744 if (ppconst->pp_used == ppconst_used + 2
2745 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2746 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2747 {
2748 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2749 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2750 varnumber_T res = 0;
2751 int failed = FALSE;
2752
2753 // both are numbers: compute the result
2754 switch (*op)
2755 {
2756 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2757 break;
2758 case '/': res = num_divide(tv1->vval.v_number,
2759 tv2->vval.v_number, &failed);
2760 break;
2761 case '%': res = num_modulus(tv1->vval.v_number,
2762 tv2->vval.v_number, &failed);
2763 break;
2764 }
2765 if (failed)
2766 return FAIL;
2767 tv1->vval.v_number = res;
2768 --ppconst->pp_used;
2769 }
2770 else
2771 {
2772 generate_ppconst(cctx, ppconst);
2773 generate_two_op(cctx, op);
2774 }
2775 }
2776
2777 return OK;
2778}
2779
2780/*
2781 * + number addition or list/blobl concatenation
2782 * - number subtraction
2783 * .. string concatenation
2784 */
2785 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002786compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002787{
2788 char_u *op;
2789 char_u *next;
2790 int oplen;
2791 int ppconst_used = ppconst->pp_used;
2792
2793 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002794 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002795 return FAIL;
2796
2797 /*
2798 * Repeat computing, until no "+", "-" or ".." is following.
2799 */
2800 for (;;)
2801 {
2802 op = may_peek_next_line(cctx, *arg, &next);
2803 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2804 break;
2805 if (op[0] == op[1] && *op != '.' && next)
2806 // Finding "++" or "--" on the next line is a separate command.
2807 // But ".." is concatenation.
2808 break;
2809 oplen = (*op == '.' ? 2 : 1);
2810 if (next != NULL)
2811 {
2812 *arg = next_line_from_context(cctx, TRUE);
2813 op = skipwhite(*arg);
2814 }
2815
2816 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2817 {
2818 error_white_both(op, oplen);
2819 return FAIL;
2820 }
2821
2822 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2823 return FAIL;
2824
2825 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002826 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002827 return FAIL;
2828
2829 if (ppconst->pp_used == ppconst_used + 2
2830 && (*op == '.'
2831 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2832 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2833 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2834 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2835 {
2836 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2837 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2838
2839 // concat/subtract/add constant numbers
2840 if (*op == '+')
2841 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2842 else if (*op == '-')
2843 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2844 else
2845 {
2846 // concatenate constant strings
2847 char_u *s1 = tv1->vval.v_string;
2848 char_u *s2 = tv2->vval.v_string;
2849 size_t len1 = STRLEN(s1);
2850
2851 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2852 if (tv1->vval.v_string == NULL)
2853 {
2854 clear_ppconst(ppconst);
2855 return FAIL;
2856 }
2857 mch_memmove(tv1->vval.v_string, s1, len1);
2858 STRCPY(tv1->vval.v_string + len1, s2);
2859 vim_free(s1);
2860 vim_free(s2);
2861 }
2862 --ppconst->pp_used;
2863 }
2864 else
2865 {
2866 generate_ppconst(cctx, ppconst);
2867 ppconst->pp_is_const = FALSE;
2868 if (*op == '.')
2869 {
2870 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2871 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2872 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002873 if (generate_CONCAT(cctx, 2) == FAIL)
2874 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002875 }
2876 else
2877 generate_two_op(cctx, op);
2878 }
2879 }
2880
2881 return OK;
2882}
2883
2884/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002885 * expr6a >> expr6b
2886 * expr6a << expr6b
2887 *
2888 * Produces instructions:
2889 * OPNR bitwise left or right shift
2890 */
2891 static int
2892compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2893{
2894 exprtype_T type = EXPR_UNKNOWN;
2895 char_u *p;
2896 char_u *next;
2897 int len = 2;
2898 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002899 isn_T *isn;
2900
2901 // get the first variable
2902 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2903 return FAIL;
2904
2905 /*
2906 * Repeat computing, until no "+", "-" or ".." is following.
2907 */
2908 for (;;)
2909 {
2910 type = EXPR_UNKNOWN;
2911
2912 p = may_peek_next_line(cctx, *arg, &next);
2913 if (p[0] == '<' && p[1] == '<')
2914 type = EXPR_LSHIFT;
2915 else if (p[0] == '>' && p[1] == '>')
2916 type = EXPR_RSHIFT;
2917
2918 if (type == EXPR_UNKNOWN)
2919 return OK;
2920
2921 // Handle a bitwise left or right shift operator
2922 if (ppconst->pp_used == ppconst_used + 1)
2923 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002924 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002925 {
2926 // left operand should be a number
2927 emsg(_(e_bitshift_ops_must_be_number));
2928 return FAIL;
2929 }
2930 }
2931 else
2932 {
2933 type_T *t = get_type_on_stack(cctx, 0);
2934
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002935 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002936 {
2937 emsg(_(e_bitshift_ops_must_be_number));
2938 return FAIL;
2939 }
2940 }
2941
2942 if (next != NULL)
2943 {
2944 *arg = next_line_from_context(cctx, TRUE);
2945 p = skipwhite(*arg);
2946 }
2947
2948 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2949 {
2950 error_white_both(p, len);
2951 return FAIL;
2952 }
2953
2954 // get the second variable
2955 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2956 return FAIL;
2957
2958 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2959 return FAIL;
2960
2961 if (ppconst->pp_used == ppconst_used + 2)
2962 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002963 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2964 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2965
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002966 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002967 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2968 {
2969 // right operand should be a positive number
2970 if (tv2->v_type != VAR_NUMBER)
2971 emsg(_(e_bitshift_ops_must_be_number));
2972 else
dundargocc57b5bc2022-11-02 13:30:51 +00002973 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002974 return FAIL;
2975 }
2976
2977 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2978 tv1->vval.v_number = 0;
2979 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002980 tv1->vval.v_number =
2981 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002982 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002983 tv1->vval.v_number =
2984 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002985 clear_tv(tv2);
2986 --ppconst->pp_used;
2987 }
2988 else
2989 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002990 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2991 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002992 {
2993 emsg(_(e_bitshift_ops_must_be_number));
2994 return FAIL;
2995 }
2996
2997 generate_ppconst(cctx, ppconst);
2998
2999 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3000 if (isn == NULL)
3001 return FAIL;
3002
3003 if (isn != NULL)
3004 isn->isn_arg.op.op_type = type;
3005 }
3006 }
3007
3008 return OK;
3009}
3010
3011/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003012 * expr5a == expr5b
3013 * expr5a =~ expr5b
3014 * expr5a != expr5b
3015 * expr5a !~ expr5b
3016 * expr5a > expr5b
3017 * expr5a >= expr5b
3018 * expr5a < expr5b
3019 * expr5a <= expr5b
3020 * expr5a is expr5b
3021 * expr5a isnot expr5b
3022 *
3023 * Produces instructions:
3024 * EVAL expr5a Push result of "expr5a"
3025 * EVAL expr5b Push result of "expr5b"
3026 * COMPARE one of the compare instructions
3027 */
3028 static int
3029compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3030{
3031 exprtype_T type = EXPR_UNKNOWN;
3032 char_u *p;
3033 char_u *next;
3034 int len = 2;
3035 int type_is = FALSE;
3036 int ppconst_used = ppconst->pp_used;
3037
3038 // get the first variable
3039 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3040 return FAIL;
3041
3042 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003043
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003044 type = get_compare_type(p, &len, &type_is);
3045
3046 /*
3047 * If there is a comparative operator, use it.
3048 */
3049 if (type != EXPR_UNKNOWN)
3050 {
3051 int ic = FALSE; // Default: do not ignore case
3052
3053 if (next != NULL)
3054 {
3055 *arg = next_line_from_context(cctx, TRUE);
3056 p = skipwhite(*arg);
3057 }
3058 if (type_is && (p[len] == '?' || p[len] == '#'))
3059 {
3060 semsg(_(e_invalid_expression_str), *arg);
3061 return FAIL;
3062 }
3063 // extra question mark appended: ignore case
3064 if (p[len] == '?')
3065 {
3066 ic = TRUE;
3067 ++len;
3068 }
3069 // extra '#' appended: match case (ignored)
3070 else if (p[len] == '#')
3071 ++len;
3072 // nothing appended: match case
3073
3074 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3075 {
3076 error_white_both(p, len);
3077 return FAIL;
3078 }
3079
3080 // get the second variable
3081 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3082 return FAIL;
3083
3084 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3085 return FAIL;
3086
3087 if (ppconst->pp_used == ppconst_used + 2)
3088 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003089 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003090 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3091 int ret;
3092
3093 // Both sides are a constant, compute the result now.
3094 // First check for a valid combination of types, this is more
3095 // strict than typval_compare().
3096 if (check_compare_types(type, tv1, tv2) == FAIL)
3097 ret = FAIL;
3098 else
3099 {
3100 ret = typval_compare(tv1, tv2, type, ic);
3101 tv1->v_type = VAR_BOOL;
3102 tv1->vval.v_number = tv1->vval.v_number
3103 ? VVAL_TRUE : VVAL_FALSE;
3104 clear_tv(tv2);
3105 --ppconst->pp_used;
3106 }
3107 return ret;
3108 }
3109
3110 generate_ppconst(cctx, ppconst);
3111 return generate_COMPARE(cctx, type, ic);
3112 }
3113
3114 return OK;
3115}
3116
3117static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3118
3119/*
3120 * Compile || or &&.
3121 */
3122 static int
3123compile_and_or(
3124 char_u **arg,
3125 cctx_T *cctx,
3126 char *op,
3127 ppconst_T *ppconst,
3128 int ppconst_used UNUSED)
3129{
3130 char_u *next;
3131 char_u *p = may_peek_next_line(cctx, *arg, &next);
3132 int opchar = *op;
3133
3134 if (p[0] == opchar && p[1] == opchar)
3135 {
3136 garray_T *instr = &cctx->ctx_instr;
3137 garray_T end_ga;
3138 int save_skip = cctx->ctx_skip;
3139
3140 /*
3141 * Repeat until there is no following "||" or "&&"
3142 */
3143 ga_init2(&end_ga, sizeof(int), 10);
3144 while (p[0] == opchar && p[1] == opchar)
3145 {
3146 long start_lnum = SOURCING_LNUM;
3147 long save_sourcing_lnum;
3148 int start_ctx_lnum = cctx->ctx_lnum;
3149 int save_lnum;
3150 int const_used;
3151 int status;
3152 jumpwhen_T jump_when = opchar == '|'
3153 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3154
3155 if (next != NULL)
3156 {
3157 *arg = next_line_from_context(cctx, TRUE);
3158 p = skipwhite(*arg);
3159 }
3160
3161 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3162 {
3163 semsg(_(e_white_space_required_before_and_after_str_at_str),
3164 op, p);
3165 ga_clear(&end_ga);
3166 return FAIL;
3167 }
3168
3169 save_sourcing_lnum = SOURCING_LNUM;
3170 SOURCING_LNUM = start_lnum;
3171 save_lnum = cctx->ctx_lnum;
3172 cctx->ctx_lnum = start_ctx_lnum;
3173
3174 status = check_ppconst_bool(ppconst);
3175 if (status != FAIL)
3176 {
3177 // Use the last ppconst if possible.
3178 if (ppconst->pp_used > 0)
3179 {
3180 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3181 int is_true = tv2bool(tv);
3182
3183 if ((is_true && opchar == '|')
3184 || (!is_true && opchar == '&'))
3185 {
3186 // For "false && expr" and "true || expr" the "expr"
3187 // does not need to be evaluated.
3188 cctx->ctx_skip = SKIP_YES;
3189 clear_tv(tv);
3190 tv->v_type = VAR_BOOL;
3191 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3192 }
3193 else
3194 {
3195 // For "true && expr" and "false || expr" only "expr"
3196 // needs to be evaluated.
3197 --ppconst->pp_used;
3198 jump_when = JUMP_NEVER;
3199 }
3200 }
3201 else
3202 {
3203 // Every part must evaluate to a bool.
3204 status = bool_on_stack(cctx);
3205 }
3206 }
3207 if (status != FAIL)
3208 status = ga_grow(&end_ga, 1);
3209 cctx->ctx_lnum = save_lnum;
3210 if (status == FAIL)
3211 {
3212 ga_clear(&end_ga);
3213 return FAIL;
3214 }
3215
3216 if (jump_when != JUMP_NEVER)
3217 {
3218 if (cctx->ctx_skip != SKIP_YES)
3219 {
3220 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3221 ++end_ga.ga_len;
3222 }
3223 generate_JUMP(cctx, jump_when, 0);
3224 }
3225
3226 // eval the next expression
3227 SOURCING_LNUM = save_sourcing_lnum;
3228 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3229 {
3230 ga_clear(&end_ga);
3231 return FAIL;
3232 }
3233
3234 const_used = ppconst->pp_used;
3235 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3236 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3237 {
3238 ga_clear(&end_ga);
3239 return FAIL;
3240 }
3241
3242 // "0 || 1" results in true, "1 && 0" results in false.
3243 if (ppconst->pp_used == const_used + 1)
3244 {
3245 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3246
3247 if (tv->v_type == VAR_NUMBER
3248 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3249 {
3250 tv->vval.v_number = tv->vval.v_number == 1
3251 ? VVAL_TRUE : VVAL_FALSE;
3252 tv->v_type = VAR_BOOL;
3253 }
3254 }
3255
3256 p = may_peek_next_line(cctx, *arg, &next);
3257 }
3258
3259 if (check_ppconst_bool(ppconst) == FAIL)
3260 {
3261 ga_clear(&end_ga);
3262 return FAIL;
3263 }
3264
3265 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3266 // Every part must evaluate to a bool.
3267 if (bool_on_stack(cctx) == FAIL)
3268 {
3269 ga_clear(&end_ga);
3270 return FAIL;
3271 }
3272
3273 if (end_ga.ga_len > 0)
3274 {
3275 // Fill in the end label in all jumps.
3276 generate_ppconst(cctx, ppconst);
3277 while (end_ga.ga_len > 0)
3278 {
3279 isn_T *isn;
3280
3281 --end_ga.ga_len;
3282 isn = ((isn_T *)instr->ga_data)
3283 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3284 isn->isn_arg.jump.jump_where = instr->ga_len;
3285 }
3286 }
3287 ga_clear(&end_ga);
3288
3289 cctx->ctx_skip = save_skip;
3290 }
3291
3292 return OK;
3293}
3294
3295/*
3296 * expr4a && expr4a && expr4a logical AND
3297 *
3298 * Produces instructions:
3299 * EVAL expr4a Push result of "expr4a"
3300 * COND2BOOL convert to bool if needed
3301 * JUMP_IF_COND_FALSE end
3302 * EVAL expr4b Push result of "expr4b"
3303 * JUMP_IF_COND_FALSE end
3304 * EVAL expr4c Push result of "expr4c"
3305 * end:
3306 */
3307 static int
3308compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3309{
3310 int ppconst_used = ppconst->pp_used;
3311
3312 // get the first variable
3313 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3314 return FAIL;
3315
3316 // || and && work almost the same
3317 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3318}
3319
3320/*
3321 * expr3a || expr3b || expr3c logical OR
3322 *
3323 * Produces instructions:
3324 * EVAL expr3a Push result of "expr3a"
3325 * COND2BOOL convert to bool if needed
3326 * JUMP_IF_COND_TRUE end
3327 * EVAL expr3b Push result of "expr3b"
3328 * JUMP_IF_COND_TRUE end
3329 * EVAL expr3c Push result of "expr3c"
3330 * end:
3331 */
3332 static int
3333compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3334{
3335 int ppconst_used = ppconst->pp_used;
3336
3337 // eval the first expression
3338 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3339 return FAIL;
3340
3341 // || and && work almost the same
3342 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3343}
3344
3345/*
3346 * Toplevel expression: expr2 ? expr1a : expr1b
3347 * Produces instructions:
3348 * EVAL expr2 Push result of "expr2"
3349 * JUMP_IF_FALSE alt jump if false
3350 * EVAL expr1a
3351 * JUMP_ALWAYS end
3352 * alt: EVAL expr1b
3353 * end:
3354 *
3355 * Toplevel expression: expr2 ?? expr1
3356 * Produces instructions:
3357 * EVAL expr2 Push result of "expr2"
3358 * JUMP_AND_KEEP_IF_TRUE end jump if true
3359 * EVAL expr1
3360 * end:
3361 */
3362 int
3363compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3364{
3365 char_u *p;
3366 int ppconst_used = ppconst->pp_used;
3367 char_u *next;
3368
3369 // Ignore all kinds of errors when not producing code.
3370 if (cctx->ctx_skip == SKIP_YES)
3371 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003372 int prev_did_emsg = did_emsg;
3373
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003374 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003375 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003376 }
3377
3378 // Evaluate the first expression.
3379 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3380 return FAIL;
3381
3382 p = may_peek_next_line(cctx, *arg, &next);
3383 if (*p == '?')
3384 {
3385 int op_falsy = p[1] == '?';
3386 garray_T *instr = &cctx->ctx_instr;
3387 garray_T *stack = &cctx->ctx_type_stack;
3388 int alt_idx = instr->ga_len;
3389 int end_idx = 0;
3390 isn_T *isn;
3391 type_T *type1 = NULL;
3392 int has_const_expr = FALSE;
3393 int const_value = FALSE;
3394 int save_skip = cctx->ctx_skip;
3395
3396 if (next != NULL)
3397 {
3398 *arg = next_line_from_context(cctx, TRUE);
3399 p = skipwhite(*arg);
3400 }
3401
3402 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3403 {
3404 semsg(_(e_white_space_required_before_and_after_str_at_str),
3405 op_falsy ? "??" : "?", p);
3406 return FAIL;
3407 }
3408
3409 if (ppconst->pp_used == ppconst_used + 1)
3410 {
3411 // the condition is a constant, we know whether the ? or the :
3412 // expression is to be evaluated.
3413 has_const_expr = TRUE;
3414 if (op_falsy)
3415 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3416 else
3417 {
3418 int error = FALSE;
3419
3420 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3421 &error);
3422 if (error)
3423 return FAIL;
3424 }
3425 cctx->ctx_skip = save_skip == SKIP_YES ||
3426 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3427
3428 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3429 // "left ?? right" and "left" is truthy: produce "left"
3430 generate_ppconst(cctx, ppconst);
3431 else
3432 {
3433 clear_tv(&ppconst->pp_tv[ppconst_used]);
3434 --ppconst->pp_used;
3435 }
3436 }
3437 else
3438 {
3439 generate_ppconst(cctx, ppconst);
3440 if (op_falsy)
3441 end_idx = instr->ga_len;
3442 generate_JUMP(cctx, op_falsy
3443 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3444 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003445 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003446 }
3447
3448 // evaluate the second expression; any type is accepted
3449 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3450 return FAIL;
3451 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3452 return FAIL;
3453
3454 if (!has_const_expr)
3455 {
3456 generate_ppconst(cctx, ppconst);
3457
3458 if (!op_falsy)
3459 {
3460 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003461 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003462 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003463
3464 end_idx = instr->ga_len;
3465 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3466
3467 // jump here from JUMP_IF_FALSE
3468 isn = ((isn_T *)instr->ga_data) + alt_idx;
3469 isn->isn_arg.jump.jump_where = instr->ga_len;
3470 }
3471 }
3472
3473 if (!op_falsy)
3474 {
3475 // Check for the ":".
3476 p = may_peek_next_line(cctx, *arg, &next);
3477 if (*p != ':')
3478 {
3479 emsg(_(e_missing_colon_after_questionmark));
3480 return FAIL;
3481 }
3482 if (next != NULL)
3483 {
3484 *arg = next_line_from_context(cctx, TRUE);
3485 p = skipwhite(*arg);
3486 }
3487
3488 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3489 {
3490 semsg(_(e_white_space_required_before_and_after_str_at_str),
3491 ":", p);
3492 return FAIL;
3493 }
3494
3495 // evaluate the third expression
3496 if (has_const_expr)
3497 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3498 ? SKIP_YES : SKIP_NOT;
3499 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3500 return FAIL;
3501 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3502 return FAIL;
3503 }
3504
3505 if (!has_const_expr)
3506 {
3507 type_T **typep;
3508
3509 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003510 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003511
3512 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003513 typep = &((((type2_T *)stack->ga_data)
3514 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003515 common_type(type1, *typep, typep, cctx->ctx_type_list);
3516
3517 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3518 isn = ((isn_T *)instr->ga_data) + end_idx;
3519 isn->isn_arg.jump.jump_where = instr->ga_len;
3520 }
3521
3522 cctx->ctx_skip = save_skip;
3523 }
3524 return OK;
3525}
3526
3527/*
3528 * Toplevel expression.
3529 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3530 * Returns OK or FAIL.
3531 */
3532 int
3533compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3534{
3535 ppconst_T ppconst;
3536
3537 CLEAR_FIELD(ppconst);
3538 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3539 {
3540 clear_ppconst(&ppconst);
3541 return FAIL;
3542 }
3543 if (is_const != NULL)
3544 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3545 if (generate_ppconst(cctx, &ppconst) == FAIL)
3546 return FAIL;
3547 return OK;
3548}
3549
3550/*
3551 * Toplevel expression.
3552 */
3553 int
3554compile_expr0(char_u **arg, cctx_T *cctx)
3555{
3556 return compile_expr0_ext(arg, cctx, NULL);
3557}
3558
3559
3560#endif // defined(FEAT_EVAL)