blob: 06d1657ec36462fc033444dd489abac67b0ae713 [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 Moolenaar58b40092023-01-11 15:59:05 +0000324 for (int i = is_super ? child_count : 0; i < function_count; ++i)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000325 {
326 ufunc_T *fp = functions[i];
327 // Use a separate pointer to avoid that ASAN complains about
328 // uf_name[] only being 4 characters.
329 char_u *ufname = (char_u *)fp->uf_name;
330 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
331 {
332 ufunc = fp;
333 break;
334 }
335 }
336 if (ufunc == NULL)
337 {
338 // TODO: different error for object method?
339 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
340 return FAIL;
341 }
342
343 // Compile the arguments and call the class function or object method.
344 // The object method will know that the object is on the stack, just
345 // before the arguments.
346 *arg = skipwhite(name_end + 1);
347 int argcount = 0;
348 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
349 return FAIL;
350 return generate_CALL(cctx, ufunc, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000351 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000352
353 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000354 {
355 for (int i = 0; i < cl->class_obj_member_count; ++i)
356 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000357 ocmember_T *m = &cl->class_obj_members[i];
358 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000359 {
Bram Moolenaar62a69232023-01-24 15:07:04 +0000360 if (*name == '_' && !inside_class(cctx, cl))
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000361 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000362 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000363 return FAIL;
364 }
365
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000366 *arg = name_end;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000367 if (cl->class_flags & CLASS_INTERFACE)
368 return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000369 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000370 }
371 }
372
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000373 // Could be a function reference: "obj.Func".
374 for (int i = 0; i < cl->class_obj_method_count; ++i)
375 {
376 ufunc_T *fp = cl->class_obj_methods[i];
377 // Use a separate pointer to avoid that ASAN complains about
378 // uf_name[] only being 4 characters.
379 char_u *ufname = (char_u *)fp->uf_name;
380 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
381 return generate_FUNCREF(cctx, fp, NULL);
382 }
383
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000384 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
385 }
386 else
387 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000388 // load class member
389 int idx;
390 for (idx = 0; idx < cl->class_class_member_count; ++idx)
391 {
392 ocmember_T *m = &cl->class_class_members[idx];
393 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
394 break;
395 }
396 if (idx < cl->class_class_member_count)
397 {
398 *arg = name_end;
399 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
400 }
401 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000402 }
403
404 return FAIL;
405}
406
407/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 * Generate an instruction to load script-local variable "name", without the
409 * leading "s:".
410 * Also finds imported variables.
411 */
412 int
413compile_load_scriptvar(
414 cctx_T *cctx,
415 char_u *name, // variable NUL terminated
416 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100417 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000418{
419 scriptitem_T *si;
420 int idx;
421 imported_T *import;
422
423 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
424 return FAIL;
425 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000426 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427 if (idx >= 0)
428 {
429 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
430
431 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
432 current_sctx.sc_sid, idx, sv->sv_type);
433 return OK;
434 }
435
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000436 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000437 if (import != NULL)
438 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000439 char_u *p = skipwhite(*end);
440 char_u *exp_name;
441 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100442 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000443 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000444 int done = FALSE;
445 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000446
447 // Need to lookup the member.
448 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000450 semsg(_(e_expected_dot_after_name_str), start);
451 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000452 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000453 ++p;
454 if (VIM_ISWHITE(*p))
455 {
456 emsg(_(e_no_white_space_allowed_after_dot));
457 return FAIL;
458 }
459
460 // isolate one name
461 exp_name = p;
462 while (eval_isnamec(*p))
463 ++p;
464 cc = *p;
465 *p = NUL;
466
Bram Moolenaard041f422022-01-12 19:54:00 +0000467 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100468 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
469 // "import autoload './dir/script.vim'" or
470 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100471 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100472
473 if (res == OK)
474 {
475 if (si->sn_autoload_prefix != NULL
476 && si->sn_state == SN_STATE_NOT_LOADED)
477 {
478 char_u *auto_name =
479 concat_str(si->sn_autoload_prefix, exp_name);
480
481 // autoload script must be loaded later, access by the autoload
482 // name. If a '(' follows it must be a function. Otherwise we
483 // don't know, it can be "script.Func".
484 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100485 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100486 else
487 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
488 vim_free(auto_name);
489 done = TRUE;
490 }
491 else if (si->sn_import_autoload
492 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100493 {
494 // If a '(' follows it must be a function. Otherwise we don't
495 // know, it can be "script.Func".
496 if (cc == '(' || paren_follows_after_expr)
497 {
498 char_u sid_name[MAX_FUNC_NAME_LEN];
499
500 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100501 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100502 }
503 else
504 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
505 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100506 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100507 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100508 else
509 {
510 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
511 cctx, NULL, TRUE);
512 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100513 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100514
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000515 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000516 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000517 if (done)
518 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000519
520 if (idx < 0)
521 {
522 if (ufunc != NULL)
523 {
524 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100525 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000526 return OK;
527 }
528 return FAIL;
529 }
530
531 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
532 import->imp_sid,
533 idx,
534 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535 return OK;
536 }
537
Bram Moolenaard0132f42022-05-12 11:05:40 +0100538 // Can only get here if we know "name" is a script variable and not in a
539 // Vim9 script (variable is not in sn_var_vals): old style script.
540 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000541 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000542}
543
544 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000545generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000547 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000548 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000549
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000550 // Reject a global non-autoload function found without the "g:" prefix.
551 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000552 return FAIL;
553
554 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000555 compile_type = get_compile_type(ufunc);
556 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000557 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000558 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100559 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000560}
561
562/*
563 * Compile a variable name into a load instruction.
564 * "end" points to just after the name.
565 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
566 * When "error" is FALSE do not give an error when not found.
567 */
568 int
569compile_load(
570 char_u **arg,
571 char_u *end_arg,
572 cctx_T *cctx,
573 int is_expr,
574 int error)
575{
576 type_T *type;
577 char_u *name = NULL;
578 char_u *end = end_arg;
579 int res = FAIL;
580 int prev_called_emsg = called_emsg;
581
582 if (*(*arg + 1) == ':')
583 {
584 if (end <= *arg + 2)
585 {
586 isntype_T isn_type;
587
588 // load dictionary of namespace
589 switch (**arg)
590 {
591 case 'g': isn_type = ISN_LOADGDICT; break;
592 case 'w': isn_type = ISN_LOADWDICT; break;
593 case 't': isn_type = ISN_LOADTDICT; break;
594 case 'b': isn_type = ISN_LOADBDICT; break;
595 default:
596 semsg(_(e_namespace_not_supported_str), *arg);
597 goto theend;
598 }
599 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
600 goto theend;
601 res = OK;
602 }
603 else
604 {
605 isntype_T isn_type = ISN_DROP;
606
607 // load namespaced variable
608 name = vim_strnsave(*arg + 2, end - (*arg + 2));
609 if (name == NULL)
610 return FAIL;
611
612 switch (**arg)
613 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100614 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000616 case 's': if (current_script_is_vim9())
617 {
618 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
619 *arg);
620 vim_free(name);
621 return FAIL;
622 }
Kota Kato948a3892022-08-16 16:09:59 +0100623 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000624 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000625 else
626 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100627 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000628 break;
629 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
630 {
631 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000632 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000633 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000634 else
635 isn_type = ISN_LOADG;
636 }
637 else
638 {
639 isn_type = ISN_LOADAUTO;
640 vim_free(name);
641 name = vim_strnsave(*arg, end - *arg);
642 if (name == NULL)
643 return FAIL;
644 }
645 break;
646 case 'w': isn_type = ISN_LOADW; break;
647 case 't': isn_type = ISN_LOADT; break;
648 case 'b': isn_type = ISN_LOADB; break;
649 default: // cannot happen, just in case
650 semsg(_(e_namespace_not_supported_str), *arg);
651 goto theend;
652 }
653 if (isn_type != ISN_DROP)
654 {
655 // Global, Buffer-local, Window-local and Tabpage-local
656 // variables can be defined later, thus we don't check if it
657 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000658 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000659 }
660 }
661 }
662 else
663 {
664 size_t len = end - *arg;
665 int idx;
666 int gen_load = FALSE;
667 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100668 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100669 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000670
671 name = vim_strnsave(*arg, end - *arg);
672 if (name == NULL)
673 return FAIL;
674
Bram Moolenaar58b40092023-01-11 15:59:05 +0000675 if (STRCMP(name, "super") == 0
676 && cctx->ctx_ufunc != NULL
677 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
678 {
679 // super.SomeFunc() in a class function: push &t_super type, this
680 // is recognized in compile_subscript().
681 res = push_type_stack(cctx, &t_super);
682 if (*end != '.')
683 emsg(_(e_super_must_be_followed_by_dot));
684 }
685 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686 {
687 script_autoload(name, FALSE);
688 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
689 }
690 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
691 == OK)
692 {
693 if (gen_load_outer == 0)
694 gen_load = TRUE;
695 }
696 else
697 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000698 lvar_T lvar;
699 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000700
701 if (lookup_local(*arg, len, &lvar, cctx) == OK)
702 {
703 type = lvar.lv_type;
704 idx = lvar.lv_idx;
705 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100706 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000707 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100708 outer_loop_depth = lvar.lv_loop_depth;
709 outer_loop_idx = lvar.lv_loop_idx;
710 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000711 else
712 gen_load = TRUE;
713 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000714 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000715 {
716 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
717 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718 else
719 {
720 // "var" can be script-local even without using "s:" if it
721 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000722 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000723 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100724 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000725
726 // When evaluating an expression and the name starts with an
727 // uppercase letter it can be a user defined function.
728 // generate_funcref() will fail if the function can't be found.
729 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000730 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000731 }
732 }
733 if (gen_load)
734 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
735 if (gen_load_outer > 0)
736 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100737 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
738 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000739 cctx->ctx_outer_used = TRUE;
740 }
741 }
742
743 *arg = end;
744
745theend:
746 if (res == FAIL && error && called_emsg == prev_called_emsg)
747 semsg(_(e_variable_not_found_str), name);
748 vim_free(name);
749 return res;
750}
751
752/*
753 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100754 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000755 * Returns FAIL if compilation fails.
756 */
757 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100758compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759{
LemonBoyf3b48952022-05-05 13:53:03 +0100760 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761 garray_T save_ga = cctx->ctx_instr;
762 int expr_res;
763 int trailing_error;
764 int instr_count;
765 isn_T *instr = NULL;
766
767 // Remove the string type from the stack.
768 --cctx->ctx_type_stack.ga_len;
769
770 // Temporarily reset the list of instructions so that the jump labels are
771 // correct.
772 cctx->ctx_instr.ga_len = 0;
773 cctx->ctx_instr.ga_maxlen = 0;
774 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000775
776 // avoid peeking a next line
777 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
778 cctx->ctx_ufunc->uf_lines.ga_len = 0;
779
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000781
782 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
783
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 s = skipwhite(s);
785 trailing_error = *s != NUL;
786
787 if (expr_res == FAIL || trailing_error
788 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
789 {
790 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000791 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 clear_instr_ga(&cctx->ctx_instr);
793 cctx->ctx_instr = save_ga;
794 ++cctx->ctx_type_stack.ga_len;
795 return FAIL;
796 }
797
798 // Move the generated instructions into the ISN_INSTR instruction, then
799 // restore the list of instructions.
800 instr_count = cctx->ctx_instr.ga_len;
801 instr = cctx->ctx_instr.ga_data;
802 instr[instr_count].isn_type = ISN_FINISH;
803
804 cctx->ctx_instr = save_ga;
805 vim_free(isn->isn_arg.string);
806 isn->isn_type = ISN_INSTR;
807 isn->isn_arg.instr = instr;
808 return OK;
809}
810
811/*
812 * Compile the argument expressions.
813 * "arg" points to just after the "(" and is advanced to after the ")"
814 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100815 int
LemonBoyf3b48952022-05-05 13:53:03 +0100816compile_arguments(
817 char_u **arg,
818 cctx_T *cctx,
819 int *argcount,
820 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821{
822 char_u *p = *arg;
823 char_u *whitep = *arg;
824 int must_end = FALSE;
825 int instr_count;
826
827 for (;;)
828 {
829 if (may_get_next_line(whitep, &p, cctx) == FAIL)
830 goto failret;
831 if (*p == ')')
832 {
833 *arg = p + 1;
834 return OK;
835 }
836 if (must_end)
837 {
838 semsg(_(e_missing_comma_before_argument_str), p);
839 return FAIL;
840 }
841
842 instr_count = cctx->ctx_instr.ga_len;
843 if (compile_expr0(&p, cctx) == FAIL)
844 return FAIL;
845 ++*argcount;
846
LemonBoyf3b48952022-05-05 13:53:03 +0100847 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 && cctx->ctx_instr.ga_len == instr_count + 1)
849 {
850 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
851
852 // {skip} argument of searchpair() can be compiled if not empty
853 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100854 compile_string(isn, cctx, 0);
855 }
856 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
857 && cctx->ctx_instr.ga_len == instr_count + 1)
858 {
859 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
860
861 // {sub} argument of substitute() can be compiled if it starts
862 // with \=
863 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100864 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100865 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 }
867
868 if (*p != ',' && *skipwhite(p) == ',')
869 {
870 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
871 p = skipwhite(p);
872 }
873 if (*p == ',')
874 {
875 ++p;
876 if (*p != NUL && !VIM_ISWHITE(*p))
877 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
878 }
879 else
880 must_end = TRUE;
881 whitep = p;
882 p = skipwhite(p);
883 }
884failret:
885 emsg(_(e_missing_closing_paren));
886 return FAIL;
887}
888
889/*
890 * Compile a function call: name(arg1, arg2)
891 * "arg" points to "name", "arg + varlen" to the "(".
892 * "argcount_init" is 1 for "value->method()"
893 * Instructions:
894 * EVAL arg1
895 * EVAL arg2
896 * BCALL / DCALL / UCALL
897 */
898 static int
899compile_call(
900 char_u **arg,
901 size_t varlen,
902 cctx_T *cctx,
903 ppconst_T *ppconst,
904 int argcount_init)
905{
906 char_u *name = *arg;
907 char_u *p;
908 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100909 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 char_u fname_buf[FLEN_FIXED + 1];
911 char_u *tofree = NULL;
912 int error = FCERR_NONE;
913 ufunc_T *ufunc = NULL;
914 int res = FAIL;
915 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000916 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100917 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000918 imported_T *import;
919
920 if (varlen >= sizeof(namebuf))
921 {
922 semsg(_(e_name_too_long_str), name);
923 return FAIL;
924 }
925 vim_strncpy(namebuf, *arg, varlen);
926
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000927 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000928 if (import != NULL)
929 {
930 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
931 return FAIL;
932 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000933
934 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100935 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100937 if ((varlen == 3
938 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000939 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
940 {
941 char_u *s = skipwhite(*arg + varlen + 1);
942 typval_T argvars[2];
943 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100944 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000945
946 argvars[0].v_type = VAR_UNKNOWN;
947 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100948 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100950 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000951 s = skipwhite(s);
952 if (*s == ')' && argvars[0].v_type == VAR_STRING
953 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
954 || !is_has))
955 {
956 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
957
958 *arg = s + 1;
959 argvars[1].v_type = VAR_UNKNOWN;
960 tv->v_type = VAR_NUMBER;
961 tv->vval.v_number = 0;
962 if (is_has)
963 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100964 else if (is_len)
965 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966 else
967 f_exists(argvars, tv);
968 clear_tv(&argvars[0]);
969 ++ppconst->pp_used;
970 return OK;
971 }
972 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100973 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000974 {
975 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
976 return FAIL;
977 }
978 }
979
980 if (generate_ppconst(cctx, ppconst) == FAIL)
981 return FAIL;
982
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
984
985 // We handle the "skip" argument of searchpair() and searchpairpos()
986 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100987 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
988 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
989 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
990 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
991 special_fn = CA_SEARCHPAIR;
992 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
993 special_fn = CA_SUBSTITUTE;
994 else
995 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996
997 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100998 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000999 goto theend;
1000
1001 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
1002 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
1003 {
1004 int idx;
1005
1006 // builtin function
1007 idx = find_internal_func(name);
1008 if (idx >= 0)
1009 {
1010 if (STRCMP(name, "flatten") == 0)
1011 {
1012 emsg(_(e_cannot_use_flatten_in_vim9_script));
1013 goto theend;
1014 }
1015
1016 if (STRCMP(name, "add") == 0 && argcount == 2)
1017 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +00001018 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001019
1020 // add() can be compiled to instructions if we know the type
1021 if (type->tt_type == VAR_LIST)
1022 {
1023 // inline "add(list, item)" so that the type can be checked
1024 res = generate_LISTAPPEND(cctx);
1025 idx = -1;
1026 }
1027 else if (type->tt_type == VAR_BLOB)
1028 {
1029 // inline "add(blob, nr)" so that the type can be checked
1030 res = generate_BLOBAPPEND(cctx);
1031 idx = -1;
1032 }
1033 }
1034
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001035 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1036 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001037 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001038 // May have the "D" or "R" flag, reserve a variable for a
1039 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001040 if (get_defer_var_idx(cctx) == 0)
1041 idx = -1;
1042 }
1043
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 if (idx >= 0)
1045 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1046 }
1047 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001048 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001049 goto theend;
1050 }
1051
Bram Moolenaar62aec932022-01-29 21:45:34 +00001052 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1053
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001054 // An argument or local variable can be a function reference, this
1055 // overrules a function name.
1056 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1057 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1058 {
1059 // If we can find the function by name generate the right call.
1060 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001061 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001062 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001063 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001064 if (!func_is_global(ufunc))
1065 {
1066 res = generate_CALL(cctx, ufunc, argcount);
1067 goto theend;
1068 }
1069 if (!has_g_namespace
1070 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1071 {
1072 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001073 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001074 goto theend;
1075 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001076 }
1077 }
1078
1079 // If the name is a variable, load it and use PCALL.
1080 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001081 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001083 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1085 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001086 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001087
1088 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1089 goto theend;
1090 }
1091
1092 // If we can find a global function by name generate the right call.
1093 if (ufunc != NULL)
1094 {
1095 res = generate_CALL(cctx, ufunc, argcount);
1096 goto theend;
1097 }
1098
1099 // A global function may be defined only later. Need to figure out at
1100 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001101 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102 res = generate_UCALL(cctx, name, argcount);
1103 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001104 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105
1106theend:
1107 vim_free(tofree);
1108 return res;
1109}
1110
1111// like NAMESPACE_CHAR but with 'a' and 'l'.
1112#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1113
1114/*
1115 * Find the end of a variable or function name. Unlike find_name_end() this
1116 * does not recognize magic braces.
1117 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1118 * Return a pointer to just after the name. Equal to "arg" if there is no
1119 * valid name.
1120 */
1121 char_u *
1122to_name_end(char_u *arg, int use_namespace)
1123{
1124 char_u *p;
1125
1126 // Quick check for valid starting character.
1127 if (!eval_isnamec1(*arg))
1128 return arg;
1129
1130 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1131 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1132 // and can be used in slice "[n:]".
1133 if (*p == ':' && (p != arg + 1
1134 || !use_namespace
1135 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1136 break;
1137 return p;
1138}
1139
1140/*
1141 * Like to_name_end() but also skip over a list or dict constant.
1142 * Also accept "<SNR>123_Func".
1143 * This intentionally does not handle line continuation.
1144 */
1145 char_u *
1146to_name_const_end(char_u *arg)
1147{
1148 char_u *p = arg;
1149 typval_T rettv;
1150
1151 if (STRNCMP(p, "<SNR>", 5) == 0)
1152 p = skipdigits(p + 5);
1153 p = to_name_end(p, TRUE);
1154 if (p == arg && *arg == '[')
1155 {
1156
1157 // Can be "[1, 2, 3]->Func()".
1158 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1159 p = arg;
1160 }
1161 return p;
1162}
1163
1164/*
1165 * parse a list: [expr, expr]
1166 * "*arg" points to the '['.
1167 * ppconst->pp_is_const is set if all items are a constant.
1168 */
1169 static int
1170compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1171{
1172 char_u *p = skipwhite(*arg + 1);
1173 char_u *whitep = *arg + 1;
1174 int count = 0;
1175 int is_const;
1176 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001177 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178
1179 for (;;)
1180 {
1181 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1182 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001183 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184 return FAIL;
1185 }
1186 if (*p == ',')
1187 {
1188 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1189 return FAIL;
1190 }
1191 if (*p == ']')
1192 {
1193 ++p;
1194 break;
1195 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001196 if (must_end)
1197 {
1198 semsg(_(e_missing_comma_in_list_str), p);
1199 return FAIL;
1200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1202 return FAIL;
1203 if (!is_const)
1204 is_all_const = FALSE;
1205 ++count;
1206 if (*p == ',')
1207 {
1208 ++p;
1209 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1210 {
1211 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1212 return FAIL;
1213 }
1214 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001215 else
1216 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001217 whitep = p;
1218 p = skipwhite(p);
1219 }
1220 *arg = p;
1221
1222 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001223 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224}
1225
1226/*
1227 * Parse a lambda: "(arg, arg) => expr"
1228 * "*arg" points to the '('.
1229 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1230 */
1231 static int
1232compile_lambda(char_u **arg, cctx_T *cctx)
1233{
1234 int r;
1235 typval_T rettv;
1236 ufunc_T *ufunc;
1237 evalarg_T evalarg;
1238
1239 init_evalarg(&evalarg);
1240 evalarg.eval_flags = EVAL_EVALUATE;
1241 evalarg.eval_cctx = cctx;
1242
1243 // Get the funcref in "rettv".
1244 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1245 if (r != OK)
1246 {
1247 clear_evalarg(&evalarg, NULL);
1248 return r;
1249 }
1250
1251 // "rettv" will now be a partial referencing the function.
1252 ufunc = rettv.vval.v_partial->pt_func;
1253 ++ufunc->uf_refcount;
1254 clear_tv(&rettv);
1255
1256 // Compile it here to get the return type. The return type is optional,
1257 // when it's missing use t_unknown. This is recognized in
1258 // compile_return().
1259 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1260 ufunc->uf_ret_type = &t_unknown;
1261 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1262
1263 // When the outer function is compiled for profiling or debugging, the
1264 // lambda may be called without profiling or debugging. Compile it here in
1265 // the right context.
1266 if (cctx->ctx_compile_type == CT_DEBUG
1267#ifdef FEAT_PROFILE
1268 || cctx->ctx_compile_type == CT_PROFILE
1269#endif
1270 )
1271 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1272
Bram Moolenaar139575d2022-03-15 19:29:30 +00001273 // if the outer function is not compiled for debugging or profiling, this
1274 // one might be
1275 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001276 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001277 compiletype_T compile_type = get_compile_type(ufunc);
1278
1279 if (compile_type != CT_NONE)
1280 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001281 }
1282
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001283 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1284 // "*arg" may point into it. Point into the original line to avoid a
1285 // dangling pointer.
1286 if (evalarg.eval_using_cmdline)
1287 {
1288 garray_T *gap = &evalarg.eval_tofree_ga;
1289 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1290
1291 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1292 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001293 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 }
1295
1296 clear_evalarg(&evalarg, NULL);
1297
1298 if (ufunc->uf_def_status == UF_COMPILED)
1299 {
1300 // The return type will now be known.
1301 set_function_type(ufunc);
1302
1303 // The function reference count will be 1. When the ISN_FUNCREF
1304 // instruction is deleted the reference count is decremented and the
1305 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001306 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307 }
1308
1309 func_ptr_unref(ufunc);
1310 return FAIL;
1311}
1312
1313/*
1314 * Get a lambda and compile it. Uses Vim9 syntax.
1315 */
1316 int
1317get_lambda_tv_and_compile(
1318 char_u **arg,
1319 typval_T *rettv,
1320 int types_optional,
1321 evalarg_T *evalarg)
1322{
1323 int r;
1324 ufunc_T *ufunc;
1325 int save_sc_version = current_sctx.sc_version;
1326
1327 // Get the funcref in "rettv".
1328 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1329 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1330 current_sctx.sc_version = save_sc_version;
1331 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001332 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333
1334 // "rettv" will now be a partial referencing the function.
1335 ufunc = rettv->vval.v_partial->pt_func;
1336
1337 // Compile it here to get the return type. The return type is optional,
1338 // when it's missing use t_unknown. This is recognized in
1339 // compile_return().
1340 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1341 ufunc->uf_ret_type = &t_unknown;
1342 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1343
1344 if (ufunc->uf_def_status == UF_COMPILED)
1345 {
1346 // The return type will now be known.
1347 set_function_type(ufunc);
1348 return OK;
1349 }
1350 clear_tv(rettv);
1351 return FAIL;
1352}
1353
1354/*
1355 * parse a dict: {key: val, [key]: val}
1356 * "*arg" points to the '{'.
1357 * ppconst->pp_is_const is set if all item values are a constant.
1358 */
1359 static int
1360compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1361{
1362 garray_T *instr = &cctx->ctx_instr;
1363 int count = 0;
1364 dict_T *d = dict_alloc();
1365 dictitem_T *item;
1366 char_u *whitep = *arg + 1;
1367 char_u *p;
1368 int is_const;
1369 int is_all_const = TRUE; // reset when non-const encountered
1370
1371 if (d == NULL)
1372 return FAIL;
1373 if (generate_ppconst(cctx, ppconst) == FAIL)
1374 return FAIL;
1375 for (;;)
1376 {
1377 char_u *key = NULL;
1378
1379 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1380 {
1381 *arg = NULL;
1382 goto failret;
1383 }
1384
1385 if (**arg == '}')
1386 break;
1387
1388 if (**arg == '[')
1389 {
1390 isn_T *isn;
1391
1392 // {[expr]: value} uses an evaluated key.
1393 *arg = skipwhite(*arg + 1);
1394 if (compile_expr0(arg, cctx) == FAIL)
1395 return FAIL;
1396 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1397 if (isn->isn_type == ISN_PUSHNR)
1398 {
1399 char buf[NUMBUFLEN];
1400
1401 // Convert to string at compile time.
1402 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1403 isn->isn_type = ISN_PUSHS;
1404 isn->isn_arg.string = vim_strsave((char_u *)buf);
1405 }
1406 if (isn->isn_type == ISN_PUSHS)
1407 key = isn->isn_arg.string;
1408 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1409 return FAIL;
1410 *arg = skipwhite(*arg);
1411 if (**arg != ']')
1412 {
1413 emsg(_(e_missing_matching_bracket_after_dict_key));
1414 return FAIL;
1415 }
1416 ++*arg;
1417 }
1418 else
1419 {
1420 // {"name": value},
1421 // {'name': value},
1422 // {name: value} use "name" as a literal key
1423 key = get_literal_key(arg);
1424 if (key == NULL)
1425 return FAIL;
1426 if (generate_PUSHS(cctx, &key) == FAIL)
1427 return FAIL;
1428 }
1429
1430 // Check for duplicate keys, if using string keys.
1431 if (key != NULL)
1432 {
1433 item = dict_find(d, key, -1);
1434 if (item != NULL)
1435 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001436 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 goto failret;
1438 }
1439 item = dictitem_alloc(key);
1440 if (item != NULL)
1441 {
1442 item->di_tv.v_type = VAR_UNKNOWN;
1443 item->di_tv.v_lock = 0;
1444 if (dict_add(d, item) == FAIL)
1445 dictitem_free(item);
1446 }
1447 }
1448
1449 if (**arg != ':')
1450 {
1451 if (*skipwhite(*arg) == ':')
1452 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1453 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001454 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001455 return FAIL;
1456 }
1457 whitep = *arg + 1;
1458 if (!IS_WHITE_OR_NUL(*whitep))
1459 {
1460 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1461 return FAIL;
1462 }
1463
1464 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1465 {
1466 *arg = NULL;
1467 goto failret;
1468 }
1469
1470 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1471 return FAIL;
1472 if (!is_const)
1473 is_all_const = FALSE;
1474 ++count;
1475
1476 whitep = *arg;
1477 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1478 {
1479 *arg = NULL;
1480 goto failret;
1481 }
1482 if (**arg == '}')
1483 break;
1484 if (**arg != ',')
1485 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001486 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487 goto failret;
1488 }
1489 if (IS_WHITE_OR_NUL(*whitep))
1490 {
1491 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1492 return FAIL;
1493 }
1494 whitep = *arg + 1;
1495 if (!IS_WHITE_OR_NUL(*whitep))
1496 {
1497 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1498 return FAIL;
1499 }
1500 *arg = skipwhite(whitep);
1501 }
1502
1503 *arg = *arg + 1;
1504
1505 // Allow for following comment, after at least one space.
1506 p = skipwhite(*arg);
1507 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1508 *arg += STRLEN(*arg);
1509
1510 dict_unref(d);
1511 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001512 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001513
1514failret:
1515 if (*arg == NULL)
1516 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001517 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001518 *arg = (char_u *)"";
1519 }
1520 dict_unref(d);
1521 return FAIL;
1522}
1523
1524/*
1525 * Compile "&option".
1526 */
1527 static int
1528compile_get_option(char_u **arg, cctx_T *cctx)
1529{
1530 typval_T rettv;
1531 char_u *start = *arg;
1532 int ret;
1533
1534 // parse the option and get the current value to get the type.
1535 rettv.v_type = VAR_UNKNOWN;
1536 ret = eval_option(arg, &rettv, TRUE);
1537 if (ret == OK)
1538 {
1539 // include the '&' in the name, eval_option() expects it.
1540 char_u *name = vim_strnsave(start, *arg - start);
1541 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1542 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1543
1544 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1545 vim_free(name);
1546 }
1547 clear_tv(&rettv);
1548
1549 return ret;
1550}
1551
1552/*
1553 * Compile "$VAR".
1554 */
1555 static int
1556compile_get_env(char_u **arg, cctx_T *cctx)
1557{
1558 char_u *start = *arg;
1559 int len;
1560 int ret;
1561 char_u *name;
1562
1563 ++*arg;
1564 len = get_env_len(arg);
1565 if (len == 0)
1566 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001567 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568 return FAIL;
1569 }
1570
1571 // include the '$' in the name, eval_env_var() expects it.
1572 name = vim_strnsave(start, len + 1);
1573 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1574 vim_free(name);
1575 return ret;
1576}
1577
1578/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001579 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001580 */
1581 static int
1582compile_interp_string(char_u **arg, cctx_T *cctx)
1583{
1584 typval_T tv;
1585 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001586 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001587 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001588 int count = 0;
1589 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001590
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001591 // *arg is on the '$' character, move it to the first string character.
1592 ++*arg;
1593 quote = **arg;
1594 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001595
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001596 for (;;)
1597 {
1598 // Get the string up to the matching quote or to a single '{'.
1599 // "arg" is advanced to either the quote or the '{'.
1600 if (quote == '"')
1601 ret = eval_string(arg, &tv, evaluate, TRUE);
1602 else
1603 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1604 if (ret == FAIL)
1605 break;
1606 if (evaluate)
1607 {
1608 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1609 || (**arg != '{' && count == 0))
1610 {
1611 // generate non-empty string or empty string if it's the only
1612 // one
1613 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1614 return FAIL;
1615 tv.vval.v_string = NULL; // don't free it now
1616 ++count;
1617 }
1618 clear_tv(&tv);
1619 }
1620
1621 if (**arg != '{')
1622 {
1623 // found terminating quote
1624 ++*arg;
1625 break;
1626 }
1627
1628 p = compile_one_expr_in_str(*arg, cctx);
1629 if (p == NULL)
1630 {
1631 ret = FAIL;
1632 break;
1633 }
1634 ++count;
1635 *arg = p;
1636 }
LemonBoy2eaef102022-05-06 13:14:50 +01001637
1638 if (ret == FAIL || !evaluate)
1639 return ret;
1640
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001641 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1642 if (count > 1)
1643 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001644
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001645 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001646}
1647
1648/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001649 * Compile "@r".
1650 */
1651 static int
1652compile_get_register(char_u **arg, cctx_T *cctx)
1653{
1654 int ret;
1655
1656 ++*arg;
1657 if (**arg == NUL)
1658 {
1659 semsg(_(e_syntax_error_at_str), *arg - 1);
1660 return FAIL;
1661 }
1662 if (!valid_yank_reg(**arg, FALSE))
1663 {
1664 emsg_invreg(**arg);
1665 return FAIL;
1666 }
1667 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1668 ++*arg;
1669 return ret;
1670}
1671
1672/*
1673 * Apply leading '!', '-' and '+' to constant "rettv".
1674 * When "numeric_only" is TRUE do not apply '!'.
1675 */
1676 static int
1677apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1678{
1679 char_u *p = *end;
1680
1681 // this works from end to start
1682 while (p > start)
1683 {
1684 --p;
1685 if (*p == '-' || *p == '+')
1686 {
1687 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 if (rettv->v_type == VAR_FLOAT)
1689 {
1690 if (*p == '-')
1691 rettv->vval.v_float = -rettv->vval.v_float;
1692 }
1693 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694 {
1695 varnumber_T val;
1696 int error = FALSE;
1697
1698 // tv_get_number_chk() accepts a string, but we don't want that
1699 // here
1700 if (check_not_string(rettv) == FAIL)
1701 return FAIL;
1702 val = tv_get_number_chk(rettv, &error);
1703 clear_tv(rettv);
1704 if (error)
1705 return FAIL;
1706 if (*p == '-')
1707 val = -val;
1708 rettv->v_type = VAR_NUMBER;
1709 rettv->vval.v_number = val;
1710 }
1711 }
1712 else if (numeric_only)
1713 {
1714 ++p;
1715 break;
1716 }
1717 else if (*p == '!')
1718 {
1719 int v = tv2bool(rettv);
1720
1721 // '!' is permissive in the type.
1722 clear_tv(rettv);
1723 rettv->v_type = VAR_BOOL;
1724 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1725 }
1726 }
1727 *end = p;
1728 return OK;
1729}
1730
1731/*
1732 * Recognize v: variables that are constants and set "rettv".
1733 */
1734 static void
1735get_vim_constant(char_u **arg, typval_T *rettv)
1736{
1737 if (STRNCMP(*arg, "v:true", 6) == 0)
1738 {
1739 rettv->v_type = VAR_BOOL;
1740 rettv->vval.v_number = VVAL_TRUE;
1741 *arg += 6;
1742 }
1743 else if (STRNCMP(*arg, "v:false", 7) == 0)
1744 {
1745 rettv->v_type = VAR_BOOL;
1746 rettv->vval.v_number = VVAL_FALSE;
1747 *arg += 7;
1748 }
1749 else if (STRNCMP(*arg, "v:null", 6) == 0)
1750 {
1751 rettv->v_type = VAR_SPECIAL;
1752 rettv->vval.v_number = VVAL_NULL;
1753 *arg += 6;
1754 }
1755 else if (STRNCMP(*arg, "v:none", 6) == 0)
1756 {
1757 rettv->v_type = VAR_SPECIAL;
1758 rettv->vval.v_number = VVAL_NONE;
1759 *arg += 6;
1760 }
1761}
1762
1763 exprtype_T
1764get_compare_type(char_u *p, int *len, int *type_is)
1765{
1766 exprtype_T type = EXPR_UNKNOWN;
1767 int i;
1768
1769 switch (p[0])
1770 {
1771 case '=': if (p[1] == '=')
1772 type = EXPR_EQUAL;
1773 else if (p[1] == '~')
1774 type = EXPR_MATCH;
1775 break;
1776 case '!': if (p[1] == '=')
1777 type = EXPR_NEQUAL;
1778 else if (p[1] == '~')
1779 type = EXPR_NOMATCH;
1780 break;
1781 case '>': if (p[1] != '=')
1782 {
1783 type = EXPR_GREATER;
1784 *len = 1;
1785 }
1786 else
1787 type = EXPR_GEQUAL;
1788 break;
1789 case '<': if (p[1] != '=')
1790 {
1791 type = EXPR_SMALLER;
1792 *len = 1;
1793 }
1794 else
1795 type = EXPR_SEQUAL;
1796 break;
1797 case 'i': if (p[1] == 's')
1798 {
1799 // "is" and "isnot"; but not a prefix of a name
1800 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1801 *len = 5;
1802 i = p[*len];
1803 if (!isalnum(i) && i != '_')
1804 {
1805 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1806 *type_is = TRUE;
1807 }
1808 }
1809 break;
1810 }
1811 return type;
1812}
1813
1814/*
1815 * Skip over an expression, ignoring most errors.
1816 */
1817 void
1818skip_expr_cctx(char_u **arg, cctx_T *cctx)
1819{
1820 evalarg_T evalarg;
1821
1822 init_evalarg(&evalarg);
1823 evalarg.eval_cctx = cctx;
1824 skip_expr(arg, &evalarg);
1825 clear_evalarg(&evalarg, NULL);
1826}
1827
1828/*
1829 * Check that the top of the type stack has a type that can be used as a
1830 * condition. Give an error and return FAIL if not.
1831 */
1832 int
1833bool_on_stack(cctx_T *cctx)
1834{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001835 type_T *type;
1836
Bram Moolenaar078a4612022-01-04 15:17:03 +00001837 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838 if (type == &t_bool)
1839 return OK;
1840
Bram Moolenaar4913d422022-10-17 13:13:32 +01001841 if (type->tt_type == VAR_ANY
1842 || type->tt_type == VAR_UNKNOWN
1843 || type->tt_type == VAR_NUMBER
1844 || type == &t_number_bool
1845 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1847 // This requires a runtime type check.
1848 return generate_COND2BOOL(cctx);
1849
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001850 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851}
1852
1853/*
1854 * Give the "white on both sides" error, taking the operator from "p[len]".
1855 */
1856 void
1857error_white_both(char_u *op, int len)
1858{
1859 char_u buf[10];
1860
1861 vim_strncpy(buf, op, len);
1862 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1863}
1864
1865/*
1866 * Compile code to apply '-', '+' and '!'.
1867 * When "numeric_only" is TRUE do not apply '!'.
1868 */
1869 static int
1870compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1871{
1872 char_u *p = *end;
1873
1874 // this works from end to start
1875 while (p > start)
1876 {
1877 --p;
1878 while (VIM_ISWHITE(*p))
1879 --p;
1880 if (*p == '-' || *p == '+')
1881 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001882 type_T *type = get_type_on_stack(cctx, 0);
1883 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001884 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001885 return FAIL;
1886
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001887 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001888 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1889 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890 }
1891 else if (numeric_only)
1892 {
1893 ++p;
1894 break;
1895 }
1896 else
1897 {
1898 int invert = *p == '!';
1899
1900 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1901 {
1902 if (p[-1] == '!')
1903 invert = !invert;
1904 --p;
1905 }
1906 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1907 return FAIL;
1908 }
1909 }
1910 *end = p;
1911 return OK;
1912}
1913
1914/*
1915 * Compile "(expression)": recursive!
1916 * Return FAIL/OK.
1917 */
1918 static int
1919compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1920{
1921 int ret;
1922 char_u *p = *arg + 1;
1923
1924 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1925 return FAIL;
1926 if (ppconst->pp_used <= PPSIZE - 10)
1927 {
1928 ret = compile_expr1(arg, cctx, ppconst);
1929 }
1930 else
1931 {
1932 // Not enough space in ppconst, flush constants.
1933 if (generate_ppconst(cctx, ppconst) == FAIL)
1934 return FAIL;
1935 ret = compile_expr0(arg, cctx);
1936 }
1937 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1938 return FAIL;
1939 if (**arg == ')')
1940 ++*arg;
1941 else if (ret == OK)
1942 {
1943 emsg(_(e_missing_closing_paren));
1944 ret = FAIL;
1945 }
1946 return ret;
1947}
1948
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001949static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001950
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951/*
1952 * Compile whatever comes after "name" or "name()".
1953 * Advances "*arg" only when something was recognized.
1954 */
1955 static int
1956compile_subscript(
1957 char_u **arg,
1958 cctx_T *cctx,
1959 char_u *start_leader,
1960 char_u **end_leader,
1961 ppconst_T *ppconst)
1962{
1963 char_u *name_start = *end_leader;
1964 int keeping_dict = FALSE;
1965
1966 for (;;)
1967 {
1968 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001969 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001970
1971 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1972 {
1973 char_u *next = peek_next_line_from_context(cctx);
1974
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001975 // If a following line starts with "->{", "->(" or "->X" advance to
1976 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001977 // Also if a following line starts with ".x".
1978 if (next != NULL &&
1979 ((next[0] == '-' && next[1] == '>'
1980 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001981 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982 || ASCII_ISALPHA(*skipwhite(next + 2))))
1983 || (next[0] == '.' && eval_isdictc(next[1]))))
1984 {
1985 next = next_line_from_context(cctx, TRUE);
1986 if (next == NULL)
1987 return FAIL;
1988 *arg = next;
1989 p = skipwhite(*arg);
1990 }
1991 }
1992
1993 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1994 // is not a function call.
1995 if (**arg == '(')
1996 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997 int argcount = 0;
1998
1999 if (generate_ppconst(cctx, ppconst) == FAIL)
2000 return FAIL;
2001 ppconst->pp_is_const = FALSE;
2002
2003 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002004 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005
2006 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002007 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008 return FAIL;
2009 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
2010 return FAIL;
2011 if (keeping_dict)
2012 {
2013 keeping_dict = FALSE;
2014 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2015 return FAIL;
2016 }
2017 }
2018 else if (*p == '-' && p[1] == '>')
2019 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002020 char_u *pstart = p;
2021 int alt;
2022 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002023
Bram Moolenaarc7349932022-01-16 20:59:39 +00002024 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002025 if (generate_ppconst(cctx, ppconst) == FAIL)
2026 return FAIL;
2027 ppconst->pp_is_const = FALSE;
2028
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029 // Apply the '!', '-' and '+' first:
2030 // -1.0->func() works like (-1.0)->func()
2031 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2032 return FAIL;
2033
2034 p += 2;
2035 *arg = skipwhite(p);
2036 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002037
2038 // Three alternatives handled here:
2039 // 1. "base->name(" only a name, use compile_call()
2040 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2041 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002042 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002043 alt = 2;
2044 else
2045 {
2046 // alternative 1 or 3
2047 p = *arg;
2048 if (!eval_isnamec1(*p))
2049 {
2050 semsg(_(e_trailing_characters_str), pstart);
2051 return FAIL;
2052 }
2053 if (ASCII_ISALPHA(*p) && p[1] == ':')
2054 p += 2;
2055 for ( ; eval_isnamec(*p); ++p)
2056 ;
2057 if (*p == '(')
2058 {
2059 // alternative 1
2060 alt = 1;
2061 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2062 return FAIL;
2063 }
2064 else
2065 {
2066 // Must be alternative 3, find the "(". Only works within
2067 // one line.
2068 alt = 3;
2069 paren = vim_strchr(p, '(');
2070 if (paren == NULL)
2071 {
2072 semsg(_(e_missing_parenthesis_str), *arg);
2073 return FAIL;
2074 }
2075 }
2076 }
2077
2078 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002079 {
2080 int argcount = 1;
2081 garray_T *stack = &cctx->ctx_type_stack;
2082 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002083 int expr_isn_start = cctx->ctx_instr.ga_len;
2084 int expr_isn_end;
2085 int arg_isn_count;
2086
Bram Moolenaarc7349932022-01-16 20:59:39 +00002087 if (alt == 2)
2088 {
2089 // Funcref call: list->(Refs[2])(arg)
2090 // or lambda: list->((arg) => expr)(arg)
2091 //
2092 // Fist compile the function expression.
2093 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2094 return FAIL;
2095 }
2096 else
2097 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002098 int fail;
2099 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002100 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002101
Bram Moolenaarc7349932022-01-16 20:59:39 +00002102 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002103
2104 // instead of using LOADG for "import.Func" use PUSHFUNC
2105 ++paren_follows_after_expr;
2106
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002107 // do not look in the next line
2108 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002109
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002110 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002111 || *skipwhite(*arg) != NUL;
2112 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002113 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002114 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002115
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002116 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002117 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002118 if (did_emsg == prev_did_emsg)
2119 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002120 return FAIL;
2121 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002122 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124 // Compile the arguments.
2125 if (**arg != '(')
2126 {
2127 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002128 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129 else
2130 semsg(_(e_missing_parenthesis_str), *arg);
2131 return FAIL;
2132 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002133
2134 // Remember the next instruction index, where the instructions
2135 // for arguments are being written.
2136 expr_isn_end = cctx->ctx_instr.ga_len;
2137
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002138 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002139 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2140 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 return FAIL;
2142
2143 // Move the instructions for the arguments to before the
2144 // instructions of the expression and move the type of the
2145 // expression after the argument types. This is what ISN_PCALL
2146 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002147 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2148 if (arg_isn_count > 0)
2149 {
2150 int expr_isn_count = expr_isn_end - expr_isn_start;
2151 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002152 type_T *decl_type;
2153 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002154
2155 if (isn == NULL)
2156 return FAIL;
2157 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2158 + expr_isn_start,
2159 sizeof(isn_T) * expr_isn_count);
2160 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2161 + expr_isn_start,
2162 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2163 sizeof(isn_T) * arg_isn_count);
2164 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2165 + expr_isn_start + arg_isn_count,
2166 isn, sizeof(isn_T) * expr_isn_count);
2167 vim_free(isn);
2168
Bram Moolenaar078a4612022-01-04 15:17:03 +00002169 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2170 type = typep->type_curr;
2171 decl_type = typep->type_decl;
2172 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2173 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2174 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002176 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2177 typep->type_curr = type;
2178 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002179 }
2180
Bram Moolenaar078a4612022-01-04 15:17:03 +00002181 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002182 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2183 return FAIL;
2184 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002185
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002186 if (keeping_dict)
2187 {
2188 keeping_dict = FALSE;
2189 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2190 return FAIL;
2191 }
2192 }
2193 else if (**arg == '[')
2194 {
2195 int is_slice = FALSE;
2196
2197 // list index: list[123]
2198 // dict member: dict[key]
2199 // string index: text[123]
2200 // blob index: blob[123]
2201 if (generate_ppconst(cctx, ppconst) == FAIL)
2202 return FAIL;
2203 ppconst->pp_is_const = FALSE;
2204
2205 ++p;
2206 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2207 return FAIL;
2208 if (**arg == ':')
2209 {
2210 // missing first index is equal to zero
2211 generate_PUSHNR(cctx, 0);
2212 }
2213 else
2214 {
2215 if (compile_expr0(arg, cctx) == FAIL)
2216 return FAIL;
2217 if (**arg == ':')
2218 {
2219 semsg(_(e_white_space_required_before_and_after_str_at_str),
2220 ":", *arg);
2221 return FAIL;
2222 }
2223 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2224 return FAIL;
2225 *arg = skipwhite(*arg);
2226 }
2227 if (**arg == ':')
2228 {
2229 is_slice = TRUE;
2230 ++*arg;
2231 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2232 {
2233 semsg(_(e_white_space_required_before_and_after_str_at_str),
2234 ":", *arg);
2235 return FAIL;
2236 }
2237 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2238 return FAIL;
2239 if (**arg == ']')
2240 // missing second index is equal to end of string
2241 generate_PUSHNR(cctx, -1);
2242 else
2243 {
2244 if (compile_expr0(arg, cctx) == FAIL)
2245 return FAIL;
2246 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2247 return FAIL;
2248 *arg = skipwhite(*arg);
2249 }
2250 }
2251
2252 if (**arg != ']')
2253 {
2254 emsg(_(e_missing_closing_square_brace));
2255 return FAIL;
2256 }
2257 *arg = *arg + 1;
2258
2259 if (keeping_dict)
2260 {
2261 keeping_dict = FALSE;
2262 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2263 return FAIL;
2264 }
2265 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2266 return FAIL;
2267 }
2268 else if (*p == '.' && p[1] != '.')
2269 {
2270 // dictionary member: dict.name
2271 if (generate_ppconst(cctx, ppconst) == FAIL)
2272 return FAIL;
2273 ppconst->pp_is_const = FALSE;
2274
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002275 if ((type = get_type_on_stack(cctx, 0)) != &t_unknown
2276 && (type->tt_type == VAR_CLASS
2277 || type->tt_type == VAR_OBJECT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002279 // class member: SomeClass.varname
2280 // class method: SomeClass.SomeMethod()
2281 // class constructor: SomeClass.new()
2282 // object member: someObject.varname, this.varname
2283 // object method: someObject.SomeMethod(), this.SomeMethod()
2284 *arg = p;
2285 if (compile_class_object_index(cctx, arg, type) == FAIL)
2286 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 }
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002288 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 {
Bram Moolenaar912bfee2023-01-15 20:18:55 +00002290 *arg = p + 1;
2291 if (IS_WHITE_OR_NUL(**arg))
2292 {
2293 emsg(_(e_missing_name_after_dot));
2294 return FAIL;
2295 }
2296 p = *arg;
2297 if (eval_isdictc(*p))
2298 while (eval_isnamec(*p))
2299 MB_PTR_ADV(p);
2300 if (p == *arg)
2301 {
2302 semsg(_(e_syntax_error_at_str), *arg);
2303 return FAIL;
2304 }
2305 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2306 return FAIL;
2307 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2308 return FAIL;
2309 keeping_dict = TRUE;
2310 *arg = p;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002311 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312 }
2313 else
2314 break;
2315 }
2316
2317 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2318 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002319 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2320 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002321 return FAIL;
2322
2323 return OK;
2324}
2325
2326/*
2327 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2328 * "arg" is advanced until after the expression, skipping white space.
2329 *
2330 * If the value is a constant "ppconst->pp_used" will be non-zero.
2331 * Before instructions are generated, any values in "ppconst" will generated.
2332 *
2333 * This is the compiling equivalent of eval1(), eval2(), etc.
2334 */
2335
2336/*
2337 * number number constant
2338 * 0zFFFFFFFF Blob constant
2339 * "string" string constant
2340 * 'string' literal string constant
2341 * &option-name option value
2342 * @r register contents
2343 * identifier variable value
2344 * function() function call
2345 * $VAR environment variable
2346 * (expression) nested expression
2347 * [expr, expr] List
2348 * {key: val, [key]: val} Dictionary
2349 *
2350 * Also handle:
2351 * ! in front logical NOT
2352 * - in front unary minus
2353 * + in front unary plus (ignored)
2354 * trailing (arg) funcref/partial call
2355 * trailing [] subscript in String or List
2356 * trailing .name entry in Dictionary
2357 * trailing ->name() method call
2358 */
2359 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002360compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002361 char_u **arg,
2362 cctx_T *cctx,
2363 ppconst_T *ppconst)
2364{
2365 char_u *start_leader, *end_leader;
2366 int ret = OK;
2367 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2368 int used_before = ppconst->pp_used;
2369
2370 ppconst->pp_is_const = FALSE;
2371
2372 /*
2373 * Skip '!', '-' and '+' characters. They are handled later.
2374 */
2375 start_leader = *arg;
2376 if (eval_leader(arg, TRUE) == FAIL)
2377 return FAIL;
2378 end_leader = *arg;
2379
2380 rettv->v_type = VAR_UNKNOWN;
2381 switch (**arg)
2382 {
2383 /*
2384 * Number constant.
2385 */
2386 case '0': // also for blob starting with 0z
2387 case '1':
2388 case '2':
2389 case '3':
2390 case '4':
2391 case '5':
2392 case '6':
2393 case '7':
2394 case '8':
2395 case '9':
2396 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2397 return FAIL;
2398 // Apply "-" and "+" just before the number now, right to
2399 // left. Matters especially when "->" follows. Stops at
2400 // '!'.
2401 if (apply_leader(rettv, TRUE,
2402 start_leader, &end_leader) == FAIL)
2403 {
2404 clear_tv(rettv);
2405 return FAIL;
2406 }
2407 break;
2408
2409 /*
2410 * String constant: "string".
2411 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002412 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002413 return FAIL;
2414 break;
2415
2416 /*
2417 * Literal string constant: 'str''ing'.
2418 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002419 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 return FAIL;
2421 break;
2422
2423 /*
2424 * Constant Vim variable.
2425 */
2426 case 'v': get_vim_constant(arg, rettv);
2427 ret = NOTDONE;
2428 break;
2429
2430 /*
2431 * "true" constant
2432 */
2433 case 't': if (STRNCMP(*arg, "true", 4) == 0
2434 && !eval_isnamec((*arg)[4]))
2435 {
2436 *arg += 4;
2437 rettv->v_type = VAR_BOOL;
2438 rettv->vval.v_number = VVAL_TRUE;
2439 }
2440 else
2441 ret = NOTDONE;
2442 break;
2443
2444 /*
2445 * "false" constant
2446 */
2447 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2448 && !eval_isnamec((*arg)[5]))
2449 {
2450 *arg += 5;
2451 rettv->v_type = VAR_BOOL;
2452 rettv->vval.v_number = VVAL_FALSE;
2453 }
2454 else
2455 ret = NOTDONE;
2456 break;
2457
2458 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002459 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002460 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002461 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002463 char_u *p = *arg + 4;
2464 int len;
2465
2466 for (len = 0; eval_isnamec(p[len]); ++len)
2467 ;
2468 ret = handle_predefined(*arg, len + 4, rettv);
2469 if (ret == FAIL)
2470 ret = NOTDONE;
2471 else
2472 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002473 }
2474 else
2475 ret = NOTDONE;
2476 break;
2477
2478 /*
2479 * List: [expr, expr]
2480 */
2481 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2482 return FAIL;
2483 ret = compile_list(arg, cctx, ppconst);
2484 break;
2485
2486 /*
2487 * Dictionary: {'key': val, 'key': val}
2488 */
2489 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2490 return FAIL;
2491 ret = compile_dict(arg, cctx, ppconst);
2492 break;
2493
2494 /*
2495 * Option value: &name
2496 */
2497 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2498 return FAIL;
2499 ret = compile_get_option(arg, cctx);
2500 break;
2501
2502 /*
2503 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002504 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002505 */
2506 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2507 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002508 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2509 ret = compile_interp_string(arg, cctx);
2510 else
2511 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002512 break;
2513
2514 /*
2515 * Register contents: @r.
2516 */
2517 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2518 return FAIL;
2519 ret = compile_get_register(arg, cctx);
2520 break;
2521 /*
2522 * nested expression: (expression).
2523 * lambda: (arg, arg) => expr
2524 * funcref: (arg, arg) => { statement }
2525 */
2526 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2527 ret = compile_lambda(arg, cctx);
2528 if (ret == NOTDONE)
2529 ret = compile_parenthesis(arg, cctx, ppconst);
2530 break;
2531
2532 default: ret = NOTDONE;
2533 break;
2534 }
2535 if (ret == FAIL)
2536 return FAIL;
2537
2538 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2539 {
2540 if (cctx->ctx_skip == SKIP_YES)
2541 clear_tv(rettv);
2542 else
2543 // A constant expression can possibly be handled compile time,
2544 // return the value instead of generating code.
2545 ++ppconst->pp_used;
2546 }
2547 else if (ret == NOTDONE)
2548 {
2549 char_u *p;
2550 int r;
2551
2552 if (!eval_isnamec1(**arg))
2553 {
2554 if (!vim9_bad_comment(*arg))
2555 {
2556 if (ends_excmd(*skipwhite(*arg)))
2557 semsg(_(e_empty_expression_str), *arg);
2558 else
2559 semsg(_(e_name_expected_str), *arg);
2560 }
2561 return FAIL;
2562 }
2563
2564 // "name" or "name()"
2565 p = to_name_end(*arg, TRUE);
2566 if (p - *arg == (size_t)1 && **arg == '_')
2567 {
2568 emsg(_(e_cannot_use_underscore_here));
2569 return FAIL;
2570 }
2571
2572 if (*p == '(')
2573 {
2574 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2575 }
2576 else
2577 {
2578 if (cctx->ctx_skip != SKIP_YES
2579 && generate_ppconst(cctx, ppconst) == FAIL)
2580 return FAIL;
2581 r = compile_load(arg, p, cctx, TRUE, TRUE);
2582 }
2583 if (r == FAIL)
2584 return FAIL;
2585 }
2586
2587 // Handle following "[]", ".member", etc.
2588 // Then deal with prefixed '-', '+' and '!', if not done already.
2589 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2590 ppconst) == FAIL)
2591 return FAIL;
2592 if (ppconst->pp_used > 0)
2593 {
2594 // apply the '!', '-' and '+' before the constant
2595 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2596 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2597 return FAIL;
2598 return OK;
2599 }
2600 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2601 return FAIL;
2602 return OK;
2603}
2604
2605/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002606 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002607 */
2608 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002609compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002610{
2611 type_T *want_type = NULL;
2612
2613 // Recognize <type>
2614 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2615 {
2616 ++*arg;
2617 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2618 if (want_type == NULL)
2619 return FAIL;
2620
2621 if (**arg != '>')
2622 {
2623 if (*skipwhite(*arg) == '>')
2624 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2625 else
2626 emsg(_(e_missing_gt));
2627 return FAIL;
2628 }
2629 ++*arg;
2630 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2631 return FAIL;
2632 }
2633
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002634 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002635 return FAIL;
2636
2637 if (want_type != NULL)
2638 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002639 type_T *actual;
2640 where_T where = WHERE_INIT;
2641
2642 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002643 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002644 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002646 if (need_type(actual, want_type, FALSE,
2647 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002648 return FAIL;
2649 }
2650 }
2651
2652 return OK;
2653}
2654
2655/*
2656 * * number multiplication
2657 * / number division
2658 * % number modulo
2659 */
2660 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002661compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002662{
2663 char_u *op;
2664 char_u *next;
2665 int ppconst_used = ppconst->pp_used;
2666
2667 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002668 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002669 return FAIL;
2670
2671 /*
2672 * Repeat computing, until no "*", "/" or "%" is following.
2673 */
2674 for (;;)
2675 {
2676 op = may_peek_next_line(cctx, *arg, &next);
2677 if (*op != '*' && *op != '/' && *op != '%')
2678 break;
2679 if (next != NULL)
2680 {
2681 *arg = next_line_from_context(cctx, TRUE);
2682 op = skipwhite(*arg);
2683 }
2684
2685 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2686 {
2687 error_white_both(op, 1);
2688 return FAIL;
2689 }
2690 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2691 return FAIL;
2692
2693 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002694 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002695 return FAIL;
2696
2697 if (ppconst->pp_used == ppconst_used + 2
2698 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2699 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2700 {
2701 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2702 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2703 varnumber_T res = 0;
2704 int failed = FALSE;
2705
2706 // both are numbers: compute the result
2707 switch (*op)
2708 {
2709 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2710 break;
2711 case '/': res = num_divide(tv1->vval.v_number,
2712 tv2->vval.v_number, &failed);
2713 break;
2714 case '%': res = num_modulus(tv1->vval.v_number,
2715 tv2->vval.v_number, &failed);
2716 break;
2717 }
2718 if (failed)
2719 return FAIL;
2720 tv1->vval.v_number = res;
2721 --ppconst->pp_used;
2722 }
2723 else
2724 {
2725 generate_ppconst(cctx, ppconst);
2726 generate_two_op(cctx, op);
2727 }
2728 }
2729
2730 return OK;
2731}
2732
2733/*
2734 * + number addition or list/blobl concatenation
2735 * - number subtraction
2736 * .. string concatenation
2737 */
2738 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002739compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002740{
2741 char_u *op;
2742 char_u *next;
2743 int oplen;
2744 int ppconst_used = ppconst->pp_used;
2745
2746 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002747 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002748 return FAIL;
2749
2750 /*
2751 * Repeat computing, until no "+", "-" or ".." is following.
2752 */
2753 for (;;)
2754 {
2755 op = may_peek_next_line(cctx, *arg, &next);
2756 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2757 break;
2758 if (op[0] == op[1] && *op != '.' && next)
2759 // Finding "++" or "--" on the next line is a separate command.
2760 // But ".." is concatenation.
2761 break;
2762 oplen = (*op == '.' ? 2 : 1);
2763 if (next != NULL)
2764 {
2765 *arg = next_line_from_context(cctx, TRUE);
2766 op = skipwhite(*arg);
2767 }
2768
2769 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2770 {
2771 error_white_both(op, oplen);
2772 return FAIL;
2773 }
2774
2775 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2776 return FAIL;
2777
2778 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002779 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002780 return FAIL;
2781
2782 if (ppconst->pp_used == ppconst_used + 2
2783 && (*op == '.'
2784 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2785 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2786 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2787 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2788 {
2789 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2790 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2791
2792 // concat/subtract/add constant numbers
2793 if (*op == '+')
2794 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2795 else if (*op == '-')
2796 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2797 else
2798 {
2799 // concatenate constant strings
2800 char_u *s1 = tv1->vval.v_string;
2801 char_u *s2 = tv2->vval.v_string;
2802 size_t len1 = STRLEN(s1);
2803
2804 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2805 if (tv1->vval.v_string == NULL)
2806 {
2807 clear_ppconst(ppconst);
2808 return FAIL;
2809 }
2810 mch_memmove(tv1->vval.v_string, s1, len1);
2811 STRCPY(tv1->vval.v_string + len1, s2);
2812 vim_free(s1);
2813 vim_free(s2);
2814 }
2815 --ppconst->pp_used;
2816 }
2817 else
2818 {
2819 generate_ppconst(cctx, ppconst);
2820 ppconst->pp_is_const = FALSE;
2821 if (*op == '.')
2822 {
2823 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2824 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2825 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002826 if (generate_CONCAT(cctx, 2) == FAIL)
2827 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002828 }
2829 else
2830 generate_two_op(cctx, op);
2831 }
2832 }
2833
2834 return OK;
2835}
2836
2837/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002838 * expr6a >> expr6b
2839 * expr6a << expr6b
2840 *
2841 * Produces instructions:
2842 * OPNR bitwise left or right shift
2843 */
2844 static int
2845compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2846{
2847 exprtype_T type = EXPR_UNKNOWN;
2848 char_u *p;
2849 char_u *next;
2850 int len = 2;
2851 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002852 isn_T *isn;
2853
2854 // get the first variable
2855 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2856 return FAIL;
2857
2858 /*
2859 * Repeat computing, until no "+", "-" or ".." is following.
2860 */
2861 for (;;)
2862 {
2863 type = EXPR_UNKNOWN;
2864
2865 p = may_peek_next_line(cctx, *arg, &next);
2866 if (p[0] == '<' && p[1] == '<')
2867 type = EXPR_LSHIFT;
2868 else if (p[0] == '>' && p[1] == '>')
2869 type = EXPR_RSHIFT;
2870
2871 if (type == EXPR_UNKNOWN)
2872 return OK;
2873
2874 // Handle a bitwise left or right shift operator
2875 if (ppconst->pp_used == ppconst_used + 1)
2876 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002877 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002878 {
2879 // left operand should be a number
2880 emsg(_(e_bitshift_ops_must_be_number));
2881 return FAIL;
2882 }
2883 }
2884 else
2885 {
2886 type_T *t = get_type_on_stack(cctx, 0);
2887
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002888 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002889 {
2890 emsg(_(e_bitshift_ops_must_be_number));
2891 return FAIL;
2892 }
2893 }
2894
2895 if (next != NULL)
2896 {
2897 *arg = next_line_from_context(cctx, TRUE);
2898 p = skipwhite(*arg);
2899 }
2900
2901 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2902 {
2903 error_white_both(p, len);
2904 return FAIL;
2905 }
2906
2907 // get the second variable
2908 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2909 return FAIL;
2910
2911 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2912 return FAIL;
2913
2914 if (ppconst->pp_used == ppconst_used + 2)
2915 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002916 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2917 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2918
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002919 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002920 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2921 {
2922 // right operand should be a positive number
2923 if (tv2->v_type != VAR_NUMBER)
2924 emsg(_(e_bitshift_ops_must_be_number));
2925 else
dundargocc57b5bc2022-11-02 13:30:51 +00002926 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002927 return FAIL;
2928 }
2929
2930 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2931 tv1->vval.v_number = 0;
2932 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002933 tv1->vval.v_number =
2934 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002935 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002936 tv1->vval.v_number =
2937 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002938 clear_tv(tv2);
2939 --ppconst->pp_used;
2940 }
2941 else
2942 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002943 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2944 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002945 {
2946 emsg(_(e_bitshift_ops_must_be_number));
2947 return FAIL;
2948 }
2949
2950 generate_ppconst(cctx, ppconst);
2951
2952 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2953 if (isn == NULL)
2954 return FAIL;
2955
2956 if (isn != NULL)
2957 isn->isn_arg.op.op_type = type;
2958 }
2959 }
2960
2961 return OK;
2962}
2963
2964/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002965 * expr5a == expr5b
2966 * expr5a =~ expr5b
2967 * expr5a != expr5b
2968 * expr5a !~ expr5b
2969 * expr5a > expr5b
2970 * expr5a >= expr5b
2971 * expr5a < expr5b
2972 * expr5a <= expr5b
2973 * expr5a is expr5b
2974 * expr5a isnot expr5b
2975 *
2976 * Produces instructions:
2977 * EVAL expr5a Push result of "expr5a"
2978 * EVAL expr5b Push result of "expr5b"
2979 * COMPARE one of the compare instructions
2980 */
2981 static int
2982compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2983{
2984 exprtype_T type = EXPR_UNKNOWN;
2985 char_u *p;
2986 char_u *next;
2987 int len = 2;
2988 int type_is = FALSE;
2989 int ppconst_used = ppconst->pp_used;
2990
2991 // get the first variable
2992 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2993 return FAIL;
2994
2995 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002996
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002997 type = get_compare_type(p, &len, &type_is);
2998
2999 /*
3000 * If there is a comparative operator, use it.
3001 */
3002 if (type != EXPR_UNKNOWN)
3003 {
3004 int ic = FALSE; // Default: do not ignore case
3005
3006 if (next != NULL)
3007 {
3008 *arg = next_line_from_context(cctx, TRUE);
3009 p = skipwhite(*arg);
3010 }
3011 if (type_is && (p[len] == '?' || p[len] == '#'))
3012 {
3013 semsg(_(e_invalid_expression_str), *arg);
3014 return FAIL;
3015 }
3016 // extra question mark appended: ignore case
3017 if (p[len] == '?')
3018 {
3019 ic = TRUE;
3020 ++len;
3021 }
3022 // extra '#' appended: match case (ignored)
3023 else if (p[len] == '#')
3024 ++len;
3025 // nothing appended: match case
3026
3027 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3028 {
3029 error_white_both(p, len);
3030 return FAIL;
3031 }
3032
3033 // get the second variable
3034 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3035 return FAIL;
3036
3037 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3038 return FAIL;
3039
3040 if (ppconst->pp_used == ppconst_used + 2)
3041 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003042 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003043 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3044 int ret;
3045
3046 // Both sides are a constant, compute the result now.
3047 // First check for a valid combination of types, this is more
3048 // strict than typval_compare().
3049 if (check_compare_types(type, tv1, tv2) == FAIL)
3050 ret = FAIL;
3051 else
3052 {
3053 ret = typval_compare(tv1, tv2, type, ic);
3054 tv1->v_type = VAR_BOOL;
3055 tv1->vval.v_number = tv1->vval.v_number
3056 ? VVAL_TRUE : VVAL_FALSE;
3057 clear_tv(tv2);
3058 --ppconst->pp_used;
3059 }
3060 return ret;
3061 }
3062
3063 generate_ppconst(cctx, ppconst);
3064 return generate_COMPARE(cctx, type, ic);
3065 }
3066
3067 return OK;
3068}
3069
3070static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3071
3072/*
3073 * Compile || or &&.
3074 */
3075 static int
3076compile_and_or(
3077 char_u **arg,
3078 cctx_T *cctx,
3079 char *op,
3080 ppconst_T *ppconst,
3081 int ppconst_used UNUSED)
3082{
3083 char_u *next;
3084 char_u *p = may_peek_next_line(cctx, *arg, &next);
3085 int opchar = *op;
3086
3087 if (p[0] == opchar && p[1] == opchar)
3088 {
3089 garray_T *instr = &cctx->ctx_instr;
3090 garray_T end_ga;
3091 int save_skip = cctx->ctx_skip;
3092
3093 /*
3094 * Repeat until there is no following "||" or "&&"
3095 */
3096 ga_init2(&end_ga, sizeof(int), 10);
3097 while (p[0] == opchar && p[1] == opchar)
3098 {
3099 long start_lnum = SOURCING_LNUM;
3100 long save_sourcing_lnum;
3101 int start_ctx_lnum = cctx->ctx_lnum;
3102 int save_lnum;
3103 int const_used;
3104 int status;
3105 jumpwhen_T jump_when = opchar == '|'
3106 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3107
3108 if (next != NULL)
3109 {
3110 *arg = next_line_from_context(cctx, TRUE);
3111 p = skipwhite(*arg);
3112 }
3113
3114 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3115 {
3116 semsg(_(e_white_space_required_before_and_after_str_at_str),
3117 op, p);
3118 ga_clear(&end_ga);
3119 return FAIL;
3120 }
3121
3122 save_sourcing_lnum = SOURCING_LNUM;
3123 SOURCING_LNUM = start_lnum;
3124 save_lnum = cctx->ctx_lnum;
3125 cctx->ctx_lnum = start_ctx_lnum;
3126
3127 status = check_ppconst_bool(ppconst);
3128 if (status != FAIL)
3129 {
3130 // Use the last ppconst if possible.
3131 if (ppconst->pp_used > 0)
3132 {
3133 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3134 int is_true = tv2bool(tv);
3135
3136 if ((is_true && opchar == '|')
3137 || (!is_true && opchar == '&'))
3138 {
3139 // For "false && expr" and "true || expr" the "expr"
3140 // does not need to be evaluated.
3141 cctx->ctx_skip = SKIP_YES;
3142 clear_tv(tv);
3143 tv->v_type = VAR_BOOL;
3144 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3145 }
3146 else
3147 {
3148 // For "true && expr" and "false || expr" only "expr"
3149 // needs to be evaluated.
3150 --ppconst->pp_used;
3151 jump_when = JUMP_NEVER;
3152 }
3153 }
3154 else
3155 {
3156 // Every part must evaluate to a bool.
3157 status = bool_on_stack(cctx);
3158 }
3159 }
3160 if (status != FAIL)
3161 status = ga_grow(&end_ga, 1);
3162 cctx->ctx_lnum = save_lnum;
3163 if (status == FAIL)
3164 {
3165 ga_clear(&end_ga);
3166 return FAIL;
3167 }
3168
3169 if (jump_when != JUMP_NEVER)
3170 {
3171 if (cctx->ctx_skip != SKIP_YES)
3172 {
3173 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3174 ++end_ga.ga_len;
3175 }
3176 generate_JUMP(cctx, jump_when, 0);
3177 }
3178
3179 // eval the next expression
3180 SOURCING_LNUM = save_sourcing_lnum;
3181 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3182 {
3183 ga_clear(&end_ga);
3184 return FAIL;
3185 }
3186
3187 const_used = ppconst->pp_used;
3188 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3189 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3190 {
3191 ga_clear(&end_ga);
3192 return FAIL;
3193 }
3194
3195 // "0 || 1" results in true, "1 && 0" results in false.
3196 if (ppconst->pp_used == const_used + 1)
3197 {
3198 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3199
3200 if (tv->v_type == VAR_NUMBER
3201 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3202 {
3203 tv->vval.v_number = tv->vval.v_number == 1
3204 ? VVAL_TRUE : VVAL_FALSE;
3205 tv->v_type = VAR_BOOL;
3206 }
3207 }
3208
3209 p = may_peek_next_line(cctx, *arg, &next);
3210 }
3211
3212 if (check_ppconst_bool(ppconst) == FAIL)
3213 {
3214 ga_clear(&end_ga);
3215 return FAIL;
3216 }
3217
3218 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3219 // Every part must evaluate to a bool.
3220 if (bool_on_stack(cctx) == FAIL)
3221 {
3222 ga_clear(&end_ga);
3223 return FAIL;
3224 }
3225
3226 if (end_ga.ga_len > 0)
3227 {
3228 // Fill in the end label in all jumps.
3229 generate_ppconst(cctx, ppconst);
3230 while (end_ga.ga_len > 0)
3231 {
3232 isn_T *isn;
3233
3234 --end_ga.ga_len;
3235 isn = ((isn_T *)instr->ga_data)
3236 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3237 isn->isn_arg.jump.jump_where = instr->ga_len;
3238 }
3239 }
3240 ga_clear(&end_ga);
3241
3242 cctx->ctx_skip = save_skip;
3243 }
3244
3245 return OK;
3246}
3247
3248/*
3249 * expr4a && expr4a && expr4a logical AND
3250 *
3251 * Produces instructions:
3252 * EVAL expr4a Push result of "expr4a"
3253 * COND2BOOL convert to bool if needed
3254 * JUMP_IF_COND_FALSE end
3255 * EVAL expr4b Push result of "expr4b"
3256 * JUMP_IF_COND_FALSE end
3257 * EVAL expr4c Push result of "expr4c"
3258 * end:
3259 */
3260 static int
3261compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3262{
3263 int ppconst_used = ppconst->pp_used;
3264
3265 // get the first variable
3266 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3267 return FAIL;
3268
3269 // || and && work almost the same
3270 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3271}
3272
3273/*
3274 * expr3a || expr3b || expr3c logical OR
3275 *
3276 * Produces instructions:
3277 * EVAL expr3a Push result of "expr3a"
3278 * COND2BOOL convert to bool if needed
3279 * JUMP_IF_COND_TRUE end
3280 * EVAL expr3b Push result of "expr3b"
3281 * JUMP_IF_COND_TRUE end
3282 * EVAL expr3c Push result of "expr3c"
3283 * end:
3284 */
3285 static int
3286compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3287{
3288 int ppconst_used = ppconst->pp_used;
3289
3290 // eval the first expression
3291 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3292 return FAIL;
3293
3294 // || and && work almost the same
3295 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3296}
3297
3298/*
3299 * Toplevel expression: expr2 ? expr1a : expr1b
3300 * Produces instructions:
3301 * EVAL expr2 Push result of "expr2"
3302 * JUMP_IF_FALSE alt jump if false
3303 * EVAL expr1a
3304 * JUMP_ALWAYS end
3305 * alt: EVAL expr1b
3306 * end:
3307 *
3308 * Toplevel expression: expr2 ?? expr1
3309 * Produces instructions:
3310 * EVAL expr2 Push result of "expr2"
3311 * JUMP_AND_KEEP_IF_TRUE end jump if true
3312 * EVAL expr1
3313 * end:
3314 */
3315 int
3316compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3317{
3318 char_u *p;
3319 int ppconst_used = ppconst->pp_used;
3320 char_u *next;
3321
3322 // Ignore all kinds of errors when not producing code.
3323 if (cctx->ctx_skip == SKIP_YES)
3324 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003325 int prev_did_emsg = did_emsg;
3326
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003327 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003328 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003329 }
3330
3331 // Evaluate the first expression.
3332 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3333 return FAIL;
3334
3335 p = may_peek_next_line(cctx, *arg, &next);
3336 if (*p == '?')
3337 {
3338 int op_falsy = p[1] == '?';
3339 garray_T *instr = &cctx->ctx_instr;
3340 garray_T *stack = &cctx->ctx_type_stack;
3341 int alt_idx = instr->ga_len;
3342 int end_idx = 0;
3343 isn_T *isn;
3344 type_T *type1 = NULL;
3345 int has_const_expr = FALSE;
3346 int const_value = FALSE;
3347 int save_skip = cctx->ctx_skip;
3348
3349 if (next != NULL)
3350 {
3351 *arg = next_line_from_context(cctx, TRUE);
3352 p = skipwhite(*arg);
3353 }
3354
3355 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3356 {
3357 semsg(_(e_white_space_required_before_and_after_str_at_str),
3358 op_falsy ? "??" : "?", p);
3359 return FAIL;
3360 }
3361
3362 if (ppconst->pp_used == ppconst_used + 1)
3363 {
3364 // the condition is a constant, we know whether the ? or the :
3365 // expression is to be evaluated.
3366 has_const_expr = TRUE;
3367 if (op_falsy)
3368 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3369 else
3370 {
3371 int error = FALSE;
3372
3373 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3374 &error);
3375 if (error)
3376 return FAIL;
3377 }
3378 cctx->ctx_skip = save_skip == SKIP_YES ||
3379 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3380
3381 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3382 // "left ?? right" and "left" is truthy: produce "left"
3383 generate_ppconst(cctx, ppconst);
3384 else
3385 {
3386 clear_tv(&ppconst->pp_tv[ppconst_used]);
3387 --ppconst->pp_used;
3388 }
3389 }
3390 else
3391 {
3392 generate_ppconst(cctx, ppconst);
3393 if (op_falsy)
3394 end_idx = instr->ga_len;
3395 generate_JUMP(cctx, op_falsy
3396 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3397 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003398 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003399 }
3400
3401 // evaluate the second expression; any type is accepted
3402 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3403 return FAIL;
3404 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3405 return FAIL;
3406
3407 if (!has_const_expr)
3408 {
3409 generate_ppconst(cctx, ppconst);
3410
3411 if (!op_falsy)
3412 {
3413 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003414 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003415 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003416
3417 end_idx = instr->ga_len;
3418 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3419
3420 // jump here from JUMP_IF_FALSE
3421 isn = ((isn_T *)instr->ga_data) + alt_idx;
3422 isn->isn_arg.jump.jump_where = instr->ga_len;
3423 }
3424 }
3425
3426 if (!op_falsy)
3427 {
3428 // Check for the ":".
3429 p = may_peek_next_line(cctx, *arg, &next);
3430 if (*p != ':')
3431 {
3432 emsg(_(e_missing_colon_after_questionmark));
3433 return FAIL;
3434 }
3435 if (next != NULL)
3436 {
3437 *arg = next_line_from_context(cctx, TRUE);
3438 p = skipwhite(*arg);
3439 }
3440
3441 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3442 {
3443 semsg(_(e_white_space_required_before_and_after_str_at_str),
3444 ":", p);
3445 return FAIL;
3446 }
3447
3448 // evaluate the third expression
3449 if (has_const_expr)
3450 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3451 ? SKIP_YES : SKIP_NOT;
3452 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3453 return FAIL;
3454 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3455 return FAIL;
3456 }
3457
3458 if (!has_const_expr)
3459 {
3460 type_T **typep;
3461
3462 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003463 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003464
3465 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003466 typep = &((((type2_T *)stack->ga_data)
3467 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003468 common_type(type1, *typep, typep, cctx->ctx_type_list);
3469
3470 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3471 isn = ((isn_T *)instr->ga_data) + end_idx;
3472 isn->isn_arg.jump.jump_where = instr->ga_len;
3473 }
3474
3475 cctx->ctx_skip = save_skip;
3476 }
3477 return OK;
3478}
3479
3480/*
3481 * Toplevel expression.
3482 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3483 * Returns OK or FAIL.
3484 */
3485 int
3486compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3487{
3488 ppconst_T ppconst;
3489
3490 CLEAR_FIELD(ppconst);
3491 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3492 {
3493 clear_ppconst(&ppconst);
3494 return FAIL;
3495 }
3496 if (is_const != NULL)
3497 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3498 if (generate_ppconst(cctx, &ppconst) == FAIL)
3499 return FAIL;
3500 return OK;
3501}
3502
3503/*
3504 * Toplevel expression.
3505 */
3506 int
3507compile_expr0(char_u **arg, cctx_T *cctx)
3508{
3509 return compile_expr0_ext(arg, cctx, NULL);
3510}
3511
3512
3513#endif // defined(FEAT_EVAL)