blob: 883219ef587fae906b162cc2ccdcec23bf998ec5 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000102 if (need_type(idxtype, &t_number, FALSE,
103 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000104 return FAIL;
105 if (is_slice)
106 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000107 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000108 if (need_type(idxtype, &t_number, FALSE,
109 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000110 return FAIL;
111 }
112 }
113
114 if (vartype == VAR_DICT)
115 {
116 if (is_slice)
117 {
118 emsg(_(e_cannot_slice_dictionary));
119 return FAIL;
120 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000122 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000123 typep->type_curr = typep->type_curr->tt_member;
124 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000125 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 typep->type_curr = &t_any;
127 if (typep->type_decl->tt_type == VAR_DICT)
128 {
129 typep->type_decl = typep->type_decl->tt_member;
130 if (typep->type_decl == &t_unknown)
131 // empty dict was used
132 typep->type_decl = &t_any;
133 }
134 else
135 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000136 }
137 else
138 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000139 if (need_type(typep->type_curr, &t_dict_any, FALSE,
140 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000141 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000142 typep->type_curr = &t_any;
143 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100145 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
146 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147 return FAIL;
148 if (keeping_dict != NULL)
149 *keeping_dict = TRUE;
150 }
151 else if (vartype == VAR_STRING)
152 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000153 typep->type_curr = &t_string;
154 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000155 if ((is_slice
156 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
157 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
158 return FAIL;
159 }
160 else if (vartype == VAR_BLOB)
161 {
162 if (is_slice)
163 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 typep->type_curr = &t_blob;
165 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
167 return FAIL;
168 }
169 else
170 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000171 typep->type_curr = &t_number;
172 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
174 return FAIL;
175 }
176 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100177 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
178 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000179 {
180 if (is_slice)
181 {
182 if (generate_instr_drop(cctx,
183 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
184 2) == FAIL)
185 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000186 // a copy is made so the member type is no longer declared
187 if (typep->type_decl->tt_type == VAR_LIST)
188 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000189
190 // a copy is made, the composite is no longer "const"
191 if (typep->type_curr->tt_flags & TTFLAG_CONST)
192 {
193 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
194
195 if (type != typep->type_curr) // did get a copy
196 {
197 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
198 typep->type_curr = type;
199 }
200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201 }
202 else
203 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 typep->type_curr = typep->type_curr->tt_member;
207 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000209 typep->type_curr = &t_any;
210 if (typep->type_decl->tt_type == VAR_LIST)
211 {
212 typep->type_decl = typep->type_decl->tt_member;
213 if (typep->type_decl == &t_unknown)
214 // empty list was used
215 typep->type_decl = &t_any;
216 }
217 else
218 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 }
220 if (generate_instr_drop(cctx,
221 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
222 == FAIL)
223 return FAIL;
224 }
225 }
226 else
227 {
228 switch (vartype)
229 {
230 case VAR_FUNC:
231 case VAR_PARTIAL:
232 emsg(_(e_cannot_index_a_funcref));
233 break;
234 case VAR_BOOL:
235 case VAR_SPECIAL:
236 case VAR_JOB:
237 case VAR_CHANNEL:
238 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 case VAR_CLASS:
240 case VAR_OBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000241 case VAR_UNKNOWN:
242 case VAR_ANY:
243 case VAR_VOID:
244 emsg(_(e_cannot_index_special_variable));
245 break;
246 default:
247 emsg(_(e_string_list_dict_or_blob_required));
248 }
249 return FAIL;
250 }
251 return OK;
252}
253
254/*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200255 * Returns TRUE if the current function is inside the class "cl" or one of the
256 * parent classes.
257 */
258 static int
259inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
260{
261 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
262 {
263 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
264 {
265 class_T *clp = cctx->ctx_ufunc->uf_class;
266 while (clp != NULL)
267 {
268 if (clp == cl)
269 return TRUE;
270 clp = clp->class_extends;
271 }
272 }
273 }
274
275 return FALSE;
276}
277
278/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000279 * Compile ".member" coming after an object or class.
280 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000281 static int
282compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
283{
284 if (VIM_ISWHITE((*arg)[1]))
285 {
286 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
287 return FAIL;
288 }
289
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000290 class_T *cl = type->tt_class;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000291 int is_super = type->tt_flags & TTFLAG_SUPER;
292 if (type == &t_super)
293 {
294 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000295 {
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000296 emsg(_(e_using_super_not_in_class_function));
297 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000298 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000299 is_super = TRUE;
300 cl = cctx->ctx_ufunc->uf_class;
301 // Remove &t_super from the stack.
302 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000303 }
304 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000305 {
306 garray_T *instr = &cctx->ctx_instr;
307 if (instr->ga_len > 0)
308 {
309 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
310 if (isn->isn_type == ISN_LOADSCRIPT)
311 {
312 // The class was recognized as a script item. We only need
313 // to know what class it is, drop the instruction.
314 --instr->ga_len;
315 vim_free(isn->isn_arg.script.scriptref);
316 }
317 }
318 }
319
Ernie Raelf77a7f72023-03-03 15:05:30 +0000320 if (cl == NULL)
321 {
322 // TODO: this should not give an error but be handled at runtime
323 emsg(_(e_incomplete_type));
324 return FAIL;
325 }
326
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000327 ++*arg;
328 char_u *name = *arg;
329 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
330 if (name_end == name)
331 return FAIL;
332 size_t len = name_end - name;
333
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000334 if (*name_end == '(')
335 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000336 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000337 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000338 ufunc_T **functions;
339
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000340 if (type->tt_type == VAR_CLASS)
341 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000342 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000343 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000344 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000345 }
346 else
347 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000348 // type->tt_type == VAR_OBJECT: method call
349 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000350 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000351 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000352 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000353
354 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000355 int fi;
356 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000357 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000358 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000359 // Use a separate pointer to avoid that ASAN complains about
360 // uf_name[] only being 4 characters.
361 char_u *ufname = (char_u *)fp->uf_name;
362 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
363 {
364 ufunc = fp;
365 break;
366 }
367 }
368 if (ufunc == NULL)
369 {
370 // TODO: different error for object method?
371 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
372 return FAIL;
373 }
374
Yegappan Lakshmanane3b6c782023-08-29 22:32:02 +0200375 if (*ufunc->uf_name == '_' && !inside_class_hierarchy(cctx, cl))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200376 {
377 semsg(_(e_cannot_access_private_method_str), name);
378 return FAIL;
379 }
380
Bram Moolenaar574950d2023-01-03 19:08:50 +0000381 // Compile the arguments and call the class function or object method.
382 // The object method will know that the object is on the stack, just
383 // before the arguments.
384 *arg = skipwhite(name_end + 1);
385 int argcount = 0;
386 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
387 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000388
389 if (type->tt_type == VAR_OBJECT
390 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-east2261c892023-08-16 21:49:54 +0900391 return generate_CALL(cctx, ufunc, cl, fi, type, argcount);
392 return generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000393 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000394
395 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000396 {
397 for (int i = 0; i < cl->class_obj_member_count; ++i)
398 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000399 ocmember_T *m = &cl->class_obj_members[i];
400 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000401 {
Bram Moolenaar62a69232023-01-24 15:07:04 +0000402 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000403 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000404 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000405 return FAIL;
406 }
407
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000408 *arg = name_end;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000409 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Ernie Rael18143d32023-09-04 22:30:41 +0200410 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type,
411 FALSE);
412 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type, FALSE);
413 }
414 }
415
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000416 // Could be a function reference: "obj.Func".
417 for (int i = 0; i < cl->class_obj_method_count; ++i)
418 {
419 ufunc_T *fp = cl->class_obj_methods[i];
420 // Use a separate pointer to avoid that ASAN complains about
421 // uf_name[] only being 4 characters.
422 char_u *ufname = (char_u *)fp->uf_name;
423 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar313e4722023-02-08 20:55:27 +0000424 {
425 if (type->tt_type == VAR_OBJECT
426 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
427 return generate_FUNCREF(cctx, fp, cl, i, NULL);
428 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
429 }
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000430 }
431
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000432 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
433 }
434 else
435 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000436 // load class member
437 int idx;
438 for (idx = 0; idx < cl->class_class_member_count; ++idx)
439 {
440 ocmember_T *m = &cl->class_class_members[idx];
441 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200442 {
Ernie Rael18143d32023-09-04 22:30:41 +0200443 // Note: type->tt_type = VAR_CLASS
444 if ((cl->class_flags & CLASS_INTERFACE) != 0)
445 {
446 semsg(_(e_interface_static_direct_access_str),
447 cl->class_name, m->ocm_name);
448 return FAIL;
449 }
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200450 if (*name == '_' && !inside_class(cctx, cl))
451 {
452 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
453 return FAIL;
454 }
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000455 break;
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +0200456 }
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000457 }
458 if (idx < cl->class_class_member_count)
459 {
460 *arg = name_end;
461 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
462 }
463 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000464 }
465
466 return FAIL;
467}
468
469/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000470 * Generate an instruction to load script-local variable "name", without the
471 * leading "s:".
472 * Also finds imported variables.
473 */
474 int
475compile_load_scriptvar(
476 cctx_T *cctx,
477 char_u *name, // variable NUL terminated
478 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100479 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000480{
481 scriptitem_T *si;
482 int idx;
483 imported_T *import;
484
485 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
486 return FAIL;
487 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000488 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000489 if (idx >= 0)
490 {
491 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
492
493 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
494 current_sctx.sc_sid, idx, sv->sv_type);
495 return OK;
496 }
497
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000498 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 if (import != NULL)
500 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000501 char_u *p = skipwhite(*end);
502 char_u *exp_name;
503 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100504 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000505 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000506 int done = FALSE;
507 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000508
509 // Need to lookup the member.
510 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000511 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000512 semsg(_(e_expected_dot_after_name_str), start);
513 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000514 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000515 ++p;
516 if (VIM_ISWHITE(*p))
517 {
518 emsg(_(e_no_white_space_allowed_after_dot));
519 return FAIL;
520 }
521
522 // isolate one name
523 exp_name = p;
524 while (eval_isnamec(*p))
525 ++p;
526 cc = *p;
527 *p = NUL;
528
Bram Moolenaard041f422022-01-12 19:54:00 +0000529 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100530 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
531 // "import autoload './dir/script.vim'" or
532 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100533 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100534
535 if (res == OK)
536 {
537 if (si->sn_autoload_prefix != NULL
538 && si->sn_state == SN_STATE_NOT_LOADED)
539 {
540 char_u *auto_name =
541 concat_str(si->sn_autoload_prefix, exp_name);
542
543 // autoload script must be loaded later, access by the autoload
544 // name. If a '(' follows it must be a function. Otherwise we
545 // don't know, it can be "script.Func".
546 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100547 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100548 else
549 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
550 vim_free(auto_name);
551 done = TRUE;
552 }
553 else if (si->sn_import_autoload
554 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100555 {
556 // If a '(' follows it must be a function. Otherwise we don't
557 // know, it can be "script.Func".
558 if (cc == '(' || paren_follows_after_expr)
559 {
560 char_u sid_name[MAX_FUNC_NAME_LEN];
561
562 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100563 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100564 }
565 else
566 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
567 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100568 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100569 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100570 else
571 {
572 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
573 cctx, NULL, TRUE);
574 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100575 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100576
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000577 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000578 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000579 if (done)
580 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000581
582 if (idx < 0)
583 {
584 if (ufunc != NULL)
585 {
586 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100587 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000588 return OK;
589 }
590 return FAIL;
591 }
592
593 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
594 import->imp_sid,
595 idx,
596 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000597 return OK;
598 }
599
Bram Moolenaard0132f42022-05-12 11:05:40 +0100600 // Can only get here if we know "name" is a script variable and not in a
601 // Vim9 script (variable is not in sn_var_vals): old style script.
602 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000603 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604}
605
606 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000607generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000608{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000609 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000610 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000612 // Reject a global non-autoload function found without the "g:" prefix.
613 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614 return FAIL;
615
616 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000617 compile_type = get_compile_type(ufunc);
618 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000619 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100621 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622}
623
624/*
625 * Compile a variable name into a load instruction.
626 * "end" points to just after the name.
627 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
628 * When "error" is FALSE do not give an error when not found.
629 */
630 int
631compile_load(
632 char_u **arg,
633 char_u *end_arg,
634 cctx_T *cctx,
635 int is_expr,
636 int error)
637{
638 type_T *type;
639 char_u *name = NULL;
640 char_u *end = end_arg;
641 int res = FAIL;
642 int prev_called_emsg = called_emsg;
643
644 if (*(*arg + 1) == ':')
645 {
646 if (end <= *arg + 2)
647 {
648 isntype_T isn_type;
649
650 // load dictionary of namespace
651 switch (**arg)
652 {
653 case 'g': isn_type = ISN_LOADGDICT; break;
654 case 'w': isn_type = ISN_LOADWDICT; break;
655 case 't': isn_type = ISN_LOADTDICT; break;
656 case 'b': isn_type = ISN_LOADBDICT; break;
657 default:
658 semsg(_(e_namespace_not_supported_str), *arg);
659 goto theend;
660 }
661 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
662 goto theend;
663 res = OK;
664 }
665 else
666 {
667 isntype_T isn_type = ISN_DROP;
668
669 // load namespaced variable
670 name = vim_strnsave(*arg + 2, end - (*arg + 2));
671 if (name == NULL)
672 return FAIL;
673
674 switch (**arg)
675 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100676 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000677 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000678 case 's': if (current_script_is_vim9())
679 {
680 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
681 *arg);
682 vim_free(name);
683 return FAIL;
684 }
Kota Kato948a3892022-08-16 16:09:59 +0100685 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000686 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 else
688 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100689 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000690 break;
691 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
692 {
693 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000694 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000695 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 else
697 isn_type = ISN_LOADG;
698 }
699 else
700 {
701 isn_type = ISN_LOADAUTO;
702 vim_free(name);
703 name = vim_strnsave(*arg, end - *arg);
704 if (name == NULL)
705 return FAIL;
706 }
707 break;
708 case 'w': isn_type = ISN_LOADW; break;
709 case 't': isn_type = ISN_LOADT; break;
710 case 'b': isn_type = ISN_LOADB; break;
711 default: // cannot happen, just in case
712 semsg(_(e_namespace_not_supported_str), *arg);
713 goto theend;
714 }
715 if (isn_type != ISN_DROP)
716 {
717 // Global, Buffer-local, Window-local and Tabpage-local
718 // variables can be defined later, thus we don't check if it
719 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000720 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721 }
722 }
723 }
724 else
725 {
726 size_t len = end - *arg;
727 int idx;
728 int gen_load = FALSE;
729 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100730 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100731 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000732
733 name = vim_strnsave(*arg, end - *arg);
734 if (name == NULL)
735 return FAIL;
736
Bram Moolenaar58b40092023-01-11 15:59:05 +0000737 if (STRCMP(name, "super") == 0
738 && cctx->ctx_ufunc != NULL
739 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
740 {
741 // super.SomeFunc() in a class function: push &t_super type, this
742 // is recognized in compile_subscript().
743 res = push_type_stack(cctx, &t_super);
744 if (*end != '.')
745 emsg(_(e_super_must_be_followed_by_dot));
746 }
747 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 {
749 script_autoload(name, FALSE);
750 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
751 }
752 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
753 == OK)
754 {
755 if (gen_load_outer == 0)
756 gen_load = TRUE;
757 }
758 else
759 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000760 lvar_T lvar;
761 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762
763 if (lookup_local(*arg, len, &lvar, cctx) == OK)
764 {
765 type = lvar.lv_type;
766 idx = lvar.lv_idx;
767 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100768 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100770 outer_loop_depth = lvar.lv_loop_depth;
771 outer_loop_idx = lvar.lv_loop_idx;
772 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 else
774 gen_load = TRUE;
775 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000776 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000777 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +0200778 // Referencing a class member without the class name. Infer
779 // the class from the def function context.
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000780 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
781 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 else
783 {
784 // "var" can be script-local even without using "s:" if it
785 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000786 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000787 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100788 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789
790 // When evaluating an expression and the name starts with an
791 // uppercase letter it can be a user defined function.
792 // generate_funcref() will fail if the function can't be found.
793 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000794 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795 }
796 }
797 if (gen_load)
798 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
799 if (gen_load_outer > 0)
800 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100801 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
802 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803 cctx->ctx_outer_used = TRUE;
804 }
805 }
806
807 *arg = end;
808
809theend:
810 if (res == FAIL && error && called_emsg == prev_called_emsg)
811 semsg(_(e_variable_not_found_str), name);
812 vim_free(name);
813 return res;
814}
815
816/*
817 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100818 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 * Returns FAIL if compilation fails.
820 */
821 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100822compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823{
LemonBoyf3b48952022-05-05 13:53:03 +0100824 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825 garray_T save_ga = cctx->ctx_instr;
826 int expr_res;
827 int trailing_error;
828 int instr_count;
829 isn_T *instr = NULL;
830
831 // Remove the string type from the stack.
832 --cctx->ctx_type_stack.ga_len;
833
834 // Temporarily reset the list of instructions so that the jump labels are
835 // correct.
836 cctx->ctx_instr.ga_len = 0;
837 cctx->ctx_instr.ga_maxlen = 0;
838 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000839
840 // avoid peeking a next line
841 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
842 cctx->ctx_ufunc->uf_lines.ga_len = 0;
843
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000844 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000845
846 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
847
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 s = skipwhite(s);
849 trailing_error = *s != NUL;
850
851 if (expr_res == FAIL || trailing_error
852 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
853 {
854 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000855 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000856 clear_instr_ga(&cctx->ctx_instr);
857 cctx->ctx_instr = save_ga;
858 ++cctx->ctx_type_stack.ga_len;
859 return FAIL;
860 }
861
862 // Move the generated instructions into the ISN_INSTR instruction, then
863 // restore the list of instructions.
864 instr_count = cctx->ctx_instr.ga_len;
865 instr = cctx->ctx_instr.ga_data;
866 instr[instr_count].isn_type = ISN_FINISH;
867
868 cctx->ctx_instr = save_ga;
869 vim_free(isn->isn_arg.string);
870 isn->isn_type = ISN_INSTR;
871 isn->isn_arg.instr = instr;
872 return OK;
873}
874
875/*
876 * Compile the argument expressions.
877 * "arg" points to just after the "(" and is advanced to after the ")"
878 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100879 int
LemonBoyf3b48952022-05-05 13:53:03 +0100880compile_arguments(
881 char_u **arg,
882 cctx_T *cctx,
883 int *argcount,
884 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885{
886 char_u *p = *arg;
887 char_u *whitep = *arg;
888 int must_end = FALSE;
889 int instr_count;
890
891 for (;;)
892 {
893 if (may_get_next_line(whitep, &p, cctx) == FAIL)
894 goto failret;
895 if (*p == ')')
896 {
897 *arg = p + 1;
898 return OK;
899 }
900 if (must_end)
901 {
902 semsg(_(e_missing_comma_before_argument_str), p);
903 return FAIL;
904 }
905
906 instr_count = cctx->ctx_instr.ga_len;
907 if (compile_expr0(&p, cctx) == FAIL)
908 return FAIL;
909 ++*argcount;
910
LemonBoyf3b48952022-05-05 13:53:03 +0100911 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000912 && cctx->ctx_instr.ga_len == instr_count + 1)
913 {
914 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
915
916 // {skip} argument of searchpair() can be compiled if not empty
917 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100918 compile_string(isn, cctx, 0);
919 }
920 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
921 && cctx->ctx_instr.ga_len == instr_count + 1)
922 {
923 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
924
925 // {sub} argument of substitute() can be compiled if it starts
926 // with \=
927 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100928 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100929 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000930 }
931
932 if (*p != ',' && *skipwhite(p) == ',')
933 {
934 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
935 p = skipwhite(p);
936 }
937 if (*p == ',')
938 {
939 ++p;
940 if (*p != NUL && !VIM_ISWHITE(*p))
941 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
942 }
943 else
944 must_end = TRUE;
945 whitep = p;
946 p = skipwhite(p);
947 }
948failret:
949 emsg(_(e_missing_closing_paren));
950 return FAIL;
951}
952
953/*
954 * Compile a function call: name(arg1, arg2)
955 * "arg" points to "name", "arg + varlen" to the "(".
956 * "argcount_init" is 1 for "value->method()"
957 * Instructions:
958 * EVAL arg1
959 * EVAL arg2
960 * BCALL / DCALL / UCALL
961 */
962 static int
963compile_call(
964 char_u **arg,
965 size_t varlen,
966 cctx_T *cctx,
967 ppconst_T *ppconst,
968 int argcount_init)
969{
970 char_u *name = *arg;
971 char_u *p;
972 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100973 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 char_u fname_buf[FLEN_FIXED + 1];
975 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000976 ufunc_T *ufunc = NULL;
977 int res = FAIL;
978 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000979 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100980 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000981 imported_T *import;
h-east2261c892023-08-16 21:49:54 +0900982 type_T *type;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000983
984 if (varlen >= sizeof(namebuf))
985 {
986 semsg(_(e_name_too_long_str), name);
987 return FAIL;
988 }
989 vim_strncpy(namebuf, *arg, varlen);
990
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000991 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000992 if (import != NULL)
993 {
994 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
995 return FAIL;
996 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000997
998 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100999 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001000 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +01001001 if ((varlen == 3
1002 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001003 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
1004 {
1005 char_u *s = skipwhite(*arg + varlen + 1);
1006 typval_T argvars[2];
1007 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +01001008 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001009
1010 argvars[0].v_type = VAR_UNKNOWN;
1011 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001012 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001013 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001014 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001015 s = skipwhite(s);
1016 if (*s == ')' && argvars[0].v_type == VAR_STRING
1017 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
1018 || !is_has))
1019 {
1020 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
1021
1022 *arg = s + 1;
1023 argvars[1].v_type = VAR_UNKNOWN;
1024 tv->v_type = VAR_NUMBER;
1025 tv->vval.v_number = 0;
1026 if (is_has)
1027 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +01001028 else if (is_len)
1029 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001030 else
1031 f_exists(argvars, tv);
1032 clear_tv(&argvars[0]);
1033 ++ppconst->pp_used;
1034 return OK;
1035 }
1036 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +01001037 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001038 {
1039 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
1040 return FAIL;
1041 }
1042 }
1043
1044 if (generate_ppconst(cctx, ppconst) == FAIL)
1045 return FAIL;
1046
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001047 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001048 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1049
1050 // We handle the "skip" argument of searchpair() and searchpairpos()
1051 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001052 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1053 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1054 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1055 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1056 special_fn = CA_SEARCHPAIR;
1057 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1058 special_fn = CA_SUBSTITUTE;
1059 else
1060 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001061
1062 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001063 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001064 goto theend;
1065
h-east2261c892023-08-16 21:49:54 +09001066 type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001067 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1068 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1069 {
1070 int idx;
1071
1072 // builtin function
1073 idx = find_internal_func(name);
1074 if (idx >= 0)
1075 {
1076 if (STRCMP(name, "flatten") == 0)
1077 {
1078 emsg(_(e_cannot_use_flatten_in_vim9_script));
1079 goto theend;
1080 }
1081
1082 if (STRCMP(name, "add") == 0 && argcount == 2)
1083 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084 // add() can be compiled to instructions if we know the type
1085 if (type->tt_type == VAR_LIST)
1086 {
1087 // inline "add(list, item)" so that the type can be checked
1088 res = generate_LISTAPPEND(cctx);
1089 idx = -1;
1090 }
1091 else if (type->tt_type == VAR_BLOB)
1092 {
1093 // inline "add(blob, nr)" so that the type can be checked
1094 res = generate_BLOBAPPEND(cctx);
1095 idx = -1;
1096 }
1097 }
1098
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001099 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1100 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001101 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001102 // May have the "D" or "R" flag, reserve a variable for a
1103 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001104 if (get_defer_var_idx(cctx) == 0)
1105 idx = -1;
1106 }
1107
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001108 if (idx >= 0)
1109 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1110 }
1111 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001112 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001113 goto theend;
1114 }
1115
Bram Moolenaar62aec932022-01-29 21:45:34 +00001116 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1117
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001118 // An argument or local variable can be a function reference, this
1119 // overrules a function name.
1120 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1121 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1122 {
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001123 class_T *cl = NULL;
1124 int mi = 0;
1125
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126 // If we can find the function by name generate the right call.
1127 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001128 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001129 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001131 if (!func_is_global(ufunc))
1132 {
h-east2261c892023-08-16 21:49:54 +09001133 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001134 goto theend;
1135 }
1136 if (!has_g_namespace
1137 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1138 {
1139 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001140 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001141 goto theend;
1142 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143 }
Yegappan Lakshmanan342f4f62023-09-09 11:37:23 +02001144 else if ((mi = class_method_index(name, varlen, &cl, cctx)) >= 0)
1145 {
1146 // Class method invocation without the class name. The
1147 // generate_CALL() function expects the class type at the top of
1148 // the stack. So push the class type to the stack.
1149 push_type_stack(cctx, &t_class);
1150 res = generate_CALL(cctx, cl->class_class_functions[mi], NULL, 0,
1151 type, argcount);
1152 goto theend;
1153 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154 }
1155
1156 // If the name is a variable, load it and use PCALL.
1157 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001158 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001159 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001160 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1162 {
Christian Brabandtd5475e82023-08-17 23:34:09 +02001163 type_T *s_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164
Christian Brabandtd5475e82023-08-17 23:34:09 +02001165 res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001166 goto theend;
1167 }
1168
1169 // If we can find a global function by name generate the right call.
1170 if (ufunc != NULL)
1171 {
h-east2261c892023-08-16 21:49:54 +09001172 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173 goto theend;
1174 }
1175
1176 // A global function may be defined only later. Need to figure out at
1177 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001178 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 res = generate_UCALL(cctx, name, argcount);
1180 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001181 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001182
1183theend:
1184 vim_free(tofree);
1185 return res;
1186}
1187
1188// like NAMESPACE_CHAR but with 'a' and 'l'.
1189#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1190
1191/*
1192 * Find the end of a variable or function name. Unlike find_name_end() this
1193 * does not recognize magic braces.
1194 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1195 * Return a pointer to just after the name. Equal to "arg" if there is no
1196 * valid name.
1197 */
1198 char_u *
1199to_name_end(char_u *arg, int use_namespace)
1200{
1201 char_u *p;
1202
1203 // Quick check for valid starting character.
1204 if (!eval_isnamec1(*arg))
1205 return arg;
1206
1207 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1208 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1209 // and can be used in slice "[n:]".
1210 if (*p == ':' && (p != arg + 1
1211 || !use_namespace
1212 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1213 break;
1214 return p;
1215}
1216
1217/*
1218 * Like to_name_end() but also skip over a list or dict constant.
1219 * Also accept "<SNR>123_Func".
1220 * This intentionally does not handle line continuation.
1221 */
1222 char_u *
1223to_name_const_end(char_u *arg)
1224{
1225 char_u *p = arg;
1226 typval_T rettv;
1227
1228 if (STRNCMP(p, "<SNR>", 5) == 0)
1229 p = skipdigits(p + 5);
1230 p = to_name_end(p, TRUE);
1231 if (p == arg && *arg == '[')
1232 {
1233
1234 // Can be "[1, 2, 3]->Func()".
1235 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1236 p = arg;
1237 }
1238 return p;
1239}
1240
1241/*
1242 * parse a list: [expr, expr]
1243 * "*arg" points to the '['.
1244 * ppconst->pp_is_const is set if all items are a constant.
1245 */
1246 static int
1247compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1248{
1249 char_u *p = skipwhite(*arg + 1);
1250 char_u *whitep = *arg + 1;
1251 int count = 0;
1252 int is_const;
1253 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001254 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001255
1256 for (;;)
1257 {
1258 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1259 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001260 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001261 return FAIL;
1262 }
1263 if (*p == ',')
1264 {
1265 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1266 return FAIL;
1267 }
1268 if (*p == ']')
1269 {
1270 ++p;
1271 break;
1272 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001273 if (must_end)
1274 {
1275 semsg(_(e_missing_comma_in_list_str), p);
1276 return FAIL;
1277 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1279 return FAIL;
1280 if (!is_const)
1281 is_all_const = FALSE;
1282 ++count;
1283 if (*p == ',')
1284 {
1285 ++p;
1286 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1287 {
1288 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1289 return FAIL;
1290 }
1291 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001292 else
1293 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 whitep = p;
1295 p = skipwhite(p);
1296 }
1297 *arg = p;
1298
1299 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001300 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001301}
1302
1303/*
1304 * Parse a lambda: "(arg, arg) => expr"
1305 * "*arg" points to the '('.
1306 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1307 */
1308 static int
1309compile_lambda(char_u **arg, cctx_T *cctx)
1310{
1311 int r;
1312 typval_T rettv;
1313 ufunc_T *ufunc;
1314 evalarg_T evalarg;
1315
1316 init_evalarg(&evalarg);
1317 evalarg.eval_flags = EVAL_EVALUATE;
1318 evalarg.eval_cctx = cctx;
1319
1320 // Get the funcref in "rettv".
1321 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1322 if (r != OK)
1323 {
1324 clear_evalarg(&evalarg, NULL);
1325 return r;
1326 }
1327
1328 // "rettv" will now be a partial referencing the function.
1329 ufunc = rettv.vval.v_partial->pt_func;
1330 ++ufunc->uf_refcount;
1331 clear_tv(&rettv);
1332
1333 // Compile it here to get the return type. The return type is optional,
1334 // when it's missing use t_unknown. This is recognized in
1335 // compile_return().
1336 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1337 ufunc->uf_ret_type = &t_unknown;
1338 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1339
1340 // When the outer function is compiled for profiling or debugging, the
1341 // lambda may be called without profiling or debugging. Compile it here in
1342 // the right context.
1343 if (cctx->ctx_compile_type == CT_DEBUG
1344#ifdef FEAT_PROFILE
1345 || cctx->ctx_compile_type == CT_PROFILE
1346#endif
1347 )
1348 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1349
Bram Moolenaar139575d2022-03-15 19:29:30 +00001350 // if the outer function is not compiled for debugging or profiling, this
1351 // one might be
1352 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001353 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001354 compiletype_T compile_type = get_compile_type(ufunc);
1355
1356 if (compile_type != CT_NONE)
1357 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001358 }
1359
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1361 // "*arg" may point into it. Point into the original line to avoid a
1362 // dangling pointer.
1363 if (evalarg.eval_using_cmdline)
1364 {
1365 garray_T *gap = &evalarg.eval_tofree_ga;
1366 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1367
1368 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1369 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001370 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371 }
1372
1373 clear_evalarg(&evalarg, NULL);
1374
1375 if (ufunc->uf_def_status == UF_COMPILED)
1376 {
1377 // The return type will now be known.
1378 set_function_type(ufunc);
1379
1380 // The function reference count will be 1. When the ISN_FUNCREF
1381 // instruction is deleted the reference count is decremented and the
1382 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001383 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001384 }
1385
1386 func_ptr_unref(ufunc);
1387 return FAIL;
1388}
1389
1390/*
1391 * Get a lambda and compile it. Uses Vim9 syntax.
1392 */
1393 int
1394get_lambda_tv_and_compile(
1395 char_u **arg,
1396 typval_T *rettv,
1397 int types_optional,
1398 evalarg_T *evalarg)
1399{
1400 int r;
1401 ufunc_T *ufunc;
1402 int save_sc_version = current_sctx.sc_version;
1403
1404 // Get the funcref in "rettv".
1405 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1406 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1407 current_sctx.sc_version = save_sc_version;
1408 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001409 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410
1411 // "rettv" will now be a partial referencing the function.
1412 ufunc = rettv->vval.v_partial->pt_func;
1413
1414 // Compile it here to get the return type. The return type is optional,
1415 // when it's missing use t_unknown. This is recognized in
1416 // compile_return().
1417 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1418 ufunc->uf_ret_type = &t_unknown;
1419 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1420
1421 if (ufunc->uf_def_status == UF_COMPILED)
1422 {
1423 // The return type will now be known.
1424 set_function_type(ufunc);
1425 return OK;
1426 }
1427 clear_tv(rettv);
1428 return FAIL;
1429}
1430
1431/*
1432 * parse a dict: {key: val, [key]: val}
1433 * "*arg" points to the '{'.
1434 * ppconst->pp_is_const is set if all item values are a constant.
1435 */
1436 static int
1437compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1438{
1439 garray_T *instr = &cctx->ctx_instr;
1440 int count = 0;
1441 dict_T *d = dict_alloc();
1442 dictitem_T *item;
1443 char_u *whitep = *arg + 1;
1444 char_u *p;
1445 int is_const;
1446 int is_all_const = TRUE; // reset when non-const encountered
1447
1448 if (d == NULL)
1449 return FAIL;
1450 if (generate_ppconst(cctx, ppconst) == FAIL)
1451 return FAIL;
1452 for (;;)
1453 {
1454 char_u *key = NULL;
1455
1456 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1457 {
1458 *arg = NULL;
1459 goto failret;
1460 }
1461
1462 if (**arg == '}')
1463 break;
1464
1465 if (**arg == '[')
1466 {
1467 isn_T *isn;
1468
1469 // {[expr]: value} uses an evaluated key.
1470 *arg = skipwhite(*arg + 1);
1471 if (compile_expr0(arg, cctx) == FAIL)
1472 return FAIL;
1473 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1474 if (isn->isn_type == ISN_PUSHNR)
1475 {
1476 char buf[NUMBUFLEN];
1477
1478 // Convert to string at compile time.
1479 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1480 isn->isn_type = ISN_PUSHS;
1481 isn->isn_arg.string = vim_strsave((char_u *)buf);
1482 }
1483 if (isn->isn_type == ISN_PUSHS)
1484 key = isn->isn_arg.string;
1485 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1486 return FAIL;
1487 *arg = skipwhite(*arg);
1488 if (**arg != ']')
1489 {
1490 emsg(_(e_missing_matching_bracket_after_dict_key));
1491 return FAIL;
1492 }
1493 ++*arg;
1494 }
1495 else
1496 {
1497 // {"name": value},
1498 // {'name': value},
1499 // {name: value} use "name" as a literal key
1500 key = get_literal_key(arg);
1501 if (key == NULL)
1502 return FAIL;
1503 if (generate_PUSHS(cctx, &key) == FAIL)
1504 return FAIL;
1505 }
1506
1507 // Check for duplicate keys, if using string keys.
1508 if (key != NULL)
1509 {
1510 item = dict_find(d, key, -1);
1511 if (item != NULL)
1512 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001513 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001514 goto failret;
1515 }
1516 item = dictitem_alloc(key);
1517 if (item != NULL)
1518 {
1519 item->di_tv.v_type = VAR_UNKNOWN;
1520 item->di_tv.v_lock = 0;
1521 if (dict_add(d, item) == FAIL)
1522 dictitem_free(item);
1523 }
1524 }
1525
1526 if (**arg != ':')
1527 {
1528 if (*skipwhite(*arg) == ':')
1529 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1530 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001531 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001532 return FAIL;
1533 }
1534 whitep = *arg + 1;
1535 if (!IS_WHITE_OR_NUL(*whitep))
1536 {
1537 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1538 return FAIL;
1539 }
1540
1541 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1542 {
1543 *arg = NULL;
1544 goto failret;
1545 }
1546
1547 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1548 return FAIL;
1549 if (!is_const)
1550 is_all_const = FALSE;
1551 ++count;
1552
1553 whitep = *arg;
1554 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1555 {
1556 *arg = NULL;
1557 goto failret;
1558 }
1559 if (**arg == '}')
1560 break;
1561 if (**arg != ',')
1562 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001563 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001564 goto failret;
1565 }
1566 if (IS_WHITE_OR_NUL(*whitep))
1567 {
1568 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1569 return FAIL;
1570 }
1571 whitep = *arg + 1;
1572 if (!IS_WHITE_OR_NUL(*whitep))
1573 {
1574 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1575 return FAIL;
1576 }
1577 *arg = skipwhite(whitep);
1578 }
1579
1580 *arg = *arg + 1;
1581
1582 // Allow for following comment, after at least one space.
1583 p = skipwhite(*arg);
1584 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1585 *arg += STRLEN(*arg);
1586
1587 dict_unref(d);
1588 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001589 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590
1591failret:
1592 if (*arg == NULL)
1593 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001594 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001595 *arg = (char_u *)"";
1596 }
1597 dict_unref(d);
1598 return FAIL;
1599}
1600
1601/*
1602 * Compile "&option".
1603 */
1604 static int
1605compile_get_option(char_u **arg, cctx_T *cctx)
1606{
1607 typval_T rettv;
1608 char_u *start = *arg;
1609 int ret;
1610
1611 // parse the option and get the current value to get the type.
1612 rettv.v_type = VAR_UNKNOWN;
1613 ret = eval_option(arg, &rettv, TRUE);
1614 if (ret == OK)
1615 {
1616 // include the '&' in the name, eval_option() expects it.
1617 char_u *name = vim_strnsave(start, *arg - start);
1618 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1619 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1620
1621 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1622 vim_free(name);
1623 }
1624 clear_tv(&rettv);
1625
1626 return ret;
1627}
1628
1629/*
1630 * Compile "$VAR".
1631 */
1632 static int
1633compile_get_env(char_u **arg, cctx_T *cctx)
1634{
1635 char_u *start = *arg;
1636 int len;
1637 int ret;
1638 char_u *name;
1639
1640 ++*arg;
1641 len = get_env_len(arg);
1642 if (len == 0)
1643 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001644 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645 return FAIL;
1646 }
1647
1648 // include the '$' in the name, eval_env_var() expects it.
1649 name = vim_strnsave(start, len + 1);
1650 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1651 vim_free(name);
1652 return ret;
1653}
1654
1655/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001656 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001657 */
1658 static int
1659compile_interp_string(char_u **arg, cctx_T *cctx)
1660{
1661 typval_T tv;
1662 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001663 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001664 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001665 int count = 0;
1666 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001667
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001668 // *arg is on the '$' character, move it to the first string character.
1669 ++*arg;
1670 quote = **arg;
1671 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001672
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001673 for (;;)
1674 {
1675 // Get the string up to the matching quote or to a single '{'.
1676 // "arg" is advanced to either the quote or the '{'.
1677 if (quote == '"')
1678 ret = eval_string(arg, &tv, evaluate, TRUE);
1679 else
1680 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1681 if (ret == FAIL)
1682 break;
1683 if (evaluate)
1684 {
1685 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1686 || (**arg != '{' && count == 0))
1687 {
1688 // generate non-empty string or empty string if it's the only
1689 // one
1690 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1691 return FAIL;
1692 tv.vval.v_string = NULL; // don't free it now
1693 ++count;
1694 }
1695 clear_tv(&tv);
1696 }
1697
1698 if (**arg != '{')
1699 {
1700 // found terminating quote
1701 ++*arg;
1702 break;
1703 }
1704
1705 p = compile_one_expr_in_str(*arg, cctx);
1706 if (p == NULL)
1707 {
1708 ret = FAIL;
1709 break;
1710 }
1711 ++count;
1712 *arg = p;
1713 }
LemonBoy2eaef102022-05-06 13:14:50 +01001714
1715 if (ret == FAIL || !evaluate)
1716 return ret;
1717
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001718 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1719 if (count > 1)
1720 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001721
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001722 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001723}
1724
1725/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726 * Compile "@r".
1727 */
1728 static int
1729compile_get_register(char_u **arg, cctx_T *cctx)
1730{
1731 int ret;
1732
1733 ++*arg;
1734 if (**arg == NUL)
1735 {
1736 semsg(_(e_syntax_error_at_str), *arg - 1);
1737 return FAIL;
1738 }
1739 if (!valid_yank_reg(**arg, FALSE))
1740 {
1741 emsg_invreg(**arg);
1742 return FAIL;
1743 }
1744 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1745 ++*arg;
1746 return ret;
1747}
1748
1749/*
1750 * Apply leading '!', '-' and '+' to constant "rettv".
1751 * When "numeric_only" is TRUE do not apply '!'.
1752 */
1753 static int
1754apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1755{
1756 char_u *p = *end;
1757
1758 // this works from end to start
1759 while (p > start)
1760 {
1761 --p;
1762 if (*p == '-' || *p == '+')
1763 {
1764 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 if (rettv->v_type == VAR_FLOAT)
1766 {
1767 if (*p == '-')
1768 rettv->vval.v_float = -rettv->vval.v_float;
1769 }
1770 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771 {
1772 varnumber_T val;
1773 int error = FALSE;
1774
1775 // tv_get_number_chk() accepts a string, but we don't want that
1776 // here
1777 if (check_not_string(rettv) == FAIL)
1778 return FAIL;
1779 val = tv_get_number_chk(rettv, &error);
1780 clear_tv(rettv);
1781 if (error)
1782 return FAIL;
1783 if (*p == '-')
1784 val = -val;
1785 rettv->v_type = VAR_NUMBER;
1786 rettv->vval.v_number = val;
1787 }
1788 }
1789 else if (numeric_only)
1790 {
1791 ++p;
1792 break;
1793 }
1794 else if (*p == '!')
1795 {
1796 int v = tv2bool(rettv);
1797
1798 // '!' is permissive in the type.
1799 clear_tv(rettv);
1800 rettv->v_type = VAR_BOOL;
1801 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1802 }
1803 }
1804 *end = p;
1805 return OK;
1806}
1807
1808/*
1809 * Recognize v: variables that are constants and set "rettv".
1810 */
1811 static void
1812get_vim_constant(char_u **arg, typval_T *rettv)
1813{
1814 if (STRNCMP(*arg, "v:true", 6) == 0)
1815 {
1816 rettv->v_type = VAR_BOOL;
1817 rettv->vval.v_number = VVAL_TRUE;
1818 *arg += 6;
1819 }
1820 else if (STRNCMP(*arg, "v:false", 7) == 0)
1821 {
1822 rettv->v_type = VAR_BOOL;
1823 rettv->vval.v_number = VVAL_FALSE;
1824 *arg += 7;
1825 }
1826 else if (STRNCMP(*arg, "v:null", 6) == 0)
1827 {
1828 rettv->v_type = VAR_SPECIAL;
1829 rettv->vval.v_number = VVAL_NULL;
1830 *arg += 6;
1831 }
1832 else if (STRNCMP(*arg, "v:none", 6) == 0)
1833 {
1834 rettv->v_type = VAR_SPECIAL;
1835 rettv->vval.v_number = VVAL_NONE;
1836 *arg += 6;
1837 }
1838}
1839
1840 exprtype_T
1841get_compare_type(char_u *p, int *len, int *type_is)
1842{
1843 exprtype_T type = EXPR_UNKNOWN;
1844 int i;
1845
1846 switch (p[0])
1847 {
1848 case '=': if (p[1] == '=')
1849 type = EXPR_EQUAL;
1850 else if (p[1] == '~')
1851 type = EXPR_MATCH;
1852 break;
1853 case '!': if (p[1] == '=')
1854 type = EXPR_NEQUAL;
1855 else if (p[1] == '~')
1856 type = EXPR_NOMATCH;
1857 break;
1858 case '>': if (p[1] != '=')
1859 {
1860 type = EXPR_GREATER;
1861 *len = 1;
1862 }
1863 else
1864 type = EXPR_GEQUAL;
1865 break;
1866 case '<': if (p[1] != '=')
1867 {
1868 type = EXPR_SMALLER;
1869 *len = 1;
1870 }
1871 else
1872 type = EXPR_SEQUAL;
1873 break;
1874 case 'i': if (p[1] == 's')
1875 {
1876 // "is" and "isnot"; but not a prefix of a name
1877 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1878 *len = 5;
1879 i = p[*len];
1880 if (!isalnum(i) && i != '_')
1881 {
1882 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1883 *type_is = TRUE;
1884 }
1885 }
1886 break;
1887 }
1888 return type;
1889}
1890
1891/*
1892 * Skip over an expression, ignoring most errors.
1893 */
1894 void
1895skip_expr_cctx(char_u **arg, cctx_T *cctx)
1896{
1897 evalarg_T evalarg;
1898
1899 init_evalarg(&evalarg);
1900 evalarg.eval_cctx = cctx;
1901 skip_expr(arg, &evalarg);
1902 clear_evalarg(&evalarg, NULL);
1903}
1904
1905/*
1906 * Check that the top of the type stack has a type that can be used as a
1907 * condition. Give an error and return FAIL if not.
1908 */
1909 int
1910bool_on_stack(cctx_T *cctx)
1911{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001912 type_T *type;
1913
Bram Moolenaar078a4612022-01-04 15:17:03 +00001914 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001915 if (type == &t_bool)
1916 return OK;
1917
Bram Moolenaar4913d422022-10-17 13:13:32 +01001918 if (type->tt_type == VAR_ANY
1919 || type->tt_type == VAR_UNKNOWN
1920 || type->tt_type == VAR_NUMBER
1921 || type == &t_number_bool
1922 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1924 // This requires a runtime type check.
1925 return generate_COND2BOOL(cctx);
1926
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001927 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928}
1929
1930/*
1931 * Give the "white on both sides" error, taking the operator from "p[len]".
1932 */
1933 void
1934error_white_both(char_u *op, int len)
1935{
1936 char_u buf[10];
1937
1938 vim_strncpy(buf, op, len);
1939 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1940}
1941
1942/*
1943 * Compile code to apply '-', '+' and '!'.
1944 * When "numeric_only" is TRUE do not apply '!'.
1945 */
1946 static int
1947compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1948{
1949 char_u *p = *end;
1950
1951 // this works from end to start
1952 while (p > start)
1953 {
1954 --p;
1955 while (VIM_ISWHITE(*p))
1956 --p;
1957 if (*p == '-' || *p == '+')
1958 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001959 type_T *type = get_type_on_stack(cctx, 0);
1960 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001961 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001962 return FAIL;
1963
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001965 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1966 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001967 }
1968 else if (numeric_only)
1969 {
1970 ++p;
1971 break;
1972 }
1973 else
1974 {
1975 int invert = *p == '!';
1976
1977 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1978 {
1979 if (p[-1] == '!')
1980 invert = !invert;
1981 --p;
1982 }
1983 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1984 return FAIL;
1985 }
1986 }
1987 *end = p;
1988 return OK;
1989}
1990
1991/*
1992 * Compile "(expression)": recursive!
1993 * Return FAIL/OK.
1994 */
1995 static int
1996compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1997{
1998 int ret;
1999 char_u *p = *arg + 1;
2000
2001 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2002 return FAIL;
2003 if (ppconst->pp_used <= PPSIZE - 10)
2004 {
2005 ret = compile_expr1(arg, cctx, ppconst);
2006 }
2007 else
2008 {
2009 // Not enough space in ppconst, flush constants.
2010 if (generate_ppconst(cctx, ppconst) == FAIL)
2011 return FAIL;
2012 ret = compile_expr0(arg, cctx);
2013 }
2014 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2015 return FAIL;
2016 if (**arg == ')')
2017 ++*arg;
2018 else if (ret == OK)
2019 {
2020 emsg(_(e_missing_closing_paren));
2021 ret = FAIL;
2022 }
2023 return ret;
2024}
2025
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002026static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002027
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002028/*
2029 * Compile whatever comes after "name" or "name()".
2030 * Advances "*arg" only when something was recognized.
2031 */
2032 static int
2033compile_subscript(
2034 char_u **arg,
2035 cctx_T *cctx,
2036 char_u *start_leader,
2037 char_u **end_leader,
2038 ppconst_T *ppconst)
2039{
2040 char_u *name_start = *end_leader;
2041 int keeping_dict = FALSE;
2042
2043 for (;;)
2044 {
2045 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002046 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047
2048 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
2049 {
2050 char_u *next = peek_next_line_from_context(cctx);
2051
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002052 // If a following line starts with "->{", "->(" or "->X" advance to
2053 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002054 // Also if a following line starts with ".x".
2055 if (next != NULL &&
2056 ((next[0] == '-' && next[1] == '>'
2057 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01002058 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059 || ASCII_ISALPHA(*skipwhite(next + 2))))
2060 || (next[0] == '.' && eval_isdictc(next[1]))))
2061 {
2062 next = next_line_from_context(cctx, TRUE);
2063 if (next == NULL)
2064 return FAIL;
2065 *arg = next;
2066 p = skipwhite(*arg);
2067 }
2068 }
2069
2070 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2071 // is not a function call.
2072 if (**arg == '(')
2073 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002074 int argcount = 0;
2075
2076 if (generate_ppconst(cctx, ppconst) == FAIL)
2077 return FAIL;
2078 ppconst->pp_is_const = FALSE;
2079
2080 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002081 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082
2083 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002084 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085 return FAIL;
2086 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2087 return FAIL;
2088 if (keeping_dict)
2089 {
2090 keeping_dict = FALSE;
2091 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2092 return FAIL;
2093 }
2094 }
2095 else if (*p == '-' && p[1] == '>')
2096 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002097 char_u *pstart = p;
2098 int alt;
2099 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002100
Bram Moolenaarc7349932022-01-16 20:59:39 +00002101 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002102 if (generate_ppconst(cctx, ppconst) == FAIL)
2103 return FAIL;
2104 ppconst->pp_is_const = FALSE;
2105
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106 // Apply the '!', '-' and '+' first:
2107 // -1.0->func() works like (-1.0)->func()
2108 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2109 return FAIL;
2110
2111 p += 2;
2112 *arg = skipwhite(p);
2113 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002114
2115 // Three alternatives handled here:
2116 // 1. "base->name(" only a name, use compile_call()
2117 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2118 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002119 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002120 alt = 2;
2121 else
2122 {
2123 // alternative 1 or 3
2124 p = *arg;
2125 if (!eval_isnamec1(*p))
2126 {
2127 semsg(_(e_trailing_characters_str), pstart);
2128 return FAIL;
2129 }
2130 if (ASCII_ISALPHA(*p) && p[1] == ':')
2131 p += 2;
2132 for ( ; eval_isnamec(*p); ++p)
2133 ;
2134 if (*p == '(')
2135 {
2136 // alternative 1
2137 alt = 1;
2138 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2139 return FAIL;
2140 }
2141 else
2142 {
2143 // Must be alternative 3, find the "(". Only works within
2144 // one line.
2145 alt = 3;
2146 paren = vim_strchr(p, '(');
2147 if (paren == NULL)
2148 {
2149 semsg(_(e_missing_parenthesis_str), *arg);
2150 return FAIL;
2151 }
2152 }
2153 }
2154
2155 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002156 {
2157 int argcount = 1;
2158 garray_T *stack = &cctx->ctx_type_stack;
2159 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002160 int expr_isn_start = cctx->ctx_instr.ga_len;
2161 int expr_isn_end;
2162 int arg_isn_count;
2163
Bram Moolenaarc7349932022-01-16 20:59:39 +00002164 if (alt == 2)
2165 {
2166 // Funcref call: list->(Refs[2])(arg)
2167 // or lambda: list->((arg) => expr)(arg)
2168 //
2169 // Fist compile the function expression.
2170 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2171 return FAIL;
2172 }
2173 else
2174 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002175 int fail;
2176 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002177 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002178
Bram Moolenaarc7349932022-01-16 20:59:39 +00002179 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002180
2181 // instead of using LOADG for "import.Func" use PUSHFUNC
2182 ++paren_follows_after_expr;
2183
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002184 // do not look in the next line
2185 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002186
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002187 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002188 || *skipwhite(*arg) != NUL;
2189 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002190 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002191 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002192
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002193 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002194 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002195 if (did_emsg == prev_did_emsg)
2196 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002197 return FAIL;
2198 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002199 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002200
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201 // Compile the arguments.
2202 if (**arg != '(')
2203 {
2204 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002205 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 else
2207 semsg(_(e_missing_parenthesis_str), *arg);
2208 return FAIL;
2209 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002210
2211 // Remember the next instruction index, where the instructions
2212 // for arguments are being written.
2213 expr_isn_end = cctx->ctx_instr.ga_len;
2214
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002215 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002216 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2217 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002218 return FAIL;
2219
2220 // Move the instructions for the arguments to before the
2221 // instructions of the expression and move the type of the
2222 // expression after the argument types. This is what ISN_PCALL
2223 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2225 if (arg_isn_count > 0)
2226 {
2227 int expr_isn_count = expr_isn_end - expr_isn_start;
2228 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002229 type_T *decl_type;
2230 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002231
2232 if (isn == NULL)
2233 return FAIL;
2234 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2235 + expr_isn_start,
2236 sizeof(isn_T) * expr_isn_count);
2237 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2238 + expr_isn_start,
2239 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2240 sizeof(isn_T) * arg_isn_count);
2241 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2242 + expr_isn_start + arg_isn_count,
2243 isn, sizeof(isn_T) * expr_isn_count);
2244 vim_free(isn);
2245
Bram Moolenaar078a4612022-01-04 15:17:03 +00002246 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2247 type = typep->type_curr;
2248 decl_type = typep->type_decl;
2249 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2250 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2251 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002253 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2254 typep->type_curr = type;
2255 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002256 }
2257
Bram Moolenaar078a4612022-01-04 15:17:03 +00002258 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002259 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2260 return FAIL;
2261 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002262
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002263 if (keeping_dict)
2264 {
2265 keeping_dict = FALSE;
2266 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2267 return FAIL;
2268 }
2269 }
2270 else if (**arg == '[')
2271 {
2272 int is_slice = FALSE;
2273
2274 // list index: list[123]
2275 // dict member: dict[key]
2276 // string index: text[123]
2277 // blob index: blob[123]
2278 if (generate_ppconst(cctx, ppconst) == FAIL)
2279 return FAIL;
2280 ppconst->pp_is_const = FALSE;
2281
2282 ++p;
2283 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2284 return FAIL;
2285 if (**arg == ':')
2286 {
2287 // missing first index is equal to zero
2288 generate_PUSHNR(cctx, 0);
2289 }
2290 else
2291 {
2292 if (compile_expr0(arg, cctx) == FAIL)
2293 return FAIL;
2294 if (**arg == ':')
2295 {
2296 semsg(_(e_white_space_required_before_and_after_str_at_str),
2297 ":", *arg);
2298 return FAIL;
2299 }
2300 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2301 return FAIL;
2302 *arg = skipwhite(*arg);
2303 }
2304 if (**arg == ':')
2305 {
2306 is_slice = TRUE;
2307 ++*arg;
2308 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2309 {
2310 semsg(_(e_white_space_required_before_and_after_str_at_str),
2311 ":", *arg);
2312 return FAIL;
2313 }
2314 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2315 return FAIL;
2316 if (**arg == ']')
2317 // missing second index is equal to end of string
2318 generate_PUSHNR(cctx, -1);
2319 else
2320 {
2321 if (compile_expr0(arg, cctx) == FAIL)
2322 return FAIL;
2323 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2324 return FAIL;
2325 *arg = skipwhite(*arg);
2326 }
2327 }
2328
2329 if (**arg != ']')
2330 {
2331 emsg(_(e_missing_closing_square_brace));
2332 return FAIL;
2333 }
2334 *arg = *arg + 1;
2335
2336 if (keeping_dict)
2337 {
2338 keeping_dict = FALSE;
2339 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2340 return FAIL;
2341 }
2342 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2343 return FAIL;
2344 }
2345 else if (*p == '.' && p[1] != '.')
2346 {
2347 // dictionary member: dict.name
2348 if (generate_ppconst(cctx, ppconst) == FAIL)
2349 return FAIL;
2350 ppconst->pp_is_const = FALSE;
2351
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002352 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2353 && (type->tt_type == VAR_CLASS
2354 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002355 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002356 // class member: SomeClass.varname
2357 // class method: SomeClass.SomeMethod()
2358 // class constructor: SomeClass.new()
2359 // object member: someObject.varname, this.varname
2360 // object method: someObject.SomeMethod(), this.SomeMethod()
2361 *arg = p;
2362 if (compile_class_object_index(cctx, arg, type) == FAIL)
2363 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002364 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002365 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002366 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002367 *arg = p + 1;
2368 if (IS_WHITE_OR_NUL(**arg))
2369 {
2370 emsg(_(e_missing_name_after_dot));
2371 return FAIL;
2372 }
2373 p = *arg;
2374 if (eval_isdictc(*p))
2375 while (eval_isnamec(*p))
2376 MB_PTR_ADV(p);
2377 if (p == *arg)
2378 {
2379 semsg(_(e_syntax_error_at_str), *arg);
2380 return FAIL;
2381 }
2382 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2383 return FAIL;
2384 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2385 return FAIL;
2386 keeping_dict = TRUE;
2387 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002388 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002389 }
2390 else
2391 break;
2392 }
2393
2394 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2395 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002396 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2397 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002398 return FAIL;
2399
2400 return OK;
2401}
2402
2403/*
2404 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2405 * "arg" is advanced until after the expression, skipping white space.
2406 *
2407 * If the value is a constant "ppconst->pp_used" will be non-zero.
2408 * Before instructions are generated, any values in "ppconst" will generated.
2409 *
2410 * This is the compiling equivalent of eval1(), eval2(), etc.
2411 */
2412
2413/*
2414 * number number constant
2415 * 0zFFFFFFFF Blob constant
2416 * "string" string constant
2417 * 'string' literal string constant
2418 * &option-name option value
2419 * @r register contents
2420 * identifier variable value
2421 * function() function call
2422 * $VAR environment variable
2423 * (expression) nested expression
2424 * [expr, expr] List
2425 * {key: val, [key]: val} Dictionary
2426 *
2427 * Also handle:
2428 * ! in front logical NOT
2429 * - in front unary minus
2430 * + in front unary plus (ignored)
2431 * trailing (arg) funcref/partial call
2432 * trailing [] subscript in String or List
2433 * trailing .name entry in Dictionary
2434 * trailing ->name() method call
2435 */
2436 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002437compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002438 char_u **arg,
2439 cctx_T *cctx,
2440 ppconst_T *ppconst)
2441{
2442 char_u *start_leader, *end_leader;
2443 int ret = OK;
2444 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2445 int used_before = ppconst->pp_used;
2446
2447 ppconst->pp_is_const = FALSE;
2448
2449 /*
2450 * Skip '!', '-' and '+' characters. They are handled later.
2451 */
2452 start_leader = *arg;
2453 if (eval_leader(arg, TRUE) == FAIL)
2454 return FAIL;
2455 end_leader = *arg;
2456
2457 rettv->v_type = VAR_UNKNOWN;
2458 switch (**arg)
2459 {
2460 /*
2461 * Number constant.
2462 */
2463 case '0': // also for blob starting with 0z
2464 case '1':
2465 case '2':
2466 case '3':
2467 case '4':
2468 case '5':
2469 case '6':
2470 case '7':
2471 case '8':
2472 case '9':
2473 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2474 return FAIL;
2475 // Apply "-" and "+" just before the number now, right to
2476 // left. Matters especially when "->" follows. Stops at
2477 // '!'.
2478 if (apply_leader(rettv, TRUE,
2479 start_leader, &end_leader) == FAIL)
2480 {
2481 clear_tv(rettv);
2482 return FAIL;
2483 }
2484 break;
2485
2486 /*
2487 * String constant: "string".
2488 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002489 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490 return FAIL;
2491 break;
2492
2493 /*
2494 * Literal string constant: 'str''ing'.
2495 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002496 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002497 return FAIL;
2498 break;
2499
2500 /*
2501 * Constant Vim variable.
2502 */
2503 case 'v': get_vim_constant(arg, rettv);
2504 ret = NOTDONE;
2505 break;
2506
2507 /*
2508 * "true" constant
2509 */
2510 case 't': if (STRNCMP(*arg, "true", 4) == 0
2511 && !eval_isnamec((*arg)[4]))
2512 {
2513 *arg += 4;
2514 rettv->v_type = VAR_BOOL;
2515 rettv->vval.v_number = VVAL_TRUE;
2516 }
2517 else
2518 ret = NOTDONE;
2519 break;
2520
2521 /*
2522 * "false" constant
2523 */
2524 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2525 && !eval_isnamec((*arg)[5]))
2526 {
2527 *arg += 5;
2528 rettv->v_type = VAR_BOOL;
2529 rettv->vval.v_number = VVAL_FALSE;
2530 }
2531 else
2532 ret = NOTDONE;
2533 break;
2534
2535 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002536 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002537 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002538 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002540 char_u *p = *arg + 4;
2541 int len;
2542
2543 for (len = 0; eval_isnamec(p[len]); ++len)
2544 ;
2545 ret = handle_predefined(*arg, len + 4, rettv);
2546 if (ret == FAIL)
2547 ret = NOTDONE;
2548 else
2549 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002550 }
2551 else
2552 ret = NOTDONE;
2553 break;
2554
2555 /*
2556 * List: [expr, expr]
2557 */
2558 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2559 return FAIL;
2560 ret = compile_list(arg, cctx, ppconst);
2561 break;
2562
2563 /*
2564 * Dictionary: {'key': val, 'key': val}
2565 */
2566 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2567 return FAIL;
2568 ret = compile_dict(arg, cctx, ppconst);
2569 break;
2570
2571 /*
2572 * Option value: &name
2573 */
2574 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2575 return FAIL;
2576 ret = compile_get_option(arg, cctx);
2577 break;
2578
2579 /*
2580 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002581 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002582 */
2583 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2584 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002585 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2586 ret = compile_interp_string(arg, cctx);
2587 else
2588 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002589 break;
2590
2591 /*
2592 * Register contents: @r.
2593 */
2594 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2595 return FAIL;
2596 ret = compile_get_register(arg, cctx);
2597 break;
2598 /*
2599 * nested expression: (expression).
2600 * lambda: (arg, arg) => expr
2601 * funcref: (arg, arg) => { statement }
2602 */
2603 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2604 ret = compile_lambda(arg, cctx);
2605 if (ret == NOTDONE)
2606 ret = compile_parenthesis(arg, cctx, ppconst);
2607 break;
2608
2609 default: ret = NOTDONE;
2610 break;
2611 }
2612 if (ret == FAIL)
2613 return FAIL;
2614
2615 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2616 {
2617 if (cctx->ctx_skip == SKIP_YES)
2618 clear_tv(rettv);
2619 else
2620 // A constant expression can possibly be handled compile time,
2621 // return the value instead of generating code.
2622 ++ppconst->pp_used;
2623 }
2624 else if (ret == NOTDONE)
2625 {
2626 char_u *p;
2627 int r;
2628
2629 if (!eval_isnamec1(**arg))
2630 {
2631 if (!vim9_bad_comment(*arg))
2632 {
2633 if (ends_excmd(*skipwhite(*arg)))
2634 semsg(_(e_empty_expression_str), *arg);
2635 else
2636 semsg(_(e_name_expected_str), *arg);
2637 }
2638 return FAIL;
2639 }
2640
2641 // "name" or "name()"
2642 p = to_name_end(*arg, TRUE);
2643 if (p - *arg == (size_t)1 && **arg == '_')
2644 {
2645 emsg(_(e_cannot_use_underscore_here));
2646 return FAIL;
2647 }
2648
2649 if (*p == '(')
2650 {
2651 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2652 }
2653 else
2654 {
2655 if (cctx->ctx_skip != SKIP_YES
2656 && generate_ppconst(cctx, ppconst) == FAIL)
2657 return FAIL;
2658 r = compile_load(arg, p, cctx, TRUE, TRUE);
2659 }
2660 if (r == FAIL)
2661 return FAIL;
2662 }
2663
2664 // Handle following "[]", ".member", etc.
2665 // Then deal with prefixed '-', '+' and '!', if not done already.
2666 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2667 ppconst) == FAIL)
2668 return FAIL;
2669 if (ppconst->pp_used > 0)
2670 {
2671 // apply the '!', '-' and '+' before the constant
2672 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2673 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2674 return FAIL;
2675 return OK;
2676 }
2677 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2678 return FAIL;
2679 return OK;
2680}
2681
2682/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002683 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002684 */
2685 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002686compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002687{
2688 type_T *want_type = NULL;
2689
2690 // Recognize <type>
2691 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2692 {
2693 ++*arg;
2694 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2695 if (want_type == NULL)
2696 return FAIL;
2697
2698 if (**arg != '>')
2699 {
2700 if (*skipwhite(*arg) == '>')
2701 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2702 else
2703 emsg(_(e_missing_gt));
2704 return FAIL;
2705 }
2706 ++*arg;
2707 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2708 return FAIL;
2709 }
2710
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002711 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002712 return FAIL;
2713
2714 if (want_type != NULL)
2715 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716 type_T *actual;
2717 where_T where = WHERE_INIT;
2718
2719 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002720 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002721 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002723 if (need_type(actual, want_type, FALSE,
2724 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002725 return FAIL;
2726 }
2727 }
2728
2729 return OK;
2730}
2731
2732/*
2733 * * number multiplication
2734 * / number division
2735 * % number modulo
2736 */
2737 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002738compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002739{
2740 char_u *op;
2741 char_u *next;
2742 int ppconst_used = ppconst->pp_used;
2743
2744 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002745 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002746 return FAIL;
2747
2748 /*
2749 * Repeat computing, until no "*", "/" or "%" is following.
2750 */
2751 for (;;)
2752 {
2753 op = may_peek_next_line(cctx, *arg, &next);
2754 if (*op != '*' && *op != '/' && *op != '%')
2755 break;
2756 if (next != NULL)
2757 {
2758 *arg = next_line_from_context(cctx, TRUE);
2759 op = skipwhite(*arg);
2760 }
2761
2762 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2763 {
2764 error_white_both(op, 1);
2765 return FAIL;
2766 }
2767 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2768 return FAIL;
2769
2770 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002771 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002772 return FAIL;
2773
2774 if (ppconst->pp_used == ppconst_used + 2
2775 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2776 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2777 {
2778 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2779 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2780 varnumber_T res = 0;
2781 int failed = FALSE;
2782
2783 // both are numbers: compute the result
2784 switch (*op)
2785 {
2786 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2787 break;
2788 case '/': res = num_divide(tv1->vval.v_number,
2789 tv2->vval.v_number, &failed);
2790 break;
2791 case '%': res = num_modulus(tv1->vval.v_number,
2792 tv2->vval.v_number, &failed);
2793 break;
2794 }
2795 if (failed)
2796 return FAIL;
2797 tv1->vval.v_number = res;
2798 --ppconst->pp_used;
2799 }
2800 else
2801 {
2802 generate_ppconst(cctx, ppconst);
2803 generate_two_op(cctx, op);
2804 }
2805 }
2806
2807 return OK;
2808}
2809
2810/*
2811 * + number addition or list/blobl concatenation
2812 * - number subtraction
2813 * .. string concatenation
2814 */
2815 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002816compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002817{
2818 char_u *op;
2819 char_u *next;
2820 int oplen;
2821 int ppconst_used = ppconst->pp_used;
2822
2823 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002824 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002825 return FAIL;
2826
2827 /*
2828 * Repeat computing, until no "+", "-" or ".." is following.
2829 */
2830 for (;;)
2831 {
2832 op = may_peek_next_line(cctx, *arg, &next);
2833 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2834 break;
2835 if (op[0] == op[1] && *op != '.' && next)
2836 // Finding "++" or "--" on the next line is a separate command.
2837 // But ".." is concatenation.
2838 break;
2839 oplen = (*op == '.' ? 2 : 1);
2840 if (next != NULL)
2841 {
2842 *arg = next_line_from_context(cctx, TRUE);
2843 op = skipwhite(*arg);
2844 }
2845
2846 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2847 {
2848 error_white_both(op, oplen);
2849 return FAIL;
2850 }
2851
2852 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2853 return FAIL;
2854
2855 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002856 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002857 return FAIL;
2858
2859 if (ppconst->pp_used == ppconst_used + 2
2860 && (*op == '.'
2861 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2862 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2863 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2864 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2865 {
2866 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2867 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2868
2869 // concat/subtract/add constant numbers
2870 if (*op == '+')
2871 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2872 else if (*op == '-')
2873 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2874 else
2875 {
2876 // concatenate constant strings
2877 char_u *s1 = tv1->vval.v_string;
2878 char_u *s2 = tv2->vval.v_string;
2879 size_t len1 = STRLEN(s1);
2880
2881 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2882 if (tv1->vval.v_string == NULL)
2883 {
2884 clear_ppconst(ppconst);
2885 return FAIL;
2886 }
2887 mch_memmove(tv1->vval.v_string, s1, len1);
2888 STRCPY(tv1->vval.v_string + len1, s2);
2889 vim_free(s1);
2890 vim_free(s2);
2891 }
2892 --ppconst->pp_used;
2893 }
2894 else
2895 {
2896 generate_ppconst(cctx, ppconst);
2897 ppconst->pp_is_const = FALSE;
2898 if (*op == '.')
2899 {
2900 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2901 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2902 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002903 if (generate_CONCAT(cctx, 2) == FAIL)
2904 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002905 }
2906 else
2907 generate_two_op(cctx, op);
2908 }
2909 }
2910
2911 return OK;
2912}
2913
2914/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002915 * expr6a >> expr6b
2916 * expr6a << expr6b
2917 *
2918 * Produces instructions:
2919 * OPNR bitwise left or right shift
2920 */
2921 static int
2922compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2923{
2924 exprtype_T type = EXPR_UNKNOWN;
2925 char_u *p;
2926 char_u *next;
2927 int len = 2;
2928 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002929 isn_T *isn;
2930
2931 // get the first variable
2932 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2933 return FAIL;
2934
2935 /*
2936 * Repeat computing, until no "+", "-" or ".." is following.
2937 */
2938 for (;;)
2939 {
2940 type = EXPR_UNKNOWN;
2941
2942 p = may_peek_next_line(cctx, *arg, &next);
2943 if (p[0] == '<' && p[1] == '<')
2944 type = EXPR_LSHIFT;
2945 else if (p[0] == '>' && p[1] == '>')
2946 type = EXPR_RSHIFT;
2947
2948 if (type == EXPR_UNKNOWN)
2949 return OK;
2950
2951 // Handle a bitwise left or right shift operator
2952 if (ppconst->pp_used == ppconst_used + 1)
2953 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002954 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002955 {
2956 // left operand should be a number
2957 emsg(_(e_bitshift_ops_must_be_number));
2958 return FAIL;
2959 }
2960 }
2961 else
2962 {
2963 type_T *t = get_type_on_stack(cctx, 0);
2964
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002965 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002966 {
2967 emsg(_(e_bitshift_ops_must_be_number));
2968 return FAIL;
2969 }
2970 }
2971
2972 if (next != NULL)
2973 {
2974 *arg = next_line_from_context(cctx, TRUE);
2975 p = skipwhite(*arg);
2976 }
2977
2978 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2979 {
2980 error_white_both(p, len);
2981 return FAIL;
2982 }
2983
2984 // get the second variable
2985 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2986 return FAIL;
2987
2988 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2989 return FAIL;
2990
2991 if (ppconst->pp_used == ppconst_used + 2)
2992 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002993 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2994 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2995
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002996 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002997 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2998 {
2999 // right operand should be a positive number
3000 if (tv2->v_type != VAR_NUMBER)
3001 emsg(_(e_bitshift_ops_must_be_number));
3002 else
dundargocc57b5bc2022-11-02 13:30:51 +00003003 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003004 return FAIL;
3005 }
3006
3007 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
3008 tv1->vval.v_number = 0;
3009 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003010 tv1->vval.v_number =
3011 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003012 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01003013 tv1->vval.v_number =
3014 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003015 clear_tv(tv2);
3016 --ppconst->pp_used;
3017 }
3018 else
3019 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003020 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
3021 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003022 {
3023 emsg(_(e_bitshift_ops_must_be_number));
3024 return FAIL;
3025 }
3026
3027 generate_ppconst(cctx, ppconst);
3028
3029 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
3030 if (isn == NULL)
3031 return FAIL;
3032
3033 if (isn != NULL)
3034 isn->isn_arg.op.op_type = type;
3035 }
3036 }
3037
3038 return OK;
3039}
3040
3041/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003042 * expr5a == expr5b
3043 * expr5a =~ expr5b
3044 * expr5a != expr5b
3045 * expr5a !~ expr5b
3046 * expr5a > expr5b
3047 * expr5a >= expr5b
3048 * expr5a < expr5b
3049 * expr5a <= expr5b
3050 * expr5a is expr5b
3051 * expr5a isnot expr5b
3052 *
3053 * Produces instructions:
3054 * EVAL expr5a Push result of "expr5a"
3055 * EVAL expr5b Push result of "expr5b"
3056 * COMPARE one of the compare instructions
3057 */
3058 static int
3059compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3060{
3061 exprtype_T type = EXPR_UNKNOWN;
3062 char_u *p;
3063 char_u *next;
3064 int len = 2;
3065 int type_is = FALSE;
3066 int ppconst_used = ppconst->pp_used;
3067
3068 // get the first variable
3069 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3070 return FAIL;
3071
3072 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003073
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003074 type = get_compare_type(p, &len, &type_is);
3075
3076 /*
3077 * If there is a comparative operator, use it.
3078 */
3079 if (type != EXPR_UNKNOWN)
3080 {
3081 int ic = FALSE; // Default: do not ignore case
3082
3083 if (next != NULL)
3084 {
3085 *arg = next_line_from_context(cctx, TRUE);
3086 p = skipwhite(*arg);
3087 }
3088 if (type_is && (p[len] == '?' || p[len] == '#'))
3089 {
3090 semsg(_(e_invalid_expression_str), *arg);
3091 return FAIL;
3092 }
3093 // extra question mark appended: ignore case
3094 if (p[len] == '?')
3095 {
3096 ic = TRUE;
3097 ++len;
3098 }
3099 // extra '#' appended: match case (ignored)
3100 else if (p[len] == '#')
3101 ++len;
3102 // nothing appended: match case
3103
3104 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3105 {
3106 error_white_both(p, len);
3107 return FAIL;
3108 }
3109
3110 // get the second variable
3111 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3112 return FAIL;
3113
3114 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3115 return FAIL;
3116
3117 if (ppconst->pp_used == ppconst_used + 2)
3118 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003119 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003120 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3121 int ret;
3122
3123 // Both sides are a constant, compute the result now.
3124 // First check for a valid combination of types, this is more
3125 // strict than typval_compare().
3126 if (check_compare_types(type, tv1, tv2) == FAIL)
3127 ret = FAIL;
3128 else
3129 {
3130 ret = typval_compare(tv1, tv2, type, ic);
3131 tv1->v_type = VAR_BOOL;
3132 tv1->vval.v_number = tv1->vval.v_number
3133 ? VVAL_TRUE : VVAL_FALSE;
3134 clear_tv(tv2);
3135 --ppconst->pp_used;
3136 }
3137 return ret;
3138 }
3139
3140 generate_ppconst(cctx, ppconst);
3141 return generate_COMPARE(cctx, type, ic);
3142 }
3143
3144 return OK;
3145}
3146
3147static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3148
3149/*
3150 * Compile || or &&.
3151 */
3152 static int
3153compile_and_or(
3154 char_u **arg,
3155 cctx_T *cctx,
3156 char *op,
3157 ppconst_T *ppconst,
3158 int ppconst_used UNUSED)
3159{
3160 char_u *next;
3161 char_u *p = may_peek_next_line(cctx, *arg, &next);
3162 int opchar = *op;
3163
3164 if (p[0] == opchar && p[1] == opchar)
3165 {
3166 garray_T *instr = &cctx->ctx_instr;
3167 garray_T end_ga;
3168 int save_skip = cctx->ctx_skip;
3169
3170 /*
3171 * Repeat until there is no following "||" or "&&"
3172 */
3173 ga_init2(&end_ga, sizeof(int), 10);
3174 while (p[0] == opchar && p[1] == opchar)
3175 {
3176 long start_lnum = SOURCING_LNUM;
3177 long save_sourcing_lnum;
3178 int start_ctx_lnum = cctx->ctx_lnum;
3179 int save_lnum;
3180 int const_used;
3181 int status;
3182 jumpwhen_T jump_when = opchar == '|'
3183 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3184
3185 if (next != NULL)
3186 {
3187 *arg = next_line_from_context(cctx, TRUE);
3188 p = skipwhite(*arg);
3189 }
3190
3191 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3192 {
3193 semsg(_(e_white_space_required_before_and_after_str_at_str),
3194 op, p);
3195 ga_clear(&end_ga);
3196 return FAIL;
3197 }
3198
3199 save_sourcing_lnum = SOURCING_LNUM;
3200 SOURCING_LNUM = start_lnum;
3201 save_lnum = cctx->ctx_lnum;
3202 cctx->ctx_lnum = start_ctx_lnum;
3203
3204 status = check_ppconst_bool(ppconst);
3205 if (status != FAIL)
3206 {
3207 // Use the last ppconst if possible.
3208 if (ppconst->pp_used > 0)
3209 {
3210 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3211 int is_true = tv2bool(tv);
3212
3213 if ((is_true && opchar == '|')
3214 || (!is_true && opchar == '&'))
3215 {
3216 // For "false && expr" and "true || expr" the "expr"
3217 // does not need to be evaluated.
3218 cctx->ctx_skip = SKIP_YES;
3219 clear_tv(tv);
3220 tv->v_type = VAR_BOOL;
3221 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3222 }
3223 else
3224 {
3225 // For "true && expr" and "false || expr" only "expr"
3226 // needs to be evaluated.
3227 --ppconst->pp_used;
3228 jump_when = JUMP_NEVER;
3229 }
3230 }
3231 else
3232 {
3233 // Every part must evaluate to a bool.
3234 status = bool_on_stack(cctx);
3235 }
3236 }
3237 if (status != FAIL)
3238 status = ga_grow(&end_ga, 1);
3239 cctx->ctx_lnum = save_lnum;
3240 if (status == FAIL)
3241 {
3242 ga_clear(&end_ga);
3243 return FAIL;
3244 }
3245
3246 if (jump_when != JUMP_NEVER)
3247 {
3248 if (cctx->ctx_skip != SKIP_YES)
3249 {
3250 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3251 ++end_ga.ga_len;
3252 }
3253 generate_JUMP(cctx, jump_when, 0);
3254 }
3255
3256 // eval the next expression
3257 SOURCING_LNUM = save_sourcing_lnum;
3258 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3259 {
3260 ga_clear(&end_ga);
3261 return FAIL;
3262 }
3263
3264 const_used = ppconst->pp_used;
3265 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3266 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3267 {
3268 ga_clear(&end_ga);
3269 return FAIL;
3270 }
3271
3272 // "0 || 1" results in true, "1 && 0" results in false.
3273 if (ppconst->pp_used == const_used + 1)
3274 {
3275 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3276
3277 if (tv->v_type == VAR_NUMBER
3278 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3279 {
3280 tv->vval.v_number = tv->vval.v_number == 1
3281 ? VVAL_TRUE : VVAL_FALSE;
3282 tv->v_type = VAR_BOOL;
3283 }
3284 }
3285
3286 p = may_peek_next_line(cctx, *arg, &next);
3287 }
3288
3289 if (check_ppconst_bool(ppconst) == FAIL)
3290 {
3291 ga_clear(&end_ga);
3292 return FAIL;
3293 }
3294
3295 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3296 // Every part must evaluate to a bool.
3297 if (bool_on_stack(cctx) == FAIL)
3298 {
3299 ga_clear(&end_ga);
3300 return FAIL;
3301 }
3302
3303 if (end_ga.ga_len > 0)
3304 {
3305 // Fill in the end label in all jumps.
3306 generate_ppconst(cctx, ppconst);
3307 while (end_ga.ga_len > 0)
3308 {
3309 isn_T *isn;
3310
3311 --end_ga.ga_len;
3312 isn = ((isn_T *)instr->ga_data)
3313 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3314 isn->isn_arg.jump.jump_where = instr->ga_len;
3315 }
3316 }
3317 ga_clear(&end_ga);
3318
3319 cctx->ctx_skip = save_skip;
3320 }
3321
3322 return OK;
3323}
3324
3325/*
3326 * expr4a && expr4a && expr4a logical AND
3327 *
3328 * Produces instructions:
3329 * EVAL expr4a Push result of "expr4a"
3330 * COND2BOOL convert to bool if needed
3331 * JUMP_IF_COND_FALSE end
3332 * EVAL expr4b Push result of "expr4b"
3333 * JUMP_IF_COND_FALSE end
3334 * EVAL expr4c Push result of "expr4c"
3335 * end:
3336 */
3337 static int
3338compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3339{
3340 int ppconst_used = ppconst->pp_used;
3341
3342 // get the first variable
3343 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3344 return FAIL;
3345
3346 // || and && work almost the same
3347 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3348}
3349
3350/*
3351 * expr3a || expr3b || expr3c logical OR
3352 *
3353 * Produces instructions:
3354 * EVAL expr3a Push result of "expr3a"
3355 * COND2BOOL convert to bool if needed
3356 * JUMP_IF_COND_TRUE end
3357 * EVAL expr3b Push result of "expr3b"
3358 * JUMP_IF_COND_TRUE end
3359 * EVAL expr3c Push result of "expr3c"
3360 * end:
3361 */
3362 static int
3363compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3364{
3365 int ppconst_used = ppconst->pp_used;
3366
3367 // eval the first expression
3368 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3369 return FAIL;
3370
3371 // || and && work almost the same
3372 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3373}
3374
3375/*
3376 * Toplevel expression: expr2 ? expr1a : expr1b
3377 * Produces instructions:
3378 * EVAL expr2 Push result of "expr2"
3379 * JUMP_IF_FALSE alt jump if false
3380 * EVAL expr1a
3381 * JUMP_ALWAYS end
3382 * alt: EVAL expr1b
3383 * end:
3384 *
3385 * Toplevel expression: expr2 ?? expr1
3386 * Produces instructions:
3387 * EVAL expr2 Push result of "expr2"
3388 * JUMP_AND_KEEP_IF_TRUE end jump if true
3389 * EVAL expr1
3390 * end:
3391 */
3392 int
3393compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3394{
3395 char_u *p;
3396 int ppconst_used = ppconst->pp_used;
3397 char_u *next;
3398
3399 // Ignore all kinds of errors when not producing code.
3400 if (cctx->ctx_skip == SKIP_YES)
3401 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003402 int prev_did_emsg = did_emsg;
3403
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003404 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003405 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003406 }
3407
3408 // Evaluate the first expression.
3409 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3410 return FAIL;
3411
3412 p = may_peek_next_line(cctx, *arg, &next);
3413 if (*p == '?')
3414 {
3415 int op_falsy = p[1] == '?';
3416 garray_T *instr = &cctx->ctx_instr;
3417 garray_T *stack = &cctx->ctx_type_stack;
3418 int alt_idx = instr->ga_len;
3419 int end_idx = 0;
3420 isn_T *isn;
3421 type_T *type1 = NULL;
3422 int has_const_expr = FALSE;
3423 int const_value = FALSE;
3424 int save_skip = cctx->ctx_skip;
3425
3426 if (next != NULL)
3427 {
3428 *arg = next_line_from_context(cctx, TRUE);
3429 p = skipwhite(*arg);
3430 }
3431
3432 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3433 {
3434 semsg(_(e_white_space_required_before_and_after_str_at_str),
3435 op_falsy ? "??" : "?", p);
3436 return FAIL;
3437 }
3438
3439 if (ppconst->pp_used == ppconst_used + 1)
3440 {
3441 // the condition is a constant, we know whether the ? or the :
3442 // expression is to be evaluated.
3443 has_const_expr = TRUE;
3444 if (op_falsy)
3445 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3446 else
3447 {
3448 int error = FALSE;
3449
3450 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3451 &error);
3452 if (error)
3453 return FAIL;
3454 }
3455 cctx->ctx_skip = save_skip == SKIP_YES ||
3456 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3457
3458 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3459 // "left ?? right" and "left" is truthy: produce "left"
3460 generate_ppconst(cctx, ppconst);
3461 else
3462 {
3463 clear_tv(&ppconst->pp_tv[ppconst_used]);
3464 --ppconst->pp_used;
3465 }
3466 }
3467 else
3468 {
3469 generate_ppconst(cctx, ppconst);
3470 if (op_falsy)
3471 end_idx = instr->ga_len;
3472 generate_JUMP(cctx, op_falsy
3473 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3474 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003475 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003476 }
3477
3478 // evaluate the second expression; any type is accepted
3479 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3480 return FAIL;
3481 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3482 return FAIL;
3483
3484 if (!has_const_expr)
3485 {
3486 generate_ppconst(cctx, ppconst);
3487
3488 if (!op_falsy)
3489 {
3490 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003491 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003492 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003493
3494 end_idx = instr->ga_len;
3495 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3496
3497 // jump here from JUMP_IF_FALSE
3498 isn = ((isn_T *)instr->ga_data) + alt_idx;
3499 isn->isn_arg.jump.jump_where = instr->ga_len;
3500 }
3501 }
3502
3503 if (!op_falsy)
3504 {
3505 // Check for the ":".
3506 p = may_peek_next_line(cctx, *arg, &next);
3507 if (*p != ':')
3508 {
3509 emsg(_(e_missing_colon_after_questionmark));
3510 return FAIL;
3511 }
3512 if (next != NULL)
3513 {
3514 *arg = next_line_from_context(cctx, TRUE);
3515 p = skipwhite(*arg);
3516 }
3517
3518 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3519 {
3520 semsg(_(e_white_space_required_before_and_after_str_at_str),
3521 ":", p);
3522 return FAIL;
3523 }
3524
3525 // evaluate the third expression
3526 if (has_const_expr)
3527 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3528 ? SKIP_YES : SKIP_NOT;
3529 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3530 return FAIL;
3531 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3532 return FAIL;
3533 }
3534
3535 if (!has_const_expr)
3536 {
3537 type_T **typep;
3538
3539 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003540 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003541
3542 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003543 typep = &((((type2_T *)stack->ga_data)
3544 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003545 common_type(type1, *typep, typep, cctx->ctx_type_list);
3546
3547 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3548 isn = ((isn_T *)instr->ga_data) + end_idx;
3549 isn->isn_arg.jump.jump_where = instr->ga_len;
3550 }
3551
3552 cctx->ctx_skip = save_skip;
3553 }
3554 return OK;
3555}
3556
3557/*
3558 * Toplevel expression.
3559 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3560 * Returns OK or FAIL.
3561 */
3562 int
3563compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3564{
3565 ppconst_T ppconst;
3566
3567 CLEAR_FIELD(ppconst);
3568 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3569 {
3570 clear_ppconst(&ppconst);
3571 return FAIL;
3572 }
3573 if (is_const != NULL)
3574 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3575 if (generate_ppconst(cctx, &ppconst) == FAIL)
3576 return FAIL;
3577 return OK;
3578}
3579
3580/*
3581 * Toplevel expression.
3582 */
3583 int
3584compile_expr0(char_u **arg, cctx_T *cctx)
3585{
3586 return compile_expr0_ext(arg, cctx, NULL);
3587}
3588
3589
3590#endif // defined(FEAT_EVAL)