blob: c65fc79fd591b1d85825c89394e65a06cd671dc5 [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 Moolenaar58b40092023-01-11 15:59:05 +0000266 class_T *cl = (class_T *)type->tt_member;
267 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)
386 return generate_FUNCREF(cctx, fp, NULL);
387 }
388
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000389 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
390 }
391 else
392 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000393 // load class member
394 int idx;
395 for (idx = 0; idx < cl->class_class_member_count; ++idx)
396 {
397 ocmember_T *m = &cl->class_class_members[idx];
398 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
399 break;
400 }
401 if (idx < cl->class_class_member_count)
402 {
403 *arg = name_end;
404 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
405 }
406 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000407 }
408
409 return FAIL;
410}
411
412/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 * Generate an instruction to load script-local variable "name", without the
414 * leading "s:".
415 * Also finds imported variables.
416 */
417 int
418compile_load_scriptvar(
419 cctx_T *cctx,
420 char_u *name, // variable NUL terminated
421 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100422 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423{
424 scriptitem_T *si;
425 int idx;
426 imported_T *import;
427
428 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
429 return FAIL;
430 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000431 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000432 if (idx >= 0)
433 {
434 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
435
436 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
437 current_sctx.sc_sid, idx, sv->sv_type);
438 return OK;
439 }
440
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000441 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000442 if (import != NULL)
443 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000444 char_u *p = skipwhite(*end);
445 char_u *exp_name;
446 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100447 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000448 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000449 int done = FALSE;
450 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000451
452 // Need to lookup the member.
453 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000455 semsg(_(e_expected_dot_after_name_str), start);
456 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000457 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000458 ++p;
459 if (VIM_ISWHITE(*p))
460 {
461 emsg(_(e_no_white_space_allowed_after_dot));
462 return FAIL;
463 }
464
465 // isolate one name
466 exp_name = p;
467 while (eval_isnamec(*p))
468 ++p;
469 cc = *p;
470 *p = NUL;
471
Bram Moolenaard041f422022-01-12 19:54:00 +0000472 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100473 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
474 // "import autoload './dir/script.vim'" or
475 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100476 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100477
478 if (res == OK)
479 {
480 if (si->sn_autoload_prefix != NULL
481 && si->sn_state == SN_STATE_NOT_LOADED)
482 {
483 char_u *auto_name =
484 concat_str(si->sn_autoload_prefix, exp_name);
485
486 // autoload script must be loaded later, access by the autoload
487 // name. If a '(' follows it must be a function. Otherwise we
488 // don't know, it can be "script.Func".
489 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100490 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100491 else
492 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
493 vim_free(auto_name);
494 done = TRUE;
495 }
496 else if (si->sn_import_autoload
497 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100498 {
499 // If a '(' follows it must be a function. Otherwise we don't
500 // know, it can be "script.Func".
501 if (cc == '(' || paren_follows_after_expr)
502 {
503 char_u sid_name[MAX_FUNC_NAME_LEN];
504
505 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100506 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100507 }
508 else
509 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
510 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100511 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100512 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100513 else
514 {
515 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
516 cctx, NULL, TRUE);
517 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100518 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100519
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000520 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000521 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000522 if (done)
523 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000524
525 if (idx < 0)
526 {
527 if (ufunc != NULL)
528 {
529 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100530 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000531 return OK;
532 }
533 return FAIL;
534 }
535
536 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
537 import->imp_sid,
538 idx,
539 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000540 return OK;
541 }
542
Bram Moolenaard0132f42022-05-12 11:05:40 +0100543 // Can only get here if we know "name" is a script variable and not in a
544 // Vim9 script (variable is not in sn_var_vals): old style script.
545 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000546 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547}
548
549 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000550generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000551{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000552 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000553 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000554
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000555 // Reject a global non-autoload function found without the "g:" prefix.
556 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000557 return FAIL;
558
559 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000560 compile_type = get_compile_type(ufunc);
561 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000562 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000563 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100564 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565}
566
567/*
568 * Compile a variable name into a load instruction.
569 * "end" points to just after the name.
570 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
571 * When "error" is FALSE do not give an error when not found.
572 */
573 int
574compile_load(
575 char_u **arg,
576 char_u *end_arg,
577 cctx_T *cctx,
578 int is_expr,
579 int error)
580{
581 type_T *type;
582 char_u *name = NULL;
583 char_u *end = end_arg;
584 int res = FAIL;
585 int prev_called_emsg = called_emsg;
586
587 if (*(*arg + 1) == ':')
588 {
589 if (end <= *arg + 2)
590 {
591 isntype_T isn_type;
592
593 // load dictionary of namespace
594 switch (**arg)
595 {
596 case 'g': isn_type = ISN_LOADGDICT; break;
597 case 'w': isn_type = ISN_LOADWDICT; break;
598 case 't': isn_type = ISN_LOADTDICT; break;
599 case 'b': isn_type = ISN_LOADBDICT; break;
600 default:
601 semsg(_(e_namespace_not_supported_str), *arg);
602 goto theend;
603 }
604 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
605 goto theend;
606 res = OK;
607 }
608 else
609 {
610 isntype_T isn_type = ISN_DROP;
611
612 // load namespaced variable
613 name = vim_strnsave(*arg + 2, end - (*arg + 2));
614 if (name == NULL)
615 return FAIL;
616
617 switch (**arg)
618 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100619 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000621 case 's': if (current_script_is_vim9())
622 {
623 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
624 *arg);
625 vim_free(name);
626 return FAIL;
627 }
Kota Kato948a3892022-08-16 16:09:59 +0100628 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000629 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000630 else
631 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100632 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000633 break;
634 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
635 {
636 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000637 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000638 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000639 else
640 isn_type = ISN_LOADG;
641 }
642 else
643 {
644 isn_type = ISN_LOADAUTO;
645 vim_free(name);
646 name = vim_strnsave(*arg, end - *arg);
647 if (name == NULL)
648 return FAIL;
649 }
650 break;
651 case 'w': isn_type = ISN_LOADW; break;
652 case 't': isn_type = ISN_LOADT; break;
653 case 'b': isn_type = ISN_LOADB; break;
654 default: // cannot happen, just in case
655 semsg(_(e_namespace_not_supported_str), *arg);
656 goto theend;
657 }
658 if (isn_type != ISN_DROP)
659 {
660 // Global, Buffer-local, Window-local and Tabpage-local
661 // variables can be defined later, thus we don't check if it
662 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000663 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000664 }
665 }
666 }
667 else
668 {
669 size_t len = end - *arg;
670 int idx;
671 int gen_load = FALSE;
672 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100673 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100674 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000675
676 name = vim_strnsave(*arg, end - *arg);
677 if (name == NULL)
678 return FAIL;
679
Bram Moolenaar58b40092023-01-11 15:59:05 +0000680 if (STRCMP(name, "super") == 0
681 && cctx->ctx_ufunc != NULL
682 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
683 {
684 // super.SomeFunc() in a class function: push &t_super type, this
685 // is recognized in compile_subscript().
686 res = push_type_stack(cctx, &t_super);
687 if (*end != '.')
688 emsg(_(e_super_must_be_followed_by_dot));
689 }
690 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000691 {
692 script_autoload(name, FALSE);
693 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
694 }
695 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
696 == OK)
697 {
698 if (gen_load_outer == 0)
699 gen_load = TRUE;
700 }
701 else
702 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000703 lvar_T lvar;
704 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000705
706 if (lookup_local(*arg, len, &lvar, cctx) == OK)
707 {
708 type = lvar.lv_type;
709 idx = lvar.lv_idx;
710 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100711 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000712 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100713 outer_loop_depth = lvar.lv_loop_depth;
714 outer_loop_idx = lvar.lv_loop_idx;
715 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 else
717 gen_load = TRUE;
718 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000719 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000720 {
721 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
722 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000723 else
724 {
725 // "var" can be script-local even without using "s:" if it
726 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000727 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000728 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100729 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730
731 // When evaluating an expression and the name starts with an
732 // uppercase letter it can be a user defined function.
733 // generate_funcref() will fail if the function can't be found.
734 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000735 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000736 }
737 }
738 if (gen_load)
739 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
740 if (gen_load_outer > 0)
741 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100742 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
743 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744 cctx->ctx_outer_used = TRUE;
745 }
746 }
747
748 *arg = end;
749
750theend:
751 if (res == FAIL && error && called_emsg == prev_called_emsg)
752 semsg(_(e_variable_not_found_str), name);
753 vim_free(name);
754 return res;
755}
756
757/*
758 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100759 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000760 * Returns FAIL if compilation fails.
761 */
762 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100763compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764{
LemonBoyf3b48952022-05-05 13:53:03 +0100765 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000766 garray_T save_ga = cctx->ctx_instr;
767 int expr_res;
768 int trailing_error;
769 int instr_count;
770 isn_T *instr = NULL;
771
772 // Remove the string type from the stack.
773 --cctx->ctx_type_stack.ga_len;
774
775 // Temporarily reset the list of instructions so that the jump labels are
776 // correct.
777 cctx->ctx_instr.ga_len = 0;
778 cctx->ctx_instr.ga_maxlen = 0;
779 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000780
781 // avoid peeking a next line
782 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
783 cctx->ctx_ufunc->uf_lines.ga_len = 0;
784
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000786
787 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
788
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 s = skipwhite(s);
790 trailing_error = *s != NUL;
791
792 if (expr_res == FAIL || trailing_error
793 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
794 {
795 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000796 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797 clear_instr_ga(&cctx->ctx_instr);
798 cctx->ctx_instr = save_ga;
799 ++cctx->ctx_type_stack.ga_len;
800 return FAIL;
801 }
802
803 // Move the generated instructions into the ISN_INSTR instruction, then
804 // restore the list of instructions.
805 instr_count = cctx->ctx_instr.ga_len;
806 instr = cctx->ctx_instr.ga_data;
807 instr[instr_count].isn_type = ISN_FINISH;
808
809 cctx->ctx_instr = save_ga;
810 vim_free(isn->isn_arg.string);
811 isn->isn_type = ISN_INSTR;
812 isn->isn_arg.instr = instr;
813 return OK;
814}
815
816/*
817 * Compile the argument expressions.
818 * "arg" points to just after the "(" and is advanced to after the ")"
819 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100820 int
LemonBoyf3b48952022-05-05 13:53:03 +0100821compile_arguments(
822 char_u **arg,
823 cctx_T *cctx,
824 int *argcount,
825 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000826{
827 char_u *p = *arg;
828 char_u *whitep = *arg;
829 int must_end = FALSE;
830 int instr_count;
831
832 for (;;)
833 {
834 if (may_get_next_line(whitep, &p, cctx) == FAIL)
835 goto failret;
836 if (*p == ')')
837 {
838 *arg = p + 1;
839 return OK;
840 }
841 if (must_end)
842 {
843 semsg(_(e_missing_comma_before_argument_str), p);
844 return FAIL;
845 }
846
847 instr_count = cctx->ctx_instr.ga_len;
848 if (compile_expr0(&p, cctx) == FAIL)
849 return FAIL;
850 ++*argcount;
851
LemonBoyf3b48952022-05-05 13:53:03 +0100852 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 && cctx->ctx_instr.ga_len == instr_count + 1)
854 {
855 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
856
857 // {skip} argument of searchpair() can be compiled if not empty
858 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100859 compile_string(isn, cctx, 0);
860 }
861 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
862 && cctx->ctx_instr.ga_len == instr_count + 1)
863 {
864 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
865
866 // {sub} argument of substitute() can be compiled if it starts
867 // with \=
868 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100869 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100870 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871 }
872
873 if (*p != ',' && *skipwhite(p) == ',')
874 {
875 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
876 p = skipwhite(p);
877 }
878 if (*p == ',')
879 {
880 ++p;
881 if (*p != NUL && !VIM_ISWHITE(*p))
882 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
883 }
884 else
885 must_end = TRUE;
886 whitep = p;
887 p = skipwhite(p);
888 }
889failret:
890 emsg(_(e_missing_closing_paren));
891 return FAIL;
892}
893
894/*
895 * Compile a function call: name(arg1, arg2)
896 * "arg" points to "name", "arg + varlen" to the "(".
897 * "argcount_init" is 1 for "value->method()"
898 * Instructions:
899 * EVAL arg1
900 * EVAL arg2
901 * BCALL / DCALL / UCALL
902 */
903 static int
904compile_call(
905 char_u **arg,
906 size_t varlen,
907 cctx_T *cctx,
908 ppconst_T *ppconst,
909 int argcount_init)
910{
911 char_u *name = *arg;
912 char_u *p;
913 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100914 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000915 char_u fname_buf[FLEN_FIXED + 1];
916 char_u *tofree = NULL;
917 int error = FCERR_NONE;
918 ufunc_T *ufunc = NULL;
919 int res = FAIL;
920 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000921 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100922 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000923 imported_T *import;
924
925 if (varlen >= sizeof(namebuf))
926 {
927 semsg(_(e_name_too_long_str), name);
928 return FAIL;
929 }
930 vim_strncpy(namebuf, *arg, varlen);
931
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000932 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000933 if (import != NULL)
934 {
935 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
936 return FAIL;
937 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938
939 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100940 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000941 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100942 if ((varlen == 3
943 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000944 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
945 {
946 char_u *s = skipwhite(*arg + varlen + 1);
947 typval_T argvars[2];
948 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100949 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950
951 argvars[0].v_type = VAR_UNKNOWN;
952 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100953 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000954 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100955 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000956 s = skipwhite(s);
957 if (*s == ')' && argvars[0].v_type == VAR_STRING
958 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
959 || !is_has))
960 {
961 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
962
963 *arg = s + 1;
964 argvars[1].v_type = VAR_UNKNOWN;
965 tv->v_type = VAR_NUMBER;
966 tv->vval.v_number = 0;
967 if (is_has)
968 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100969 else if (is_len)
970 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971 else
972 f_exists(argvars, tv);
973 clear_tv(&argvars[0]);
974 ++ppconst->pp_used;
975 return OK;
976 }
977 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100978 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000979 {
980 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
981 return FAIL;
982 }
983 }
984
985 if (generate_ppconst(cctx, ppconst) == FAIL)
986 return FAIL;
987
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
989
990 // We handle the "skip" argument of searchpair() and searchpairpos()
991 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100992 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
993 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
994 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
995 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
996 special_fn = CA_SEARCHPAIR;
997 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
998 special_fn = CA_SUBSTITUTE;
999 else
1000 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001
1002 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001003 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001004 goto theend;
1005
1006 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1007 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1008 {
1009 int idx;
1010
1011 // builtin function
1012 idx = find_internal_func(name);
1013 if (idx >= 0)
1014 {
1015 if (STRCMP(name, "flatten") == 0)
1016 {
1017 emsg(_(e_cannot_use_flatten_in_vim9_script));
1018 goto theend;
1019 }
1020
1021 if (STRCMP(name, "add") == 0 && argcount == 2)
1022 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +00001023 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001024
1025 // add() can be compiled to instructions if we know the type
1026 if (type->tt_type == VAR_LIST)
1027 {
1028 // inline "add(list, item)" so that the type can be checked
1029 res = generate_LISTAPPEND(cctx);
1030 idx = -1;
1031 }
1032 else if (type->tt_type == VAR_BLOB)
1033 {
1034 // inline "add(blob, nr)" so that the type can be checked
1035 res = generate_BLOBAPPEND(cctx);
1036 idx = -1;
1037 }
1038 }
1039
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001040 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1041 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001042 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001043 // May have the "D" or "R" flag, reserve a variable for a
1044 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001045 if (get_defer_var_idx(cctx) == 0)
1046 idx = -1;
1047 }
1048
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001049 if (idx >= 0)
1050 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1051 }
1052 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001053 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001054 goto theend;
1055 }
1056
Bram Moolenaar62aec932022-01-29 21:45:34 +00001057 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1058
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059 // An argument or local variable can be a function reference, this
1060 // overrules a function name.
1061 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1062 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1063 {
1064 // If we can find the function by name generate the right call.
1065 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001066 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001067 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001068 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001069 if (!func_is_global(ufunc))
1070 {
Bram Moolenaard0200c82023-01-28 15:19:40 +00001071 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001072 goto theend;
1073 }
1074 if (!has_g_namespace
1075 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1076 {
1077 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001078 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001079 goto theend;
1080 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 }
1082 }
1083
1084 // If the name is a variable, load it and use PCALL.
1085 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001086 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001087 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001088 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1090 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001091 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092
1093 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1094 goto theend;
1095 }
1096
1097 // If we can find a global function by name generate the right call.
1098 if (ufunc != NULL)
1099 {
Bram Moolenaard0200c82023-01-28 15:19:40 +00001100 res = generate_CALL(cctx, ufunc, NULL, 0, argcount);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101 goto theend;
1102 }
1103
1104 // A global function may be defined only later. Need to figure out at
1105 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001106 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107 res = generate_UCALL(cctx, name, argcount);
1108 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001109 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001110
1111theend:
1112 vim_free(tofree);
1113 return res;
1114}
1115
1116// like NAMESPACE_CHAR but with 'a' and 'l'.
1117#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1118
1119/*
1120 * Find the end of a variable or function name. Unlike find_name_end() this
1121 * does not recognize magic braces.
1122 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1123 * Return a pointer to just after the name. Equal to "arg" if there is no
1124 * valid name.
1125 */
1126 char_u *
1127to_name_end(char_u *arg, int use_namespace)
1128{
1129 char_u *p;
1130
1131 // Quick check for valid starting character.
1132 if (!eval_isnamec1(*arg))
1133 return arg;
1134
1135 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1136 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1137 // and can be used in slice "[n:]".
1138 if (*p == ':' && (p != arg + 1
1139 || !use_namespace
1140 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1141 break;
1142 return p;
1143}
1144
1145/*
1146 * Like to_name_end() but also skip over a list or dict constant.
1147 * Also accept "<SNR>123_Func".
1148 * This intentionally does not handle line continuation.
1149 */
1150 char_u *
1151to_name_const_end(char_u *arg)
1152{
1153 char_u *p = arg;
1154 typval_T rettv;
1155
1156 if (STRNCMP(p, "<SNR>", 5) == 0)
1157 p = skipdigits(p + 5);
1158 p = to_name_end(p, TRUE);
1159 if (p == arg && *arg == '[')
1160 {
1161
1162 // Can be "[1, 2, 3]->Func()".
1163 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1164 p = arg;
1165 }
1166 return p;
1167}
1168
1169/*
1170 * parse a list: [expr, expr]
1171 * "*arg" points to the '['.
1172 * ppconst->pp_is_const is set if all items are a constant.
1173 */
1174 static int
1175compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1176{
1177 char_u *p = skipwhite(*arg + 1);
1178 char_u *whitep = *arg + 1;
1179 int count = 0;
1180 int is_const;
1181 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001182 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183
1184 for (;;)
1185 {
1186 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1187 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001188 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001189 return FAIL;
1190 }
1191 if (*p == ',')
1192 {
1193 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1194 return FAIL;
1195 }
1196 if (*p == ']')
1197 {
1198 ++p;
1199 break;
1200 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001201 if (must_end)
1202 {
1203 semsg(_(e_missing_comma_in_list_str), p);
1204 return FAIL;
1205 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001206 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1207 return FAIL;
1208 if (!is_const)
1209 is_all_const = FALSE;
1210 ++count;
1211 if (*p == ',')
1212 {
1213 ++p;
1214 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1215 {
1216 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1217 return FAIL;
1218 }
1219 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001220 else
1221 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001222 whitep = p;
1223 p = skipwhite(p);
1224 }
1225 *arg = p;
1226
1227 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001228 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229}
1230
1231/*
1232 * Parse a lambda: "(arg, arg) => expr"
1233 * "*arg" points to the '('.
1234 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1235 */
1236 static int
1237compile_lambda(char_u **arg, cctx_T *cctx)
1238{
1239 int r;
1240 typval_T rettv;
1241 ufunc_T *ufunc;
1242 evalarg_T evalarg;
1243
1244 init_evalarg(&evalarg);
1245 evalarg.eval_flags = EVAL_EVALUATE;
1246 evalarg.eval_cctx = cctx;
1247
1248 // Get the funcref in "rettv".
1249 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1250 if (r != OK)
1251 {
1252 clear_evalarg(&evalarg, NULL);
1253 return r;
1254 }
1255
1256 // "rettv" will now be a partial referencing the function.
1257 ufunc = rettv.vval.v_partial->pt_func;
1258 ++ufunc->uf_refcount;
1259 clear_tv(&rettv);
1260
1261 // Compile it here to get the return type. The return type is optional,
1262 // when it's missing use t_unknown. This is recognized in
1263 // compile_return().
1264 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1265 ufunc->uf_ret_type = &t_unknown;
1266 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1267
1268 // When the outer function is compiled for profiling or debugging, the
1269 // lambda may be called without profiling or debugging. Compile it here in
1270 // the right context.
1271 if (cctx->ctx_compile_type == CT_DEBUG
1272#ifdef FEAT_PROFILE
1273 || cctx->ctx_compile_type == CT_PROFILE
1274#endif
1275 )
1276 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1277
Bram Moolenaar139575d2022-03-15 19:29:30 +00001278 // if the outer function is not compiled for debugging or profiling, this
1279 // one might be
1280 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001281 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001282 compiletype_T compile_type = get_compile_type(ufunc);
1283
1284 if (compile_type != CT_NONE)
1285 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001286 }
1287
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001288 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1289 // "*arg" may point into it. Point into the original line to avoid a
1290 // dangling pointer.
1291 if (evalarg.eval_using_cmdline)
1292 {
1293 garray_T *gap = &evalarg.eval_tofree_ga;
1294 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1295
1296 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1297 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001298 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299 }
1300
1301 clear_evalarg(&evalarg, NULL);
1302
1303 if (ufunc->uf_def_status == UF_COMPILED)
1304 {
1305 // The return type will now be known.
1306 set_function_type(ufunc);
1307
1308 // The function reference count will be 1. When the ISN_FUNCREF
1309 // instruction is deleted the reference count is decremented and the
1310 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001311 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312 }
1313
1314 func_ptr_unref(ufunc);
1315 return FAIL;
1316}
1317
1318/*
1319 * Get a lambda and compile it. Uses Vim9 syntax.
1320 */
1321 int
1322get_lambda_tv_and_compile(
1323 char_u **arg,
1324 typval_T *rettv,
1325 int types_optional,
1326 evalarg_T *evalarg)
1327{
1328 int r;
1329 ufunc_T *ufunc;
1330 int save_sc_version = current_sctx.sc_version;
1331
1332 // Get the funcref in "rettv".
1333 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1334 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1335 current_sctx.sc_version = save_sc_version;
1336 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001337 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338
1339 // "rettv" will now be a partial referencing the function.
1340 ufunc = rettv->vval.v_partial->pt_func;
1341
1342 // Compile it here to get the return type. The return type is optional,
1343 // when it's missing use t_unknown. This is recognized in
1344 // compile_return().
1345 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1346 ufunc->uf_ret_type = &t_unknown;
1347 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1348
1349 if (ufunc->uf_def_status == UF_COMPILED)
1350 {
1351 // The return type will now be known.
1352 set_function_type(ufunc);
1353 return OK;
1354 }
1355 clear_tv(rettv);
1356 return FAIL;
1357}
1358
1359/*
1360 * parse a dict: {key: val, [key]: val}
1361 * "*arg" points to the '{'.
1362 * ppconst->pp_is_const is set if all item values are a constant.
1363 */
1364 static int
1365compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1366{
1367 garray_T *instr = &cctx->ctx_instr;
1368 int count = 0;
1369 dict_T *d = dict_alloc();
1370 dictitem_T *item;
1371 char_u *whitep = *arg + 1;
1372 char_u *p;
1373 int is_const;
1374 int is_all_const = TRUE; // reset when non-const encountered
1375
1376 if (d == NULL)
1377 return FAIL;
1378 if (generate_ppconst(cctx, ppconst) == FAIL)
1379 return FAIL;
1380 for (;;)
1381 {
1382 char_u *key = NULL;
1383
1384 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1385 {
1386 *arg = NULL;
1387 goto failret;
1388 }
1389
1390 if (**arg == '}')
1391 break;
1392
1393 if (**arg == '[')
1394 {
1395 isn_T *isn;
1396
1397 // {[expr]: value} uses an evaluated key.
1398 *arg = skipwhite(*arg + 1);
1399 if (compile_expr0(arg, cctx) == FAIL)
1400 return FAIL;
1401 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1402 if (isn->isn_type == ISN_PUSHNR)
1403 {
1404 char buf[NUMBUFLEN];
1405
1406 // Convert to string at compile time.
1407 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1408 isn->isn_type = ISN_PUSHS;
1409 isn->isn_arg.string = vim_strsave((char_u *)buf);
1410 }
1411 if (isn->isn_type == ISN_PUSHS)
1412 key = isn->isn_arg.string;
1413 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1414 return FAIL;
1415 *arg = skipwhite(*arg);
1416 if (**arg != ']')
1417 {
1418 emsg(_(e_missing_matching_bracket_after_dict_key));
1419 return FAIL;
1420 }
1421 ++*arg;
1422 }
1423 else
1424 {
1425 // {"name": value},
1426 // {'name': value},
1427 // {name: value} use "name" as a literal key
1428 key = get_literal_key(arg);
1429 if (key == NULL)
1430 return FAIL;
1431 if (generate_PUSHS(cctx, &key) == FAIL)
1432 return FAIL;
1433 }
1434
1435 // Check for duplicate keys, if using string keys.
1436 if (key != NULL)
1437 {
1438 item = dict_find(d, key, -1);
1439 if (item != NULL)
1440 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001441 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 goto failret;
1443 }
1444 item = dictitem_alloc(key);
1445 if (item != NULL)
1446 {
1447 item->di_tv.v_type = VAR_UNKNOWN;
1448 item->di_tv.v_lock = 0;
1449 if (dict_add(d, item) == FAIL)
1450 dictitem_free(item);
1451 }
1452 }
1453
1454 if (**arg != ':')
1455 {
1456 if (*skipwhite(*arg) == ':')
1457 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1458 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001459 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001460 return FAIL;
1461 }
1462 whitep = *arg + 1;
1463 if (!IS_WHITE_OR_NUL(*whitep))
1464 {
1465 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1466 return FAIL;
1467 }
1468
1469 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1470 {
1471 *arg = NULL;
1472 goto failret;
1473 }
1474
1475 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1476 return FAIL;
1477 if (!is_const)
1478 is_all_const = FALSE;
1479 ++count;
1480
1481 whitep = *arg;
1482 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1483 {
1484 *arg = NULL;
1485 goto failret;
1486 }
1487 if (**arg == '}')
1488 break;
1489 if (**arg != ',')
1490 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001491 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492 goto failret;
1493 }
1494 if (IS_WHITE_OR_NUL(*whitep))
1495 {
1496 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1497 return FAIL;
1498 }
1499 whitep = *arg + 1;
1500 if (!IS_WHITE_OR_NUL(*whitep))
1501 {
1502 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1503 return FAIL;
1504 }
1505 *arg = skipwhite(whitep);
1506 }
1507
1508 *arg = *arg + 1;
1509
1510 // Allow for following comment, after at least one space.
1511 p = skipwhite(*arg);
1512 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1513 *arg += STRLEN(*arg);
1514
1515 dict_unref(d);
1516 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001517 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001518
1519failret:
1520 if (*arg == NULL)
1521 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001522 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001523 *arg = (char_u *)"";
1524 }
1525 dict_unref(d);
1526 return FAIL;
1527}
1528
1529/*
1530 * Compile "&option".
1531 */
1532 static int
1533compile_get_option(char_u **arg, cctx_T *cctx)
1534{
1535 typval_T rettv;
1536 char_u *start = *arg;
1537 int ret;
1538
1539 // parse the option and get the current value to get the type.
1540 rettv.v_type = VAR_UNKNOWN;
1541 ret = eval_option(arg, &rettv, TRUE);
1542 if (ret == OK)
1543 {
1544 // include the '&' in the name, eval_option() expects it.
1545 char_u *name = vim_strnsave(start, *arg - start);
1546 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1547 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1548
1549 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1550 vim_free(name);
1551 }
1552 clear_tv(&rettv);
1553
1554 return ret;
1555}
1556
1557/*
1558 * Compile "$VAR".
1559 */
1560 static int
1561compile_get_env(char_u **arg, cctx_T *cctx)
1562{
1563 char_u *start = *arg;
1564 int len;
1565 int ret;
1566 char_u *name;
1567
1568 ++*arg;
1569 len = get_env_len(arg);
1570 if (len == 0)
1571 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001572 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573 return FAIL;
1574 }
1575
1576 // include the '$' in the name, eval_env_var() expects it.
1577 name = vim_strnsave(start, len + 1);
1578 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1579 vim_free(name);
1580 return ret;
1581}
1582
1583/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001584 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001585 */
1586 static int
1587compile_interp_string(char_u **arg, cctx_T *cctx)
1588{
1589 typval_T tv;
1590 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001591 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001592 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001593 int count = 0;
1594 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001595
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001596 // *arg is on the '$' character, move it to the first string character.
1597 ++*arg;
1598 quote = **arg;
1599 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001600
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001601 for (;;)
1602 {
1603 // Get the string up to the matching quote or to a single '{'.
1604 // "arg" is advanced to either the quote or the '{'.
1605 if (quote == '"')
1606 ret = eval_string(arg, &tv, evaluate, TRUE);
1607 else
1608 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1609 if (ret == FAIL)
1610 break;
1611 if (evaluate)
1612 {
1613 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1614 || (**arg != '{' && count == 0))
1615 {
1616 // generate non-empty string or empty string if it's the only
1617 // one
1618 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1619 return FAIL;
1620 tv.vval.v_string = NULL; // don't free it now
1621 ++count;
1622 }
1623 clear_tv(&tv);
1624 }
1625
1626 if (**arg != '{')
1627 {
1628 // found terminating quote
1629 ++*arg;
1630 break;
1631 }
1632
1633 p = compile_one_expr_in_str(*arg, cctx);
1634 if (p == NULL)
1635 {
1636 ret = FAIL;
1637 break;
1638 }
1639 ++count;
1640 *arg = p;
1641 }
LemonBoy2eaef102022-05-06 13:14:50 +01001642
1643 if (ret == FAIL || !evaluate)
1644 return ret;
1645
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001646 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1647 if (count > 1)
1648 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001649
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001650 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001651}
1652
1653/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654 * Compile "@r".
1655 */
1656 static int
1657compile_get_register(char_u **arg, cctx_T *cctx)
1658{
1659 int ret;
1660
1661 ++*arg;
1662 if (**arg == NUL)
1663 {
1664 semsg(_(e_syntax_error_at_str), *arg - 1);
1665 return FAIL;
1666 }
1667 if (!valid_yank_reg(**arg, FALSE))
1668 {
1669 emsg_invreg(**arg);
1670 return FAIL;
1671 }
1672 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1673 ++*arg;
1674 return ret;
1675}
1676
1677/*
1678 * Apply leading '!', '-' and '+' to constant "rettv".
1679 * When "numeric_only" is TRUE do not apply '!'.
1680 */
1681 static int
1682apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1683{
1684 char_u *p = *end;
1685
1686 // this works from end to start
1687 while (p > start)
1688 {
1689 --p;
1690 if (*p == '-' || *p == '+')
1691 {
1692 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001693 if (rettv->v_type == VAR_FLOAT)
1694 {
1695 if (*p == '-')
1696 rettv->vval.v_float = -rettv->vval.v_float;
1697 }
1698 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699 {
1700 varnumber_T val;
1701 int error = FALSE;
1702
1703 // tv_get_number_chk() accepts a string, but we don't want that
1704 // here
1705 if (check_not_string(rettv) == FAIL)
1706 return FAIL;
1707 val = tv_get_number_chk(rettv, &error);
1708 clear_tv(rettv);
1709 if (error)
1710 return FAIL;
1711 if (*p == '-')
1712 val = -val;
1713 rettv->v_type = VAR_NUMBER;
1714 rettv->vval.v_number = val;
1715 }
1716 }
1717 else if (numeric_only)
1718 {
1719 ++p;
1720 break;
1721 }
1722 else if (*p == '!')
1723 {
1724 int v = tv2bool(rettv);
1725
1726 // '!' is permissive in the type.
1727 clear_tv(rettv);
1728 rettv->v_type = VAR_BOOL;
1729 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1730 }
1731 }
1732 *end = p;
1733 return OK;
1734}
1735
1736/*
1737 * Recognize v: variables that are constants and set "rettv".
1738 */
1739 static void
1740get_vim_constant(char_u **arg, typval_T *rettv)
1741{
1742 if (STRNCMP(*arg, "v:true", 6) == 0)
1743 {
1744 rettv->v_type = VAR_BOOL;
1745 rettv->vval.v_number = VVAL_TRUE;
1746 *arg += 6;
1747 }
1748 else if (STRNCMP(*arg, "v:false", 7) == 0)
1749 {
1750 rettv->v_type = VAR_BOOL;
1751 rettv->vval.v_number = VVAL_FALSE;
1752 *arg += 7;
1753 }
1754 else if (STRNCMP(*arg, "v:null", 6) == 0)
1755 {
1756 rettv->v_type = VAR_SPECIAL;
1757 rettv->vval.v_number = VVAL_NULL;
1758 *arg += 6;
1759 }
1760 else if (STRNCMP(*arg, "v:none", 6) == 0)
1761 {
1762 rettv->v_type = VAR_SPECIAL;
1763 rettv->vval.v_number = VVAL_NONE;
1764 *arg += 6;
1765 }
1766}
1767
1768 exprtype_T
1769get_compare_type(char_u *p, int *len, int *type_is)
1770{
1771 exprtype_T type = EXPR_UNKNOWN;
1772 int i;
1773
1774 switch (p[0])
1775 {
1776 case '=': if (p[1] == '=')
1777 type = EXPR_EQUAL;
1778 else if (p[1] == '~')
1779 type = EXPR_MATCH;
1780 break;
1781 case '!': if (p[1] == '=')
1782 type = EXPR_NEQUAL;
1783 else if (p[1] == '~')
1784 type = EXPR_NOMATCH;
1785 break;
1786 case '>': if (p[1] != '=')
1787 {
1788 type = EXPR_GREATER;
1789 *len = 1;
1790 }
1791 else
1792 type = EXPR_GEQUAL;
1793 break;
1794 case '<': if (p[1] != '=')
1795 {
1796 type = EXPR_SMALLER;
1797 *len = 1;
1798 }
1799 else
1800 type = EXPR_SEQUAL;
1801 break;
1802 case 'i': if (p[1] == 's')
1803 {
1804 // "is" and "isnot"; but not a prefix of a name
1805 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1806 *len = 5;
1807 i = p[*len];
1808 if (!isalnum(i) && i != '_')
1809 {
1810 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1811 *type_is = TRUE;
1812 }
1813 }
1814 break;
1815 }
1816 return type;
1817}
1818
1819/*
1820 * Skip over an expression, ignoring most errors.
1821 */
1822 void
1823skip_expr_cctx(char_u **arg, cctx_T *cctx)
1824{
1825 evalarg_T evalarg;
1826
1827 init_evalarg(&evalarg);
1828 evalarg.eval_cctx = cctx;
1829 skip_expr(arg, &evalarg);
1830 clear_evalarg(&evalarg, NULL);
1831}
1832
1833/*
1834 * Check that the top of the type stack has a type that can be used as a
1835 * condition. Give an error and return FAIL if not.
1836 */
1837 int
1838bool_on_stack(cctx_T *cctx)
1839{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001840 type_T *type;
1841
Bram Moolenaar078a4612022-01-04 15:17:03 +00001842 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 if (type == &t_bool)
1844 return OK;
1845
Bram Moolenaar4913d422022-10-17 13:13:32 +01001846 if (type->tt_type == VAR_ANY
1847 || type->tt_type == VAR_UNKNOWN
1848 || type->tt_type == VAR_NUMBER
1849 || type == &t_number_bool
1850 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1852 // This requires a runtime type check.
1853 return generate_COND2BOOL(cctx);
1854
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001855 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856}
1857
1858/*
1859 * Give the "white on both sides" error, taking the operator from "p[len]".
1860 */
1861 void
1862error_white_both(char_u *op, int len)
1863{
1864 char_u buf[10];
1865
1866 vim_strncpy(buf, op, len);
1867 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1868}
1869
1870/*
1871 * Compile code to apply '-', '+' and '!'.
1872 * When "numeric_only" is TRUE do not apply '!'.
1873 */
1874 static int
1875compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1876{
1877 char_u *p = *end;
1878
1879 // this works from end to start
1880 while (p > start)
1881 {
1882 --p;
1883 while (VIM_ISWHITE(*p))
1884 --p;
1885 if (*p == '-' || *p == '+')
1886 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001887 type_T *type = get_type_on_stack(cctx, 0);
1888 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001889 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890 return FAIL;
1891
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001892 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001893 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1894 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895 }
1896 else if (numeric_only)
1897 {
1898 ++p;
1899 break;
1900 }
1901 else
1902 {
1903 int invert = *p == '!';
1904
1905 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1906 {
1907 if (p[-1] == '!')
1908 invert = !invert;
1909 --p;
1910 }
1911 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1912 return FAIL;
1913 }
1914 }
1915 *end = p;
1916 return OK;
1917}
1918
1919/*
1920 * Compile "(expression)": recursive!
1921 * Return FAIL/OK.
1922 */
1923 static int
1924compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1925{
1926 int ret;
1927 char_u *p = *arg + 1;
1928
1929 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1930 return FAIL;
1931 if (ppconst->pp_used <= PPSIZE - 10)
1932 {
1933 ret = compile_expr1(arg, cctx, ppconst);
1934 }
1935 else
1936 {
1937 // Not enough space in ppconst, flush constants.
1938 if (generate_ppconst(cctx, ppconst) == FAIL)
1939 return FAIL;
1940 ret = compile_expr0(arg, cctx);
1941 }
1942 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1943 return FAIL;
1944 if (**arg == ')')
1945 ++*arg;
1946 else if (ret == OK)
1947 {
1948 emsg(_(e_missing_closing_paren));
1949 ret = FAIL;
1950 }
1951 return ret;
1952}
1953
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001954static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001955
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956/*
1957 * Compile whatever comes after "name" or "name()".
1958 * Advances "*arg" only when something was recognized.
1959 */
1960 static int
1961compile_subscript(
1962 char_u **arg,
1963 cctx_T *cctx,
1964 char_u *start_leader,
1965 char_u **end_leader,
1966 ppconst_T *ppconst)
1967{
1968 char_u *name_start = *end_leader;
1969 int keeping_dict = FALSE;
1970
1971 for (;;)
1972 {
1973 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001974 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001975
1976 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1977 {
1978 char_u *next = peek_next_line_from_context(cctx);
1979
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001980 // If a following line starts with "->{", "->(" or "->X" advance to
1981 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982 // Also if a following line starts with ".x".
1983 if (next != NULL &&
1984 ((next[0] == '-' && next[1] == '>'
1985 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001986 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 || ASCII_ISALPHA(*skipwhite(next + 2))))
1988 || (next[0] == '.' && eval_isdictc(next[1]))))
1989 {
1990 next = next_line_from_context(cctx, TRUE);
1991 if (next == NULL)
1992 return FAIL;
1993 *arg = next;
1994 p = skipwhite(*arg);
1995 }
1996 }
1997
1998 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1999 // is not a function call.
2000 if (**arg == '(')
2001 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002 int argcount = 0;
2003
2004 if (generate_ppconst(cctx, ppconst) == FAIL)
2005 return FAIL;
2006 ppconst->pp_is_const = FALSE;
2007
2008 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002009 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010
2011 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002012 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013 return FAIL;
2014 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2015 return FAIL;
2016 if (keeping_dict)
2017 {
2018 keeping_dict = FALSE;
2019 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2020 return FAIL;
2021 }
2022 }
2023 else if (*p == '-' && p[1] == '>')
2024 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002025 char_u *pstart = p;
2026 int alt;
2027 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002028
Bram Moolenaarc7349932022-01-16 20:59:39 +00002029 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002030 if (generate_ppconst(cctx, ppconst) == FAIL)
2031 return FAIL;
2032 ppconst->pp_is_const = FALSE;
2033
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034 // Apply the '!', '-' and '+' first:
2035 // -1.0->func() works like (-1.0)->func()
2036 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2037 return FAIL;
2038
2039 p += 2;
2040 *arg = skipwhite(p);
2041 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002042
2043 // Three alternatives handled here:
2044 // 1. "base->name(" only a name, use compile_call()
2045 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2046 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002048 alt = 2;
2049 else
2050 {
2051 // alternative 1 or 3
2052 p = *arg;
2053 if (!eval_isnamec1(*p))
2054 {
2055 semsg(_(e_trailing_characters_str), pstart);
2056 return FAIL;
2057 }
2058 if (ASCII_ISALPHA(*p) && p[1] == ':')
2059 p += 2;
2060 for ( ; eval_isnamec(*p); ++p)
2061 ;
2062 if (*p == '(')
2063 {
2064 // alternative 1
2065 alt = 1;
2066 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2067 return FAIL;
2068 }
2069 else
2070 {
2071 // Must be alternative 3, find the "(". Only works within
2072 // one line.
2073 alt = 3;
2074 paren = vim_strchr(p, '(');
2075 if (paren == NULL)
2076 {
2077 semsg(_(e_missing_parenthesis_str), *arg);
2078 return FAIL;
2079 }
2080 }
2081 }
2082
2083 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002084 {
2085 int argcount = 1;
2086 garray_T *stack = &cctx->ctx_type_stack;
2087 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002088 int expr_isn_start = cctx->ctx_instr.ga_len;
2089 int expr_isn_end;
2090 int arg_isn_count;
2091
Bram Moolenaarc7349932022-01-16 20:59:39 +00002092 if (alt == 2)
2093 {
2094 // Funcref call: list->(Refs[2])(arg)
2095 // or lambda: list->((arg) => expr)(arg)
2096 //
2097 // Fist compile the function expression.
2098 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2099 return FAIL;
2100 }
2101 else
2102 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002103 int fail;
2104 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002105 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002106
Bram Moolenaarc7349932022-01-16 20:59:39 +00002107 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002108
2109 // instead of using LOADG for "import.Func" use PUSHFUNC
2110 ++paren_follows_after_expr;
2111
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002112 // do not look in the next line
2113 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002114
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002115 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002116 || *skipwhite(*arg) != NUL;
2117 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002118 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002119 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002120
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002121 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002122 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002123 if (did_emsg == prev_did_emsg)
2124 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002125 return FAIL;
2126 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002127 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002128
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129 // Compile the arguments.
2130 if (**arg != '(')
2131 {
2132 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002133 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002134 else
2135 semsg(_(e_missing_parenthesis_str), *arg);
2136 return FAIL;
2137 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002138
2139 // Remember the next instruction index, where the instructions
2140 // for arguments are being written.
2141 expr_isn_end = cctx->ctx_instr.ga_len;
2142
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002143 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002144 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2145 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002146 return FAIL;
2147
2148 // Move the instructions for the arguments to before the
2149 // instructions of the expression and move the type of the
2150 // expression after the argument types. This is what ISN_PCALL
2151 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2153 if (arg_isn_count > 0)
2154 {
2155 int expr_isn_count = expr_isn_end - expr_isn_start;
2156 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002157 type_T *decl_type;
2158 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159
2160 if (isn == NULL)
2161 return FAIL;
2162 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2163 + expr_isn_start,
2164 sizeof(isn_T) * expr_isn_count);
2165 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2166 + expr_isn_start,
2167 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2168 sizeof(isn_T) * arg_isn_count);
2169 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2170 + expr_isn_start + arg_isn_count,
2171 isn, sizeof(isn_T) * expr_isn_count);
2172 vim_free(isn);
2173
Bram Moolenaar078a4612022-01-04 15:17:03 +00002174 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2175 type = typep->type_curr;
2176 decl_type = typep->type_decl;
2177 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2178 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2179 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002180 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002181 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2182 typep->type_curr = type;
2183 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002184 }
2185
Bram Moolenaar078a4612022-01-04 15:17:03 +00002186 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2188 return FAIL;
2189 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002190
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002191 if (keeping_dict)
2192 {
2193 keeping_dict = FALSE;
2194 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2195 return FAIL;
2196 }
2197 }
2198 else if (**arg == '[')
2199 {
2200 int is_slice = FALSE;
2201
2202 // list index: list[123]
2203 // dict member: dict[key]
2204 // string index: text[123]
2205 // blob index: blob[123]
2206 if (generate_ppconst(cctx, ppconst) == FAIL)
2207 return FAIL;
2208 ppconst->pp_is_const = FALSE;
2209
2210 ++p;
2211 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2212 return FAIL;
2213 if (**arg == ':')
2214 {
2215 // missing first index is equal to zero
2216 generate_PUSHNR(cctx, 0);
2217 }
2218 else
2219 {
2220 if (compile_expr0(arg, cctx) == FAIL)
2221 return FAIL;
2222 if (**arg == ':')
2223 {
2224 semsg(_(e_white_space_required_before_and_after_str_at_str),
2225 ":", *arg);
2226 return FAIL;
2227 }
2228 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2229 return FAIL;
2230 *arg = skipwhite(*arg);
2231 }
2232 if (**arg == ':')
2233 {
2234 is_slice = TRUE;
2235 ++*arg;
2236 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2237 {
2238 semsg(_(e_white_space_required_before_and_after_str_at_str),
2239 ":", *arg);
2240 return FAIL;
2241 }
2242 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2243 return FAIL;
2244 if (**arg == ']')
2245 // missing second index is equal to end of string
2246 generate_PUSHNR(cctx, -1);
2247 else
2248 {
2249 if (compile_expr0(arg, cctx) == FAIL)
2250 return FAIL;
2251 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2252 return FAIL;
2253 *arg = skipwhite(*arg);
2254 }
2255 }
2256
2257 if (**arg != ']')
2258 {
2259 emsg(_(e_missing_closing_square_brace));
2260 return FAIL;
2261 }
2262 *arg = *arg + 1;
2263
2264 if (keeping_dict)
2265 {
2266 keeping_dict = FALSE;
2267 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2268 return FAIL;
2269 }
2270 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2271 return FAIL;
2272 }
2273 else if (*p == '.' && p[1] != '.')
2274 {
2275 // dictionary member: dict.name
2276 if (generate_ppconst(cctx, ppconst) == FAIL)
2277 return FAIL;
2278 ppconst->pp_is_const = FALSE;
2279
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002280 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2281 && (type->tt_type == VAR_CLASS
2282 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002283 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002284 // class member: SomeClass.varname
2285 // class method: SomeClass.SomeMethod()
2286 // class constructor: SomeClass.new()
2287 // object member: someObject.varname, this.varname
2288 // object method: someObject.SomeMethod(), this.SomeMethod()
2289 *arg = p;
2290 if (compile_class_object_index(cctx, arg, type) == FAIL)
2291 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002292 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002293 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002295 *arg = p + 1;
2296 if (IS_WHITE_OR_NUL(**arg))
2297 {
2298 emsg(_(e_missing_name_after_dot));
2299 return FAIL;
2300 }
2301 p = *arg;
2302 if (eval_isdictc(*p))
2303 while (eval_isnamec(*p))
2304 MB_PTR_ADV(p);
2305 if (p == *arg)
2306 {
2307 semsg(_(e_syntax_error_at_str), *arg);
2308 return FAIL;
2309 }
2310 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2311 return FAIL;
2312 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2313 return FAIL;
2314 keeping_dict = TRUE;
2315 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002316 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002317 }
2318 else
2319 break;
2320 }
2321
2322 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2323 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002324 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2325 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 return FAIL;
2327
2328 return OK;
2329}
2330
2331/*
2332 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2333 * "arg" is advanced until after the expression, skipping white space.
2334 *
2335 * If the value is a constant "ppconst->pp_used" will be non-zero.
2336 * Before instructions are generated, any values in "ppconst" will generated.
2337 *
2338 * This is the compiling equivalent of eval1(), eval2(), etc.
2339 */
2340
2341/*
2342 * number number constant
2343 * 0zFFFFFFFF Blob constant
2344 * "string" string constant
2345 * 'string' literal string constant
2346 * &option-name option value
2347 * @r register contents
2348 * identifier variable value
2349 * function() function call
2350 * $VAR environment variable
2351 * (expression) nested expression
2352 * [expr, expr] List
2353 * {key: val, [key]: val} Dictionary
2354 *
2355 * Also handle:
2356 * ! in front logical NOT
2357 * - in front unary minus
2358 * + in front unary plus (ignored)
2359 * trailing (arg) funcref/partial call
2360 * trailing [] subscript in String or List
2361 * trailing .name entry in Dictionary
2362 * trailing ->name() method call
2363 */
2364 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002365compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002366 char_u **arg,
2367 cctx_T *cctx,
2368 ppconst_T *ppconst)
2369{
2370 char_u *start_leader, *end_leader;
2371 int ret = OK;
2372 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2373 int used_before = ppconst->pp_used;
2374
2375 ppconst->pp_is_const = FALSE;
2376
2377 /*
2378 * Skip '!', '-' and '+' characters. They are handled later.
2379 */
2380 start_leader = *arg;
2381 if (eval_leader(arg, TRUE) == FAIL)
2382 return FAIL;
2383 end_leader = *arg;
2384
2385 rettv->v_type = VAR_UNKNOWN;
2386 switch (**arg)
2387 {
2388 /*
2389 * Number constant.
2390 */
2391 case '0': // also for blob starting with 0z
2392 case '1':
2393 case '2':
2394 case '3':
2395 case '4':
2396 case '5':
2397 case '6':
2398 case '7':
2399 case '8':
2400 case '9':
2401 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2402 return FAIL;
2403 // Apply "-" and "+" just before the number now, right to
2404 // left. Matters especially when "->" follows. Stops at
2405 // '!'.
2406 if (apply_leader(rettv, TRUE,
2407 start_leader, &end_leader) == FAIL)
2408 {
2409 clear_tv(rettv);
2410 return FAIL;
2411 }
2412 break;
2413
2414 /*
2415 * String constant: "string".
2416 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002417 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002418 return FAIL;
2419 break;
2420
2421 /*
2422 * Literal string constant: 'str''ing'.
2423 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002424 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002425 return FAIL;
2426 break;
2427
2428 /*
2429 * Constant Vim variable.
2430 */
2431 case 'v': get_vim_constant(arg, rettv);
2432 ret = NOTDONE;
2433 break;
2434
2435 /*
2436 * "true" constant
2437 */
2438 case 't': if (STRNCMP(*arg, "true", 4) == 0
2439 && !eval_isnamec((*arg)[4]))
2440 {
2441 *arg += 4;
2442 rettv->v_type = VAR_BOOL;
2443 rettv->vval.v_number = VVAL_TRUE;
2444 }
2445 else
2446 ret = NOTDONE;
2447 break;
2448
2449 /*
2450 * "false" constant
2451 */
2452 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2453 && !eval_isnamec((*arg)[5]))
2454 {
2455 *arg += 5;
2456 rettv->v_type = VAR_BOOL;
2457 rettv->vval.v_number = VVAL_FALSE;
2458 }
2459 else
2460 ret = NOTDONE;
2461 break;
2462
2463 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002464 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002465 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002466 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002468 char_u *p = *arg + 4;
2469 int len;
2470
2471 for (len = 0; eval_isnamec(p[len]); ++len)
2472 ;
2473 ret = handle_predefined(*arg, len + 4, rettv);
2474 if (ret == FAIL)
2475 ret = NOTDONE;
2476 else
2477 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002478 }
2479 else
2480 ret = NOTDONE;
2481 break;
2482
2483 /*
2484 * List: [expr, expr]
2485 */
2486 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2487 return FAIL;
2488 ret = compile_list(arg, cctx, ppconst);
2489 break;
2490
2491 /*
2492 * Dictionary: {'key': val, 'key': val}
2493 */
2494 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2495 return FAIL;
2496 ret = compile_dict(arg, cctx, ppconst);
2497 break;
2498
2499 /*
2500 * Option value: &name
2501 */
2502 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2503 return FAIL;
2504 ret = compile_get_option(arg, cctx);
2505 break;
2506
2507 /*
2508 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002509 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002510 */
2511 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2512 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002513 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2514 ret = compile_interp_string(arg, cctx);
2515 else
2516 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002517 break;
2518
2519 /*
2520 * Register contents: @r.
2521 */
2522 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2523 return FAIL;
2524 ret = compile_get_register(arg, cctx);
2525 break;
2526 /*
2527 * nested expression: (expression).
2528 * lambda: (arg, arg) => expr
2529 * funcref: (arg, arg) => { statement }
2530 */
2531 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2532 ret = compile_lambda(arg, cctx);
2533 if (ret == NOTDONE)
2534 ret = compile_parenthesis(arg, cctx, ppconst);
2535 break;
2536
2537 default: ret = NOTDONE;
2538 break;
2539 }
2540 if (ret == FAIL)
2541 return FAIL;
2542
2543 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2544 {
2545 if (cctx->ctx_skip == SKIP_YES)
2546 clear_tv(rettv);
2547 else
2548 // A constant expression can possibly be handled compile time,
2549 // return the value instead of generating code.
2550 ++ppconst->pp_used;
2551 }
2552 else if (ret == NOTDONE)
2553 {
2554 char_u *p;
2555 int r;
2556
2557 if (!eval_isnamec1(**arg))
2558 {
2559 if (!vim9_bad_comment(*arg))
2560 {
2561 if (ends_excmd(*skipwhite(*arg)))
2562 semsg(_(e_empty_expression_str), *arg);
2563 else
2564 semsg(_(e_name_expected_str), *arg);
2565 }
2566 return FAIL;
2567 }
2568
2569 // "name" or "name()"
2570 p = to_name_end(*arg, TRUE);
2571 if (p - *arg == (size_t)1 && **arg == '_')
2572 {
2573 emsg(_(e_cannot_use_underscore_here));
2574 return FAIL;
2575 }
2576
2577 if (*p == '(')
2578 {
2579 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2580 }
2581 else
2582 {
2583 if (cctx->ctx_skip != SKIP_YES
2584 && generate_ppconst(cctx, ppconst) == FAIL)
2585 return FAIL;
2586 r = compile_load(arg, p, cctx, TRUE, TRUE);
2587 }
2588 if (r == FAIL)
2589 return FAIL;
2590 }
2591
2592 // Handle following "[]", ".member", etc.
2593 // Then deal with prefixed '-', '+' and '!', if not done already.
2594 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2595 ppconst) == FAIL)
2596 return FAIL;
2597 if (ppconst->pp_used > 0)
2598 {
2599 // apply the '!', '-' and '+' before the constant
2600 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2601 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2602 return FAIL;
2603 return OK;
2604 }
2605 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2606 return FAIL;
2607 return OK;
2608}
2609
2610/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002611 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002612 */
2613 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002614compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615{
2616 type_T *want_type = NULL;
2617
2618 // Recognize <type>
2619 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2620 {
2621 ++*arg;
2622 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2623 if (want_type == NULL)
2624 return FAIL;
2625
2626 if (**arg != '>')
2627 {
2628 if (*skipwhite(*arg) == '>')
2629 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2630 else
2631 emsg(_(e_missing_gt));
2632 return FAIL;
2633 }
2634 ++*arg;
2635 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2636 return FAIL;
2637 }
2638
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002639 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002640 return FAIL;
2641
2642 if (want_type != NULL)
2643 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002644 type_T *actual;
2645 where_T where = WHERE_INIT;
2646
2647 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002648 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002649 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002651 if (need_type(actual, want_type, FALSE,
2652 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002653 return FAIL;
2654 }
2655 }
2656
2657 return OK;
2658}
2659
2660/*
2661 * * number multiplication
2662 * / number division
2663 * % number modulo
2664 */
2665 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002666compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002667{
2668 char_u *op;
2669 char_u *next;
2670 int ppconst_used = ppconst->pp_used;
2671
2672 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002673 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002674 return FAIL;
2675
2676 /*
2677 * Repeat computing, until no "*", "/" or "%" is following.
2678 */
2679 for (;;)
2680 {
2681 op = may_peek_next_line(cctx, *arg, &next);
2682 if (*op != '*' && *op != '/' && *op != '%')
2683 break;
2684 if (next != NULL)
2685 {
2686 *arg = next_line_from_context(cctx, TRUE);
2687 op = skipwhite(*arg);
2688 }
2689
2690 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2691 {
2692 error_white_both(op, 1);
2693 return FAIL;
2694 }
2695 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2696 return FAIL;
2697
2698 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002699 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002700 return FAIL;
2701
2702 if (ppconst->pp_used == ppconst_used + 2
2703 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2704 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2705 {
2706 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2707 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2708 varnumber_T res = 0;
2709 int failed = FALSE;
2710
2711 // both are numbers: compute the result
2712 switch (*op)
2713 {
2714 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2715 break;
2716 case '/': res = num_divide(tv1->vval.v_number,
2717 tv2->vval.v_number, &failed);
2718 break;
2719 case '%': res = num_modulus(tv1->vval.v_number,
2720 tv2->vval.v_number, &failed);
2721 break;
2722 }
2723 if (failed)
2724 return FAIL;
2725 tv1->vval.v_number = res;
2726 --ppconst->pp_used;
2727 }
2728 else
2729 {
2730 generate_ppconst(cctx, ppconst);
2731 generate_two_op(cctx, op);
2732 }
2733 }
2734
2735 return OK;
2736}
2737
2738/*
2739 * + number addition or list/blobl concatenation
2740 * - number subtraction
2741 * .. string concatenation
2742 */
2743 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002744compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002745{
2746 char_u *op;
2747 char_u *next;
2748 int oplen;
2749 int ppconst_used = ppconst->pp_used;
2750
2751 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002752 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002753 return FAIL;
2754
2755 /*
2756 * Repeat computing, until no "+", "-" or ".." is following.
2757 */
2758 for (;;)
2759 {
2760 op = may_peek_next_line(cctx, *arg, &next);
2761 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2762 break;
2763 if (op[0] == op[1] && *op != '.' && next)
2764 // Finding "++" or "--" on the next line is a separate command.
2765 // But ".." is concatenation.
2766 break;
2767 oplen = (*op == '.' ? 2 : 1);
2768 if (next != NULL)
2769 {
2770 *arg = next_line_from_context(cctx, TRUE);
2771 op = skipwhite(*arg);
2772 }
2773
2774 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2775 {
2776 error_white_both(op, oplen);
2777 return FAIL;
2778 }
2779
2780 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2781 return FAIL;
2782
2783 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002784 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002785 return FAIL;
2786
2787 if (ppconst->pp_used == ppconst_used + 2
2788 && (*op == '.'
2789 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2790 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2791 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2792 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2793 {
2794 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2795 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2796
2797 // concat/subtract/add constant numbers
2798 if (*op == '+')
2799 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2800 else if (*op == '-')
2801 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2802 else
2803 {
2804 // concatenate constant strings
2805 char_u *s1 = tv1->vval.v_string;
2806 char_u *s2 = tv2->vval.v_string;
2807 size_t len1 = STRLEN(s1);
2808
2809 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2810 if (tv1->vval.v_string == NULL)
2811 {
2812 clear_ppconst(ppconst);
2813 return FAIL;
2814 }
2815 mch_memmove(tv1->vval.v_string, s1, len1);
2816 STRCPY(tv1->vval.v_string + len1, s2);
2817 vim_free(s1);
2818 vim_free(s2);
2819 }
2820 --ppconst->pp_used;
2821 }
2822 else
2823 {
2824 generate_ppconst(cctx, ppconst);
2825 ppconst->pp_is_const = FALSE;
2826 if (*op == '.')
2827 {
2828 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2829 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2830 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002831 if (generate_CONCAT(cctx, 2) == FAIL)
2832 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002833 }
2834 else
2835 generate_two_op(cctx, op);
2836 }
2837 }
2838
2839 return OK;
2840}
2841
2842/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002843 * expr6a >> expr6b
2844 * expr6a << expr6b
2845 *
2846 * Produces instructions:
2847 * OPNR bitwise left or right shift
2848 */
2849 static int
2850compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2851{
2852 exprtype_T type = EXPR_UNKNOWN;
2853 char_u *p;
2854 char_u *next;
2855 int len = 2;
2856 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002857 isn_T *isn;
2858
2859 // get the first variable
2860 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2861 return FAIL;
2862
2863 /*
2864 * Repeat computing, until no "+", "-" or ".." is following.
2865 */
2866 for (;;)
2867 {
2868 type = EXPR_UNKNOWN;
2869
2870 p = may_peek_next_line(cctx, *arg, &next);
2871 if (p[0] == '<' && p[1] == '<')
2872 type = EXPR_LSHIFT;
2873 else if (p[0] == '>' && p[1] == '>')
2874 type = EXPR_RSHIFT;
2875
2876 if (type == EXPR_UNKNOWN)
2877 return OK;
2878
2879 // Handle a bitwise left or right shift operator
2880 if (ppconst->pp_used == ppconst_used + 1)
2881 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002882 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002883 {
2884 // left operand should be a number
2885 emsg(_(e_bitshift_ops_must_be_number));
2886 return FAIL;
2887 }
2888 }
2889 else
2890 {
2891 type_T *t = get_type_on_stack(cctx, 0);
2892
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002893 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002894 {
2895 emsg(_(e_bitshift_ops_must_be_number));
2896 return FAIL;
2897 }
2898 }
2899
2900 if (next != NULL)
2901 {
2902 *arg = next_line_from_context(cctx, TRUE);
2903 p = skipwhite(*arg);
2904 }
2905
2906 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2907 {
2908 error_white_both(p, len);
2909 return FAIL;
2910 }
2911
2912 // get the second variable
2913 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2914 return FAIL;
2915
2916 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2917 return FAIL;
2918
2919 if (ppconst->pp_used == ppconst_used + 2)
2920 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002921 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2922 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2923
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002924 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002925 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2926 {
2927 // right operand should be a positive number
2928 if (tv2->v_type != VAR_NUMBER)
2929 emsg(_(e_bitshift_ops_must_be_number));
2930 else
dundargocc57b5bc2022-11-02 13:30:51 +00002931 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002932 return FAIL;
2933 }
2934
2935 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2936 tv1->vval.v_number = 0;
2937 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002938 tv1->vval.v_number =
2939 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002940 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002941 tv1->vval.v_number =
2942 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002943 clear_tv(tv2);
2944 --ppconst->pp_used;
2945 }
2946 else
2947 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002948 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2949 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002950 {
2951 emsg(_(e_bitshift_ops_must_be_number));
2952 return FAIL;
2953 }
2954
2955 generate_ppconst(cctx, ppconst);
2956
2957 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2958 if (isn == NULL)
2959 return FAIL;
2960
2961 if (isn != NULL)
2962 isn->isn_arg.op.op_type = type;
2963 }
2964 }
2965
2966 return OK;
2967}
2968
2969/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002970 * expr5a == expr5b
2971 * expr5a =~ expr5b
2972 * expr5a != expr5b
2973 * expr5a !~ expr5b
2974 * expr5a > expr5b
2975 * expr5a >= expr5b
2976 * expr5a < expr5b
2977 * expr5a <= expr5b
2978 * expr5a is expr5b
2979 * expr5a isnot expr5b
2980 *
2981 * Produces instructions:
2982 * EVAL expr5a Push result of "expr5a"
2983 * EVAL expr5b Push result of "expr5b"
2984 * COMPARE one of the compare instructions
2985 */
2986 static int
2987compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2988{
2989 exprtype_T type = EXPR_UNKNOWN;
2990 char_u *p;
2991 char_u *next;
2992 int len = 2;
2993 int type_is = FALSE;
2994 int ppconst_used = ppconst->pp_used;
2995
2996 // get the first variable
2997 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2998 return FAIL;
2999
3000 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003001
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003002 type = get_compare_type(p, &len, &type_is);
3003
3004 /*
3005 * If there is a comparative operator, use it.
3006 */
3007 if (type != EXPR_UNKNOWN)
3008 {
3009 int ic = FALSE; // Default: do not ignore case
3010
3011 if (next != NULL)
3012 {
3013 *arg = next_line_from_context(cctx, TRUE);
3014 p = skipwhite(*arg);
3015 }
3016 if (type_is && (p[len] == '?' || p[len] == '#'))
3017 {
3018 semsg(_(e_invalid_expression_str), *arg);
3019 return FAIL;
3020 }
3021 // extra question mark appended: ignore case
3022 if (p[len] == '?')
3023 {
3024 ic = TRUE;
3025 ++len;
3026 }
3027 // extra '#' appended: match case (ignored)
3028 else if (p[len] == '#')
3029 ++len;
3030 // nothing appended: match case
3031
3032 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3033 {
3034 error_white_both(p, len);
3035 return FAIL;
3036 }
3037
3038 // get the second variable
3039 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3040 return FAIL;
3041
3042 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3043 return FAIL;
3044
3045 if (ppconst->pp_used == ppconst_used + 2)
3046 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003047 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003048 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3049 int ret;
3050
3051 // Both sides are a constant, compute the result now.
3052 // First check for a valid combination of types, this is more
3053 // strict than typval_compare().
3054 if (check_compare_types(type, tv1, tv2) == FAIL)
3055 ret = FAIL;
3056 else
3057 {
3058 ret = typval_compare(tv1, tv2, type, ic);
3059 tv1->v_type = VAR_BOOL;
3060 tv1->vval.v_number = tv1->vval.v_number
3061 ? VVAL_TRUE : VVAL_FALSE;
3062 clear_tv(tv2);
3063 --ppconst->pp_used;
3064 }
3065 return ret;
3066 }
3067
3068 generate_ppconst(cctx, ppconst);
3069 return generate_COMPARE(cctx, type, ic);
3070 }
3071
3072 return OK;
3073}
3074
3075static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3076
3077/*
3078 * Compile || or &&.
3079 */
3080 static int
3081compile_and_or(
3082 char_u **arg,
3083 cctx_T *cctx,
3084 char *op,
3085 ppconst_T *ppconst,
3086 int ppconst_used UNUSED)
3087{
3088 char_u *next;
3089 char_u *p = may_peek_next_line(cctx, *arg, &next);
3090 int opchar = *op;
3091
3092 if (p[0] == opchar && p[1] == opchar)
3093 {
3094 garray_T *instr = &cctx->ctx_instr;
3095 garray_T end_ga;
3096 int save_skip = cctx->ctx_skip;
3097
3098 /*
3099 * Repeat until there is no following "||" or "&&"
3100 */
3101 ga_init2(&end_ga, sizeof(int), 10);
3102 while (p[0] == opchar && p[1] == opchar)
3103 {
3104 long start_lnum = SOURCING_LNUM;
3105 long save_sourcing_lnum;
3106 int start_ctx_lnum = cctx->ctx_lnum;
3107 int save_lnum;
3108 int const_used;
3109 int status;
3110 jumpwhen_T jump_when = opchar == '|'
3111 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3112
3113 if (next != NULL)
3114 {
3115 *arg = next_line_from_context(cctx, TRUE);
3116 p = skipwhite(*arg);
3117 }
3118
3119 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3120 {
3121 semsg(_(e_white_space_required_before_and_after_str_at_str),
3122 op, p);
3123 ga_clear(&end_ga);
3124 return FAIL;
3125 }
3126
3127 save_sourcing_lnum = SOURCING_LNUM;
3128 SOURCING_LNUM = start_lnum;
3129 save_lnum = cctx->ctx_lnum;
3130 cctx->ctx_lnum = start_ctx_lnum;
3131
3132 status = check_ppconst_bool(ppconst);
3133 if (status != FAIL)
3134 {
3135 // Use the last ppconst if possible.
3136 if (ppconst->pp_used > 0)
3137 {
3138 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3139 int is_true = tv2bool(tv);
3140
3141 if ((is_true && opchar == '|')
3142 || (!is_true && opchar == '&'))
3143 {
3144 // For "false && expr" and "true || expr" the "expr"
3145 // does not need to be evaluated.
3146 cctx->ctx_skip = SKIP_YES;
3147 clear_tv(tv);
3148 tv->v_type = VAR_BOOL;
3149 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3150 }
3151 else
3152 {
3153 // For "true && expr" and "false || expr" only "expr"
3154 // needs to be evaluated.
3155 --ppconst->pp_used;
3156 jump_when = JUMP_NEVER;
3157 }
3158 }
3159 else
3160 {
3161 // Every part must evaluate to a bool.
3162 status = bool_on_stack(cctx);
3163 }
3164 }
3165 if (status != FAIL)
3166 status = ga_grow(&end_ga, 1);
3167 cctx->ctx_lnum = save_lnum;
3168 if (status == FAIL)
3169 {
3170 ga_clear(&end_ga);
3171 return FAIL;
3172 }
3173
3174 if (jump_when != JUMP_NEVER)
3175 {
3176 if (cctx->ctx_skip != SKIP_YES)
3177 {
3178 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3179 ++end_ga.ga_len;
3180 }
3181 generate_JUMP(cctx, jump_when, 0);
3182 }
3183
3184 // eval the next expression
3185 SOURCING_LNUM = save_sourcing_lnum;
3186 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3187 {
3188 ga_clear(&end_ga);
3189 return FAIL;
3190 }
3191
3192 const_used = ppconst->pp_used;
3193 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3194 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3195 {
3196 ga_clear(&end_ga);
3197 return FAIL;
3198 }
3199
3200 // "0 || 1" results in true, "1 && 0" results in false.
3201 if (ppconst->pp_used == const_used + 1)
3202 {
3203 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3204
3205 if (tv->v_type == VAR_NUMBER
3206 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3207 {
3208 tv->vval.v_number = tv->vval.v_number == 1
3209 ? VVAL_TRUE : VVAL_FALSE;
3210 tv->v_type = VAR_BOOL;
3211 }
3212 }
3213
3214 p = may_peek_next_line(cctx, *arg, &next);
3215 }
3216
3217 if (check_ppconst_bool(ppconst) == FAIL)
3218 {
3219 ga_clear(&end_ga);
3220 return FAIL;
3221 }
3222
3223 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3224 // Every part must evaluate to a bool.
3225 if (bool_on_stack(cctx) == FAIL)
3226 {
3227 ga_clear(&end_ga);
3228 return FAIL;
3229 }
3230
3231 if (end_ga.ga_len > 0)
3232 {
3233 // Fill in the end label in all jumps.
3234 generate_ppconst(cctx, ppconst);
3235 while (end_ga.ga_len > 0)
3236 {
3237 isn_T *isn;
3238
3239 --end_ga.ga_len;
3240 isn = ((isn_T *)instr->ga_data)
3241 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3242 isn->isn_arg.jump.jump_where = instr->ga_len;
3243 }
3244 }
3245 ga_clear(&end_ga);
3246
3247 cctx->ctx_skip = save_skip;
3248 }
3249
3250 return OK;
3251}
3252
3253/*
3254 * expr4a && expr4a && expr4a logical AND
3255 *
3256 * Produces instructions:
3257 * EVAL expr4a Push result of "expr4a"
3258 * COND2BOOL convert to bool if needed
3259 * JUMP_IF_COND_FALSE end
3260 * EVAL expr4b Push result of "expr4b"
3261 * JUMP_IF_COND_FALSE end
3262 * EVAL expr4c Push result of "expr4c"
3263 * end:
3264 */
3265 static int
3266compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3267{
3268 int ppconst_used = ppconst->pp_used;
3269
3270 // get the first variable
3271 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3272 return FAIL;
3273
3274 // || and && work almost the same
3275 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3276}
3277
3278/*
3279 * expr3a || expr3b || expr3c logical OR
3280 *
3281 * Produces instructions:
3282 * EVAL expr3a Push result of "expr3a"
3283 * COND2BOOL convert to bool if needed
3284 * JUMP_IF_COND_TRUE end
3285 * EVAL expr3b Push result of "expr3b"
3286 * JUMP_IF_COND_TRUE end
3287 * EVAL expr3c Push result of "expr3c"
3288 * end:
3289 */
3290 static int
3291compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3292{
3293 int ppconst_used = ppconst->pp_used;
3294
3295 // eval the first expression
3296 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3297 return FAIL;
3298
3299 // || and && work almost the same
3300 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3301}
3302
3303/*
3304 * Toplevel expression: expr2 ? expr1a : expr1b
3305 * Produces instructions:
3306 * EVAL expr2 Push result of "expr2"
3307 * JUMP_IF_FALSE alt jump if false
3308 * EVAL expr1a
3309 * JUMP_ALWAYS end
3310 * alt: EVAL expr1b
3311 * end:
3312 *
3313 * Toplevel expression: expr2 ?? expr1
3314 * Produces instructions:
3315 * EVAL expr2 Push result of "expr2"
3316 * JUMP_AND_KEEP_IF_TRUE end jump if true
3317 * EVAL expr1
3318 * end:
3319 */
3320 int
3321compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3322{
3323 char_u *p;
3324 int ppconst_used = ppconst->pp_used;
3325 char_u *next;
3326
3327 // Ignore all kinds of errors when not producing code.
3328 if (cctx->ctx_skip == SKIP_YES)
3329 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003330 int prev_did_emsg = did_emsg;
3331
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003332 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003333 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003334 }
3335
3336 // Evaluate the first expression.
3337 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3338 return FAIL;
3339
3340 p = may_peek_next_line(cctx, *arg, &next);
3341 if (*p == '?')
3342 {
3343 int op_falsy = p[1] == '?';
3344 garray_T *instr = &cctx->ctx_instr;
3345 garray_T *stack = &cctx->ctx_type_stack;
3346 int alt_idx = instr->ga_len;
3347 int end_idx = 0;
3348 isn_T *isn;
3349 type_T *type1 = NULL;
3350 int has_const_expr = FALSE;
3351 int const_value = FALSE;
3352 int save_skip = cctx->ctx_skip;
3353
3354 if (next != NULL)
3355 {
3356 *arg = next_line_from_context(cctx, TRUE);
3357 p = skipwhite(*arg);
3358 }
3359
3360 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3361 {
3362 semsg(_(e_white_space_required_before_and_after_str_at_str),
3363 op_falsy ? "??" : "?", p);
3364 return FAIL;
3365 }
3366
3367 if (ppconst->pp_used == ppconst_used + 1)
3368 {
3369 // the condition is a constant, we know whether the ? or the :
3370 // expression is to be evaluated.
3371 has_const_expr = TRUE;
3372 if (op_falsy)
3373 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3374 else
3375 {
3376 int error = FALSE;
3377
3378 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3379 &error);
3380 if (error)
3381 return FAIL;
3382 }
3383 cctx->ctx_skip = save_skip == SKIP_YES ||
3384 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3385
3386 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3387 // "left ?? right" and "left" is truthy: produce "left"
3388 generate_ppconst(cctx, ppconst);
3389 else
3390 {
3391 clear_tv(&ppconst->pp_tv[ppconst_used]);
3392 --ppconst->pp_used;
3393 }
3394 }
3395 else
3396 {
3397 generate_ppconst(cctx, ppconst);
3398 if (op_falsy)
3399 end_idx = instr->ga_len;
3400 generate_JUMP(cctx, op_falsy
3401 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3402 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003403 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003404 }
3405
3406 // evaluate the second expression; any type is accepted
3407 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3408 return FAIL;
3409 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3410 return FAIL;
3411
3412 if (!has_const_expr)
3413 {
3414 generate_ppconst(cctx, ppconst);
3415
3416 if (!op_falsy)
3417 {
3418 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003419 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003420 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003421
3422 end_idx = instr->ga_len;
3423 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3424
3425 // jump here from JUMP_IF_FALSE
3426 isn = ((isn_T *)instr->ga_data) + alt_idx;
3427 isn->isn_arg.jump.jump_where = instr->ga_len;
3428 }
3429 }
3430
3431 if (!op_falsy)
3432 {
3433 // Check for the ":".
3434 p = may_peek_next_line(cctx, *arg, &next);
3435 if (*p != ':')
3436 {
3437 emsg(_(e_missing_colon_after_questionmark));
3438 return FAIL;
3439 }
3440 if (next != NULL)
3441 {
3442 *arg = next_line_from_context(cctx, TRUE);
3443 p = skipwhite(*arg);
3444 }
3445
3446 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3447 {
3448 semsg(_(e_white_space_required_before_and_after_str_at_str),
3449 ":", p);
3450 return FAIL;
3451 }
3452
3453 // evaluate the third expression
3454 if (has_const_expr)
3455 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3456 ? SKIP_YES : SKIP_NOT;
3457 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3458 return FAIL;
3459 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3460 return FAIL;
3461 }
3462
3463 if (!has_const_expr)
3464 {
3465 type_T **typep;
3466
3467 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003468 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003469
3470 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003471 typep = &((((type2_T *)stack->ga_data)
3472 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003473 common_type(type1, *typep, typep, cctx->ctx_type_list);
3474
3475 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3476 isn = ((isn_T *)instr->ga_data) + end_idx;
3477 isn->isn_arg.jump.jump_where = instr->ga_len;
3478 }
3479
3480 cctx->ctx_skip = save_skip;
3481 }
3482 return OK;
3483}
3484
3485/*
3486 * Toplevel expression.
3487 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3488 * Returns OK or FAIL.
3489 */
3490 int
3491compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3492{
3493 ppconst_T ppconst;
3494
3495 CLEAR_FIELD(ppconst);
3496 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3497 {
3498 clear_ppconst(&ppconst);
3499 return FAIL;
3500 }
3501 if (is_const != NULL)
3502 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3503 if (generate_ppconst(cctx, &ppconst) == FAIL)
3504 return FAIL;
3505 return OK;
3506}
3507
3508/*
3509 * Toplevel expression.
3510 */
3511 int
3512compile_expr0(char_u **arg, cctx_T *cctx)
3513{
3514 return compile_expr0_ext(arg, cctx, NULL);
3515}
3516
3517
3518#endif // defined(FEAT_EVAL)