blob: b8458aa41712cf79da7ad44bc71030fcd5c9de74 [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
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000296 ++*arg;
297 char_u *name = *arg;
298 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
299 if (name_end == name)
300 return FAIL;
301 size_t len = name_end - name;
302
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000303 if (*name_end == '(')
304 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000305 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000306 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000307 ufunc_T **functions;
308
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000309 if (type->tt_type == VAR_CLASS)
310 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000311 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000312 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000313 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000314 }
315 else
316 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000317 // type->tt_type == VAR_OBJECT: method call
318 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000319 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000320 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000321 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000322
323 ufunc_T *ufunc = NULL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000324 int fi;
325 for (fi = is_super ? child_count : 0; fi < function_count; ++fi)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000326 {
Bram Moolenaard0200c82023-01-28 15:19:40 +0000327 ufunc_T *fp = functions[fi];
Bram Moolenaar574950d2023-01-03 19:08:50 +0000328 // Use a separate pointer to avoid that ASAN complains about
329 // uf_name[] only being 4 characters.
330 char_u *ufname = (char_u *)fp->uf_name;
331 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
332 {
333 ufunc = fp;
334 break;
335 }
336 }
337 if (ufunc == NULL)
338 {
339 // TODO: different error for object method?
340 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
341 return FAIL;
342 }
343
344 // Compile the arguments and call the class function or object method.
345 // The object method will know that the object is on the stack, just
346 // before the arguments.
347 *arg = skipwhite(name_end + 1);
348 int argcount = 0;
349 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
350 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000351
352 if (type->tt_type == VAR_OBJECT
353 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
354 return generate_CALL(cctx, ufunc, cl, fi, argcount);
355 return generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000356 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000357
358 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000359 {
360 for (int i = 0; i < cl->class_obj_member_count; ++i)
361 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000362 ocmember_T *m = &cl->class_obj_members[i];
363 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000364 {
Bram Moolenaar62a69232023-01-24 15:07:04 +0000365 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000366 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000367 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000368 return FAIL;
369 }
370
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000371 *arg = name_end;
Bram Moolenaard0200c82023-01-28 15:19:40 +0000372 if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000373 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000374 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000375 }
376 }
377
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000378 // Could be a function reference: "obj.Func".
379 for (int i = 0; i < cl->class_obj_method_count; ++i)
380 {
381 ufunc_T *fp = cl->class_obj_methods[i];
382 // Use a separate pointer to avoid that ASAN complains about
383 // uf_name[] only being 4 characters.
384 char_u *ufname = (char_u *)fp->uf_name;
385 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
Bram Moolenaar313e4722023-02-08 20:55:27 +0000386 {
387 if (type->tt_type == VAR_OBJECT
388 && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
389 return generate_FUNCREF(cctx, fp, cl, i, NULL);
390 return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
391 }
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000392 }
393
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000394 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
395 }
396 else
397 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000398 // load class member
399 int idx;
400 for (idx = 0; idx < cl->class_class_member_count; ++idx)
401 {
402 ocmember_T *m = &cl->class_class_members[idx];
403 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
404 break;
405 }
406 if (idx < cl->class_class_member_count)
407 {
408 *arg = name_end;
409 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
410 }
411 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000412 }
413
414 return FAIL;
415}
416
417/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000418 * Generate an instruction to load script-local variable "name", without the
419 * leading "s:".
420 * Also finds imported variables.
421 */
422 int
423compile_load_scriptvar(
424 cctx_T *cctx,
425 char_u *name, // variable NUL terminated
426 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100427 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000428{
429 scriptitem_T *si;
430 int idx;
431 imported_T *import;
432
433 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
434 return FAIL;
435 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000436 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000437 if (idx >= 0)
438 {
439 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
440
441 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
442 current_sctx.sc_sid, idx, sv->sv_type);
443 return OK;
444 }
445
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000446 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447 if (import != NULL)
448 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000449 char_u *p = skipwhite(*end);
450 char_u *exp_name;
451 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100452 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000453 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000454 int done = FALSE;
455 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000456
457 // Need to lookup the member.
458 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000460 semsg(_(e_expected_dot_after_name_str), start);
461 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000463 ++p;
464 if (VIM_ISWHITE(*p))
465 {
466 emsg(_(e_no_white_space_allowed_after_dot));
467 return FAIL;
468 }
469
470 // isolate one name
471 exp_name = p;
472 while (eval_isnamec(*p))
473 ++p;
474 cc = *p;
475 *p = NUL;
476
Bram Moolenaard041f422022-01-12 19:54:00 +0000477 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100478 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
479 // "import autoload './dir/script.vim'" or
480 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100481 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100482
483 if (res == OK)
484 {
485 if (si->sn_autoload_prefix != NULL
486 && si->sn_state == SN_STATE_NOT_LOADED)
487 {
488 char_u *auto_name =
489 concat_str(si->sn_autoload_prefix, exp_name);
490
491 // autoload script must be loaded later, access by the autoload
492 // name. If a '(' follows it must be a function. Otherwise we
493 // don't know, it can be "script.Func".
494 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100495 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100496 else
497 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
498 vim_free(auto_name);
499 done = TRUE;
500 }
501 else if (si->sn_import_autoload
502 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100503 {
504 // If a '(' follows it must be a function. Otherwise we don't
505 // know, it can be "script.Func".
506 if (cc == '(' || paren_follows_after_expr)
507 {
508 char_u sid_name[MAX_FUNC_NAME_LEN];
509
510 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100511 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100512 }
513 else
514 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
515 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100516 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100517 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100518 else
519 {
520 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
521 cctx, NULL, TRUE);
522 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100523 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100524
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000525 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000526 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000527 if (done)
528 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000529
530 if (idx < 0)
531 {
532 if (ufunc != NULL)
533 {
534 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100535 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000536 return OK;
537 }
538 return FAIL;
539 }
540
541 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
542 import->imp_sid,
543 idx,
544 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545 return OK;
546 }
547
Bram Moolenaard0132f42022-05-12 11:05:40 +0100548 // Can only get here if we know "name" is a script variable and not in a
549 // Vim9 script (variable is not in sn_var_vals): old style script.
550 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000551 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000552}
553
554 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000555generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000556{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000557 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000558 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000559
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000560 // Reject a global non-autoload function found without the "g:" prefix.
561 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000562 return FAIL;
563
564 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000565 compile_type = get_compile_type(ufunc);
566 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000567 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100569 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570}
571
572/*
573 * Compile a variable name into a load instruction.
574 * "end" points to just after the name.
575 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
576 * When "error" is FALSE do not give an error when not found.
577 */
578 int
579compile_load(
580 char_u **arg,
581 char_u *end_arg,
582 cctx_T *cctx,
583 int is_expr,
584 int error)
585{
586 type_T *type;
587 char_u *name = NULL;
588 char_u *end = end_arg;
589 int res = FAIL;
590 int prev_called_emsg = called_emsg;
591
592 if (*(*arg + 1) == ':')
593 {
594 if (end <= *arg + 2)
595 {
596 isntype_T isn_type;
597
598 // load dictionary of namespace
599 switch (**arg)
600 {
601 case 'g': isn_type = ISN_LOADGDICT; break;
602 case 'w': isn_type = ISN_LOADWDICT; break;
603 case 't': isn_type = ISN_LOADTDICT; break;
604 case 'b': isn_type = ISN_LOADBDICT; break;
605 default:
606 semsg(_(e_namespace_not_supported_str), *arg);
607 goto theend;
608 }
609 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
610 goto theend;
611 res = OK;
612 }
613 else
614 {
615 isntype_T isn_type = ISN_DROP;
616
617 // load namespaced variable
618 name = vim_strnsave(*arg + 2, end - (*arg + 2));
619 if (name == NULL)
620 return FAIL;
621
622 switch (**arg)
623 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100624 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000625 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000626 case 's': if (current_script_is_vim9())
627 {
628 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
629 *arg);
630 vim_free(name);
631 return FAIL;
632 }
Kota Kato948a3892022-08-16 16:09:59 +0100633 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000634 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000635 else
636 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100637 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000638 break;
639 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
640 {
641 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000642 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000643 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644 else
645 isn_type = ISN_LOADG;
646 }
647 else
648 {
649 isn_type = ISN_LOADAUTO;
650 vim_free(name);
651 name = vim_strnsave(*arg, end - *arg);
652 if (name == NULL)
653 return FAIL;
654 }
655 break;
656 case 'w': isn_type = ISN_LOADW; break;
657 case 't': isn_type = ISN_LOADT; break;
658 case 'b': isn_type = ISN_LOADB; break;
659 default: // cannot happen, just in case
660 semsg(_(e_namespace_not_supported_str), *arg);
661 goto theend;
662 }
663 if (isn_type != ISN_DROP)
664 {
665 // Global, Buffer-local, Window-local and Tabpage-local
666 // variables can be defined later, thus we don't check if it
667 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000668 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669 }
670 }
671 }
672 else
673 {
674 size_t len = end - *arg;
675 int idx;
676 int gen_load = FALSE;
677 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100678 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100679 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000680
681 name = vim_strnsave(*arg, end - *arg);
682 if (name == NULL)
683 return FAIL;
684
Bram Moolenaar58b40092023-01-11 15:59:05 +0000685 if (STRCMP(name, "super") == 0
686 && cctx->ctx_ufunc != NULL
687 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
688 {
689 // super.SomeFunc() in a class function: push &t_super type, this
690 // is recognized in compile_subscript().
691 res = push_type_stack(cctx, &t_super);
692 if (*end != '.')
693 emsg(_(e_super_must_be_followed_by_dot));
694 }
695 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 {
697 script_autoload(name, FALSE);
698 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
699 }
700 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
701 == OK)
702 {
703 if (gen_load_outer == 0)
704 gen_load = TRUE;
705 }
706 else
707 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000708 lvar_T lvar;
709 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000710
711 if (lookup_local(*arg, len, &lvar, cctx) == OK)
712 {
713 type = lvar.lv_type;
714 idx = lvar.lv_idx;
715 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100716 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100718 outer_loop_depth = lvar.lv_loop_depth;
719 outer_loop_idx = lvar.lv_loop_idx;
720 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721 else
722 gen_load = TRUE;
723 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000724 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000725 {
726 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
727 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000728 else
729 {
730 // "var" can be script-local even without using "s:" if it
731 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000732 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000733 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100734 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000735
736 // When evaluating an expression and the name starts with an
737 // uppercase letter it can be a user defined function.
738 // generate_funcref() will fail if the function can't be found.
739 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000740 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741 }
742 }
743 if (gen_load)
744 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
745 if (gen_load_outer > 0)
746 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100747 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
748 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 cctx->ctx_outer_used = TRUE;
750 }
751 }
752
753 *arg = end;
754
755theend:
756 if (res == FAIL && error && called_emsg == prev_called_emsg)
757 semsg(_(e_variable_not_found_str), name);
758 vim_free(name);
759 return res;
760}
761
762/*
763 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100764 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765 * Returns FAIL if compilation fails.
766 */
767 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100768compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769{
LemonBoyf3b48952022-05-05 13:53:03 +0100770 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 garray_T save_ga = cctx->ctx_instr;
772 int expr_res;
773 int trailing_error;
774 int instr_count;
775 isn_T *instr = NULL;
776
777 // Remove the string type from the stack.
778 --cctx->ctx_type_stack.ga_len;
779
780 // Temporarily reset the list of instructions so that the jump labels are
781 // correct.
782 cctx->ctx_instr.ga_len = 0;
783 cctx->ctx_instr.ga_maxlen = 0;
784 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000785
786 // avoid peeking a next line
787 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
788 cctx->ctx_ufunc->uf_lines.ga_len = 0;
789
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000791
792 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
793
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794 s = skipwhite(s);
795 trailing_error = *s != NUL;
796
797 if (expr_res == FAIL || trailing_error
798 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
799 {
800 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000801 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802 clear_instr_ga(&cctx->ctx_instr);
803 cctx->ctx_instr = save_ga;
804 ++cctx->ctx_type_stack.ga_len;
805 return FAIL;
806 }
807
808 // Move the generated instructions into the ISN_INSTR instruction, then
809 // restore the list of instructions.
810 instr_count = cctx->ctx_instr.ga_len;
811 instr = cctx->ctx_instr.ga_data;
812 instr[instr_count].isn_type = ISN_FINISH;
813
814 cctx->ctx_instr = save_ga;
815 vim_free(isn->isn_arg.string);
816 isn->isn_type = ISN_INSTR;
817 isn->isn_arg.instr = instr;
818 return OK;
819}
820
821/*
822 * Compile the argument expressions.
823 * "arg" points to just after the "(" and is advanced to after the ")"
824 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100825 int
LemonBoyf3b48952022-05-05 13:53:03 +0100826compile_arguments(
827 char_u **arg,
828 cctx_T *cctx,
829 int *argcount,
830 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831{
832 char_u *p = *arg;
833 char_u *whitep = *arg;
834 int must_end = FALSE;
835 int instr_count;
836
837 for (;;)
838 {
839 if (may_get_next_line(whitep, &p, cctx) == FAIL)
840 goto failret;
841 if (*p == ')')
842 {
843 *arg = p + 1;
844 return OK;
845 }
846 if (must_end)
847 {
848 semsg(_(e_missing_comma_before_argument_str), p);
849 return FAIL;
850 }
851
852 instr_count = cctx->ctx_instr.ga_len;
853 if (compile_expr0(&p, cctx) == FAIL)
854 return FAIL;
855 ++*argcount;
856
LemonBoyf3b48952022-05-05 13:53:03 +0100857 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000858 && cctx->ctx_instr.ga_len == instr_count + 1)
859 {
860 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
861
862 // {skip} argument of searchpair() can be compiled if not empty
863 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100864 compile_string(isn, cctx, 0);
865 }
866 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
867 && cctx->ctx_instr.ga_len == instr_count + 1)
868 {
869 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
870
871 // {sub} argument of substitute() can be compiled if it starts
872 // with \=
873 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100874 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100875 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876 }
877
878 if (*p != ',' && *skipwhite(p) == ',')
879 {
880 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
881 p = skipwhite(p);
882 }
883 if (*p == ',')
884 {
885 ++p;
886 if (*p != NUL && !VIM_ISWHITE(*p))
887 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
888 }
889 else
890 must_end = TRUE;
891 whitep = p;
892 p = skipwhite(p);
893 }
894failret:
895 emsg(_(e_missing_closing_paren));
896 return FAIL;
897}
898
899/*
900 * Compile a function call: name(arg1, arg2)
901 * "arg" points to "name", "arg + varlen" to the "(".
902 * "argcount_init" is 1 for "value->method()"
903 * Instructions:
904 * EVAL arg1
905 * EVAL arg2
906 * BCALL / DCALL / UCALL
907 */
908 static int
909compile_call(
910 char_u **arg,
911 size_t varlen,
912 cctx_T *cctx,
913 ppconst_T *ppconst,
914 int argcount_init)
915{
916 char_u *name = *arg;
917 char_u *p;
918 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100919 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920 char_u fname_buf[FLEN_FIXED + 1];
921 char_u *tofree = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 ufunc_T *ufunc = NULL;
923 int res = FAIL;
924 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000925 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100926 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000927 imported_T *import;
928
929 if (varlen >= sizeof(namebuf))
930 {
931 semsg(_(e_name_too_long_str), name);
932 return FAIL;
933 }
934 vim_strncpy(namebuf, *arg, varlen);
935
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000936 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000937 if (import != NULL)
938 {
939 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
940 return FAIL;
941 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942
943 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100944 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000945 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100946 if ((varlen == 3
947 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000948 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
949 {
950 char_u *s = skipwhite(*arg + varlen + 1);
951 typval_T argvars[2];
952 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100953 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000954
955 argvars[0].v_type = VAR_UNKNOWN;
956 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100957 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000958 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100959 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000960 s = skipwhite(s);
961 if (*s == ')' && argvars[0].v_type == VAR_STRING
962 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
963 || !is_has))
964 {
965 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
966
967 *arg = s + 1;
968 argvars[1].v_type = VAR_UNKNOWN;
969 tv->v_type = VAR_NUMBER;
970 tv->vval.v_number = 0;
971 if (is_has)
972 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100973 else if (is_len)
974 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000975 else
976 f_exists(argvars, tv);
977 clear_tv(&argvars[0]);
978 ++ppconst->pp_used;
979 return OK;
980 }
981 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100982 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 {
984 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
985 return FAIL;
986 }
987 }
988
989 if (generate_ppconst(cctx, ppconst) == FAIL)
990 return FAIL;
991
Bram Moolenaar3e1ac142023-02-18 19:49:32 +0000992 funcerror_T error;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
994
995 // We handle the "skip" argument of searchpair() and searchpairpos()
996 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100997 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
998 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
999 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
1000 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
1001 special_fn = CA_SEARCHPAIR;
1002 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
1003 special_fn = CA_SUBSTITUTE;
1004 else
1005 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001006
1007 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001008 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001009 goto theend;
1010
1011 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1012 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1013 {
1014 int idx;
1015
1016 // builtin function
1017 idx = find_internal_func(name);
1018 if (idx >= 0)
1019 {
1020 if (STRCMP(name, "flatten") == 0)
1021 {
1022 emsg(_(e_cannot_use_flatten_in_vim9_script));
1023 goto theend;
1024 }
1025
1026 if (STRCMP(name, "add") == 0 && argcount == 2)
1027 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +00001028 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001029
1030 // add() can be compiled to instructions if we know the type
1031 if (type->tt_type == VAR_LIST)
1032 {
1033 // inline "add(list, item)" so that the type can be checked
1034 res = generate_LISTAPPEND(cctx);
1035 idx = -1;
1036 }
1037 else if (type->tt_type == VAR_BLOB)
1038 {
1039 // inline "add(blob, nr)" so that the type can be checked
1040 res = generate_BLOBAPPEND(cctx);
1041 idx = -1;
1042 }
1043 }
1044
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001045 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1046 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001047 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001048 // May have the "D" or "R" flag, reserve a variable for a
1049 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001050 if (get_defer_var_idx(cctx) == 0)
1051 idx = -1;
1052 }
1053
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001054 if (idx >= 0)
1055 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1056 }
1057 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001058 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059 goto theend;
1060 }
1061
Bram Moolenaar62aec932022-01-29 21:45:34 +00001062 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1063
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001064 // An argument or local variable can be a function reference, this
1065 // overrules a function name.
1066 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1067 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1068 {
1069 // If we can find the function by name generate the right call.
1070 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001071 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001072 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001073 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001074 if (!func_is_global(ufunc))
1075 {
Bram Moolenaard0200c82023-01-28 15:19:40 +00001076 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001077 goto theend;
1078 }
1079 if (!has_g_namespace
1080 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1081 {
1082 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001083 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001084 goto theend;
1085 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086 }
1087 }
1088
1089 // If the name is a variable, load it and use PCALL.
1090 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001091 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001093 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001094 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1095 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001096 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001097
1098 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1099 goto theend;
1100 }
1101
1102 // If we can find a global function by name generate the right call.
1103 if (ufunc != NULL)
1104 {
Bram Moolenaard0200c82023-01-28 15:19:40 +00001105 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 goto theend;
1107 }
1108
1109 // A global function may be defined only later. Need to figure out at
1110 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001111 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001112 res = generate_UCALL(cctx, name, argcount);
1113 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001114 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115
1116theend:
1117 vim_free(tofree);
1118 return res;
1119}
1120
1121// like NAMESPACE_CHAR but with 'a' and 'l'.
1122#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1123
1124/*
1125 * Find the end of a variable or function name. Unlike find_name_end() this
1126 * does not recognize magic braces.
1127 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1128 * Return a pointer to just after the name. Equal to "arg" if there is no
1129 * valid name.
1130 */
1131 char_u *
1132to_name_end(char_u *arg, int use_namespace)
1133{
1134 char_u *p;
1135
1136 // Quick check for valid starting character.
1137 if (!eval_isnamec1(*arg))
1138 return arg;
1139
1140 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1141 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1142 // and can be used in slice "[n:]".
1143 if (*p == ':' && (p != arg + 1
1144 || !use_namespace
1145 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1146 break;
1147 return p;
1148}
1149
1150/*
1151 * Like to_name_end() but also skip over a list or dict constant.
1152 * Also accept "<SNR>123_Func".
1153 * This intentionally does not handle line continuation.
1154 */
1155 char_u *
1156to_name_const_end(char_u *arg)
1157{
1158 char_u *p = arg;
1159 typval_T rettv;
1160
1161 if (STRNCMP(p, "<SNR>", 5) == 0)
1162 p = skipdigits(p + 5);
1163 p = to_name_end(p, TRUE);
1164 if (p == arg && *arg == '[')
1165 {
1166
1167 // Can be "[1, 2, 3]->Func()".
1168 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1169 p = arg;
1170 }
1171 return p;
1172}
1173
1174/*
1175 * parse a list: [expr, expr]
1176 * "*arg" points to the '['.
1177 * ppconst->pp_is_const is set if all items are a constant.
1178 */
1179 static int
1180compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1181{
1182 char_u *p = skipwhite(*arg + 1);
1183 char_u *whitep = *arg + 1;
1184 int count = 0;
1185 int is_const;
1186 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001187 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188
1189 for (;;)
1190 {
1191 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1192 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001193 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001194 return FAIL;
1195 }
1196 if (*p == ',')
1197 {
1198 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1199 return FAIL;
1200 }
1201 if (*p == ']')
1202 {
1203 ++p;
1204 break;
1205 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001206 if (must_end)
1207 {
1208 semsg(_(e_missing_comma_in_list_str), p);
1209 return FAIL;
1210 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1212 return FAIL;
1213 if (!is_const)
1214 is_all_const = FALSE;
1215 ++count;
1216 if (*p == ',')
1217 {
1218 ++p;
1219 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1220 {
1221 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1222 return FAIL;
1223 }
1224 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001225 else
1226 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001227 whitep = p;
1228 p = skipwhite(p);
1229 }
1230 *arg = p;
1231
1232 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001233 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234}
1235
1236/*
1237 * Parse a lambda: "(arg, arg) => expr"
1238 * "*arg" points to the '('.
1239 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1240 */
1241 static int
1242compile_lambda(char_u **arg, cctx_T *cctx)
1243{
1244 int r;
1245 typval_T rettv;
1246 ufunc_T *ufunc;
1247 evalarg_T evalarg;
1248
1249 init_evalarg(&evalarg);
1250 evalarg.eval_flags = EVAL_EVALUATE;
1251 evalarg.eval_cctx = cctx;
1252
1253 // Get the funcref in "rettv".
1254 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1255 if (r != OK)
1256 {
1257 clear_evalarg(&evalarg, NULL);
1258 return r;
1259 }
1260
1261 // "rettv" will now be a partial referencing the function.
1262 ufunc = rettv.vval.v_partial->pt_func;
1263 ++ufunc->uf_refcount;
1264 clear_tv(&rettv);
1265
1266 // Compile it here to get the return type. The return type is optional,
1267 // when it's missing use t_unknown. This is recognized in
1268 // compile_return().
1269 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1270 ufunc->uf_ret_type = &t_unknown;
1271 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1272
1273 // When the outer function is compiled for profiling or debugging, the
1274 // lambda may be called without profiling or debugging. Compile it here in
1275 // the right context.
1276 if (cctx->ctx_compile_type == CT_DEBUG
1277#ifdef FEAT_PROFILE
1278 || cctx->ctx_compile_type == CT_PROFILE
1279#endif
1280 )
1281 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1282
Bram Moolenaar139575d2022-03-15 19:29:30 +00001283 // if the outer function is not compiled for debugging or profiling, this
1284 // one might be
1285 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001286 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001287 compiletype_T compile_type = get_compile_type(ufunc);
1288
1289 if (compile_type != CT_NONE)
1290 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001291 }
1292
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001293 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1294 // "*arg" may point into it. Point into the original line to avoid a
1295 // dangling pointer.
1296 if (evalarg.eval_using_cmdline)
1297 {
1298 garray_T *gap = &evalarg.eval_tofree_ga;
1299 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1300
1301 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1302 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001303 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304 }
1305
1306 clear_evalarg(&evalarg, NULL);
1307
1308 if (ufunc->uf_def_status == UF_COMPILED)
1309 {
1310 // The return type will now be known.
1311 set_function_type(ufunc);
1312
1313 // The function reference count will be 1. When the ISN_FUNCREF
1314 // instruction is deleted the reference count is decremented and the
1315 // function is freed.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001316 return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317 }
1318
1319 func_ptr_unref(ufunc);
1320 return FAIL;
1321}
1322
1323/*
1324 * Get a lambda and compile it. Uses Vim9 syntax.
1325 */
1326 int
1327get_lambda_tv_and_compile(
1328 char_u **arg,
1329 typval_T *rettv,
1330 int types_optional,
1331 evalarg_T *evalarg)
1332{
1333 int r;
1334 ufunc_T *ufunc;
1335 int save_sc_version = current_sctx.sc_version;
1336
1337 // Get the funcref in "rettv".
1338 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1339 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1340 current_sctx.sc_version = save_sc_version;
1341 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001342 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001343
1344 // "rettv" will now be a partial referencing the function.
1345 ufunc = rettv->vval.v_partial->pt_func;
1346
1347 // Compile it here to get the return type. The return type is optional,
1348 // when it's missing use t_unknown. This is recognized in
1349 // compile_return().
1350 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1351 ufunc->uf_ret_type = &t_unknown;
1352 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1353
1354 if (ufunc->uf_def_status == UF_COMPILED)
1355 {
1356 // The return type will now be known.
1357 set_function_type(ufunc);
1358 return OK;
1359 }
1360 clear_tv(rettv);
1361 return FAIL;
1362}
1363
1364/*
1365 * parse a dict: {key: val, [key]: val}
1366 * "*arg" points to the '{'.
1367 * ppconst->pp_is_const is set if all item values are a constant.
1368 */
1369 static int
1370compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1371{
1372 garray_T *instr = &cctx->ctx_instr;
1373 int count = 0;
1374 dict_T *d = dict_alloc();
1375 dictitem_T *item;
1376 char_u *whitep = *arg + 1;
1377 char_u *p;
1378 int is_const;
1379 int is_all_const = TRUE; // reset when non-const encountered
1380
1381 if (d == NULL)
1382 return FAIL;
1383 if (generate_ppconst(cctx, ppconst) == FAIL)
1384 return FAIL;
1385 for (;;)
1386 {
1387 char_u *key = NULL;
1388
1389 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1390 {
1391 *arg = NULL;
1392 goto failret;
1393 }
1394
1395 if (**arg == '}')
1396 break;
1397
1398 if (**arg == '[')
1399 {
1400 isn_T *isn;
1401
1402 // {[expr]: value} uses an evaluated key.
1403 *arg = skipwhite(*arg + 1);
1404 if (compile_expr0(arg, cctx) == FAIL)
1405 return FAIL;
1406 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1407 if (isn->isn_type == ISN_PUSHNR)
1408 {
1409 char buf[NUMBUFLEN];
1410
1411 // Convert to string at compile time.
1412 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1413 isn->isn_type = ISN_PUSHS;
1414 isn->isn_arg.string = vim_strsave((char_u *)buf);
1415 }
1416 if (isn->isn_type == ISN_PUSHS)
1417 key = isn->isn_arg.string;
1418 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1419 return FAIL;
1420 *arg = skipwhite(*arg);
1421 if (**arg != ']')
1422 {
1423 emsg(_(e_missing_matching_bracket_after_dict_key));
1424 return FAIL;
1425 }
1426 ++*arg;
1427 }
1428 else
1429 {
1430 // {"name": value},
1431 // {'name': value},
1432 // {name: value} use "name" as a literal key
1433 key = get_literal_key(arg);
1434 if (key == NULL)
1435 return FAIL;
1436 if (generate_PUSHS(cctx, &key) == FAIL)
1437 return FAIL;
1438 }
1439
1440 // Check for duplicate keys, if using string keys.
1441 if (key != NULL)
1442 {
1443 item = dict_find(d, key, -1);
1444 if (item != NULL)
1445 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001446 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001447 goto failret;
1448 }
1449 item = dictitem_alloc(key);
1450 if (item != NULL)
1451 {
1452 item->di_tv.v_type = VAR_UNKNOWN;
1453 item->di_tv.v_lock = 0;
1454 if (dict_add(d, item) == FAIL)
1455 dictitem_free(item);
1456 }
1457 }
1458
1459 if (**arg != ':')
1460 {
1461 if (*skipwhite(*arg) == ':')
1462 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1463 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001464 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001465 return FAIL;
1466 }
1467 whitep = *arg + 1;
1468 if (!IS_WHITE_OR_NUL(*whitep))
1469 {
1470 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1471 return FAIL;
1472 }
1473
1474 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1475 {
1476 *arg = NULL;
1477 goto failret;
1478 }
1479
1480 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1481 return FAIL;
1482 if (!is_const)
1483 is_all_const = FALSE;
1484 ++count;
1485
1486 whitep = *arg;
1487 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1488 {
1489 *arg = NULL;
1490 goto failret;
1491 }
1492 if (**arg == '}')
1493 break;
1494 if (**arg != ',')
1495 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001496 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001497 goto failret;
1498 }
1499 if (IS_WHITE_OR_NUL(*whitep))
1500 {
1501 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1502 return FAIL;
1503 }
1504 whitep = *arg + 1;
1505 if (!IS_WHITE_OR_NUL(*whitep))
1506 {
1507 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1508 return FAIL;
1509 }
1510 *arg = skipwhite(whitep);
1511 }
1512
1513 *arg = *arg + 1;
1514
1515 // Allow for following comment, after at least one space.
1516 p = skipwhite(*arg);
1517 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1518 *arg += STRLEN(*arg);
1519
1520 dict_unref(d);
1521 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001522 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001523
1524failret:
1525 if (*arg == NULL)
1526 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001527 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001528 *arg = (char_u *)"";
1529 }
1530 dict_unref(d);
1531 return FAIL;
1532}
1533
1534/*
1535 * Compile "&option".
1536 */
1537 static int
1538compile_get_option(char_u **arg, cctx_T *cctx)
1539{
1540 typval_T rettv;
1541 char_u *start = *arg;
1542 int ret;
1543
1544 // parse the option and get the current value to get the type.
1545 rettv.v_type = VAR_UNKNOWN;
1546 ret = eval_option(arg, &rettv, TRUE);
1547 if (ret == OK)
1548 {
1549 // include the '&' in the name, eval_option() expects it.
1550 char_u *name = vim_strnsave(start, *arg - start);
1551 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1552 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1553
1554 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1555 vim_free(name);
1556 }
1557 clear_tv(&rettv);
1558
1559 return ret;
1560}
1561
1562/*
1563 * Compile "$VAR".
1564 */
1565 static int
1566compile_get_env(char_u **arg, cctx_T *cctx)
1567{
1568 char_u *start = *arg;
1569 int len;
1570 int ret;
1571 char_u *name;
1572
1573 ++*arg;
1574 len = get_env_len(arg);
1575 if (len == 0)
1576 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001577 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578 return FAIL;
1579 }
1580
1581 // include the '$' in the name, eval_env_var() expects it.
1582 name = vim_strnsave(start, len + 1);
1583 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1584 vim_free(name);
1585 return ret;
1586}
1587
1588/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001589 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001590 */
1591 static int
1592compile_interp_string(char_u **arg, cctx_T *cctx)
1593{
1594 typval_T tv;
1595 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001596 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001597 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001598 int count = 0;
1599 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001600
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001601 // *arg is on the '$' character, move it to the first string character.
1602 ++*arg;
1603 quote = **arg;
1604 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001605
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001606 for (;;)
1607 {
1608 // Get the string up to the matching quote or to a single '{'.
1609 // "arg" is advanced to either the quote or the '{'.
1610 if (quote == '"')
1611 ret = eval_string(arg, &tv, evaluate, TRUE);
1612 else
1613 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1614 if (ret == FAIL)
1615 break;
1616 if (evaluate)
1617 {
1618 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1619 || (**arg != '{' && count == 0))
1620 {
1621 // generate non-empty string or empty string if it's the only
1622 // one
1623 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1624 return FAIL;
1625 tv.vval.v_string = NULL; // don't free it now
1626 ++count;
1627 }
1628 clear_tv(&tv);
1629 }
1630
1631 if (**arg != '{')
1632 {
1633 // found terminating quote
1634 ++*arg;
1635 break;
1636 }
1637
1638 p = compile_one_expr_in_str(*arg, cctx);
1639 if (p == NULL)
1640 {
1641 ret = FAIL;
1642 break;
1643 }
1644 ++count;
1645 *arg = p;
1646 }
LemonBoy2eaef102022-05-06 13:14:50 +01001647
1648 if (ret == FAIL || !evaluate)
1649 return ret;
1650
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001651 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1652 if (count > 1)
1653 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001654
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001655 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001656}
1657
1658/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659 * Compile "@r".
1660 */
1661 static int
1662compile_get_register(char_u **arg, cctx_T *cctx)
1663{
1664 int ret;
1665
1666 ++*arg;
1667 if (**arg == NUL)
1668 {
1669 semsg(_(e_syntax_error_at_str), *arg - 1);
1670 return FAIL;
1671 }
1672 if (!valid_yank_reg(**arg, FALSE))
1673 {
1674 emsg_invreg(**arg);
1675 return FAIL;
1676 }
1677 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1678 ++*arg;
1679 return ret;
1680}
1681
1682/*
1683 * Apply leading '!', '-' and '+' to constant "rettv".
1684 * When "numeric_only" is TRUE do not apply '!'.
1685 */
1686 static int
1687apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1688{
1689 char_u *p = *end;
1690
1691 // this works from end to start
1692 while (p > start)
1693 {
1694 --p;
1695 if (*p == '-' || *p == '+')
1696 {
1697 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698 if (rettv->v_type == VAR_FLOAT)
1699 {
1700 if (*p == '-')
1701 rettv->vval.v_float = -rettv->vval.v_float;
1702 }
1703 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001704 {
1705 varnumber_T val;
1706 int error = FALSE;
1707
1708 // tv_get_number_chk() accepts a string, but we don't want that
1709 // here
1710 if (check_not_string(rettv) == FAIL)
1711 return FAIL;
1712 val = tv_get_number_chk(rettv, &error);
1713 clear_tv(rettv);
1714 if (error)
1715 return FAIL;
1716 if (*p == '-')
1717 val = -val;
1718 rettv->v_type = VAR_NUMBER;
1719 rettv->vval.v_number = val;
1720 }
1721 }
1722 else if (numeric_only)
1723 {
1724 ++p;
1725 break;
1726 }
1727 else if (*p == '!')
1728 {
1729 int v = tv2bool(rettv);
1730
1731 // '!' is permissive in the type.
1732 clear_tv(rettv);
1733 rettv->v_type = VAR_BOOL;
1734 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1735 }
1736 }
1737 *end = p;
1738 return OK;
1739}
1740
1741/*
1742 * Recognize v: variables that are constants and set "rettv".
1743 */
1744 static void
1745get_vim_constant(char_u **arg, typval_T *rettv)
1746{
1747 if (STRNCMP(*arg, "v:true", 6) == 0)
1748 {
1749 rettv->v_type = VAR_BOOL;
1750 rettv->vval.v_number = VVAL_TRUE;
1751 *arg += 6;
1752 }
1753 else if (STRNCMP(*arg, "v:false", 7) == 0)
1754 {
1755 rettv->v_type = VAR_BOOL;
1756 rettv->vval.v_number = VVAL_FALSE;
1757 *arg += 7;
1758 }
1759 else if (STRNCMP(*arg, "v:null", 6) == 0)
1760 {
1761 rettv->v_type = VAR_SPECIAL;
1762 rettv->vval.v_number = VVAL_NULL;
1763 *arg += 6;
1764 }
1765 else if (STRNCMP(*arg, "v:none", 6) == 0)
1766 {
1767 rettv->v_type = VAR_SPECIAL;
1768 rettv->vval.v_number = VVAL_NONE;
1769 *arg += 6;
1770 }
1771}
1772
1773 exprtype_T
1774get_compare_type(char_u *p, int *len, int *type_is)
1775{
1776 exprtype_T type = EXPR_UNKNOWN;
1777 int i;
1778
1779 switch (p[0])
1780 {
1781 case '=': if (p[1] == '=')
1782 type = EXPR_EQUAL;
1783 else if (p[1] == '~')
1784 type = EXPR_MATCH;
1785 break;
1786 case '!': if (p[1] == '=')
1787 type = EXPR_NEQUAL;
1788 else if (p[1] == '~')
1789 type = EXPR_NOMATCH;
1790 break;
1791 case '>': if (p[1] != '=')
1792 {
1793 type = EXPR_GREATER;
1794 *len = 1;
1795 }
1796 else
1797 type = EXPR_GEQUAL;
1798 break;
1799 case '<': if (p[1] != '=')
1800 {
1801 type = EXPR_SMALLER;
1802 *len = 1;
1803 }
1804 else
1805 type = EXPR_SEQUAL;
1806 break;
1807 case 'i': if (p[1] == 's')
1808 {
1809 // "is" and "isnot"; but not a prefix of a name
1810 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1811 *len = 5;
1812 i = p[*len];
1813 if (!isalnum(i) && i != '_')
1814 {
1815 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1816 *type_is = TRUE;
1817 }
1818 }
1819 break;
1820 }
1821 return type;
1822}
1823
1824/*
1825 * Skip over an expression, ignoring most errors.
1826 */
1827 void
1828skip_expr_cctx(char_u **arg, cctx_T *cctx)
1829{
1830 evalarg_T evalarg;
1831
1832 init_evalarg(&evalarg);
1833 evalarg.eval_cctx = cctx;
1834 skip_expr(arg, &evalarg);
1835 clear_evalarg(&evalarg, NULL);
1836}
1837
1838/*
1839 * Check that the top of the type stack has a type that can be used as a
1840 * condition. Give an error and return FAIL if not.
1841 */
1842 int
1843bool_on_stack(cctx_T *cctx)
1844{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001845 type_T *type;
1846
Bram Moolenaar078a4612022-01-04 15:17:03 +00001847 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001848 if (type == &t_bool)
1849 return OK;
1850
Bram Moolenaar4913d422022-10-17 13:13:32 +01001851 if (type->tt_type == VAR_ANY
1852 || type->tt_type == VAR_UNKNOWN
1853 || type->tt_type == VAR_NUMBER
1854 || type == &t_number_bool
1855 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1857 // This requires a runtime type check.
1858 return generate_COND2BOOL(cctx);
1859
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001860 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001861}
1862
1863/*
1864 * Give the "white on both sides" error, taking the operator from "p[len]".
1865 */
1866 void
1867error_white_both(char_u *op, int len)
1868{
1869 char_u buf[10];
1870
1871 vim_strncpy(buf, op, len);
1872 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1873}
1874
1875/*
1876 * Compile code to apply '-', '+' and '!'.
1877 * When "numeric_only" is TRUE do not apply '!'.
1878 */
1879 static int
1880compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1881{
1882 char_u *p = *end;
1883
1884 // this works from end to start
1885 while (p > start)
1886 {
1887 --p;
1888 while (VIM_ISWHITE(*p))
1889 --p;
1890 if (*p == '-' || *p == '+')
1891 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001892 type_T *type = get_type_on_stack(cctx, 0);
1893 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001894 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895 return FAIL;
1896
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001897 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001898 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1899 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001900 }
1901 else if (numeric_only)
1902 {
1903 ++p;
1904 break;
1905 }
1906 else
1907 {
1908 int invert = *p == '!';
1909
1910 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1911 {
1912 if (p[-1] == '!')
1913 invert = !invert;
1914 --p;
1915 }
1916 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1917 return FAIL;
1918 }
1919 }
1920 *end = p;
1921 return OK;
1922}
1923
1924/*
1925 * Compile "(expression)": recursive!
1926 * Return FAIL/OK.
1927 */
1928 static int
1929compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1930{
1931 int ret;
1932 char_u *p = *arg + 1;
1933
1934 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1935 return FAIL;
1936 if (ppconst->pp_used <= PPSIZE - 10)
1937 {
1938 ret = compile_expr1(arg, cctx, ppconst);
1939 }
1940 else
1941 {
1942 // Not enough space in ppconst, flush constants.
1943 if (generate_ppconst(cctx, ppconst) == FAIL)
1944 return FAIL;
1945 ret = compile_expr0(arg, cctx);
1946 }
1947 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1948 return FAIL;
1949 if (**arg == ')')
1950 ++*arg;
1951 else if (ret == OK)
1952 {
1953 emsg(_(e_missing_closing_paren));
1954 ret = FAIL;
1955 }
1956 return ret;
1957}
1958
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001959static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001960
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961/*
1962 * Compile whatever comes after "name" or "name()".
1963 * Advances "*arg" only when something was recognized.
1964 */
1965 static int
1966compile_subscript(
1967 char_u **arg,
1968 cctx_T *cctx,
1969 char_u *start_leader,
1970 char_u **end_leader,
1971 ppconst_T *ppconst)
1972{
1973 char_u *name_start = *end_leader;
1974 int keeping_dict = FALSE;
1975
1976 for (;;)
1977 {
1978 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001979 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001980
1981 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1982 {
1983 char_u *next = peek_next_line_from_context(cctx);
1984
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001985 // If a following line starts with "->{", "->(" or "->X" advance to
1986 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 // Also if a following line starts with ".x".
1988 if (next != NULL &&
1989 ((next[0] == '-' && next[1] == '>'
1990 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001991 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001992 || ASCII_ISALPHA(*skipwhite(next + 2))))
1993 || (next[0] == '.' && eval_isdictc(next[1]))))
1994 {
1995 next = next_line_from_context(cctx, TRUE);
1996 if (next == NULL)
1997 return FAIL;
1998 *arg = next;
1999 p = skipwhite(*arg);
2000 }
2001 }
2002
2003 // Do not skip over white space to find the "(", "execute 'x' (expr)"
2004 // is not a function call.
2005 if (**arg == '(')
2006 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007 int argcount = 0;
2008
2009 if (generate_ppconst(cctx, ppconst) == FAIL)
2010 return FAIL;
2011 ppconst->pp_is_const = FALSE;
2012
2013 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002014 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002015
2016 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002017 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002018 return FAIL;
2019 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2020 return FAIL;
2021 if (keeping_dict)
2022 {
2023 keeping_dict = FALSE;
2024 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2025 return FAIL;
2026 }
2027 }
2028 else if (*p == '-' && p[1] == '>')
2029 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002030 char_u *pstart = p;
2031 int alt;
2032 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033
Bram Moolenaarc7349932022-01-16 20:59:39 +00002034 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002035 if (generate_ppconst(cctx, ppconst) == FAIL)
2036 return FAIL;
2037 ppconst->pp_is_const = FALSE;
2038
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002039 // Apply the '!', '-' and '+' first:
2040 // -1.0->func() works like (-1.0)->func()
2041 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2042 return FAIL;
2043
2044 p += 2;
2045 *arg = skipwhite(p);
2046 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002047
2048 // Three alternatives handled here:
2049 // 1. "base->name(" only a name, use compile_call()
2050 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2051 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002052 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002053 alt = 2;
2054 else
2055 {
2056 // alternative 1 or 3
2057 p = *arg;
2058 if (!eval_isnamec1(*p))
2059 {
2060 semsg(_(e_trailing_characters_str), pstart);
2061 return FAIL;
2062 }
2063 if (ASCII_ISALPHA(*p) && p[1] == ':')
2064 p += 2;
2065 for ( ; eval_isnamec(*p); ++p)
2066 ;
2067 if (*p == '(')
2068 {
2069 // alternative 1
2070 alt = 1;
2071 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2072 return FAIL;
2073 }
2074 else
2075 {
2076 // Must be alternative 3, find the "(". Only works within
2077 // one line.
2078 alt = 3;
2079 paren = vim_strchr(p, '(');
2080 if (paren == NULL)
2081 {
2082 semsg(_(e_missing_parenthesis_str), *arg);
2083 return FAIL;
2084 }
2085 }
2086 }
2087
2088 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002089 {
2090 int argcount = 1;
2091 garray_T *stack = &cctx->ctx_type_stack;
2092 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002093 int expr_isn_start = cctx->ctx_instr.ga_len;
2094 int expr_isn_end;
2095 int arg_isn_count;
2096
Bram Moolenaarc7349932022-01-16 20:59:39 +00002097 if (alt == 2)
2098 {
2099 // Funcref call: list->(Refs[2])(arg)
2100 // or lambda: list->((arg) => expr)(arg)
2101 //
2102 // Fist compile the function expression.
2103 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2104 return FAIL;
2105 }
2106 else
2107 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002108 int fail;
2109 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002110 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002111
Bram Moolenaarc7349932022-01-16 20:59:39 +00002112 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002113
2114 // instead of using LOADG for "import.Func" use PUSHFUNC
2115 ++paren_follows_after_expr;
2116
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002117 // do not look in the next line
2118 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002119
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002120 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002121 || *skipwhite(*arg) != NUL;
2122 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002123 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002124 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002125
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002126 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002127 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002128 if (did_emsg == prev_did_emsg)
2129 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002130 return FAIL;
2131 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002132 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002133
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002134 // Compile the arguments.
2135 if (**arg != '(')
2136 {
2137 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002138 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002139 else
2140 semsg(_(e_missing_parenthesis_str), *arg);
2141 return FAIL;
2142 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002143
2144 // Remember the next instruction index, where the instructions
2145 // for arguments are being written.
2146 expr_isn_end = cctx->ctx_instr.ga_len;
2147
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002148 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002149 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2150 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002151 return FAIL;
2152
2153 // Move the instructions for the arguments to before the
2154 // instructions of the expression and move the type of the
2155 // expression after the argument types. This is what ISN_PCALL
2156 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002157 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2158 if (arg_isn_count > 0)
2159 {
2160 int expr_isn_count = expr_isn_end - expr_isn_start;
2161 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002162 type_T *decl_type;
2163 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164
2165 if (isn == NULL)
2166 return FAIL;
2167 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2168 + expr_isn_start,
2169 sizeof(isn_T) * expr_isn_count);
2170 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2171 + expr_isn_start,
2172 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2173 sizeof(isn_T) * arg_isn_count);
2174 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2175 + expr_isn_start + arg_isn_count,
2176 isn, sizeof(isn_T) * expr_isn_count);
2177 vim_free(isn);
2178
Bram Moolenaar078a4612022-01-04 15:17:03 +00002179 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2180 type = typep->type_curr;
2181 decl_type = typep->type_decl;
2182 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2183 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2184 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002185 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002186 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2187 typep->type_curr = type;
2188 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002189 }
2190
Bram Moolenaar078a4612022-01-04 15:17:03 +00002191 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002192 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2193 return FAIL;
2194 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002195
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002196 if (keeping_dict)
2197 {
2198 keeping_dict = FALSE;
2199 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2200 return FAIL;
2201 }
2202 }
2203 else if (**arg == '[')
2204 {
2205 int is_slice = FALSE;
2206
2207 // list index: list[123]
2208 // dict member: dict[key]
2209 // string index: text[123]
2210 // blob index: blob[123]
2211 if (generate_ppconst(cctx, ppconst) == FAIL)
2212 return FAIL;
2213 ppconst->pp_is_const = FALSE;
2214
2215 ++p;
2216 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2217 return FAIL;
2218 if (**arg == ':')
2219 {
2220 // missing first index is equal to zero
2221 generate_PUSHNR(cctx, 0);
2222 }
2223 else
2224 {
2225 if (compile_expr0(arg, cctx) == FAIL)
2226 return FAIL;
2227 if (**arg == ':')
2228 {
2229 semsg(_(e_white_space_required_before_and_after_str_at_str),
2230 ":", *arg);
2231 return FAIL;
2232 }
2233 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2234 return FAIL;
2235 *arg = skipwhite(*arg);
2236 }
2237 if (**arg == ':')
2238 {
2239 is_slice = TRUE;
2240 ++*arg;
2241 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2242 {
2243 semsg(_(e_white_space_required_before_and_after_str_at_str),
2244 ":", *arg);
2245 return FAIL;
2246 }
2247 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2248 return FAIL;
2249 if (**arg == ']')
2250 // missing second index is equal to end of string
2251 generate_PUSHNR(cctx, -1);
2252 else
2253 {
2254 if (compile_expr0(arg, cctx) == FAIL)
2255 return FAIL;
2256 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2257 return FAIL;
2258 *arg = skipwhite(*arg);
2259 }
2260 }
2261
2262 if (**arg != ']')
2263 {
2264 emsg(_(e_missing_closing_square_brace));
2265 return FAIL;
2266 }
2267 *arg = *arg + 1;
2268
2269 if (keeping_dict)
2270 {
2271 keeping_dict = FALSE;
2272 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2273 return FAIL;
2274 }
2275 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2276 return FAIL;
2277 }
2278 else if (*p == '.' && p[1] != '.')
2279 {
2280 // dictionary member: dict.name
2281 if (generate_ppconst(cctx, ppconst) == FAIL)
2282 return FAIL;
2283 ppconst->pp_is_const = FALSE;
2284
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002285 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2286 && (type->tt_type == VAR_CLASS
2287 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002288 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002289 // class member: SomeClass.varname
2290 // class method: SomeClass.SomeMethod()
2291 // class constructor: SomeClass.new()
2292 // object member: someObject.varname, this.varname
2293 // object method: someObject.SomeMethod(), this.SomeMethod()
2294 *arg = p;
2295 if (compile_class_object_index(cctx, arg, type) == FAIL)
2296 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002298 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002299 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002300 *arg = p + 1;
2301 if (IS_WHITE_OR_NUL(**arg))
2302 {
2303 emsg(_(e_missing_name_after_dot));
2304 return FAIL;
2305 }
2306 p = *arg;
2307 if (eval_isdictc(*p))
2308 while (eval_isnamec(*p))
2309 MB_PTR_ADV(p);
2310 if (p == *arg)
2311 {
2312 semsg(_(e_syntax_error_at_str), *arg);
2313 return FAIL;
2314 }
2315 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2316 return FAIL;
2317 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2318 return FAIL;
2319 keeping_dict = TRUE;
2320 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002321 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002322 }
2323 else
2324 break;
2325 }
2326
2327 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2328 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002329 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2330 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002331 return FAIL;
2332
2333 return OK;
2334}
2335
2336/*
2337 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2338 * "arg" is advanced until after the expression, skipping white space.
2339 *
2340 * If the value is a constant "ppconst->pp_used" will be non-zero.
2341 * Before instructions are generated, any values in "ppconst" will generated.
2342 *
2343 * This is the compiling equivalent of eval1(), eval2(), etc.
2344 */
2345
2346/*
2347 * number number constant
2348 * 0zFFFFFFFF Blob constant
2349 * "string" string constant
2350 * 'string' literal string constant
2351 * &option-name option value
2352 * @r register contents
2353 * identifier variable value
2354 * function() function call
2355 * $VAR environment variable
2356 * (expression) nested expression
2357 * [expr, expr] List
2358 * {key: val, [key]: val} Dictionary
2359 *
2360 * Also handle:
2361 * ! in front logical NOT
2362 * - in front unary minus
2363 * + in front unary plus (ignored)
2364 * trailing (arg) funcref/partial call
2365 * trailing [] subscript in String or List
2366 * trailing .name entry in Dictionary
2367 * trailing ->name() method call
2368 */
2369 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002370compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371 char_u **arg,
2372 cctx_T *cctx,
2373 ppconst_T *ppconst)
2374{
2375 char_u *start_leader, *end_leader;
2376 int ret = OK;
2377 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2378 int used_before = ppconst->pp_used;
2379
2380 ppconst->pp_is_const = FALSE;
2381
2382 /*
2383 * Skip '!', '-' and '+' characters. They are handled later.
2384 */
2385 start_leader = *arg;
2386 if (eval_leader(arg, TRUE) == FAIL)
2387 return FAIL;
2388 end_leader = *arg;
2389
2390 rettv->v_type = VAR_UNKNOWN;
2391 switch (**arg)
2392 {
2393 /*
2394 * Number constant.
2395 */
2396 case '0': // also for blob starting with 0z
2397 case '1':
2398 case '2':
2399 case '3':
2400 case '4':
2401 case '5':
2402 case '6':
2403 case '7':
2404 case '8':
2405 case '9':
2406 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2407 return FAIL;
2408 // Apply "-" and "+" just before the number now, right to
2409 // left. Matters especially when "->" follows. Stops at
2410 // '!'.
2411 if (apply_leader(rettv, TRUE,
2412 start_leader, &end_leader) == FAIL)
2413 {
2414 clear_tv(rettv);
2415 return FAIL;
2416 }
2417 break;
2418
2419 /*
2420 * String constant: "string".
2421 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002422 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 return FAIL;
2424 break;
2425
2426 /*
2427 * Literal string constant: 'str''ing'.
2428 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002429 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 return FAIL;
2431 break;
2432
2433 /*
2434 * Constant Vim variable.
2435 */
2436 case 'v': get_vim_constant(arg, rettv);
2437 ret = NOTDONE;
2438 break;
2439
2440 /*
2441 * "true" constant
2442 */
2443 case 't': if (STRNCMP(*arg, "true", 4) == 0
2444 && !eval_isnamec((*arg)[4]))
2445 {
2446 *arg += 4;
2447 rettv->v_type = VAR_BOOL;
2448 rettv->vval.v_number = VVAL_TRUE;
2449 }
2450 else
2451 ret = NOTDONE;
2452 break;
2453
2454 /*
2455 * "false" constant
2456 */
2457 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2458 && !eval_isnamec((*arg)[5]))
2459 {
2460 *arg += 5;
2461 rettv->v_type = VAR_BOOL;
2462 rettv->vval.v_number = VVAL_FALSE;
2463 }
2464 else
2465 ret = NOTDONE;
2466 break;
2467
2468 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002469 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002470 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002471 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002473 char_u *p = *arg + 4;
2474 int len;
2475
2476 for (len = 0; eval_isnamec(p[len]); ++len)
2477 ;
2478 ret = handle_predefined(*arg, len + 4, rettv);
2479 if (ret == FAIL)
2480 ret = NOTDONE;
2481 else
2482 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002483 }
2484 else
2485 ret = NOTDONE;
2486 break;
2487
2488 /*
2489 * List: [expr, expr]
2490 */
2491 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2492 return FAIL;
2493 ret = compile_list(arg, cctx, ppconst);
2494 break;
2495
2496 /*
2497 * Dictionary: {'key': val, 'key': val}
2498 */
2499 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2500 return FAIL;
2501 ret = compile_dict(arg, cctx, ppconst);
2502 break;
2503
2504 /*
2505 * Option value: &name
2506 */
2507 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2508 return FAIL;
2509 ret = compile_get_option(arg, cctx);
2510 break;
2511
2512 /*
2513 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002514 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 */
2516 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2517 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002518 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2519 ret = compile_interp_string(arg, cctx);
2520 else
2521 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 break;
2523
2524 /*
2525 * Register contents: @r.
2526 */
2527 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2528 return FAIL;
2529 ret = compile_get_register(arg, cctx);
2530 break;
2531 /*
2532 * nested expression: (expression).
2533 * lambda: (arg, arg) => expr
2534 * funcref: (arg, arg) => { statement }
2535 */
2536 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2537 ret = compile_lambda(arg, cctx);
2538 if (ret == NOTDONE)
2539 ret = compile_parenthesis(arg, cctx, ppconst);
2540 break;
2541
2542 default: ret = NOTDONE;
2543 break;
2544 }
2545 if (ret == FAIL)
2546 return FAIL;
2547
2548 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2549 {
2550 if (cctx->ctx_skip == SKIP_YES)
2551 clear_tv(rettv);
2552 else
2553 // A constant expression can possibly be handled compile time,
2554 // return the value instead of generating code.
2555 ++ppconst->pp_used;
2556 }
2557 else if (ret == NOTDONE)
2558 {
2559 char_u *p;
2560 int r;
2561
2562 if (!eval_isnamec1(**arg))
2563 {
2564 if (!vim9_bad_comment(*arg))
2565 {
2566 if (ends_excmd(*skipwhite(*arg)))
2567 semsg(_(e_empty_expression_str), *arg);
2568 else
2569 semsg(_(e_name_expected_str), *arg);
2570 }
2571 return FAIL;
2572 }
2573
2574 // "name" or "name()"
2575 p = to_name_end(*arg, TRUE);
2576 if (p - *arg == (size_t)1 && **arg == '_')
2577 {
2578 emsg(_(e_cannot_use_underscore_here));
2579 return FAIL;
2580 }
2581
2582 if (*p == '(')
2583 {
2584 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2585 }
2586 else
2587 {
2588 if (cctx->ctx_skip != SKIP_YES
2589 && generate_ppconst(cctx, ppconst) == FAIL)
2590 return FAIL;
2591 r = compile_load(arg, p, cctx, TRUE, TRUE);
2592 }
2593 if (r == FAIL)
2594 return FAIL;
2595 }
2596
2597 // Handle following "[]", ".member", etc.
2598 // Then deal with prefixed '-', '+' and '!', if not done already.
2599 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2600 ppconst) == FAIL)
2601 return FAIL;
2602 if (ppconst->pp_used > 0)
2603 {
2604 // apply the '!', '-' and '+' before the constant
2605 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2606 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2607 return FAIL;
2608 return OK;
2609 }
2610 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2611 return FAIL;
2612 return OK;
2613}
2614
2615/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002616 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002617 */
2618 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002619compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002620{
2621 type_T *want_type = NULL;
2622
2623 // Recognize <type>
2624 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2625 {
2626 ++*arg;
2627 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2628 if (want_type == NULL)
2629 return FAIL;
2630
2631 if (**arg != '>')
2632 {
2633 if (*skipwhite(*arg) == '>')
2634 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2635 else
2636 emsg(_(e_missing_gt));
2637 return FAIL;
2638 }
2639 ++*arg;
2640 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2641 return FAIL;
2642 }
2643
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002644 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645 return FAIL;
2646
2647 if (want_type != NULL)
2648 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002649 type_T *actual;
2650 where_T where = WHERE_INIT;
2651
2652 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002653 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002654 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002655 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002656 if (need_type(actual, want_type, FALSE,
2657 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002658 return FAIL;
2659 }
2660 }
2661
2662 return OK;
2663}
2664
2665/*
2666 * * number multiplication
2667 * / number division
2668 * % number modulo
2669 */
2670 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002671compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002672{
2673 char_u *op;
2674 char_u *next;
2675 int ppconst_used = ppconst->pp_used;
2676
2677 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002678 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679 return FAIL;
2680
2681 /*
2682 * Repeat computing, until no "*", "/" or "%" is following.
2683 */
2684 for (;;)
2685 {
2686 op = may_peek_next_line(cctx, *arg, &next);
2687 if (*op != '*' && *op != '/' && *op != '%')
2688 break;
2689 if (next != NULL)
2690 {
2691 *arg = next_line_from_context(cctx, TRUE);
2692 op = skipwhite(*arg);
2693 }
2694
2695 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2696 {
2697 error_white_both(op, 1);
2698 return FAIL;
2699 }
2700 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2701 return FAIL;
2702
2703 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002704 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002705 return FAIL;
2706
2707 if (ppconst->pp_used == ppconst_used + 2
2708 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2709 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2710 {
2711 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2712 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2713 varnumber_T res = 0;
2714 int failed = FALSE;
2715
2716 // both are numbers: compute the result
2717 switch (*op)
2718 {
2719 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2720 break;
2721 case '/': res = num_divide(tv1->vval.v_number,
2722 tv2->vval.v_number, &failed);
2723 break;
2724 case '%': res = num_modulus(tv1->vval.v_number,
2725 tv2->vval.v_number, &failed);
2726 break;
2727 }
2728 if (failed)
2729 return FAIL;
2730 tv1->vval.v_number = res;
2731 --ppconst->pp_used;
2732 }
2733 else
2734 {
2735 generate_ppconst(cctx, ppconst);
2736 generate_two_op(cctx, op);
2737 }
2738 }
2739
2740 return OK;
2741}
2742
2743/*
2744 * + number addition or list/blobl concatenation
2745 * - number subtraction
2746 * .. string concatenation
2747 */
2748 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002749compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750{
2751 char_u *op;
2752 char_u *next;
2753 int oplen;
2754 int ppconst_used = ppconst->pp_used;
2755
2756 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002757 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002758 return FAIL;
2759
2760 /*
2761 * Repeat computing, until no "+", "-" or ".." is following.
2762 */
2763 for (;;)
2764 {
2765 op = may_peek_next_line(cctx, *arg, &next);
2766 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2767 break;
2768 if (op[0] == op[1] && *op != '.' && next)
2769 // Finding "++" or "--" on the next line is a separate command.
2770 // But ".." is concatenation.
2771 break;
2772 oplen = (*op == '.' ? 2 : 1);
2773 if (next != NULL)
2774 {
2775 *arg = next_line_from_context(cctx, TRUE);
2776 op = skipwhite(*arg);
2777 }
2778
2779 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2780 {
2781 error_white_both(op, oplen);
2782 return FAIL;
2783 }
2784
2785 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2786 return FAIL;
2787
2788 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002789 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002790 return FAIL;
2791
2792 if (ppconst->pp_used == ppconst_used + 2
2793 && (*op == '.'
2794 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2795 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2796 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2797 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2798 {
2799 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2800 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2801
2802 // concat/subtract/add constant numbers
2803 if (*op == '+')
2804 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2805 else if (*op == '-')
2806 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2807 else
2808 {
2809 // concatenate constant strings
2810 char_u *s1 = tv1->vval.v_string;
2811 char_u *s2 = tv2->vval.v_string;
2812 size_t len1 = STRLEN(s1);
2813
2814 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2815 if (tv1->vval.v_string == NULL)
2816 {
2817 clear_ppconst(ppconst);
2818 return FAIL;
2819 }
2820 mch_memmove(tv1->vval.v_string, s1, len1);
2821 STRCPY(tv1->vval.v_string + len1, s2);
2822 vim_free(s1);
2823 vim_free(s2);
2824 }
2825 --ppconst->pp_used;
2826 }
2827 else
2828 {
2829 generate_ppconst(cctx, ppconst);
2830 ppconst->pp_is_const = FALSE;
2831 if (*op == '.')
2832 {
2833 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2834 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2835 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002836 if (generate_CONCAT(cctx, 2) == FAIL)
2837 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002838 }
2839 else
2840 generate_two_op(cctx, op);
2841 }
2842 }
2843
2844 return OK;
2845}
2846
2847/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002848 * expr6a >> expr6b
2849 * expr6a << expr6b
2850 *
2851 * Produces instructions:
2852 * OPNR bitwise left or right shift
2853 */
2854 static int
2855compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2856{
2857 exprtype_T type = EXPR_UNKNOWN;
2858 char_u *p;
2859 char_u *next;
2860 int len = 2;
2861 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002862 isn_T *isn;
2863
2864 // get the first variable
2865 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2866 return FAIL;
2867
2868 /*
2869 * Repeat computing, until no "+", "-" or ".." is following.
2870 */
2871 for (;;)
2872 {
2873 type = EXPR_UNKNOWN;
2874
2875 p = may_peek_next_line(cctx, *arg, &next);
2876 if (p[0] == '<' && p[1] == '<')
2877 type = EXPR_LSHIFT;
2878 else if (p[0] == '>' && p[1] == '>')
2879 type = EXPR_RSHIFT;
2880
2881 if (type == EXPR_UNKNOWN)
2882 return OK;
2883
2884 // Handle a bitwise left or right shift operator
2885 if (ppconst->pp_used == ppconst_used + 1)
2886 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002887 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002888 {
2889 // left operand should be a number
2890 emsg(_(e_bitshift_ops_must_be_number));
2891 return FAIL;
2892 }
2893 }
2894 else
2895 {
2896 type_T *t = get_type_on_stack(cctx, 0);
2897
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002898 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002899 {
2900 emsg(_(e_bitshift_ops_must_be_number));
2901 return FAIL;
2902 }
2903 }
2904
2905 if (next != NULL)
2906 {
2907 *arg = next_line_from_context(cctx, TRUE);
2908 p = skipwhite(*arg);
2909 }
2910
2911 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2912 {
2913 error_white_both(p, len);
2914 return FAIL;
2915 }
2916
2917 // get the second variable
2918 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2919 return FAIL;
2920
2921 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2922 return FAIL;
2923
2924 if (ppconst->pp_used == ppconst_used + 2)
2925 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002926 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2927 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2928
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002929 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002930 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2931 {
2932 // right operand should be a positive number
2933 if (tv2->v_type != VAR_NUMBER)
2934 emsg(_(e_bitshift_ops_must_be_number));
2935 else
dundargocc57b5bc2022-11-02 13:30:51 +00002936 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002937 return FAIL;
2938 }
2939
2940 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2941 tv1->vval.v_number = 0;
2942 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002943 tv1->vval.v_number =
2944 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002945 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002946 tv1->vval.v_number =
2947 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002948 clear_tv(tv2);
2949 --ppconst->pp_used;
2950 }
2951 else
2952 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002953 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2954 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002955 {
2956 emsg(_(e_bitshift_ops_must_be_number));
2957 return FAIL;
2958 }
2959
2960 generate_ppconst(cctx, ppconst);
2961
2962 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2963 if (isn == NULL)
2964 return FAIL;
2965
2966 if (isn != NULL)
2967 isn->isn_arg.op.op_type = type;
2968 }
2969 }
2970
2971 return OK;
2972}
2973
2974/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002975 * expr5a == expr5b
2976 * expr5a =~ expr5b
2977 * expr5a != expr5b
2978 * expr5a !~ expr5b
2979 * expr5a > expr5b
2980 * expr5a >= expr5b
2981 * expr5a < expr5b
2982 * expr5a <= expr5b
2983 * expr5a is expr5b
2984 * expr5a isnot expr5b
2985 *
2986 * Produces instructions:
2987 * EVAL expr5a Push result of "expr5a"
2988 * EVAL expr5b Push result of "expr5b"
2989 * COMPARE one of the compare instructions
2990 */
2991 static int
2992compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2993{
2994 exprtype_T type = EXPR_UNKNOWN;
2995 char_u *p;
2996 char_u *next;
2997 int len = 2;
2998 int type_is = FALSE;
2999 int ppconst_used = ppconst->pp_used;
3000
3001 // get the first variable
3002 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3003 return FAIL;
3004
3005 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003006
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003007 type = get_compare_type(p, &len, &type_is);
3008
3009 /*
3010 * If there is a comparative operator, use it.
3011 */
3012 if (type != EXPR_UNKNOWN)
3013 {
3014 int ic = FALSE; // Default: do not ignore case
3015
3016 if (next != NULL)
3017 {
3018 *arg = next_line_from_context(cctx, TRUE);
3019 p = skipwhite(*arg);
3020 }
3021 if (type_is && (p[len] == '?' || p[len] == '#'))
3022 {
3023 semsg(_(e_invalid_expression_str), *arg);
3024 return FAIL;
3025 }
3026 // extra question mark appended: ignore case
3027 if (p[len] == '?')
3028 {
3029 ic = TRUE;
3030 ++len;
3031 }
3032 // extra '#' appended: match case (ignored)
3033 else if (p[len] == '#')
3034 ++len;
3035 // nothing appended: match case
3036
3037 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3038 {
3039 error_white_both(p, len);
3040 return FAIL;
3041 }
3042
3043 // get the second variable
3044 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3045 return FAIL;
3046
3047 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3048 return FAIL;
3049
3050 if (ppconst->pp_used == ppconst_used + 2)
3051 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003052 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003053 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3054 int ret;
3055
3056 // Both sides are a constant, compute the result now.
3057 // First check for a valid combination of types, this is more
3058 // strict than typval_compare().
3059 if (check_compare_types(type, tv1, tv2) == FAIL)
3060 ret = FAIL;
3061 else
3062 {
3063 ret = typval_compare(tv1, tv2, type, ic);
3064 tv1->v_type = VAR_BOOL;
3065 tv1->vval.v_number = tv1->vval.v_number
3066 ? VVAL_TRUE : VVAL_FALSE;
3067 clear_tv(tv2);
3068 --ppconst->pp_used;
3069 }
3070 return ret;
3071 }
3072
3073 generate_ppconst(cctx, ppconst);
3074 return generate_COMPARE(cctx, type, ic);
3075 }
3076
3077 return OK;
3078}
3079
3080static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3081
3082/*
3083 * Compile || or &&.
3084 */
3085 static int
3086compile_and_or(
3087 char_u **arg,
3088 cctx_T *cctx,
3089 char *op,
3090 ppconst_T *ppconst,
3091 int ppconst_used UNUSED)
3092{
3093 char_u *next;
3094 char_u *p = may_peek_next_line(cctx, *arg, &next);
3095 int opchar = *op;
3096
3097 if (p[0] == opchar && p[1] == opchar)
3098 {
3099 garray_T *instr = &cctx->ctx_instr;
3100 garray_T end_ga;
3101 int save_skip = cctx->ctx_skip;
3102
3103 /*
3104 * Repeat until there is no following "||" or "&&"
3105 */
3106 ga_init2(&end_ga, sizeof(int), 10);
3107 while (p[0] == opchar && p[1] == opchar)
3108 {
3109 long start_lnum = SOURCING_LNUM;
3110 long save_sourcing_lnum;
3111 int start_ctx_lnum = cctx->ctx_lnum;
3112 int save_lnum;
3113 int const_used;
3114 int status;
3115 jumpwhen_T jump_when = opchar == '|'
3116 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3117
3118 if (next != NULL)
3119 {
3120 *arg = next_line_from_context(cctx, TRUE);
3121 p = skipwhite(*arg);
3122 }
3123
3124 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3125 {
3126 semsg(_(e_white_space_required_before_and_after_str_at_str),
3127 op, p);
3128 ga_clear(&end_ga);
3129 return FAIL;
3130 }
3131
3132 save_sourcing_lnum = SOURCING_LNUM;
3133 SOURCING_LNUM = start_lnum;
3134 save_lnum = cctx->ctx_lnum;
3135 cctx->ctx_lnum = start_ctx_lnum;
3136
3137 status = check_ppconst_bool(ppconst);
3138 if (status != FAIL)
3139 {
3140 // Use the last ppconst if possible.
3141 if (ppconst->pp_used > 0)
3142 {
3143 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3144 int is_true = tv2bool(tv);
3145
3146 if ((is_true && opchar == '|')
3147 || (!is_true && opchar == '&'))
3148 {
3149 // For "false && expr" and "true || expr" the "expr"
3150 // does not need to be evaluated.
3151 cctx->ctx_skip = SKIP_YES;
3152 clear_tv(tv);
3153 tv->v_type = VAR_BOOL;
3154 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3155 }
3156 else
3157 {
3158 // For "true && expr" and "false || expr" only "expr"
3159 // needs to be evaluated.
3160 --ppconst->pp_used;
3161 jump_when = JUMP_NEVER;
3162 }
3163 }
3164 else
3165 {
3166 // Every part must evaluate to a bool.
3167 status = bool_on_stack(cctx);
3168 }
3169 }
3170 if (status != FAIL)
3171 status = ga_grow(&end_ga, 1);
3172 cctx->ctx_lnum = save_lnum;
3173 if (status == FAIL)
3174 {
3175 ga_clear(&end_ga);
3176 return FAIL;
3177 }
3178
3179 if (jump_when != JUMP_NEVER)
3180 {
3181 if (cctx->ctx_skip != SKIP_YES)
3182 {
3183 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3184 ++end_ga.ga_len;
3185 }
3186 generate_JUMP(cctx, jump_when, 0);
3187 }
3188
3189 // eval the next expression
3190 SOURCING_LNUM = save_sourcing_lnum;
3191 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3192 {
3193 ga_clear(&end_ga);
3194 return FAIL;
3195 }
3196
3197 const_used = ppconst->pp_used;
3198 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3199 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3200 {
3201 ga_clear(&end_ga);
3202 return FAIL;
3203 }
3204
3205 // "0 || 1" results in true, "1 && 0" results in false.
3206 if (ppconst->pp_used == const_used + 1)
3207 {
3208 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3209
3210 if (tv->v_type == VAR_NUMBER
3211 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3212 {
3213 tv->vval.v_number = tv->vval.v_number == 1
3214 ? VVAL_TRUE : VVAL_FALSE;
3215 tv->v_type = VAR_BOOL;
3216 }
3217 }
3218
3219 p = may_peek_next_line(cctx, *arg, &next);
3220 }
3221
3222 if (check_ppconst_bool(ppconst) == FAIL)
3223 {
3224 ga_clear(&end_ga);
3225 return FAIL;
3226 }
3227
3228 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3229 // Every part must evaluate to a bool.
3230 if (bool_on_stack(cctx) == FAIL)
3231 {
3232 ga_clear(&end_ga);
3233 return FAIL;
3234 }
3235
3236 if (end_ga.ga_len > 0)
3237 {
3238 // Fill in the end label in all jumps.
3239 generate_ppconst(cctx, ppconst);
3240 while (end_ga.ga_len > 0)
3241 {
3242 isn_T *isn;
3243
3244 --end_ga.ga_len;
3245 isn = ((isn_T *)instr->ga_data)
3246 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3247 isn->isn_arg.jump.jump_where = instr->ga_len;
3248 }
3249 }
3250 ga_clear(&end_ga);
3251
3252 cctx->ctx_skip = save_skip;
3253 }
3254
3255 return OK;
3256}
3257
3258/*
3259 * expr4a && expr4a && expr4a logical AND
3260 *
3261 * Produces instructions:
3262 * EVAL expr4a Push result of "expr4a"
3263 * COND2BOOL convert to bool if needed
3264 * JUMP_IF_COND_FALSE end
3265 * EVAL expr4b Push result of "expr4b"
3266 * JUMP_IF_COND_FALSE end
3267 * EVAL expr4c Push result of "expr4c"
3268 * end:
3269 */
3270 static int
3271compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3272{
3273 int ppconst_used = ppconst->pp_used;
3274
3275 // get the first variable
3276 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3277 return FAIL;
3278
3279 // || and && work almost the same
3280 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3281}
3282
3283/*
3284 * expr3a || expr3b || expr3c logical OR
3285 *
3286 * Produces instructions:
3287 * EVAL expr3a Push result of "expr3a"
3288 * COND2BOOL convert to bool if needed
3289 * JUMP_IF_COND_TRUE end
3290 * EVAL expr3b Push result of "expr3b"
3291 * JUMP_IF_COND_TRUE end
3292 * EVAL expr3c Push result of "expr3c"
3293 * end:
3294 */
3295 static int
3296compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3297{
3298 int ppconst_used = ppconst->pp_used;
3299
3300 // eval the first expression
3301 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3302 return FAIL;
3303
3304 // || and && work almost the same
3305 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3306}
3307
3308/*
3309 * Toplevel expression: expr2 ? expr1a : expr1b
3310 * Produces instructions:
3311 * EVAL expr2 Push result of "expr2"
3312 * JUMP_IF_FALSE alt jump if false
3313 * EVAL expr1a
3314 * JUMP_ALWAYS end
3315 * alt: EVAL expr1b
3316 * end:
3317 *
3318 * Toplevel expression: expr2 ?? expr1
3319 * Produces instructions:
3320 * EVAL expr2 Push result of "expr2"
3321 * JUMP_AND_KEEP_IF_TRUE end jump if true
3322 * EVAL expr1
3323 * end:
3324 */
3325 int
3326compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3327{
3328 char_u *p;
3329 int ppconst_used = ppconst->pp_used;
3330 char_u *next;
3331
3332 // Ignore all kinds of errors when not producing code.
3333 if (cctx->ctx_skip == SKIP_YES)
3334 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003335 int prev_did_emsg = did_emsg;
3336
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003337 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003338 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003339 }
3340
3341 // Evaluate the first expression.
3342 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3343 return FAIL;
3344
3345 p = may_peek_next_line(cctx, *arg, &next);
3346 if (*p == '?')
3347 {
3348 int op_falsy = p[1] == '?';
3349 garray_T *instr = &cctx->ctx_instr;
3350 garray_T *stack = &cctx->ctx_type_stack;
3351 int alt_idx = instr->ga_len;
3352 int end_idx = 0;
3353 isn_T *isn;
3354 type_T *type1 = NULL;
3355 int has_const_expr = FALSE;
3356 int const_value = FALSE;
3357 int save_skip = cctx->ctx_skip;
3358
3359 if (next != NULL)
3360 {
3361 *arg = next_line_from_context(cctx, TRUE);
3362 p = skipwhite(*arg);
3363 }
3364
3365 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3366 {
3367 semsg(_(e_white_space_required_before_and_after_str_at_str),
3368 op_falsy ? "??" : "?", p);
3369 return FAIL;
3370 }
3371
3372 if (ppconst->pp_used == ppconst_used + 1)
3373 {
3374 // the condition is a constant, we know whether the ? or the :
3375 // expression is to be evaluated.
3376 has_const_expr = TRUE;
3377 if (op_falsy)
3378 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3379 else
3380 {
3381 int error = FALSE;
3382
3383 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3384 &error);
3385 if (error)
3386 return FAIL;
3387 }
3388 cctx->ctx_skip = save_skip == SKIP_YES ||
3389 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3390
3391 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3392 // "left ?? right" and "left" is truthy: produce "left"
3393 generate_ppconst(cctx, ppconst);
3394 else
3395 {
3396 clear_tv(&ppconst->pp_tv[ppconst_used]);
3397 --ppconst->pp_used;
3398 }
3399 }
3400 else
3401 {
3402 generate_ppconst(cctx, ppconst);
3403 if (op_falsy)
3404 end_idx = instr->ga_len;
3405 generate_JUMP(cctx, op_falsy
3406 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3407 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003408 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003409 }
3410
3411 // evaluate the second expression; any type is accepted
3412 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3413 return FAIL;
3414 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3415 return FAIL;
3416
3417 if (!has_const_expr)
3418 {
3419 generate_ppconst(cctx, ppconst);
3420
3421 if (!op_falsy)
3422 {
3423 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003424 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003425 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003426
3427 end_idx = instr->ga_len;
3428 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3429
3430 // jump here from JUMP_IF_FALSE
3431 isn = ((isn_T *)instr->ga_data) + alt_idx;
3432 isn->isn_arg.jump.jump_where = instr->ga_len;
3433 }
3434 }
3435
3436 if (!op_falsy)
3437 {
3438 // Check for the ":".
3439 p = may_peek_next_line(cctx, *arg, &next);
3440 if (*p != ':')
3441 {
3442 emsg(_(e_missing_colon_after_questionmark));
3443 return FAIL;
3444 }
3445 if (next != NULL)
3446 {
3447 *arg = next_line_from_context(cctx, TRUE);
3448 p = skipwhite(*arg);
3449 }
3450
3451 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3452 {
3453 semsg(_(e_white_space_required_before_and_after_str_at_str),
3454 ":", p);
3455 return FAIL;
3456 }
3457
3458 // evaluate the third expression
3459 if (has_const_expr)
3460 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3461 ? SKIP_YES : SKIP_NOT;
3462 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3463 return FAIL;
3464 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3465 return FAIL;
3466 }
3467
3468 if (!has_const_expr)
3469 {
3470 type_T **typep;
3471
3472 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003473 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003474
3475 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003476 typep = &((((type2_T *)stack->ga_data)
3477 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003478 common_type(type1, *typep, typep, cctx->ctx_type_list);
3479
3480 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3481 isn = ((isn_T *)instr->ga_data) + end_idx;
3482 isn->isn_arg.jump.jump_where = instr->ga_len;
3483 }
3484
3485 cctx->ctx_skip = save_skip;
3486 }
3487 return OK;
3488}
3489
3490/*
3491 * Toplevel expression.
3492 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3493 * Returns OK or FAIL.
3494 */
3495 int
3496compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3497{
3498 ppconst_T ppconst;
3499
3500 CLEAR_FIELD(ppconst);
3501 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3502 {
3503 clear_ppconst(&ppconst);
3504 return FAIL;
3505 }
3506 if (is_const != NULL)
3507 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3508 if (generate_ppconst(cctx, &ppconst) == FAIL)
3509 return FAIL;
3510 return OK;
3511}
3512
3513/*
3514 * Toplevel expression.
3515 */
3516 int
3517compile_expr0(char_u **arg, cctx_T *cctx)
3518{
3519 return compile_expr0_ext(arg, cctx, NULL);
3520}
3521
3522
3523#endif // defined(FEAT_EVAL)