blob: db4cee1b16a81c451cc03728c185211fa32bbae5 [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/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000255 * Compile ".member" coming after an object or class.
256 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000257 static int
258compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
259{
260 if (VIM_ISWHITE((*arg)[1]))
261 {
262 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
263 return FAIL;
264 }
265
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000266 class_T *cl = type->tt_class;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000267 int is_super = type->tt_flags & TTFLAG_SUPER;
268 if (type == &t_super)
269 {
270 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +0000271 {
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000272 emsg(_(e_using_super_not_in_class_function));
273 return FAIL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000274 }
Bram Moolenaar6aa09372023-01-11 17:59:38 +0000275 is_super = TRUE;
276 cl = cctx->ctx_ufunc->uf_class;
277 // Remove &t_super from the stack.
278 --cctx->ctx_type_stack.ga_len;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000279 }
280 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000281 {
282 garray_T *instr = &cctx->ctx_instr;
283 if (instr->ga_len > 0)
284 {
285 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
286 if (isn->isn_type == ISN_LOADSCRIPT)
287 {
288 // The class was recognized as a script item. We only need
289 // to know what class it is, drop the instruction.
290 --instr->ga_len;
291 vim_free(isn->isn_arg.script.scriptref);
292 }
293 }
294 }
295
Ernie Raelf77a7f72023-03-03 15:05:30 +0000296 if (cl == NULL)
297 {
298 // TODO: this should not give an error but be handled at runtime
299 emsg(_(e_incomplete_type));
300 return FAIL;
301 }
302
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000303 ++*arg;
304 char_u *name = *arg;
305 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
306 if (name_end == name)
307 return FAIL;
308 size_t len = name_end - name;
309
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000310 if (*name_end == '(')
311 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000312 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000313 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000314 ufunc_T **functions;
315
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000316 if (type->tt_type == VAR_CLASS)
317 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000318 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000319 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000320 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000321 }
322 else
323 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000324 // type->tt_type == VAR_OBJECT: method call
325 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000326 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000327 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000328 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000329
330 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000331 int fi;
332 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000333 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000334 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000335 // Use a separate pointer to avoid that ASAN complains about
336 // uf_name[] only being 4 characters.
337 char_u *ufname = (char_u *)fp->uf_name;
338 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
339 {
340 ufunc = fp;
341 break;
342 }
343 }
344 if (ufunc == NULL)
345 {
346 // TODO: different error for object method?
347 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
348 return FAIL;
349 }
350
351 // Compile the arguments and call the class function or object method.
352 // The object method will know that the object is on the stack, just
353 // before the arguments.
354 *arg = skipwhite(name_end + 1);
355 int argcount = 0;
356 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
357 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000358
359 if (type->tt_type == VAR_OBJECT
360 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
h-east2261c892023-08-16 21:49:54 +0900361 return generate_CALL(cctx, ufunc, cl, fi, type, argcount);
362 return generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000363 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000364
365 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000366 {
367 for (int i = 0; i < cl->class_obj_member_count; ++i)
368 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000369 ocmember_T *m = &cl->class_obj_members[i];
370 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000371 {
Bram Moolenaar62a69232023-01-24 15:07:04 +0000372 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000373 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000374 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000375 return FAIL;
376 }
377
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000378 *arg = name_end;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000379 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000380 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000381 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000382 }
383 }
384
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000385 // Could be a function reference: "obj.Func".
386 for (int i = 0; i < cl->class_obj_method_count; ++i)
387 {
388 ufunc_T *fp = cl->class_obj_methods[i];
389 // Use a separate pointer to avoid that ASAN complains about
390 // uf_name[] only being 4 characters.
391 char_u *ufname = (char_u *)fp->uf_name;
392 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar313e4722023-02-08 20:55:27 +0000393 {
394 if (type->tt_type == VAR_OBJECT
395 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
396 return generate_FUNCREF(cctx, fp, cl, i, NULL);
397 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
398 }
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000399 }
400
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000401 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
402 }
403 else
404 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000405 // load class member
406 int idx;
407 for (idx = 0; idx < cl->class_class_member_count; ++idx)
408 {
409 ocmember_T *m = &cl->class_class_members[idx];
410 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
411 break;
412 }
413 if (idx < cl->class_class_member_count)
414 {
415 *arg = name_end;
416 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
417 }
418 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000419 }
420
421 return FAIL;
422}
423
424/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000425 * Generate an instruction to load script-local variable "name", without the
426 * leading "s:".
427 * Also finds imported variables.
428 */
429 int
430compile_load_scriptvar(
431 cctx_T *cctx,
432 char_u *name, // variable NUL terminated
433 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100434 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000435{
436 scriptitem_T *si;
437 int idx;
438 imported_T *import;
439
440 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
441 return FAIL;
442 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000443 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000444 if (idx >= 0)
445 {
446 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
447
448 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
449 current_sctx.sc_sid, idx, sv->sv_type);
450 return OK;
451 }
452
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000453 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454 if (import != NULL)
455 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000456 char_u *p = skipwhite(*end);
457 char_u *exp_name;
458 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100459 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000460 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000461 int done = FALSE;
462 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000463
464 // Need to lookup the member.
465 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000466 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000467 semsg(_(e_expected_dot_after_name_str), start);
468 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000470 ++p;
471 if (VIM_ISWHITE(*p))
472 {
473 emsg(_(e_no_white_space_allowed_after_dot));
474 return FAIL;
475 }
476
477 // isolate one name
478 exp_name = p;
479 while (eval_isnamec(*p))
480 ++p;
481 cc = *p;
482 *p = NUL;
483
Bram Moolenaard041f422022-01-12 19:54:00 +0000484 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100485 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
486 // "import autoload './dir/script.vim'" or
487 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100488 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100489
490 if (res == OK)
491 {
492 if (si->sn_autoload_prefix != NULL
493 && si->sn_state == SN_STATE_NOT_LOADED)
494 {
495 char_u *auto_name =
496 concat_str(si->sn_autoload_prefix, exp_name);
497
498 // autoload script must be loaded later, access by the autoload
499 // name. If a '(' follows it must be a function. Otherwise we
500 // don't know, it can be "script.Func".
501 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100502 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100503 else
504 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
505 vim_free(auto_name);
506 done = TRUE;
507 }
508 else if (si->sn_import_autoload
509 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100510 {
511 // If a '(' follows it must be a function. Otherwise we don't
512 // know, it can be "script.Func".
513 if (cc == '(' || paren_follows_after_expr)
514 {
515 char_u sid_name[MAX_FUNC_NAME_LEN];
516
517 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100518 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100519 }
520 else
521 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
522 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100523 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100524 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100525 else
526 {
527 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
528 cctx, NULL, TRUE);
529 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100530 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100531
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000532 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000533 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000534 if (done)
535 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000536
537 if (idx < 0)
538 {
539 if (ufunc != NULL)
540 {
541 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100542 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000543 return OK;
544 }
545 return FAIL;
546 }
547
548 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
549 import->imp_sid,
550 idx,
551 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000552 return OK;
553 }
554
Bram Moolenaard0132f42022-05-12 11:05:40 +0100555 // Can only get here if we know "name" is a script variable and not in a
556 // Vim9 script (variable is not in sn_var_vals): old style script.
557 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000558 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000559}
560
561 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000562generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000563{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000564 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000565 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000566
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000567 // Reject a global non-autoload function found without the "g:" prefix.
568 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569 return FAIL;
570
571 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000572 compile_type = get_compile_type(ufunc);
573 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000574 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000575 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100576 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000577}
578
579/*
580 * Compile a variable name into a load instruction.
581 * "end" points to just after the name.
582 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
583 * When "error" is FALSE do not give an error when not found.
584 */
585 int
586compile_load(
587 char_u **arg,
588 char_u *end_arg,
589 cctx_T *cctx,
590 int is_expr,
591 int error)
592{
593 type_T *type;
594 char_u *name = NULL;
595 char_u *end = end_arg;
596 int res = FAIL;
597 int prev_called_emsg = called_emsg;
598
599 if (*(*arg + 1) == ':')
600 {
601 if (end <= *arg + 2)
602 {
603 isntype_T isn_type;
604
605 // load dictionary of namespace
606 switch (**arg)
607 {
608 case 'g': isn_type = ISN_LOADGDICT; break;
609 case 'w': isn_type = ISN_LOADWDICT; break;
610 case 't': isn_type = ISN_LOADTDICT; break;
611 case 'b': isn_type = ISN_LOADBDICT; break;
612 default:
613 semsg(_(e_namespace_not_supported_str), *arg);
614 goto theend;
615 }
616 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
617 goto theend;
618 res = OK;
619 }
620 else
621 {
622 isntype_T isn_type = ISN_DROP;
623
624 // load namespaced variable
625 name = vim_strnsave(*arg + 2, end - (*arg + 2));
626 if (name == NULL)
627 return FAIL;
628
629 switch (**arg)
630 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100631 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000632 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000633 case 's': if (current_script_is_vim9())
634 {
635 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
636 *arg);
637 vim_free(name);
638 return FAIL;
639 }
Kota Kato948a3892022-08-16 16:09:59 +0100640 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000641 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 else
643 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100644 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645 break;
646 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
647 {
648 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000649 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000650 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000651 else
652 isn_type = ISN_LOADG;
653 }
654 else
655 {
656 isn_type = ISN_LOADAUTO;
657 vim_free(name);
658 name = vim_strnsave(*arg, end - *arg);
659 if (name == NULL)
660 return FAIL;
661 }
662 break;
663 case 'w': isn_type = ISN_LOADW; break;
664 case 't': isn_type = ISN_LOADT; break;
665 case 'b': isn_type = ISN_LOADB; break;
666 default: // cannot happen, just in case
667 semsg(_(e_namespace_not_supported_str), *arg);
668 goto theend;
669 }
670 if (isn_type != ISN_DROP)
671 {
672 // Global, Buffer-local, Window-local and Tabpage-local
673 // variables can be defined later, thus we don't check if it
674 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000675 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676 }
677 }
678 }
679 else
680 {
681 size_t len = end - *arg;
682 int idx;
683 int gen_load = FALSE;
684 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100685 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100686 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687
688 name = vim_strnsave(*arg, end - *arg);
689 if (name == NULL)
690 return FAIL;
691
Bram Moolenaar58b40092023-01-11 15:59:05 +0000692 if (STRCMP(name, "super") == 0
693 && cctx->ctx_ufunc != NULL
694 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
695 {
696 // super.SomeFunc() in a class function: push &t_super type, this
697 // is recognized in compile_subscript().
698 res = push_type_stack(cctx, &t_super);
699 if (*end != '.')
700 emsg(_(e_super_must_be_followed_by_dot));
701 }
702 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000703 {
704 script_autoload(name, FALSE);
705 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
706 }
707 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
708 == OK)
709 {
710 if (gen_load_outer == 0)
711 gen_load = TRUE;
712 }
713 else
714 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000715 lvar_T lvar;
716 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717
718 if (lookup_local(*arg, len, &lvar, cctx) == OK)
719 {
720 type = lvar.lv_type;
721 idx = lvar.lv_idx;
722 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100723 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000724 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100725 outer_loop_depth = lvar.lv_loop_depth;
726 outer_loop_idx = lvar.lv_loop_idx;
727 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000728 else
729 gen_load = TRUE;
730 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000731 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000732 {
733 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
734 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000735 else
736 {
737 // "var" can be script-local even without using "s:" if it
738 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000739 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000740 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100741 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742
743 // When evaluating an expression and the name starts with an
744 // uppercase letter it can be a user defined function.
745 // generate_funcref() will fail if the function can't be found.
746 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000747 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 }
749 }
750 if (gen_load)
751 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
752 if (gen_load_outer > 0)
753 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100754 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
755 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756 cctx->ctx_outer_used = TRUE;
757 }
758 }
759
760 *arg = end;
761
762theend:
763 if (res == FAIL && error && called_emsg == prev_called_emsg)
764 semsg(_(e_variable_not_found_str), name);
765 vim_free(name);
766 return res;
767}
768
769/*
770 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100771 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 * Returns FAIL if compilation fails.
773 */
774 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100775compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776{
LemonBoyf3b48952022-05-05 13:53:03 +0100777 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000778 garray_T save_ga = cctx->ctx_instr;
779 int expr_res;
780 int trailing_error;
781 int instr_count;
782 isn_T *instr = NULL;
783
784 // Remove the string type from the stack.
785 --cctx->ctx_type_stack.ga_len;
786
787 // Temporarily reset the list of instructions so that the jump labels are
788 // correct.
789 cctx->ctx_instr.ga_len = 0;
790 cctx->ctx_instr.ga_maxlen = 0;
791 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000792
793 // avoid peeking a next line
794 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
795 cctx->ctx_ufunc->uf_lines.ga_len = 0;
796
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000798
799 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
800
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 s = skipwhite(s);
802 trailing_error = *s != NUL;
803
804 if (expr_res == FAIL || trailing_error
805 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
806 {
807 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000808 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809 clear_instr_ga(&cctx->ctx_instr);
810 cctx->ctx_instr = save_ga;
811 ++cctx->ctx_type_stack.ga_len;
812 return FAIL;
813 }
814
815 // Move the generated instructions into the ISN_INSTR instruction, then
816 // restore the list of instructions.
817 instr_count = cctx->ctx_instr.ga_len;
818 instr = cctx->ctx_instr.ga_data;
819 instr[instr_count].isn_type = ISN_FINISH;
820
821 cctx->ctx_instr = save_ga;
822 vim_free(isn->isn_arg.string);
823 isn->isn_type = ISN_INSTR;
824 isn->isn_arg.instr = instr;
825 return OK;
826}
827
828/*
829 * Compile the argument expressions.
830 * "arg" points to just after the "(" and is advanced to after the ")"
831 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100832 int
LemonBoyf3b48952022-05-05 13:53:03 +0100833compile_arguments(
834 char_u **arg,
835 cctx_T *cctx,
836 int *argcount,
837 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000838{
839 char_u *p = *arg;
840 char_u *whitep = *arg;
841 int must_end = FALSE;
842 int instr_count;
843
844 for (;;)
845 {
846 if (may_get_next_line(whitep, &p, cctx) == FAIL)
847 goto failret;
848 if (*p == ')')
849 {
850 *arg = p + 1;
851 return OK;
852 }
853 if (must_end)
854 {
855 semsg(_(e_missing_comma_before_argument_str), p);
856 return FAIL;
857 }
858
859 instr_count = cctx->ctx_instr.ga_len;
860 if (compile_expr0(&p, cctx) == FAIL)
861 return FAIL;
862 ++*argcount;
863
LemonBoyf3b48952022-05-05 13:53:03 +0100864 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 && cctx->ctx_instr.ga_len == instr_count + 1)
866 {
867 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
868
869 // {skip} argument of searchpair() can be compiled if not empty
870 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100871 compile_string(isn, cctx, 0);
872 }
873 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
874 && cctx->ctx_instr.ga_len == instr_count + 1)
875 {
876 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
877
878 // {sub} argument of substitute() can be compiled if it starts
879 // with \=
880 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100881 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100882 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 }
884
885 if (*p != ',' && *skipwhite(p) == ',')
886 {
887 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
888 p = skipwhite(p);
889 }
890 if (*p == ',')
891 {
892 ++p;
893 if (*p != NUL && !VIM_ISWHITE(*p))
894 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
895 }
896 else
897 must_end = TRUE;
898 whitep = p;
899 p = skipwhite(p);
900 }
901failret:
902 emsg(_(e_missing_closing_paren));
903 return FAIL;
904}
905
906/*
907 * Compile a function call: name(arg1, arg2)
908 * "arg" points to "name", "arg + varlen" to the "(".
909 * "argcount_init" is 1 for "value->method()"
910 * Instructions:
911 * EVAL arg1
912 * EVAL arg2
913 * BCALL / DCALL / UCALL
914 */
915 static int
916compile_call(
917 char_u **arg,
918 size_t varlen,
919 cctx_T *cctx,
920 ppconst_T *ppconst,
921 int argcount_init)
922{
923 char_u *name = *arg;
924 char_u *p;
925 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100926 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 char_u fname_buf[FLEN_FIXED + 1];
928 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929 ufunc_T *ufunc = NULL;
930 int res = FAIL;
931 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000932 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100933 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000934 imported_T *import;
h-east2261c892023-08-16 21:49:54 +0900935 type_T *type;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000936
937 if (varlen >= sizeof(namebuf))
938 {
939 semsg(_(e_name_too_long_str), name);
940 return FAIL;
941 }
942 vim_strncpy(namebuf, *arg, varlen);
943
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000944 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000945 if (import != NULL)
946 {
947 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
948 return FAIL;
949 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950
951 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100952 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000953 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100954 if ((varlen == 3
955 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000956 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
957 {
958 char_u *s = skipwhite(*arg + varlen + 1);
959 typval_T argvars[2];
960 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100961 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000962
963 argvars[0].v_type = VAR_UNKNOWN;
964 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100965 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100967 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000968 s = skipwhite(s);
969 if (*s == ')' && argvars[0].v_type == VAR_STRING
970 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
971 || !is_has))
972 {
973 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
974
975 *arg = s + 1;
976 argvars[1].v_type = VAR_UNKNOWN;
977 tv->v_type = VAR_NUMBER;
978 tv->vval.v_number = 0;
979 if (is_has)
980 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100981 else if (is_len)
982 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 else
984 f_exists(argvars, tv);
985 clear_tv(&argvars[0]);
986 ++ppconst->pp_used;
987 return OK;
988 }
989 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100990 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000991 {
992 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
993 return FAIL;
994 }
995 }
996
997 if (generate_ppconst(cctx, ppconst) == FAIL)
998 return FAIL;
999
Bram Moolenaar3e1ac142023-02-18 19:49:32 +00001000 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
1002
1003 // We handle the "skip" argument of searchpair() and searchpairpos()
1004 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +01001005 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
1006 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
1007 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1008 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1009 special_fn = CA_SEARCHPAIR;
1010 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1011 special_fn = CA_SUBSTITUTE;
1012 else
1013 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001014
1015 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001016 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001017 goto theend;
1018
h-east2261c892023-08-16 21:49:54 +09001019 type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1021 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1022 {
1023 int idx;
1024
1025 // builtin function
1026 idx = find_internal_func(name);
1027 if (idx >= 0)
1028 {
1029 if (STRCMP(name, "flatten") == 0)
1030 {
1031 emsg(_(e_cannot_use_flatten_in_vim9_script));
1032 goto theend;
1033 }
1034
1035 if (STRCMP(name, "add") == 0 && argcount == 2)
1036 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001037 // add() can be compiled to instructions if we know the type
1038 if (type->tt_type == VAR_LIST)
1039 {
1040 // inline "add(list, item)" so that the type can be checked
1041 res = generate_LISTAPPEND(cctx);
1042 idx = -1;
1043 }
1044 else if (type->tt_type == VAR_BLOB)
1045 {
1046 // inline "add(blob, nr)" so that the type can be checked
1047 res = generate_BLOBAPPEND(cctx);
1048 idx = -1;
1049 }
1050 }
1051
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001052 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1053 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001054 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001055 // May have the "D" or "R" flag, reserve a variable for a
1056 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001057 if (get_defer_var_idx(cctx) == 0)
1058 idx = -1;
1059 }
1060
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001061 if (idx >= 0)
1062 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1063 }
1064 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001065 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001066 goto theend;
1067 }
1068
Bram Moolenaar62aec932022-01-29 21:45:34 +00001069 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1070
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 // An argument or local variable can be a function reference, this
1072 // overrules a function name.
1073 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1074 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1075 {
1076 // If we can find the function by name generate the right call.
1077 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001078 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001079 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001080 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001081 if (!func_is_global(ufunc))
1082 {
h-east2261c892023-08-16 21:49:54 +09001083 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001084 goto theend;
1085 }
1086 if (!has_g_namespace
1087 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1088 {
1089 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001090 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001091 goto theend;
1092 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001093 }
1094 }
1095
1096 // If the name is a variable, load it and use PCALL.
1097 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001098 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001100 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1102 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001103 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001104
1105 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1106 goto theend;
1107 }
1108
1109 // If we can find a global function by name generate the right call.
1110 if (ufunc != NULL)
1111 {
h-east2261c892023-08-16 21:49:54 +09001112 res = generate_CALL(cctx, ufunc, NULL, 0, type, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001113 goto theend;
1114 }
1115
1116 // A global function may be defined only later. Need to figure out at
1117 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001118 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001119 res = generate_UCALL(cctx, name, argcount);
1120 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001121 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001122
1123theend:
1124 vim_free(tofree);
1125 return res;
1126}
1127
1128// like NAMESPACE_CHAR but with 'a' and 'l'.
1129#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1130
1131/*
1132 * Find the end of a variable or function name. Unlike find_name_end() this
1133 * does not recognize magic braces.
1134 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1135 * Return a pointer to just after the name. Equal to "arg" if there is no
1136 * valid name.
1137 */
1138 char_u *
1139to_name_end(char_u *arg, int use_namespace)
1140{
1141 char_u *p;
1142
1143 // Quick check for valid starting character.
1144 if (!eval_isnamec1(*arg))
1145 return arg;
1146
1147 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1148 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1149 // and can be used in slice "[n:]".
1150 if (*p == ':' && (p != arg + 1
1151 || !use_namespace
1152 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1153 break;
1154 return p;
1155}
1156
1157/*
1158 * Like to_name_end() but also skip over a list or dict constant.
1159 * Also accept "<SNR>123_Func".
1160 * This intentionally does not handle line continuation.
1161 */
1162 char_u *
1163to_name_const_end(char_u *arg)
1164{
1165 char_u *p = arg;
1166 typval_T rettv;
1167
1168 if (STRNCMP(p, "<SNR>", 5) == 0)
1169 p = skipdigits(p + 5);
1170 p = to_name_end(p, TRUE);
1171 if (p == arg && *arg == '[')
1172 {
1173
1174 // Can be "[1, 2, 3]->Func()".
1175 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1176 p = arg;
1177 }
1178 return p;
1179}
1180
1181/*
1182 * parse a list: [expr, expr]
1183 * "*arg" points to the '['.
1184 * ppconst->pp_is_const is set if all items are a constant.
1185 */
1186 static int
1187compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1188{
1189 char_u *p = skipwhite(*arg + 1);
1190 char_u *whitep = *arg + 1;
1191 int count = 0;
1192 int is_const;
1193 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001194 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195
1196 for (;;)
1197 {
1198 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1199 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001200 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 return FAIL;
1202 }
1203 if (*p == ',')
1204 {
1205 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1206 return FAIL;
1207 }
1208 if (*p == ']')
1209 {
1210 ++p;
1211 break;
1212 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001213 if (must_end)
1214 {
1215 semsg(_(e_missing_comma_in_list_str), p);
1216 return FAIL;
1217 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1219 return FAIL;
1220 if (!is_const)
1221 is_all_const = FALSE;
1222 ++count;
1223 if (*p == ',')
1224 {
1225 ++p;
1226 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1227 {
1228 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1229 return FAIL;
1230 }
1231 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001232 else
1233 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234 whitep = p;
1235 p = skipwhite(p);
1236 }
1237 *arg = p;
1238
1239 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001240 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001241}
1242
1243/*
1244 * Parse a lambda: "(arg, arg) => expr"
1245 * "*arg" points to the '('.
1246 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1247 */
1248 static int
1249compile_lambda(char_u **arg, cctx_T *cctx)
1250{
1251 int r;
1252 typval_T rettv;
1253 ufunc_T *ufunc;
1254 evalarg_T evalarg;
1255
1256 init_evalarg(&evalarg);
1257 evalarg.eval_flags = EVAL_EVALUATE;
1258 evalarg.eval_cctx = cctx;
1259
1260 // Get the funcref in "rettv".
1261 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1262 if (r != OK)
1263 {
1264 clear_evalarg(&evalarg, NULL);
1265 return r;
1266 }
1267
1268 // "rettv" will now be a partial referencing the function.
1269 ufunc = rettv.vval.v_partial->pt_func;
1270 ++ufunc->uf_refcount;
1271 clear_tv(&rettv);
1272
1273 // Compile it here to get the return type. The return type is optional,
1274 // when it's missing use t_unknown. This is recognized in
1275 // compile_return().
1276 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1277 ufunc->uf_ret_type = &t_unknown;
1278 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1279
1280 // When the outer function is compiled for profiling or debugging, the
1281 // lambda may be called without profiling or debugging. Compile it here in
1282 // the right context.
1283 if (cctx->ctx_compile_type == CT_DEBUG
1284#ifdef FEAT_PROFILE
1285 || cctx->ctx_compile_type == CT_PROFILE
1286#endif
1287 )
1288 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1289
Bram Moolenaar139575d2022-03-15 19:29:30 +00001290 // if the outer function is not compiled for debugging or profiling, this
1291 // one might be
1292 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001293 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001294 compiletype_T compile_type = get_compile_type(ufunc);
1295
1296 if (compile_type != CT_NONE)
1297 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001298 }
1299
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1301 // "*arg" may point into it. Point into the original line to avoid a
1302 // dangling pointer.
1303 if (evalarg.eval_using_cmdline)
1304 {
1305 garray_T *gap = &evalarg.eval_tofree_ga;
1306 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1307
1308 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1309 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001310 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001311 }
1312
1313 clear_evalarg(&evalarg, NULL);
1314
1315 if (ufunc->uf_def_status == UF_COMPILED)
1316 {
1317 // The return type will now be known.
1318 set_function_type(ufunc);
1319
1320 // The function reference count will be 1. When the ISN_FUNCREF
1321 // instruction is deleted the reference count is decremented and the
1322 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001323 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 }
1325
1326 func_ptr_unref(ufunc);
1327 return FAIL;
1328}
1329
1330/*
1331 * Get a lambda and compile it. Uses Vim9 syntax.
1332 */
1333 int
1334get_lambda_tv_and_compile(
1335 char_u **arg,
1336 typval_T *rettv,
1337 int types_optional,
1338 evalarg_T *evalarg)
1339{
1340 int r;
1341 ufunc_T *ufunc;
1342 int save_sc_version = current_sctx.sc_version;
1343
1344 // Get the funcref in "rettv".
1345 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1346 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1347 current_sctx.sc_version = save_sc_version;
1348 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001349 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350
1351 // "rettv" will now be a partial referencing the function.
1352 ufunc = rettv->vval.v_partial->pt_func;
1353
1354 // Compile it here to get the return type. The return type is optional,
1355 // when it's missing use t_unknown. This is recognized in
1356 // compile_return().
1357 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1358 ufunc->uf_ret_type = &t_unknown;
1359 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1360
1361 if (ufunc->uf_def_status == UF_COMPILED)
1362 {
1363 // The return type will now be known.
1364 set_function_type(ufunc);
1365 return OK;
1366 }
1367 clear_tv(rettv);
1368 return FAIL;
1369}
1370
1371/*
1372 * parse a dict: {key: val, [key]: val}
1373 * "*arg" points to the '{'.
1374 * ppconst->pp_is_const is set if all item values are a constant.
1375 */
1376 static int
1377compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1378{
1379 garray_T *instr = &cctx->ctx_instr;
1380 int count = 0;
1381 dict_T *d = dict_alloc();
1382 dictitem_T *item;
1383 char_u *whitep = *arg + 1;
1384 char_u *p;
1385 int is_const;
1386 int is_all_const = TRUE; // reset when non-const encountered
1387
1388 if (d == NULL)
1389 return FAIL;
1390 if (generate_ppconst(cctx, ppconst) == FAIL)
1391 return FAIL;
1392 for (;;)
1393 {
1394 char_u *key = NULL;
1395
1396 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1397 {
1398 *arg = NULL;
1399 goto failret;
1400 }
1401
1402 if (**arg == '}')
1403 break;
1404
1405 if (**arg == '[')
1406 {
1407 isn_T *isn;
1408
1409 // {[expr]: value} uses an evaluated key.
1410 *arg = skipwhite(*arg + 1);
1411 if (compile_expr0(arg, cctx) == FAIL)
1412 return FAIL;
1413 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1414 if (isn->isn_type == ISN_PUSHNR)
1415 {
1416 char buf[NUMBUFLEN];
1417
1418 // Convert to string at compile time.
1419 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1420 isn->isn_type = ISN_PUSHS;
1421 isn->isn_arg.string = vim_strsave((char_u *)buf);
1422 }
1423 if (isn->isn_type == ISN_PUSHS)
1424 key = isn->isn_arg.string;
1425 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1426 return FAIL;
1427 *arg = skipwhite(*arg);
1428 if (**arg != ']')
1429 {
1430 emsg(_(e_missing_matching_bracket_after_dict_key));
1431 return FAIL;
1432 }
1433 ++*arg;
1434 }
1435 else
1436 {
1437 // {"name": value},
1438 // {'name': value},
1439 // {name: value} use "name" as a literal key
1440 key = get_literal_key(arg);
1441 if (key == NULL)
1442 return FAIL;
1443 if (generate_PUSHS(cctx, &key) == FAIL)
1444 return FAIL;
1445 }
1446
1447 // Check for duplicate keys, if using string keys.
1448 if (key != NULL)
1449 {
1450 item = dict_find(d, key, -1);
1451 if (item != NULL)
1452 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001453 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 goto failret;
1455 }
1456 item = dictitem_alloc(key);
1457 if (item != NULL)
1458 {
1459 item->di_tv.v_type = VAR_UNKNOWN;
1460 item->di_tv.v_lock = 0;
1461 if (dict_add(d, item) == FAIL)
1462 dictitem_free(item);
1463 }
1464 }
1465
1466 if (**arg != ':')
1467 {
1468 if (*skipwhite(*arg) == ':')
1469 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1470 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001471 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001472 return FAIL;
1473 }
1474 whitep = *arg + 1;
1475 if (!IS_WHITE_OR_NUL(*whitep))
1476 {
1477 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1478 return FAIL;
1479 }
1480
1481 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1482 {
1483 *arg = NULL;
1484 goto failret;
1485 }
1486
1487 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1488 return FAIL;
1489 if (!is_const)
1490 is_all_const = FALSE;
1491 ++count;
1492
1493 whitep = *arg;
1494 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1495 {
1496 *arg = NULL;
1497 goto failret;
1498 }
1499 if (**arg == '}')
1500 break;
1501 if (**arg != ',')
1502 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001503 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001504 goto failret;
1505 }
1506 if (IS_WHITE_OR_NUL(*whitep))
1507 {
1508 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1509 return FAIL;
1510 }
1511 whitep = *arg + 1;
1512 if (!IS_WHITE_OR_NUL(*whitep))
1513 {
1514 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1515 return FAIL;
1516 }
1517 *arg = skipwhite(whitep);
1518 }
1519
1520 *arg = *arg + 1;
1521
1522 // Allow for following comment, after at least one space.
1523 p = skipwhite(*arg);
1524 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1525 *arg += STRLEN(*arg);
1526
1527 dict_unref(d);
1528 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001529 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001530
1531failret:
1532 if (*arg == NULL)
1533 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001534 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001535 *arg = (char_u *)"";
1536 }
1537 dict_unref(d);
1538 return FAIL;
1539}
1540
1541/*
1542 * Compile "&option".
1543 */
1544 static int
1545compile_get_option(char_u **arg, cctx_T *cctx)
1546{
1547 typval_T rettv;
1548 char_u *start = *arg;
1549 int ret;
1550
1551 // parse the option and get the current value to get the type.
1552 rettv.v_type = VAR_UNKNOWN;
1553 ret = eval_option(arg, &rettv, TRUE);
1554 if (ret == OK)
1555 {
1556 // include the '&' in the name, eval_option() expects it.
1557 char_u *name = vim_strnsave(start, *arg - start);
1558 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1559 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1560
1561 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1562 vim_free(name);
1563 }
1564 clear_tv(&rettv);
1565
1566 return ret;
1567}
1568
1569/*
1570 * Compile "$VAR".
1571 */
1572 static int
1573compile_get_env(char_u **arg, cctx_T *cctx)
1574{
1575 char_u *start = *arg;
1576 int len;
1577 int ret;
1578 char_u *name;
1579
1580 ++*arg;
1581 len = get_env_len(arg);
1582 if (len == 0)
1583 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001584 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001585 return FAIL;
1586 }
1587
1588 // include the '$' in the name, eval_env_var() expects it.
1589 name = vim_strnsave(start, len + 1);
1590 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1591 vim_free(name);
1592 return ret;
1593}
1594
1595/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001596 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001597 */
1598 static int
1599compile_interp_string(char_u **arg, cctx_T *cctx)
1600{
1601 typval_T tv;
1602 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001603 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001604 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001605 int count = 0;
1606 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001607
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001608 // *arg is on the '$' character, move it to the first string character.
1609 ++*arg;
1610 quote = **arg;
1611 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001612
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001613 for (;;)
1614 {
1615 // Get the string up to the matching quote or to a single '{'.
1616 // "arg" is advanced to either the quote or the '{'.
1617 if (quote == '"')
1618 ret = eval_string(arg, &tv, evaluate, TRUE);
1619 else
1620 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1621 if (ret == FAIL)
1622 break;
1623 if (evaluate)
1624 {
1625 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1626 || (**arg != '{' && count == 0))
1627 {
1628 // generate non-empty string or empty string if it's the only
1629 // one
1630 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1631 return FAIL;
1632 tv.vval.v_string = NULL; // don't free it now
1633 ++count;
1634 }
1635 clear_tv(&tv);
1636 }
1637
1638 if (**arg != '{')
1639 {
1640 // found terminating quote
1641 ++*arg;
1642 break;
1643 }
1644
1645 p = compile_one_expr_in_str(*arg, cctx);
1646 if (p == NULL)
1647 {
1648 ret = FAIL;
1649 break;
1650 }
1651 ++count;
1652 *arg = p;
1653 }
LemonBoy2eaef102022-05-06 13:14:50 +01001654
1655 if (ret == FAIL || !evaluate)
1656 return ret;
1657
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001658 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1659 if (count > 1)
1660 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001661
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001662 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001663}
1664
1665/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001666 * Compile "@r".
1667 */
1668 static int
1669compile_get_register(char_u **arg, cctx_T *cctx)
1670{
1671 int ret;
1672
1673 ++*arg;
1674 if (**arg == NUL)
1675 {
1676 semsg(_(e_syntax_error_at_str), *arg - 1);
1677 return FAIL;
1678 }
1679 if (!valid_yank_reg(**arg, FALSE))
1680 {
1681 emsg_invreg(**arg);
1682 return FAIL;
1683 }
1684 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1685 ++*arg;
1686 return ret;
1687}
1688
1689/*
1690 * Apply leading '!', '-' and '+' to constant "rettv".
1691 * When "numeric_only" is TRUE do not apply '!'.
1692 */
1693 static int
1694apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1695{
1696 char_u *p = *end;
1697
1698 // this works from end to start
1699 while (p > start)
1700 {
1701 --p;
1702 if (*p == '-' || *p == '+')
1703 {
1704 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001705 if (rettv->v_type == VAR_FLOAT)
1706 {
1707 if (*p == '-')
1708 rettv->vval.v_float = -rettv->vval.v_float;
1709 }
1710 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001711 {
1712 varnumber_T val;
1713 int error = FALSE;
1714
1715 // tv_get_number_chk() accepts a string, but we don't want that
1716 // here
1717 if (check_not_string(rettv) == FAIL)
1718 return FAIL;
1719 val = tv_get_number_chk(rettv, &error);
1720 clear_tv(rettv);
1721 if (error)
1722 return FAIL;
1723 if (*p == '-')
1724 val = -val;
1725 rettv->v_type = VAR_NUMBER;
1726 rettv->vval.v_number = val;
1727 }
1728 }
1729 else if (numeric_only)
1730 {
1731 ++p;
1732 break;
1733 }
1734 else if (*p == '!')
1735 {
1736 int v = tv2bool(rettv);
1737
1738 // '!' is permissive in the type.
1739 clear_tv(rettv);
1740 rettv->v_type = VAR_BOOL;
1741 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1742 }
1743 }
1744 *end = p;
1745 return OK;
1746}
1747
1748/*
1749 * Recognize v: variables that are constants and set "rettv".
1750 */
1751 static void
1752get_vim_constant(char_u **arg, typval_T *rettv)
1753{
1754 if (STRNCMP(*arg, "v:true", 6) == 0)
1755 {
1756 rettv->v_type = VAR_BOOL;
1757 rettv->vval.v_number = VVAL_TRUE;
1758 *arg += 6;
1759 }
1760 else if (STRNCMP(*arg, "v:false", 7) == 0)
1761 {
1762 rettv->v_type = VAR_BOOL;
1763 rettv->vval.v_number = VVAL_FALSE;
1764 *arg += 7;
1765 }
1766 else if (STRNCMP(*arg, "v:null", 6) == 0)
1767 {
1768 rettv->v_type = VAR_SPECIAL;
1769 rettv->vval.v_number = VVAL_NULL;
1770 *arg += 6;
1771 }
1772 else if (STRNCMP(*arg, "v:none", 6) == 0)
1773 {
1774 rettv->v_type = VAR_SPECIAL;
1775 rettv->vval.v_number = VVAL_NONE;
1776 *arg += 6;
1777 }
1778}
1779
1780 exprtype_T
1781get_compare_type(char_u *p, int *len, int *type_is)
1782{
1783 exprtype_T type = EXPR_UNKNOWN;
1784 int i;
1785
1786 switch (p[0])
1787 {
1788 case '=': if (p[1] == '=')
1789 type = EXPR_EQUAL;
1790 else if (p[1] == '~')
1791 type = EXPR_MATCH;
1792 break;
1793 case '!': if (p[1] == '=')
1794 type = EXPR_NEQUAL;
1795 else if (p[1] == '~')
1796 type = EXPR_NOMATCH;
1797 break;
1798 case '>': if (p[1] != '=')
1799 {
1800 type = EXPR_GREATER;
1801 *len = 1;
1802 }
1803 else
1804 type = EXPR_GEQUAL;
1805 break;
1806 case '<': if (p[1] != '=')
1807 {
1808 type = EXPR_SMALLER;
1809 *len = 1;
1810 }
1811 else
1812 type = EXPR_SEQUAL;
1813 break;
1814 case 'i': if (p[1] == 's')
1815 {
1816 // "is" and "isnot"; but not a prefix of a name
1817 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1818 *len = 5;
1819 i = p[*len];
1820 if (!isalnum(i) && i != '_')
1821 {
1822 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1823 *type_is = TRUE;
1824 }
1825 }
1826 break;
1827 }
1828 return type;
1829}
1830
1831/*
1832 * Skip over an expression, ignoring most errors.
1833 */
1834 void
1835skip_expr_cctx(char_u **arg, cctx_T *cctx)
1836{
1837 evalarg_T evalarg;
1838
1839 init_evalarg(&evalarg);
1840 evalarg.eval_cctx = cctx;
1841 skip_expr(arg, &evalarg);
1842 clear_evalarg(&evalarg, NULL);
1843}
1844
1845/*
1846 * Check that the top of the type stack has a type that can be used as a
1847 * condition. Give an error and return FAIL if not.
1848 */
1849 int
1850bool_on_stack(cctx_T *cctx)
1851{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001852 type_T *type;
1853
Bram Moolenaar078a4612022-01-04 15:17:03 +00001854 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855 if (type == &t_bool)
1856 return OK;
1857
Bram Moolenaar4913d422022-10-17 13:13:32 +01001858 if (type->tt_type == VAR_ANY
1859 || type->tt_type == VAR_UNKNOWN
1860 || type->tt_type == VAR_NUMBER
1861 || type == &t_number_bool
1862 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1864 // This requires a runtime type check.
1865 return generate_COND2BOOL(cctx);
1866
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001867 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001868}
1869
1870/*
1871 * Give the "white on both sides" error, taking the operator from "p[len]".
1872 */
1873 void
1874error_white_both(char_u *op, int len)
1875{
1876 char_u buf[10];
1877
1878 vim_strncpy(buf, op, len);
1879 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1880}
1881
1882/*
1883 * Compile code to apply '-', '+' and '!'.
1884 * When "numeric_only" is TRUE do not apply '!'.
1885 */
1886 static int
1887compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1888{
1889 char_u *p = *end;
1890
1891 // this works from end to start
1892 while (p > start)
1893 {
1894 --p;
1895 while (VIM_ISWHITE(*p))
1896 --p;
1897 if (*p == '-' || *p == '+')
1898 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001899 type_T *type = get_type_on_stack(cctx, 0);
1900 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001901 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001902 return FAIL;
1903
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001905 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1906 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907 }
1908 else if (numeric_only)
1909 {
1910 ++p;
1911 break;
1912 }
1913 else
1914 {
1915 int invert = *p == '!';
1916
1917 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1918 {
1919 if (p[-1] == '!')
1920 invert = !invert;
1921 --p;
1922 }
1923 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1924 return FAIL;
1925 }
1926 }
1927 *end = p;
1928 return OK;
1929}
1930
1931/*
1932 * Compile "(expression)": recursive!
1933 * Return FAIL/OK.
1934 */
1935 static int
1936compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1937{
1938 int ret;
1939 char_u *p = *arg + 1;
1940
1941 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1942 return FAIL;
1943 if (ppconst->pp_used <= PPSIZE - 10)
1944 {
1945 ret = compile_expr1(arg, cctx, ppconst);
1946 }
1947 else
1948 {
1949 // Not enough space in ppconst, flush constants.
1950 if (generate_ppconst(cctx, ppconst) == FAIL)
1951 return FAIL;
1952 ret = compile_expr0(arg, cctx);
1953 }
1954 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1955 return FAIL;
1956 if (**arg == ')')
1957 ++*arg;
1958 else if (ret == OK)
1959 {
1960 emsg(_(e_missing_closing_paren));
1961 ret = FAIL;
1962 }
1963 return ret;
1964}
1965
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001966static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001967
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001968/*
1969 * Compile whatever comes after "name" or "name()".
1970 * Advances "*arg" only when something was recognized.
1971 */
1972 static int
1973compile_subscript(
1974 char_u **arg,
1975 cctx_T *cctx,
1976 char_u *start_leader,
1977 char_u **end_leader,
1978 ppconst_T *ppconst)
1979{
1980 char_u *name_start = *end_leader;
1981 int keeping_dict = FALSE;
1982
1983 for (;;)
1984 {
1985 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001986 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987
1988 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1989 {
1990 char_u *next = peek_next_line_from_context(cctx);
1991
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001992 // If a following line starts with "->{", "->(" or "->X" advance to
1993 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001994 // Also if a following line starts with ".x".
1995 if (next != NULL &&
1996 ((next[0] == '-' && next[1] == '>'
1997 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001998 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001999 || ASCII_ISALPHA(*skipwhite(next + 2))))
2000 || (next[0] == '.' && eval_isdictc(next[1]))))
2001 {
2002 next = next_line_from_context(cctx, TRUE);
2003 if (next == NULL)
2004 return FAIL;
2005 *arg = next;
2006 p = skipwhite(*arg);
2007 }
2008 }
2009
2010 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2011 // is not a function call.
2012 if (**arg == '(')
2013 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002014 int argcount = 0;
2015
2016 if (generate_ppconst(cctx, ppconst) == FAIL)
2017 return FAIL;
2018 ppconst->pp_is_const = FALSE;
2019
2020 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002021 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002022
2023 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002024 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002025 return FAIL;
2026 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2027 return FAIL;
2028 if (keeping_dict)
2029 {
2030 keeping_dict = FALSE;
2031 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2032 return FAIL;
2033 }
2034 }
2035 else if (*p == '-' && p[1] == '>')
2036 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002037 char_u *pstart = p;
2038 int alt;
2039 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040
Bram Moolenaarc7349932022-01-16 20:59:39 +00002041 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002042 if (generate_ppconst(cctx, ppconst) == FAIL)
2043 return FAIL;
2044 ppconst->pp_is_const = FALSE;
2045
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002046 // Apply the '!', '-' and '+' first:
2047 // -1.0->func() works like (-1.0)->func()
2048 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2049 return FAIL;
2050
2051 p += 2;
2052 *arg = skipwhite(p);
2053 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002054
2055 // Three alternatives handled here:
2056 // 1. "base->name(" only a name, use compile_call()
2057 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2058 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002060 alt = 2;
2061 else
2062 {
2063 // alternative 1 or 3
2064 p = *arg;
2065 if (!eval_isnamec1(*p))
2066 {
2067 semsg(_(e_trailing_characters_str), pstart);
2068 return FAIL;
2069 }
2070 if (ASCII_ISALPHA(*p) && p[1] == ':')
2071 p += 2;
2072 for ( ; eval_isnamec(*p); ++p)
2073 ;
2074 if (*p == '(')
2075 {
2076 // alternative 1
2077 alt = 1;
2078 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2079 return FAIL;
2080 }
2081 else
2082 {
2083 // Must be alternative 3, find the "(". Only works within
2084 // one line.
2085 alt = 3;
2086 paren = vim_strchr(p, '(');
2087 if (paren == NULL)
2088 {
2089 semsg(_(e_missing_parenthesis_str), *arg);
2090 return FAIL;
2091 }
2092 }
2093 }
2094
2095 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 {
2097 int argcount = 1;
2098 garray_T *stack = &cctx->ctx_type_stack;
2099 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002100 int expr_isn_start = cctx->ctx_instr.ga_len;
2101 int expr_isn_end;
2102 int arg_isn_count;
2103
Bram Moolenaarc7349932022-01-16 20:59:39 +00002104 if (alt == 2)
2105 {
2106 // Funcref call: list->(Refs[2])(arg)
2107 // or lambda: list->((arg) => expr)(arg)
2108 //
2109 // Fist compile the function expression.
2110 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2111 return FAIL;
2112 }
2113 else
2114 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002115 int fail;
2116 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002117 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002118
Bram Moolenaarc7349932022-01-16 20:59:39 +00002119 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002120
2121 // instead of using LOADG for "import.Func" use PUSHFUNC
2122 ++paren_follows_after_expr;
2123
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002124 // do not look in the next line
2125 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002126
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002127 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002128 || *skipwhite(*arg) != NUL;
2129 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002130 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002131 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002132
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002133 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002134 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002135 if (did_emsg == prev_did_emsg)
2136 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002137 return FAIL;
2138 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002139 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002140
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 // Compile the arguments.
2142 if (**arg != '(')
2143 {
2144 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002145 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002146 else
2147 semsg(_(e_missing_parenthesis_str), *arg);
2148 return FAIL;
2149 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002150
2151 // Remember the next instruction index, where the instructions
2152 // for arguments are being written.
2153 expr_isn_end = cctx->ctx_instr.ga_len;
2154
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002155 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002156 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2157 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002158 return FAIL;
2159
2160 // Move the instructions for the arguments to before the
2161 // instructions of the expression and move the type of the
2162 // expression after the argument types. This is what ISN_PCALL
2163 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2165 if (arg_isn_count > 0)
2166 {
2167 int expr_isn_count = expr_isn_end - expr_isn_start;
2168 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002169 type_T *decl_type;
2170 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002171
2172 if (isn == NULL)
2173 return FAIL;
2174 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2175 + expr_isn_start,
2176 sizeof(isn_T) * expr_isn_count);
2177 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2178 + expr_isn_start,
2179 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2180 sizeof(isn_T) * arg_isn_count);
2181 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2182 + expr_isn_start + arg_isn_count,
2183 isn, sizeof(isn_T) * expr_isn_count);
2184 vim_free(isn);
2185
Bram Moolenaar078a4612022-01-04 15:17:03 +00002186 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2187 type = typep->type_curr;
2188 decl_type = typep->type_decl;
2189 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2190 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2191 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002192 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002193 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2194 typep->type_curr = type;
2195 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002196 }
2197
Bram Moolenaar078a4612022-01-04 15:17:03 +00002198 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002199 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2200 return FAIL;
2201 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002202
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002203 if (keeping_dict)
2204 {
2205 keeping_dict = FALSE;
2206 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2207 return FAIL;
2208 }
2209 }
2210 else if (**arg == '[')
2211 {
2212 int is_slice = FALSE;
2213
2214 // list index: list[123]
2215 // dict member: dict[key]
2216 // string index: text[123]
2217 // blob index: blob[123]
2218 if (generate_ppconst(cctx, ppconst) == FAIL)
2219 return FAIL;
2220 ppconst->pp_is_const = FALSE;
2221
2222 ++p;
2223 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2224 return FAIL;
2225 if (**arg == ':')
2226 {
2227 // missing first index is equal to zero
2228 generate_PUSHNR(cctx, 0);
2229 }
2230 else
2231 {
2232 if (compile_expr0(arg, cctx) == FAIL)
2233 return FAIL;
2234 if (**arg == ':')
2235 {
2236 semsg(_(e_white_space_required_before_and_after_str_at_str),
2237 ":", *arg);
2238 return FAIL;
2239 }
2240 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2241 return FAIL;
2242 *arg = skipwhite(*arg);
2243 }
2244 if (**arg == ':')
2245 {
2246 is_slice = TRUE;
2247 ++*arg;
2248 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2249 {
2250 semsg(_(e_white_space_required_before_and_after_str_at_str),
2251 ":", *arg);
2252 return FAIL;
2253 }
2254 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2255 return FAIL;
2256 if (**arg == ']')
2257 // missing second index is equal to end of string
2258 generate_PUSHNR(cctx, -1);
2259 else
2260 {
2261 if (compile_expr0(arg, cctx) == FAIL)
2262 return FAIL;
2263 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2264 return FAIL;
2265 *arg = skipwhite(*arg);
2266 }
2267 }
2268
2269 if (**arg != ']')
2270 {
2271 emsg(_(e_missing_closing_square_brace));
2272 return FAIL;
2273 }
2274 *arg = *arg + 1;
2275
2276 if (keeping_dict)
2277 {
2278 keeping_dict = FALSE;
2279 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2280 return FAIL;
2281 }
2282 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2283 return FAIL;
2284 }
2285 else if (*p == '.' && p[1] != '.')
2286 {
2287 // dictionary member: dict.name
2288 if (generate_ppconst(cctx, ppconst) == FAIL)
2289 return FAIL;
2290 ppconst->pp_is_const = FALSE;
2291
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002292 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2293 && (type->tt_type == VAR_CLASS
2294 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002295 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002296 // class member: SomeClass.varname
2297 // class method: SomeClass.SomeMethod()
2298 // class constructor: SomeClass.new()
2299 // object member: someObject.varname, this.varname
2300 // object method: someObject.SomeMethod(), this.SomeMethod()
2301 *arg = p;
2302 if (compile_class_object_index(cctx, arg, type) == FAIL)
2303 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002305 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002306 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002307 *arg = p + 1;
2308 if (IS_WHITE_OR_NUL(**arg))
2309 {
2310 emsg(_(e_missing_name_after_dot));
2311 return FAIL;
2312 }
2313 p = *arg;
2314 if (eval_isdictc(*p))
2315 while (eval_isnamec(*p))
2316 MB_PTR_ADV(p);
2317 if (p == *arg)
2318 {
2319 semsg(_(e_syntax_error_at_str), *arg);
2320 return FAIL;
2321 }
2322 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2323 return FAIL;
2324 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2325 return FAIL;
2326 keeping_dict = TRUE;
2327 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329 }
2330 else
2331 break;
2332 }
2333
2334 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2335 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002336 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2337 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002338 return FAIL;
2339
2340 return OK;
2341}
2342
2343/*
2344 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2345 * "arg" is advanced until after the expression, skipping white space.
2346 *
2347 * If the value is a constant "ppconst->pp_used" will be non-zero.
2348 * Before instructions are generated, any values in "ppconst" will generated.
2349 *
2350 * This is the compiling equivalent of eval1(), eval2(), etc.
2351 */
2352
2353/*
2354 * number number constant
2355 * 0zFFFFFFFF Blob constant
2356 * "string" string constant
2357 * 'string' literal string constant
2358 * &option-name option value
2359 * @r register contents
2360 * identifier variable value
2361 * function() function call
2362 * $VAR environment variable
2363 * (expression) nested expression
2364 * [expr, expr] List
2365 * {key: val, [key]: val} Dictionary
2366 *
2367 * Also handle:
2368 * ! in front logical NOT
2369 * - in front unary minus
2370 * + in front unary plus (ignored)
2371 * trailing (arg) funcref/partial call
2372 * trailing [] subscript in String or List
2373 * trailing .name entry in Dictionary
2374 * trailing ->name() method call
2375 */
2376 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002377compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002378 char_u **arg,
2379 cctx_T *cctx,
2380 ppconst_T *ppconst)
2381{
2382 char_u *start_leader, *end_leader;
2383 int ret = OK;
2384 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2385 int used_before = ppconst->pp_used;
2386
2387 ppconst->pp_is_const = FALSE;
2388
2389 /*
2390 * Skip '!', '-' and '+' characters. They are handled later.
2391 */
2392 start_leader = *arg;
2393 if (eval_leader(arg, TRUE) == FAIL)
2394 return FAIL;
2395 end_leader = *arg;
2396
2397 rettv->v_type = VAR_UNKNOWN;
2398 switch (**arg)
2399 {
2400 /*
2401 * Number constant.
2402 */
2403 case '0': // also for blob starting with 0z
2404 case '1':
2405 case '2':
2406 case '3':
2407 case '4':
2408 case '5':
2409 case '6':
2410 case '7':
2411 case '8':
2412 case '9':
2413 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2414 return FAIL;
2415 // Apply "-" and "+" just before the number now, right to
2416 // left. Matters especially when "->" follows. Stops at
2417 // '!'.
2418 if (apply_leader(rettv, TRUE,
2419 start_leader, &end_leader) == FAIL)
2420 {
2421 clear_tv(rettv);
2422 return FAIL;
2423 }
2424 break;
2425
2426 /*
2427 * String constant: "string".
2428 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002429 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 return FAIL;
2431 break;
2432
2433 /*
2434 * Literal string constant: 'str''ing'.
2435 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002436 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437 return FAIL;
2438 break;
2439
2440 /*
2441 * Constant Vim variable.
2442 */
2443 case 'v': get_vim_constant(arg, rettv);
2444 ret = NOTDONE;
2445 break;
2446
2447 /*
2448 * "true" constant
2449 */
2450 case 't': if (STRNCMP(*arg, "true", 4) == 0
2451 && !eval_isnamec((*arg)[4]))
2452 {
2453 *arg += 4;
2454 rettv->v_type = VAR_BOOL;
2455 rettv->vval.v_number = VVAL_TRUE;
2456 }
2457 else
2458 ret = NOTDONE;
2459 break;
2460
2461 /*
2462 * "false" constant
2463 */
2464 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2465 && !eval_isnamec((*arg)[5]))
2466 {
2467 *arg += 5;
2468 rettv->v_type = VAR_BOOL;
2469 rettv->vval.v_number = VVAL_FALSE;
2470 }
2471 else
2472 ret = NOTDONE;
2473 break;
2474
2475 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002476 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002477 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002478 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002479 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002480 char_u *p = *arg + 4;
2481 int len;
2482
2483 for (len = 0; eval_isnamec(p[len]); ++len)
2484 ;
2485 ret = handle_predefined(*arg, len + 4, rettv);
2486 if (ret == FAIL)
2487 ret = NOTDONE;
2488 else
2489 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490 }
2491 else
2492 ret = NOTDONE;
2493 break;
2494
2495 /*
2496 * List: [expr, expr]
2497 */
2498 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2499 return FAIL;
2500 ret = compile_list(arg, cctx, ppconst);
2501 break;
2502
2503 /*
2504 * Dictionary: {'key': val, 'key': val}
2505 */
2506 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2507 return FAIL;
2508 ret = compile_dict(arg, cctx, ppconst);
2509 break;
2510
2511 /*
2512 * Option value: &name
2513 */
2514 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2515 return FAIL;
2516 ret = compile_get_option(arg, cctx);
2517 break;
2518
2519 /*
2520 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002521 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 */
2523 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2524 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002525 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2526 ret = compile_interp_string(arg, cctx);
2527 else
2528 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002529 break;
2530
2531 /*
2532 * Register contents: @r.
2533 */
2534 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2535 return FAIL;
2536 ret = compile_get_register(arg, cctx);
2537 break;
2538 /*
2539 * nested expression: (expression).
2540 * lambda: (arg, arg) => expr
2541 * funcref: (arg, arg) => { statement }
2542 */
2543 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2544 ret = compile_lambda(arg, cctx);
2545 if (ret == NOTDONE)
2546 ret = compile_parenthesis(arg, cctx, ppconst);
2547 break;
2548
2549 default: ret = NOTDONE;
2550 break;
2551 }
2552 if (ret == FAIL)
2553 return FAIL;
2554
2555 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2556 {
2557 if (cctx->ctx_skip == SKIP_YES)
2558 clear_tv(rettv);
2559 else
2560 // A constant expression can possibly be handled compile time,
2561 // return the value instead of generating code.
2562 ++ppconst->pp_used;
2563 }
2564 else if (ret == NOTDONE)
2565 {
2566 char_u *p;
2567 int r;
2568
2569 if (!eval_isnamec1(**arg))
2570 {
2571 if (!vim9_bad_comment(*arg))
2572 {
2573 if (ends_excmd(*skipwhite(*arg)))
2574 semsg(_(e_empty_expression_str), *arg);
2575 else
2576 semsg(_(e_name_expected_str), *arg);
2577 }
2578 return FAIL;
2579 }
2580
2581 // "name" or "name()"
2582 p = to_name_end(*arg, TRUE);
2583 if (p - *arg == (size_t)1 && **arg == '_')
2584 {
2585 emsg(_(e_cannot_use_underscore_here));
2586 return FAIL;
2587 }
2588
2589 if (*p == '(')
2590 {
2591 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2592 }
2593 else
2594 {
2595 if (cctx->ctx_skip != SKIP_YES
2596 && generate_ppconst(cctx, ppconst) == FAIL)
2597 return FAIL;
2598 r = compile_load(arg, p, cctx, TRUE, TRUE);
2599 }
2600 if (r == FAIL)
2601 return FAIL;
2602 }
2603
2604 // Handle following "[]", ".member", etc.
2605 // Then deal with prefixed '-', '+' and '!', if not done already.
2606 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2607 ppconst) == FAIL)
2608 return FAIL;
2609 if (ppconst->pp_used > 0)
2610 {
2611 // apply the '!', '-' and '+' before the constant
2612 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2613 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2614 return FAIL;
2615 return OK;
2616 }
2617 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2618 return FAIL;
2619 return OK;
2620}
2621
2622/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002623 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002624 */
2625 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002626compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002627{
2628 type_T *want_type = NULL;
2629
2630 // Recognize <type>
2631 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2632 {
2633 ++*arg;
2634 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2635 if (want_type == NULL)
2636 return FAIL;
2637
2638 if (**arg != '>')
2639 {
2640 if (*skipwhite(*arg) == '>')
2641 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2642 else
2643 emsg(_(e_missing_gt));
2644 return FAIL;
2645 }
2646 ++*arg;
2647 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2648 return FAIL;
2649 }
2650
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002651 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002652 return FAIL;
2653
2654 if (want_type != NULL)
2655 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002656 type_T *actual;
2657 where_T where = WHERE_INIT;
2658
2659 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002660 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002661 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002662 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002663 if (need_type(actual, want_type, FALSE,
2664 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002665 return FAIL;
2666 }
2667 }
2668
2669 return OK;
2670}
2671
2672/*
2673 * * number multiplication
2674 * / number division
2675 * % number modulo
2676 */
2677 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002678compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679{
2680 char_u *op;
2681 char_u *next;
2682 int ppconst_used = ppconst->pp_used;
2683
2684 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002685 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002686 return FAIL;
2687
2688 /*
2689 * Repeat computing, until no "*", "/" or "%" is following.
2690 */
2691 for (;;)
2692 {
2693 op = may_peek_next_line(cctx, *arg, &next);
2694 if (*op != '*' && *op != '/' && *op != '%')
2695 break;
2696 if (next != NULL)
2697 {
2698 *arg = next_line_from_context(cctx, TRUE);
2699 op = skipwhite(*arg);
2700 }
2701
2702 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2703 {
2704 error_white_both(op, 1);
2705 return FAIL;
2706 }
2707 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2708 return FAIL;
2709
2710 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002711 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002712 return FAIL;
2713
2714 if (ppconst->pp_used == ppconst_used + 2
2715 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2716 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2717 {
2718 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2719 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2720 varnumber_T res = 0;
2721 int failed = FALSE;
2722
2723 // both are numbers: compute the result
2724 switch (*op)
2725 {
2726 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2727 break;
2728 case '/': res = num_divide(tv1->vval.v_number,
2729 tv2->vval.v_number, &failed);
2730 break;
2731 case '%': res = num_modulus(tv1->vval.v_number,
2732 tv2->vval.v_number, &failed);
2733 break;
2734 }
2735 if (failed)
2736 return FAIL;
2737 tv1->vval.v_number = res;
2738 --ppconst->pp_used;
2739 }
2740 else
2741 {
2742 generate_ppconst(cctx, ppconst);
2743 generate_two_op(cctx, op);
2744 }
2745 }
2746
2747 return OK;
2748}
2749
2750/*
2751 * + number addition or list/blobl concatenation
2752 * - number subtraction
2753 * .. string concatenation
2754 */
2755 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002756compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002757{
2758 char_u *op;
2759 char_u *next;
2760 int oplen;
2761 int ppconst_used = ppconst->pp_used;
2762
2763 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002764 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002765 return FAIL;
2766
2767 /*
2768 * Repeat computing, until no "+", "-" or ".." is following.
2769 */
2770 for (;;)
2771 {
2772 op = may_peek_next_line(cctx, *arg, &next);
2773 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2774 break;
2775 if (op[0] == op[1] && *op != '.' && next)
2776 // Finding "++" or "--" on the next line is a separate command.
2777 // But ".." is concatenation.
2778 break;
2779 oplen = (*op == '.' ? 2 : 1);
2780 if (next != NULL)
2781 {
2782 *arg = next_line_from_context(cctx, TRUE);
2783 op = skipwhite(*arg);
2784 }
2785
2786 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2787 {
2788 error_white_both(op, oplen);
2789 return FAIL;
2790 }
2791
2792 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2793 return FAIL;
2794
2795 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002796 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002797 return FAIL;
2798
2799 if (ppconst->pp_used == ppconst_used + 2
2800 && (*op == '.'
2801 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2802 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2803 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2804 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2805 {
2806 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2807 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2808
2809 // concat/subtract/add constant numbers
2810 if (*op == '+')
2811 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2812 else if (*op == '-')
2813 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2814 else
2815 {
2816 // concatenate constant strings
2817 char_u *s1 = tv1->vval.v_string;
2818 char_u *s2 = tv2->vval.v_string;
2819 size_t len1 = STRLEN(s1);
2820
2821 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2822 if (tv1->vval.v_string == NULL)
2823 {
2824 clear_ppconst(ppconst);
2825 return FAIL;
2826 }
2827 mch_memmove(tv1->vval.v_string, s1, len1);
2828 STRCPY(tv1->vval.v_string + len1, s2);
2829 vim_free(s1);
2830 vim_free(s2);
2831 }
2832 --ppconst->pp_used;
2833 }
2834 else
2835 {
2836 generate_ppconst(cctx, ppconst);
2837 ppconst->pp_is_const = FALSE;
2838 if (*op == '.')
2839 {
2840 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2841 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2842 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002843 if (generate_CONCAT(cctx, 2) == FAIL)
2844 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002845 }
2846 else
2847 generate_two_op(cctx, op);
2848 }
2849 }
2850
2851 return OK;
2852}
2853
2854/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002855 * expr6a >> expr6b
2856 * expr6a << expr6b
2857 *
2858 * Produces instructions:
2859 * OPNR bitwise left or right shift
2860 */
2861 static int
2862compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2863{
2864 exprtype_T type = EXPR_UNKNOWN;
2865 char_u *p;
2866 char_u *next;
2867 int len = 2;
2868 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002869 isn_T *isn;
2870
2871 // get the first variable
2872 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2873 return FAIL;
2874
2875 /*
2876 * Repeat computing, until no "+", "-" or ".." is following.
2877 */
2878 for (;;)
2879 {
2880 type = EXPR_UNKNOWN;
2881
2882 p = may_peek_next_line(cctx, *arg, &next);
2883 if (p[0] == '<' && p[1] == '<')
2884 type = EXPR_LSHIFT;
2885 else if (p[0] == '>' && p[1] == '>')
2886 type = EXPR_RSHIFT;
2887
2888 if (type == EXPR_UNKNOWN)
2889 return OK;
2890
2891 // Handle a bitwise left or right shift operator
2892 if (ppconst->pp_used == ppconst_used + 1)
2893 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002894 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002895 {
2896 // left operand should be a number
2897 emsg(_(e_bitshift_ops_must_be_number));
2898 return FAIL;
2899 }
2900 }
2901 else
2902 {
2903 type_T *t = get_type_on_stack(cctx, 0);
2904
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002905 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002906 {
2907 emsg(_(e_bitshift_ops_must_be_number));
2908 return FAIL;
2909 }
2910 }
2911
2912 if (next != NULL)
2913 {
2914 *arg = next_line_from_context(cctx, TRUE);
2915 p = skipwhite(*arg);
2916 }
2917
2918 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2919 {
2920 error_white_both(p, len);
2921 return FAIL;
2922 }
2923
2924 // get the second variable
2925 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2926 return FAIL;
2927
2928 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2929 return FAIL;
2930
2931 if (ppconst->pp_used == ppconst_used + 2)
2932 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002933 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2934 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2935
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002936 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002937 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2938 {
2939 // right operand should be a positive number
2940 if (tv2->v_type != VAR_NUMBER)
2941 emsg(_(e_bitshift_ops_must_be_number));
2942 else
dundargocc57b5bc2022-11-02 13:30:51 +00002943 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002944 return FAIL;
2945 }
2946
2947 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2948 tv1->vval.v_number = 0;
2949 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002950 tv1->vval.v_number =
2951 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002952 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002953 tv1->vval.v_number =
2954 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002955 clear_tv(tv2);
2956 --ppconst->pp_used;
2957 }
2958 else
2959 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002960 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2961 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002962 {
2963 emsg(_(e_bitshift_ops_must_be_number));
2964 return FAIL;
2965 }
2966
2967 generate_ppconst(cctx, ppconst);
2968
2969 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2970 if (isn == NULL)
2971 return FAIL;
2972
2973 if (isn != NULL)
2974 isn->isn_arg.op.op_type = type;
2975 }
2976 }
2977
2978 return OK;
2979}
2980
2981/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002982 * expr5a == expr5b
2983 * expr5a =~ expr5b
2984 * expr5a != expr5b
2985 * expr5a !~ expr5b
2986 * expr5a > expr5b
2987 * expr5a >= expr5b
2988 * expr5a < expr5b
2989 * expr5a <= expr5b
2990 * expr5a is expr5b
2991 * expr5a isnot expr5b
2992 *
2993 * Produces instructions:
2994 * EVAL expr5a Push result of "expr5a"
2995 * EVAL expr5b Push result of "expr5b"
2996 * COMPARE one of the compare instructions
2997 */
2998 static int
2999compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3000{
3001 exprtype_T type = EXPR_UNKNOWN;
3002 char_u *p;
3003 char_u *next;
3004 int len = 2;
3005 int type_is = FALSE;
3006 int ppconst_used = ppconst->pp_used;
3007
3008 // get the first variable
3009 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3010 return FAIL;
3011
3012 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003013
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003014 type = get_compare_type(p, &len, &type_is);
3015
3016 /*
3017 * If there is a comparative operator, use it.
3018 */
3019 if (type != EXPR_UNKNOWN)
3020 {
3021 int ic = FALSE; // Default: do not ignore case
3022
3023 if (next != NULL)
3024 {
3025 *arg = next_line_from_context(cctx, TRUE);
3026 p = skipwhite(*arg);
3027 }
3028 if (type_is && (p[len] == '?' || p[len] == '#'))
3029 {
3030 semsg(_(e_invalid_expression_str), *arg);
3031 return FAIL;
3032 }
3033 // extra question mark appended: ignore case
3034 if (p[len] == '?')
3035 {
3036 ic = TRUE;
3037 ++len;
3038 }
3039 // extra '#' appended: match case (ignored)
3040 else if (p[len] == '#')
3041 ++len;
3042 // nothing appended: match case
3043
3044 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3045 {
3046 error_white_both(p, len);
3047 return FAIL;
3048 }
3049
3050 // get the second variable
3051 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3052 return FAIL;
3053
3054 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3055 return FAIL;
3056
3057 if (ppconst->pp_used == ppconst_used + 2)
3058 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003059 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003060 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3061 int ret;
3062
3063 // Both sides are a constant, compute the result now.
3064 // First check for a valid combination of types, this is more
3065 // strict than typval_compare().
3066 if (check_compare_types(type, tv1, tv2) == FAIL)
3067 ret = FAIL;
3068 else
3069 {
3070 ret = typval_compare(tv1, tv2, type, ic);
3071 tv1->v_type = VAR_BOOL;
3072 tv1->vval.v_number = tv1->vval.v_number
3073 ? VVAL_TRUE : VVAL_FALSE;
3074 clear_tv(tv2);
3075 --ppconst->pp_used;
3076 }
3077 return ret;
3078 }
3079
3080 generate_ppconst(cctx, ppconst);
3081 return generate_COMPARE(cctx, type, ic);
3082 }
3083
3084 return OK;
3085}
3086
3087static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3088
3089/*
3090 * Compile || or &&.
3091 */
3092 static int
3093compile_and_or(
3094 char_u **arg,
3095 cctx_T *cctx,
3096 char *op,
3097 ppconst_T *ppconst,
3098 int ppconst_used UNUSED)
3099{
3100 char_u *next;
3101 char_u *p = may_peek_next_line(cctx, *arg, &next);
3102 int opchar = *op;
3103
3104 if (p[0] == opchar && p[1] == opchar)
3105 {
3106 garray_T *instr = &cctx->ctx_instr;
3107 garray_T end_ga;
3108 int save_skip = cctx->ctx_skip;
3109
3110 /*
3111 * Repeat until there is no following "||" or "&&"
3112 */
3113 ga_init2(&end_ga, sizeof(int), 10);
3114 while (p[0] == opchar && p[1] == opchar)
3115 {
3116 long start_lnum = SOURCING_LNUM;
3117 long save_sourcing_lnum;
3118 int start_ctx_lnum = cctx->ctx_lnum;
3119 int save_lnum;
3120 int const_used;
3121 int status;
3122 jumpwhen_T jump_when = opchar == '|'
3123 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3124
3125 if (next != NULL)
3126 {
3127 *arg = next_line_from_context(cctx, TRUE);
3128 p = skipwhite(*arg);
3129 }
3130
3131 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3132 {
3133 semsg(_(e_white_space_required_before_and_after_str_at_str),
3134 op, p);
3135 ga_clear(&end_ga);
3136 return FAIL;
3137 }
3138
3139 save_sourcing_lnum = SOURCING_LNUM;
3140 SOURCING_LNUM = start_lnum;
3141 save_lnum = cctx->ctx_lnum;
3142 cctx->ctx_lnum = start_ctx_lnum;
3143
3144 status = check_ppconst_bool(ppconst);
3145 if (status != FAIL)
3146 {
3147 // Use the last ppconst if possible.
3148 if (ppconst->pp_used > 0)
3149 {
3150 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3151 int is_true = tv2bool(tv);
3152
3153 if ((is_true && opchar == '|')
3154 || (!is_true && opchar == '&'))
3155 {
3156 // For "false && expr" and "true || expr" the "expr"
3157 // does not need to be evaluated.
3158 cctx->ctx_skip = SKIP_YES;
3159 clear_tv(tv);
3160 tv->v_type = VAR_BOOL;
3161 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3162 }
3163 else
3164 {
3165 // For "true && expr" and "false || expr" only "expr"
3166 // needs to be evaluated.
3167 --ppconst->pp_used;
3168 jump_when = JUMP_NEVER;
3169 }
3170 }
3171 else
3172 {
3173 // Every part must evaluate to a bool.
3174 status = bool_on_stack(cctx);
3175 }
3176 }
3177 if (status != FAIL)
3178 status = ga_grow(&end_ga, 1);
3179 cctx->ctx_lnum = save_lnum;
3180 if (status == FAIL)
3181 {
3182 ga_clear(&end_ga);
3183 return FAIL;
3184 }
3185
3186 if (jump_when != JUMP_NEVER)
3187 {
3188 if (cctx->ctx_skip != SKIP_YES)
3189 {
3190 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3191 ++end_ga.ga_len;
3192 }
3193 generate_JUMP(cctx, jump_when, 0);
3194 }
3195
3196 // eval the next expression
3197 SOURCING_LNUM = save_sourcing_lnum;
3198 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3199 {
3200 ga_clear(&end_ga);
3201 return FAIL;
3202 }
3203
3204 const_used = ppconst->pp_used;
3205 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3206 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3207 {
3208 ga_clear(&end_ga);
3209 return FAIL;
3210 }
3211
3212 // "0 || 1" results in true, "1 && 0" results in false.
3213 if (ppconst->pp_used == const_used + 1)
3214 {
3215 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3216
3217 if (tv->v_type == VAR_NUMBER
3218 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3219 {
3220 tv->vval.v_number = tv->vval.v_number == 1
3221 ? VVAL_TRUE : VVAL_FALSE;
3222 tv->v_type = VAR_BOOL;
3223 }
3224 }
3225
3226 p = may_peek_next_line(cctx, *arg, &next);
3227 }
3228
3229 if (check_ppconst_bool(ppconst) == FAIL)
3230 {
3231 ga_clear(&end_ga);
3232 return FAIL;
3233 }
3234
3235 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3236 // Every part must evaluate to a bool.
3237 if (bool_on_stack(cctx) == FAIL)
3238 {
3239 ga_clear(&end_ga);
3240 return FAIL;
3241 }
3242
3243 if (end_ga.ga_len > 0)
3244 {
3245 // Fill in the end label in all jumps.
3246 generate_ppconst(cctx, ppconst);
3247 while (end_ga.ga_len > 0)
3248 {
3249 isn_T *isn;
3250
3251 --end_ga.ga_len;
3252 isn = ((isn_T *)instr->ga_data)
3253 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3254 isn->isn_arg.jump.jump_where = instr->ga_len;
3255 }
3256 }
3257 ga_clear(&end_ga);
3258
3259 cctx->ctx_skip = save_skip;
3260 }
3261
3262 return OK;
3263}
3264
3265/*
3266 * expr4a && expr4a && expr4a logical AND
3267 *
3268 * Produces instructions:
3269 * EVAL expr4a Push result of "expr4a"
3270 * COND2BOOL convert to bool if needed
3271 * JUMP_IF_COND_FALSE end
3272 * EVAL expr4b Push result of "expr4b"
3273 * JUMP_IF_COND_FALSE end
3274 * EVAL expr4c Push result of "expr4c"
3275 * end:
3276 */
3277 static int
3278compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3279{
3280 int ppconst_used = ppconst->pp_used;
3281
3282 // get the first variable
3283 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3284 return FAIL;
3285
3286 // || and && work almost the same
3287 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3288}
3289
3290/*
3291 * expr3a || expr3b || expr3c logical OR
3292 *
3293 * Produces instructions:
3294 * EVAL expr3a Push result of "expr3a"
3295 * COND2BOOL convert to bool if needed
3296 * JUMP_IF_COND_TRUE end
3297 * EVAL expr3b Push result of "expr3b"
3298 * JUMP_IF_COND_TRUE end
3299 * EVAL expr3c Push result of "expr3c"
3300 * end:
3301 */
3302 static int
3303compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3304{
3305 int ppconst_used = ppconst->pp_used;
3306
3307 // eval the first expression
3308 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3309 return FAIL;
3310
3311 // || and && work almost the same
3312 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3313}
3314
3315/*
3316 * Toplevel expression: expr2 ? expr1a : expr1b
3317 * Produces instructions:
3318 * EVAL expr2 Push result of "expr2"
3319 * JUMP_IF_FALSE alt jump if false
3320 * EVAL expr1a
3321 * JUMP_ALWAYS end
3322 * alt: EVAL expr1b
3323 * end:
3324 *
3325 * Toplevel expression: expr2 ?? expr1
3326 * Produces instructions:
3327 * EVAL expr2 Push result of "expr2"
3328 * JUMP_AND_KEEP_IF_TRUE end jump if true
3329 * EVAL expr1
3330 * end:
3331 */
3332 int
3333compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3334{
3335 char_u *p;
3336 int ppconst_used = ppconst->pp_used;
3337 char_u *next;
3338
3339 // Ignore all kinds of errors when not producing code.
3340 if (cctx->ctx_skip == SKIP_YES)
3341 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003342 int prev_did_emsg = did_emsg;
3343
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003344 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003345 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003346 }
3347
3348 // Evaluate the first expression.
3349 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3350 return FAIL;
3351
3352 p = may_peek_next_line(cctx, *arg, &next);
3353 if (*p == '?')
3354 {
3355 int op_falsy = p[1] == '?';
3356 garray_T *instr = &cctx->ctx_instr;
3357 garray_T *stack = &cctx->ctx_type_stack;
3358 int alt_idx = instr->ga_len;
3359 int end_idx = 0;
3360 isn_T *isn;
3361 type_T *type1 = NULL;
3362 int has_const_expr = FALSE;
3363 int const_value = FALSE;
3364 int save_skip = cctx->ctx_skip;
3365
3366 if (next != NULL)
3367 {
3368 *arg = next_line_from_context(cctx, TRUE);
3369 p = skipwhite(*arg);
3370 }
3371
3372 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3373 {
3374 semsg(_(e_white_space_required_before_and_after_str_at_str),
3375 op_falsy ? "??" : "?", p);
3376 return FAIL;
3377 }
3378
3379 if (ppconst->pp_used == ppconst_used + 1)
3380 {
3381 // the condition is a constant, we know whether the ? or the :
3382 // expression is to be evaluated.
3383 has_const_expr = TRUE;
3384 if (op_falsy)
3385 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3386 else
3387 {
3388 int error = FALSE;
3389
3390 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3391 &error);
3392 if (error)
3393 return FAIL;
3394 }
3395 cctx->ctx_skip = save_skip == SKIP_YES ||
3396 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3397
3398 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3399 // "left ?? right" and "left" is truthy: produce "left"
3400 generate_ppconst(cctx, ppconst);
3401 else
3402 {
3403 clear_tv(&ppconst->pp_tv[ppconst_used]);
3404 --ppconst->pp_used;
3405 }
3406 }
3407 else
3408 {
3409 generate_ppconst(cctx, ppconst);
3410 if (op_falsy)
3411 end_idx = instr->ga_len;
3412 generate_JUMP(cctx, op_falsy
3413 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3414 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003415 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003416 }
3417
3418 // evaluate the second expression; any type is accepted
3419 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3420 return FAIL;
3421 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3422 return FAIL;
3423
3424 if (!has_const_expr)
3425 {
3426 generate_ppconst(cctx, ppconst);
3427
3428 if (!op_falsy)
3429 {
3430 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003431 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003432 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003433
3434 end_idx = instr->ga_len;
3435 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3436
3437 // jump here from JUMP_IF_FALSE
3438 isn = ((isn_T *)instr->ga_data) + alt_idx;
3439 isn->isn_arg.jump.jump_where = instr->ga_len;
3440 }
3441 }
3442
3443 if (!op_falsy)
3444 {
3445 // Check for the ":".
3446 p = may_peek_next_line(cctx, *arg, &next);
3447 if (*p != ':')
3448 {
3449 emsg(_(e_missing_colon_after_questionmark));
3450 return FAIL;
3451 }
3452 if (next != NULL)
3453 {
3454 *arg = next_line_from_context(cctx, TRUE);
3455 p = skipwhite(*arg);
3456 }
3457
3458 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3459 {
3460 semsg(_(e_white_space_required_before_and_after_str_at_str),
3461 ":", p);
3462 return FAIL;
3463 }
3464
3465 // evaluate the third expression
3466 if (has_const_expr)
3467 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3468 ? SKIP_YES : SKIP_NOT;
3469 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3470 return FAIL;
3471 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3472 return FAIL;
3473 }
3474
3475 if (!has_const_expr)
3476 {
3477 type_T **typep;
3478
3479 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003480 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003481
3482 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003483 typep = &((((type2_T *)stack->ga_data)
3484 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003485 common_type(type1, *typep, typep, cctx->ctx_type_list);
3486
3487 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3488 isn = ((isn_T *)instr->ga_data) + end_idx;
3489 isn->isn_arg.jump.jump_where = instr->ga_len;
3490 }
3491
3492 cctx->ctx_skip = save_skip;
3493 }
3494 return OK;
3495}
3496
3497/*
3498 * Toplevel expression.
3499 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3500 * Returns OK or FAIL.
3501 */
3502 int
3503compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3504{
3505 ppconst_T ppconst;
3506
3507 CLEAR_FIELD(ppconst);
3508 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3509 {
3510 clear_ppconst(&ppconst);
3511 return FAIL;
3512 }
3513 if (is_const != NULL)
3514 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3515 if (generate_ppconst(cctx, &ppconst) == FAIL)
3516 return FAIL;
3517 return OK;
3518}
3519
3520/*
3521 * Toplevel expression.
3522 */
3523 int
3524compile_expr0(char_u **arg, cctx_T *cctx)
3525{
3526 return compile_expr0_ext(arg, cctx, NULL);
3527}
3528
3529
3530#endif // defined(FEAT_EVAL)