blob: 322b45aaefdcd0bceb4ac3d43679d81f9eb245a6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100136 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100138 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200253 * Evaluate a partial.
254 * Pass arguments "argv[argc]".
255 * "fc_arg" is from eval_expr_get_funccal() or NULL;
256 * Return the result in "rettv" and OK or FAIL.
257 */
258 static int
259eval_expr_partial(
260 typval_T *expr,
261 typval_T *argv,
262 int argc,
263 funccall_T *fc_arg,
264 typval_T *rettv)
265{
266 partial_T *partial = expr->vval.v_partial;
267
268 if (partial == NULL)
269 return FAIL;
270
271 if (partial->pt_func != NULL
272 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
273 {
274 funccall_T *fc = fc_arg != NULL ? fc_arg
275 : create_funccal(partial->pt_func, rettv);
276 int r;
277
278 if (fc == NULL)
279 return FAIL;
280
281 // Shortcut to call a compiled function with minimal overhead.
282 r = call_def_function(partial->pt_func, argc, argv, DEF_USE_PT_ARGV,
283 partial, NULL, fc, rettv);
284 if (fc_arg == NULL)
285 remove_funccal();
286 if (r == FAIL)
287 return FAIL;
288 }
289 else
290 {
291 char_u *s = partial_name(partial);
292 funcexe_T funcexe;
293
294 if (s == NULL || *s == NUL)
295 return FAIL;
296
297 CLEAR_FIELD(funcexe);
298 funcexe.fe_evaluate = TRUE;
299 funcexe.fe_partial = partial;
300 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
301 return FAIL;
302 }
303
304 return OK;
305}
306
307/*
308 * Evaluate an expression which is a function.
309 * Pass arguments "argv[argc]".
310 * Return the result in "rettv" and OK or FAIL.
311 */
312 static int
313eval_expr_func(
314 typval_T *expr,
315 typval_T *argv,
316 int argc,
317 typval_T *rettv)
318{
319 funcexe_T funcexe;
320 char_u buf[NUMBUFLEN];
321 char_u *s;
322
323 if (expr->v_type == VAR_FUNC)
324 s = expr->vval.v_string;
325 else
326 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
327 if (s == NULL || *s == NUL)
328 return FAIL;
329
330 CLEAR_FIELD(funcexe);
331 funcexe.fe_evaluate = TRUE;
332 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
333 return FAIL;
334
335 return OK;
336}
337
338/*
339 * Evaluate an expression, which is a string.
340 * Return the result in "rettv" and OK or FAIL.
341 */
342 static int
343eval_expr_string(
344 typval_T *expr,
345 typval_T *rettv)
346{
347 char_u *s;
348 char_u buf[NUMBUFLEN];
349
350 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
351 if (s == NULL)
352 return FAIL;
353
354 s = skipwhite(s);
355 if (eval1_emsg(&s, rettv, NULL) == FAIL)
356 return FAIL;
357
358 if (*skipwhite(s) != NUL) // check for trailing chars after expr
359 {
360 clear_tv(rettv);
361 semsg(_(e_invalid_expression_str), s);
362 return FAIL;
363 }
364
365 return OK;
366}
367
368/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 * Evaluate an expression, which can be a function, partial or string.
370 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200371 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100372 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 * Return the result in "rettv" and OK or FAIL.
374 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200375 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100376eval_expr_typval(
377 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200378 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100379 typval_T *argv,
380 int argc,
381 funccall_T *fc_arg,
382 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100383{
zeertzjqad0c4422023-08-17 22:15:47 +0200384 if (expr->v_type == VAR_PARTIAL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200385 return eval_expr_partial(expr, argv, argc, fc_arg, rettv);
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200386 else if (expr->v_type == VAR_INSTR)
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200387 return exe_typval_instr(expr, rettv);
zeertzjqad0c4422023-08-17 22:15:47 +0200388 else if (expr->v_type == VAR_FUNC || want_func)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200389 return eval_expr_func(expr, argv, argc, rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100390 else
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +0200391 return eval_expr_string(expr, rettv);
392
Bram Moolenaar48570482017-10-30 21:48:41 +0100393 return OK;
394}
395
396/*
397 * Like eval_to_bool() but using a typval_T instead of a string.
398 * Works for string, funcref and partial.
399 */
400 int
401eval_expr_to_bool(typval_T *expr, int *error)
402{
403 typval_T rettv;
404 int res;
405
zeertzjqad0c4422023-08-17 22:15:47 +0200406 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100407 {
408 *error = TRUE;
409 return FALSE;
410 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200411 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100412 clear_tv(&rettv);
413 return res;
414}
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416/*
417 * Top level evaluation function, returning a string. If "skip" is TRUE,
418 * only parsing to "nextcmd" is done, without reporting errors. Return
419 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
420 */
421 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100422eval_to_string_skip(
423 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200424 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100425 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200429 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
Bram Moolenaar37c83712020-06-30 21:18:36 +0200431 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 if (skip)
433 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200434 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 retval = NULL;
436 else
437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100438 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000439 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 }
441 if (skip)
442 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200443 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 return retval;
446}
447
448/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100449 * Initialize "evalarg" for use.
450 */
451 void
452init_evalarg(evalarg_T *evalarg)
453{
454 CLEAR_POINTER(evalarg);
455 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
456}
457
458/*
459 * If "evalarg->eval_tofree" is not NULL free it later.
460 * Caller is expected to overwrite "evalarg->eval_tofree" next.
461 */
462 static void
463free_eval_tofree_later(evalarg_T *evalarg)
464{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000465 if (evalarg->eval_tofree == NULL)
466 return;
467
468 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
469 ((char_u **)evalarg->eval_tofree_ga.ga_data)
470 [evalarg->eval_tofree_ga.ga_len++]
471 = evalarg->eval_tofree;
472 else
473 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100474}
475
476/*
477 * After using "evalarg" filled from "eap": free the memory.
478 */
479 void
480clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
481{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000482 if (evalarg == NULL)
483 return;
484
485 garray_T *etga = &evalarg->eval_tofree_ga;
486
487 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100488 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000489 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100490 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000491 // We may need to keep the original command line, e.g. for
492 // ":let" it has the variable names. But we may also need
493 // the new one, "nextcmd" points into it. Keep both.
494 vim_free(eap->cmdline_tofree);
495 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100496
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000497 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
498 {
499 // "nextcmd" points into the last line in eval_tofree_ga,
500 // need to keep it around.
501 --etga->ga_len;
502 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
503 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100504 }
505 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000506 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100507 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000508 else
509 vim_free(evalarg->eval_tofree);
510 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100511 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000512
513 ga_clear_strings(etga);
514 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100515}
516
517/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000518 * Skip over an expression at "*pp".
519 * Return FAIL for an error, OK otherwise.
520 */
521 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200522skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000523{
Bram Moolenaar33570922005-01-25 22:26:29 +0000524 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000525
526 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200527 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000528}
529
530/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200531 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200532 * If in Vim9 script and line breaks are encountered, the lines are
533 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 * "arg" is advanced to just after the expression.
535 * "start" is set to the start of the expression, "end" to just after the end.
536 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200537 * Return FAIL for an error, OK otherwise.
538 */
539 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200540skip_expr_concatenate(
541 char_u **arg,
542 char_u **start,
543 char_u **end,
544 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200545{
546 typval_T rettv;
547 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200548 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200549 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200550 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200551 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200552 int evaluate = evalarg == NULL
553 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200554
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200555 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200557 {
558 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200559 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200560 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200561 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200562 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200563 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200564 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200565
566 // Don't evaluate the expression.
567 if (evalarg != NULL)
568 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200569 *arg = skipwhite(*arg);
570 res = eval1(arg, &rettv, evalarg);
571 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200572 if (evalarg != NULL)
573 evalarg->eval_flags = save_flags;
574
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200575 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200576 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200577 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200578 if (evalarg->eval_ga.ga_len == 1)
579 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200580 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200581 ga_clear(gap);
582 gap->ga_itemsize = 0;
583 }
584 else
585 {
586 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200587 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200588
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200589 // Line breaks encountered, concatenate all the lines.
590 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200591 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200592
593 // free the lines only when using getsourceline()
594 if (evalarg->eval_cookie != NULL)
595 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200596 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200597 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200598 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100599 // later. Also free "eval_tofree" later if needed.
600 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200601 evalarg->eval_tofree =
602 ((char_u **)gap->ga_data)[gap->ga_len - 1];
603 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 ga_clear_strings(gap);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +0200605 ga_clear(freegap);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200606 }
607 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200608 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200609 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200610
611 // free lines that were explicitly marked for freeing
612 ga_clear_strings(freegap);
613 }
614
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200615 gap->ga_itemsize = 0;
616 if (p == NULL)
617 return FAIL;
618 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200619 vim_free(evalarg->eval_tofree_lambda);
620 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200621 // Compute "end" relative to the end.
622 *end = *start + STRLEN(*start) - endoff;
623 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200624 }
625
626 return res;
627}
628
629/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 * Convert "tv" to a string.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200631 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100632 * Returns an allocated string (NULL when out of memory).
633 */
634 char_u *
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200635typval2string(typval_T *tv, int join_list)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100636{
637 garray_T ga;
638 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100639
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200640 if (join_list && tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100641 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000642 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100643 if (tv->vval.v_list != NULL)
644 {
645 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
646 if (tv->vval.v_list->lv_len > 0)
647 ga_append(&ga, NL);
648 }
649 ga_append(&ga, NUL);
650 retval = (char_u *)ga.ga_data;
651 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200652 else if (tv->v_type == VAR_LIST || tv->v_type == VAR_DICT)
653 {
654 char_u *tofree;
655 char_u numbuf[NUMBUFLEN];
656
657 retval = tv2string(tv, &tofree, numbuf, 0);
658 // Make a copy if we have a value but it's not in allocated memory.
659 if (retval != NULL && tofree == NULL)
660 retval = vim_strsave(retval);
661 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100662 else
663 retval = vim_strsave(tv_get_string(tv));
664 return retval;
665}
666
667/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200668 * Top level evaluation function, returning a string. Does not handle line
669 * breaks.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200670 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 * Return pointer to allocated memory, or NULL for failure.
672 */
673 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100674eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100675 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200676 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100677 exarg_T *eap,
678 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
Bram Moolenaar33570922005-01-25 22:26:29 +0000680 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100682 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100683 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100685 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100686 if (use_simple_function)
687 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
688 else
689 r = eval0(arg, &tv, NULL, &evalarg);
690 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 retval = NULL;
692 else
693 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200694 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000695 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100697 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699 return retval;
700}
701
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100702 char_u *
703eval_to_string(
704 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200705 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100706 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100707{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200708 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100709}
710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000712 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100713 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200714 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 */
716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100717eval_to_string_safe(
718 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000719 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 int keep_script_version,
721 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200724 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200725 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200726 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaar9530b582022-01-22 13:39:08 +0000728 if (!keep_script_version)
729 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200730 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000731 if (use_sandbox)
732 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100733 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200734 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100735 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000736 if (use_sandbox)
737 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100738 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200739 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200740 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200741 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 return retval;
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
746 * Top level evaluation function, returning a number.
747 * Evaluates "expr" silently.
748 * Returns -1 for an error.
749 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200750 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100751eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200754 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000755 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100756 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 ++emsg_off;
759
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100760 if (use_simple_function)
761 r = may_call_simple_func(expr, &rettv);
762 if (r == NOTDONE)
763 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
764 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 retval = -1;
766 else
767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100768 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000769 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 --emsg_off;
772
773 return retval;
774}
775
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000776/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000777 * Top level evaluation function.
778 * Returns an allocated typval_T with the result.
779 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000780 */
781 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200782eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000783{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100784 return eval_expr_ext(arg, eap, FALSE);
785}
786
787 typval_T *
788eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
789{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000790 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200791 evalarg_T evalarg;
792
793 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000794
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200795 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100796 if (tv != NULL)
797 {
798 int r = NOTDONE;
799
800 if (use_simple_function)
801 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
802 if (r == NOTDONE)
803 r = eval0(arg, tv, eap, &evalarg);
804
805 if (r == FAIL)
806 VIM_CLEAR(tv);
807 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000808
Bram Moolenaar37c83712020-06-30 21:18:36 +0200809 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000810 return tv;
811}
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000814 * "*arg" points to what can be a function name in the form of "import.Name" or
815 * "Funcref". Return the name of the function. Set "tofree" to something that
816 * was allocated.
817 * If "verbose" is FALSE no errors are given.
818 * Return NULL for any failure.
819 */
820 static char_u *
821deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000822 char_u **arg,
823 char_u **tofree,
824 evalarg_T *evalarg,
825 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000826{
827 typval_T ref;
828 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100829 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000830
831 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100832 if (evalarg != NULL)
833 {
834 // need to evaluate this to get an import, like in "a.Func"
835 save_flags = evalarg->eval_flags;
836 evalarg->eval_flags |= EVAL_EVALUATE;
837 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100838 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100839 {
840 dictitem_T *v;
841
842 // If <SID>VarName was used it would not be found, try another way.
843 v = find_var_also_in_script(name, NULL, FALSE);
844 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100845 {
846 name = NULL;
847 goto theend;
848 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100849 copy_tv(&v->di_tv, &ref);
850 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000851 if (*skipwhite(*arg) != NUL)
852 {
853 if (verbose)
854 semsg(_(e_trailing_characters_str), *arg);
855 name = NULL;
856 }
857 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
858 {
859 name = ref.vval.v_string;
860 ref.vval.v_string = NULL;
861 *tofree = name;
862 }
863 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
864 {
865 if (ref.vval.v_partial->pt_argc > 0
866 || ref.vval.v_partial->pt_dict != NULL)
867 {
868 if (verbose)
869 emsg(_(e_cannot_use_partial_here));
870 name = NULL;
871 }
872 else
873 {
874 name = vim_strsave(partial_name(ref.vval.v_partial));
875 *tofree = name;
876 }
877 }
878 else
879 {
880 if (verbose)
881 semsg(_(e_not_callable_type_str), name);
882 name = NULL;
883 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100884
885theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000886 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100887 if (evalarg != NULL)
888 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000889 return name;
890}
891
892/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100893 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200894 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
895 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200898 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100899call_vim_function(
900 char_u *func,
901 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200902 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200903 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200906 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000907 char_u *arg;
908 char_u *name;
909 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000910 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200913 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000914 funcexe.fe_firstline = curwin->w_cursor.lnum;
915 funcexe.fe_lastline = curwin->w_cursor.lnum;
916 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000917
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000918 // The name might be "import.Func" or "Funcref". We don't know, we need to
919 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000920 // autoload script has errors. Guess that when there is a dot in the name
921 // showing errors is the right choice.
922 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000923 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000924 if (ignore_errors)
925 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000926 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000927 if (ignore_errors)
928 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000929 if (name == NULL)
930 name = func;
931
932 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
933
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000934 if (ret == FAIL)
935 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000936 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000937
938 return ret;
939}
940
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100941/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100942 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000943 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
944 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000945 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000946 */
947 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948call_func_retstr(
949 char_u *func,
950 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200951 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000952{
953 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000954 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000955
Bram Moolenaarded27a12018-08-01 19:06:03 +0200956 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000957 return NULL;
958
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100959 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000960 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return retval;
962}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000963
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000964/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100965 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000966 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000967 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100968 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000969 */
970 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971call_func_retlist(
972 char_u *func,
973 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200974 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000975{
976 typval_T rettv;
977
Bram Moolenaarded27a12018-08-01 19:06:03 +0200978 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000979 return NULL;
980
981 if (rettv.v_type != VAR_LIST)
982 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100983 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
984 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000985 clear_tv(&rettv);
986 return NULL;
987 }
988
989 return rettv.vval.v_list;
990}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaare70dd112022-01-21 16:31:11 +0000992#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100994 * Evaluate "arg", which is 'foldexpr'.
995 * Note: caller must set "curwin" to match "arg".
996 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
997 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 */
999 int
Bram Moolenaare70dd112022-01-21 16:31:11 +00001000eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
Bram Moolenaare70dd112022-01-21 16:31:11 +00001002 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001004 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +00001006 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001007 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001008 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001010 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001011 current_sctx = wp->w_p_script_ctx[WV_FDE];
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001014 if (use_sandbox)
1015 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001016 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001018
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001019 // Evaluate the expression. If the expression is "FuncName()" call the
1020 // function directly.
1021 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 retval = 0;
1023 else
1024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 if (tv.v_type == VAR_NUMBER)
1027 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001028 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 retval = 0;
1030 else
1031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 // If the result is a string, check if there is a non-digit before
1033 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001034 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +02001035 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 *cp = *s++;
1037 retval = atol((char *)s);
1038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001042 if (use_sandbox)
1043 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +01001044 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001045 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +00001046 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001048 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050#endif
1051
Ernie Rael64885642023-10-04 20:16:22 +02001052#ifdef LOG_LOCKVAR
1053typedef struct flag_string_S
1054{
1055 int flag;
1056 char *str;
1057} flag_string_T;
1058
1059 static char *
1060flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1061{
1062 char *p = buf;
1063 *p = NUL;
1064 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1065 {
1066 if ((fstring->flag & flags) != 0)
1067 {
1068 size_t len = STRLEN(fstring->str);
1069 if (n > p - buf + len + 7)
1070 {
1071 STRCAT(p, fstring->str);
1072 p += len;
1073 STRCAT(p, " ");
1074 ++p;
1075 }
1076 else
1077 {
1078 STRCAT(buf, "...");
1079 break;
1080 }
1081 }
1082 }
1083 return buf;
1084}
1085
1086flag_string_T glv_flag_strings[] = {
1087 { GLV_QUIET, "QUIET" },
1088 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1089 { GLV_READ_ONLY, "READ_ONLY" },
1090 { GLV_NO_DECL, "NO_DECL" },
1091 { GLV_COMPILING, "COMPILING" },
1092 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1093 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1094 { 0, NULL }
1095};
1096#endif
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
Ernie Raelee865f32023-09-29 19:53:55 +02001099 * Fill in "lp" using "root". This is used in a special case when
1100 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1101 *
1102 * This is typically called with "lval_root" as "root". For a class, find
1103 * the name from lp in the class from root, fill in lval_T if found. For a
1104 * complex type, list/dict use it as the result; just put the root into
1105 * ll_tv.
1106 *
1107 * "lval_root" is a hack used during run-time/instr-execution to provide the
1108 * starting point for "get_lval()" to traverse a chain of indexes. In some
1109 * cases get_lval sees a bare name and uses this function to populate the
1110 * lval_T.
1111 *
1112 * For setting up "lval_root" (currently only used with lockvar)
1113 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1114 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1115 */
1116 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001117fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001118{
1119#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001120 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1121 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001122#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001123 if (lr->lr_tv == NULL)
1124 return;
Ernie Rael64885642023-10-04 20:16:22 +02001125 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001126 {
Ernie Rael64885642023-10-04 20:16:22 +02001127 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001128 {
1129 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001130 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001131 int m_idx;
1132 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1133 lp->ll_name_end - lp->ll_name, &m_idx);
1134 if (m != NULL)
1135 {
1136 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001137 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001138 lp->ll_oi = m_idx;
1139 lp->ll_valtype = m->ocm_type;
1140 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1141#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001142 ch_log(NULL, "LKVAR: ... class member %s.%s",
1143 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001144#endif
1145 return;
1146 }
1147 }
1148 }
1149
1150#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001151 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001152#endif
Ernie Rael64885642023-10-04 20:16:22 +02001153 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001154 lp->ll_is_root = TRUE;
1155}
1156
1157/*
Ernie Rael64885642023-10-04 20:16:22 +02001158 * Check if the class has permission to access the member.
1159 * Returns OK or FAIL.
1160 */
1161 static int
1162get_lval_check_access(
1163 class_T *cl_exec, // executing class, NULL if :def or script level
1164 class_T *cl, // class which contains the member
1165 ocmember_T *om, // member being accessed
1166 char_u *p, // char after member name
1167 int flags) // GLV flags to check if writing to lval
1168{
1169#ifdef LOG_LOCKVAR
1170 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1171 (void*)cl_exec, (void*)cl, *p);
1172#endif
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001173 if (cl_exec != NULL && cl_exec == cl)
1174 return OK;
1175
1176 char *msg = NULL;
1177 switch (om->ocm_access)
Ernie Rael64885642023-10-04 20:16:22 +02001178 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001179 case VIM_ACCESS_PRIVATE:
1180 msg = e_cannot_access_protected_variable_str;
1181 break;
1182 case VIM_ACCESS_READ:
1183 // If [idx] or .key following, read only OK.
1184 if (*p == '[' || *p == '.')
Ernie Raele6c9aa52023-10-06 19:55:52 +02001185 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001186 if ((flags & GLV_READ_ONLY) == 0)
1187 {
1188 if (IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001189 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001190 if (om->ocm_type->tt_type == VAR_OBJECT)
1191 semsg(_(e_enumvalue_str_cannot_be_modified),
1192 cl->class_name, om->ocm_name);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001193 else
1194 msg = e_variable_is_not_writable_str;
1195 }
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001196 else
1197 msg = e_variable_is_not_writable_str;
1198 }
1199 break;
1200 case VIM_ACCESS_ALL:
1201 break;
1202 }
1203 if (msg != NULL)
1204 {
1205 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1206 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02001207 }
1208 return OK;
1209}
1210
1211/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001212 * Get lval information for a variable imported from script "imp_sid". On
1213 * success, updates "lp" with the variable name, type, script ID and typval.
1214 * The variable name starts at or after "p".
1215 * If "rettv" is not NULL it points to the value to be assigned. This used to
1216 * match the rhs and lhs types.
1217 * Returns a pointer to the character after the variable name if the imported
1218 * variable is valid and writable.
1219 * Returns NULL if the variable is not exported or typval is not found or the
1220 * rhs type doesn't match the lhs type or the variable is not writable.
1221 */
1222 static char_u *
1223get_lval_imported(
1224 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001225 scid_T imp_sid,
1226 char_u *p,
1227 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001228 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001229{
1230 ufunc_T *ufunc;
1231 type_T *type = NULL;
1232 int cc;
1233 int rc = FAIL;
1234
1235 p = skipwhite(p);
1236
1237 import_check_sourced_sid(&imp_sid);
1238 lp->ll_sid = imp_sid;
1239 lp->ll_name = p;
1240 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1241 lp->ll_name_end = p;
1242
1243 // check the item is exported
1244 cc = *p;
1245 *p = NUL;
1246 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1247 TRUE) == -1)
1248 goto failed;
1249
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001250 // Get the typval for the exported item
1251 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1252 if (ht == NULL)
1253 goto failed;
1254
1255 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1256 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001257 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001258 goto success;
1259
1260 *dip = di;
1261
1262 // Check whether the variable is writable.
1263 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1264 if (sv != NULL && sv->sv_const != 0)
1265 {
1266 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1267 goto failed;
1268 }
1269
1270 // check whether variable is locked
1271 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1272 goto failed;
1273
1274 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001275 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001276
1277success:
1278 rc = OK;
1279
1280failed:
1281 *p = cc;
1282 return rc == OK ? p : NULL;
1283}
1284
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02001285typedef enum {
1286 GLV_FAIL,
1287 GLV_OK,
1288 GLV_STOP
1289} glv_status_T;
1290
1291/*
1292 * Get an Dict lval variable that can be assigned a value to: "name",
1293 * "name[expr]", "name[expr][expr]", "name.key", "name.key[expr]" etc.
1294 * "name" points to the start of the name.
1295 * If "rettv" is not NULL it points to the value to be assigned.
1296 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1297 * wrong; must end in space or cmd separator.
1298 *
1299 * flags:
1300 * GLV_QUIET: do not give error messages
1301 * GLV_READ_ONLY: will not change the variable
1302 * GLV_NO_AUTOLOAD: do not use script autoloading
1303 *
1304 * The Dict is returned in 'lp'. Returns GLV_OK on success and GLV_FAIL on
1305 * failure. Returns GLV_STOP to stop processing the characters following
1306 * 'key_end'.
1307 */
1308 static int
1309get_lval_dict_item(
1310 char_u *name,
1311 lval_T *lp,
1312 char_u *key,
1313 int len,
1314 char_u **key_end,
1315 typval_T *var1,
1316 int flags,
1317 int unlet,
1318 typval_T *rettv)
1319{
1320 int quiet = flags & GLV_QUIET;
1321 char_u *p = *key_end;
1322
1323 if (len == -1)
1324 {
1325 // "[key]": get key from "var1"
1326 key = tv_get_string_chk(var1); // is number or string
1327 if (key == NULL)
1328 {
1329 clear_tv(var1);
1330 return GLV_FAIL;
1331 }
1332 }
1333 lp->ll_list = NULL;
1334 lp->ll_object = NULL;
1335 lp->ll_class = NULL;
1336
1337 // a NULL dict is equivalent with an empty dict
1338 if (lp->ll_tv->vval.v_dict == NULL)
1339 {
1340 lp->ll_tv->vval.v_dict = dict_alloc();
1341 if (lp->ll_tv->vval.v_dict == NULL)
1342 {
1343 clear_tv(var1);
1344 return GLV_FAIL;
1345 }
1346 ++lp->ll_tv->vval.v_dict->dv_refcount;
1347 }
1348 lp->ll_dict = lp->ll_tv->vval.v_dict;
1349
1350 lp->ll_di = dict_find(lp->ll_dict, key, len);
1351
1352 // When assigning to a scope dictionary check that a function and
1353 // variable name is valid (only variable name unless it is l: or
1354 // g: dictionary). Disallow overwriting a builtin function.
1355 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
1356 {
1357 int prevval;
1358
1359 if (len != -1)
1360 {
1361 prevval = key[len];
1362 key[len] = NUL;
1363 }
1364 else
1365 prevval = 0; // avoid compiler warning
1366 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1367 && (rettv->v_type == VAR_FUNC
1368 || rettv->v_type == VAR_PARTIAL)
1369 && var_wrong_func_name(key, lp->ll_di == NULL))
1370 || !valid_varname(key, -1, TRUE);
1371 if (len != -1)
1372 key[len] = prevval;
1373 if (wrong)
1374 {
1375 clear_tv(var1);
1376 return GLV_FAIL;
1377 }
1378 }
1379
1380 if (lp->ll_valtype != NULL)
1381 // use the type of the member
1382 lp->ll_valtype = lp->ll_valtype->tt_member;
1383
1384 if (lp->ll_di == NULL)
1385 {
1386 // Can't add "v:" or "a:" variable.
1387 if (lp->ll_dict == get_vimvar_dict()
1388 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
1389 {
1390 semsg(_(e_illegal_variable_name_str), name);
1391 clear_tv(var1);
1392 return GLV_FAIL;
1393 }
1394
1395 // Key does not exist in dict: may need to add it.
1396 if (*p == '[' || *p == '.' || unlet)
1397 {
1398 if (!quiet)
1399 semsg(_(e_key_not_present_in_dictionary_str), key);
1400 clear_tv(var1);
1401 return GLV_FAIL;
1402 }
1403 if (len == -1)
1404 lp->ll_newkey = vim_strsave(key);
1405 else
1406 lp->ll_newkey = vim_strnsave(key, len);
1407 clear_tv(var1);
1408 if (lp->ll_newkey == NULL)
1409 p = NULL;
1410
1411 *key_end = p;
1412 return GLV_STOP;
1413 }
1414 // existing variable, need to check if it can be changed
1415 else if ((flags & GLV_READ_ONLY) == 0
1416 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1417 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
1418 {
1419 clear_tv(var1);
1420 return GLV_FAIL;
1421 }
1422
1423 clear_tv(var1);
1424 lp->ll_tv = &lp->ll_di->di_tv;
1425
1426 return GLV_OK;
1427}
1428
1429/*
1430 * Get an blob lval variable that can be assigned a value to: "name",
1431 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1432 *
1433 * 'var1' specifies the starting blob index and 'var2' specifies the ending
1434 * blob index. If the first index is not specified in a range, then 'empty1'
1435 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1436 * invalid indexes.
1437 *
1438 * The blob is returned in 'lp'. Returns OK on success and FAIL on failure.
1439 */
1440 static int
1441get_lval_blob(
1442 lval_T *lp,
1443 typval_T *var1,
1444 typval_T *var2,
1445 int empty1,
1446 int quiet)
1447{
1448 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1449
1450 // Get the number and item for the only or first index of the List.
1451 if (empty1)
1452 lp->ll_n1 = 0;
1453 else
1454 // is number or string
1455 lp->ll_n1 = (long)tv_get_number(var1);
1456 clear_tv(var1);
1457
1458 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
1459 {
1460 clear_tv(var2);
1461 return FAIL;
1462 }
1463 if (lp->ll_range && !lp->ll_empty2)
1464 {
1465 lp->ll_n2 = (long)tv_get_number(var2);
1466 clear_tv(var2);
1467 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL)
1468 return FAIL;
1469 }
1470
1471 if (!lp->ll_range)
1472 // Indexing a single byte in a blob. So the rhs type is a
1473 // number.
1474 lp->ll_valtype = &t_number;
1475
1476 lp->ll_blob = lp->ll_tv->vval.v_blob;
1477 lp->ll_tv = NULL;
1478
1479 return OK;
1480}
1481
1482/*
1483 * Get a List lval variable that can be assigned a value to: "name",
1484 * "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", etc.
1485 *
1486 * 'var1' specifies the starting List index and 'var2' specifies the ending
1487 * List index. If the first index is not specified in a range, then 'empty1'
1488 * is TRUE. If 'quiet' is TRUE, then error messages are not displayed for
1489 * invalid indexes.
1490 *
1491 * The List is returned in 'lp'. Returns OK on success and FAIL on failure.
1492 */
1493 static int
1494get_lval_list(
1495 lval_T *lp,
1496 typval_T *var1,
1497 typval_T *var2,
1498 int empty1,
1499 int flags,
1500 int quiet)
1501{
1502 /*
1503 * Get the number and item for the only or first index of the List.
1504 */
1505 if (empty1)
1506 lp->ll_n1 = 0;
1507 else
1508 // is number or string
1509 lp->ll_n1 = (long)tv_get_number(var1);
1510 clear_tv(var1);
1511
1512 lp->ll_dict = NULL;
1513 lp->ll_object = NULL;
1514 lp->ll_class = NULL;
1515 lp->ll_list = lp->ll_tv->vval.v_list;
1516 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1517 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
1518 if (lp->ll_li == NULL)
1519 {
1520 clear_tv(var2);
1521 return FAIL;
1522 }
1523
1524 if (lp->ll_valtype != NULL && !lp->ll_range)
1525 // use the type of the member
1526 lp->ll_valtype = lp->ll_valtype->tt_member;
1527
1528 /*
1529 * May need to find the item or absolute index for the second
1530 * index of a range.
1531 * When no index given: "lp->ll_empty2" is TRUE.
1532 * Otherwise "lp->ll_n2" is set to the second index.
1533 */
1534 if (lp->ll_range && !lp->ll_empty2)
1535 {
1536 lp->ll_n2 = (long)tv_get_number(var2);
1537 // is number or string
1538 clear_tv(var2);
1539 if (check_range_index_two(lp->ll_list,
1540 &lp->ll_n1, lp->ll_li, &lp->ll_n2, quiet) == FAIL)
1541 return FAIL;
1542 }
1543
1544 lp->ll_tv = &lp->ll_li->li_tv;
1545
1546 return OK;
1547}
1548
1549/*
1550 * Get a Class or Object lval variable that can be assigned a value to:
1551 * "name", "name.key", "name.key[expr]" etc.
1552 *
1553 * 'cl_exec' is the class that is executing, or NULL. 'v_type' is VAR_CLASS or
1554 * VAR_OBJECT. 'key' points to the member variable name and 'key_end' points
1555 * to the character after 'key'. If 'quiet' is TRUE, then error messages
1556 * are not displayed for invalid indexes.
1557 *
1558 * The Class or Object is returned in 'lp'. Returns OK on success and FAIL on
1559 * failure.
1560 */
1561 static int
1562get_lval_class_or_obj(
1563 class_T *cl_exec,
1564 vartype_T v_type,
1565 lval_T *lp,
1566 char_u *key,
1567 char_u *key_end,
1568 int flags,
1569 int quiet)
1570{
1571 lp->ll_dict = NULL;
1572 lp->ll_list = NULL;
1573
1574 class_T *cl;
1575 if (v_type == VAR_OBJECT)
1576 {
1577 if (lp->ll_tv->vval.v_object == NULL)
1578 {
1579 if (!quiet)
1580 emsg(_(e_using_null_object));
1581 return FAIL;
1582 }
1583 cl = lp->ll_tv->vval.v_object->obj_class;
1584 lp->ll_object = lp->ll_tv->vval.v_object;
1585 }
1586 else
1587 {
1588 cl = lp->ll_tv->vval.v_class;
1589 lp->ll_object = NULL;
1590 }
1591 lp->ll_class = cl;
1592
1593 // TODO: what if class is NULL?
1594 if (cl != NULL)
1595 {
1596 lp->ll_valtype = NULL;
1597
1598 if (flags & GLV_PREFER_FUNC)
1599 {
1600 // First look for a function with this name.
1601 // round 1: class functions (skipped for an object)
1602 // round 2: object methods
1603 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1604 round <= 2; ++round)
1605 {
1606 int m_idx;
1607 ufunc_T *fp;
1608
1609 fp = method_lookup(cl,
1610 round == 1 ? VAR_CLASS : VAR_OBJECT,
1611 key, key_end - key, &m_idx);
1612 lp->ll_oi = m_idx;
1613 if (fp != NULL)
1614 {
1615 lp->ll_ufunc = fp;
1616 lp->ll_valtype = fp->uf_func_type;
1617 break;
1618 }
1619 }
1620 }
1621
1622 if (lp->ll_valtype == NULL)
1623 {
1624 int m_idx;
1625 ocmember_T *om
1626 = member_lookup(cl, v_type, key, key_end - key, &m_idx);
1627 lp->ll_oi = m_idx;
1628 if (om != NULL)
1629 {
1630 if (get_lval_check_access(cl_exec, cl, om,
1631 key_end, flags) == FAIL)
1632 return FAIL;
1633
1634 // When lhs is used to modify the variable, check it is
1635 // not a read-only variable.
1636 if ((flags & GLV_READ_ONLY) == 0
1637 && (*key_end != '.' && *key_end != '[')
1638 && oc_var_check_ro(cl, om))
1639 return FAIL;
1640
1641 lp->ll_valtype = om->ocm_type;
1642
1643 if (v_type == VAR_OBJECT)
1644 lp->ll_tv = ((typval_T *)(
1645 lp->ll_tv->vval.v_object + 1)) + m_idx;
1646 else
1647 lp->ll_tv = &cl->class_members_tv[m_idx];
1648 }
1649 }
1650
1651 if (lp->ll_valtype == NULL)
1652 {
1653 member_not_found_msg(cl, v_type, key, key_end - key);
1654 return FAIL;
1655 }
1656 }
1657
1658 return OK;
1659}
1660
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001661/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001662 * Get an lval: variable, Dict item or List item that can be assigned a value
1663 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1664 * "name.key", "name.key[expr]" etc.
1665 * Indexing only works if "name" is an existing List or Dictionary.
1666 * "name" points to the start of the name.
1667 * If "rettv" is not NULL it points to the value to be assigned.
1668 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1669 * wrong; must end in space or cmd separator.
1670 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001671 * flags:
1672 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001673 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001674 * GLV_NO_AUTOLOAD: do not use script autoloading
1675 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001676 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001677 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001678 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001681get_lval(
1682 char_u *name,
1683 typval_T *rettv,
1684 lval_T *lp,
1685 int unlet,
1686 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001687 int flags, // GLV_ values
1688 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 char_u *expr_start, *expr_end;
1692 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001693 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 typval_T var1;
1695 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001696 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001698 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001699 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001700 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001701 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001702 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001703 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704
Ernie Raelee865f32023-09-29 19:53:55 +02001705#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001706 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001707 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001708 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001709 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1710 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001711 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001712 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001713 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001714#endif
1715
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001716 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001717 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001718
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001719 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001720 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001721 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001722 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001723 lp->ll_name_end = find_name_end(name, NULL, NULL,
1724 FNE_INCL_BR | fne_flags);
1725 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001726 }
1727
Bram Moolenaara749a422022-02-12 19:52:25 +00001728 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001729 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001730 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1731 {
1732 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1733 return NULL;
1734 }
1735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001736 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001737 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001739 if (expr_start != NULL)
1740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001741 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001742 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001743 && *p != '[' && *p != '.')
1744 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001745 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001746 return NULL;
1747 }
1748
1749 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1750 if (lp->ll_exp_name == NULL)
1751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001752 // Report an invalid expression in braces, unless the
1753 // expression evaluation has been cancelled due to an
1754 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001755 if (!aborting() && !quiet)
1756 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001757 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001758 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001759 return NULL;
1760 }
1761 }
1762 lp->ll_name = lp->ll_exp_name;
1763 }
1764 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001766 lp->ll_name = name;
1767
Bram Moolenaar4525a572022-02-13 11:57:33 +00001768 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001770 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001771 // However, "g:[key]" is indexing a dictionary.
1772 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001773 {
1774 --p;
1775 lp->ll_name_end = p;
1776 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001777 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001778 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001779 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001780
Bram Moolenaar9510d222022-09-11 15:14:05 +01001781 if (is_scoped_variable(name))
1782 {
1783 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1784 return NULL;
1785 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001786 if (VIM_ISWHITE(*p))
1787 {
1788 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1789 return NULL;
1790 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001791 if (tp == p + 1 && !quiet)
1792 {
1793 semsg(_(e_white_space_required_after_str_str), ":", p);
1794 return NULL;
1795 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001796 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001797 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001798 semsg(_(e_using_type_not_in_script_context_str), p);
1799 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001800 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001801 if (vim9script && (flags & GLV_NO_DECL) &&
1802 !(flags & GLV_FOR_LOOP))
1803 {
1804 // Using a type and not in a "var" declaration.
1805 semsg(_(e_trailing_characters_str), p);
1806 return NULL;
1807 }
1808
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001809
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001810 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001811 lp->ll_type = parse_type(&tp,
1812 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1813 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001814 if (lp->ll_type == NULL && !quiet)
1815 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001816 lp->ll_name_end = tp;
1817 }
Ernie Raelee865f32023-09-29 19:53:55 +02001818 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001821 if (lp->ll_name == NULL)
1822 return p;
1823
Bram Moolenaar62aec932022-01-29 21:45:34 +00001824 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001825 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001826 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001827 if (import != NULL)
1828 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001829 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001830 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001831 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001832 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001833 }
1834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835
Ernie Rael0f058d12023-10-14 11:25:04 +02001836 // Without [idx] or .key we are done.
1837 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001838 {
1839 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001840 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001842 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843
Ernie Rael0f058d12023-10-14 11:25:04 +02001844 if (vim9script && lval_root != NULL)
1845 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001846 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001847 {
1848 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001849 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001850 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001851 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001852 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001853 {
1854 cc = *p;
1855 *p = NUL;
1856 // When we would write to the variable pass &ht and prevent autoload.
1857 writing = !(flags & GLV_READ_ONLY);
1858 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001859 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001860 if (v == NULL && !quiet)
1861 semsg(_(e_undefined_variable_str), lp->ll_name);
1862 *p = cc;
1863 if (v == NULL)
1864 return NULL;
1865 lp->ll_tv = &v->di_tv;
1866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867
Bram Moolenaar4525a572022-02-13 11:57:33 +00001868 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001869 {
1870 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001871 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001872 return NULL;
1873 }
1874
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 /*
1876 * Loop until no more [idx] or .key is following.
1877 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001878 var1.v_type = VAR_UNKNOWN;
1879 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001880 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001882 vartype_T v_type = lp->ll_tv->v_type;
1883
1884 if (*p == '.' && v_type != VAR_DICT
1885 && v_type != VAR_OBJECT
1886 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001887 {
1888 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001889 semsg(_(e_dot_not_allowed_after_str_str),
1890 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001891 return NULL;
1892 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001893 if (v_type != VAR_LIST
1894 && v_type != VAR_DICT
1895 && v_type != VAR_BLOB
1896 && v_type != VAR_OBJECT
1897 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001900 semsg(_(e_index_not_allowed_after_str_str),
1901 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001904
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001905 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001906 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001907 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001908 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001909 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001910 r = rettv_blob_alloc(lp->ll_tv);
1911 if (r == FAIL)
1912 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001917 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001920#ifdef LOG_LOCKVAR
1921 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1922 vartype_name(v_type));
1923#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001924
Bram Moolenaar4525a572022-02-13 11:57:33 +00001925 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001926 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001927 && lp->ll_tv == &v->di_tv
1928 && ht != NULL && ht == get_script_local_ht())
1929 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001930 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001931
1932 // Vim9 script local variable: get the type
1933 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001934 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001935 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001936#ifdef LOG_LOCKVAR
1937 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1938 vartype_name(lp->ll_valtype->tt_type));
1939#endif
1940 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001941 }
1942
Bram Moolenaar8c711452005-01-14 21:53:12 +00001943 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001944 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001945 {
1946 key = p + 1;
1947 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1948 ;
1949 if (len == 0)
1950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001952 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001954 }
1955 p = key + len;
1956 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001957 else
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001960 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 if (*p == ':')
1962 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001963 else
1964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001966 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001968 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001971 clear_tv(&var1);
1972 return NULL;
1973 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001974 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001975 }
1976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 if (*p == ':')
1979 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001980 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001983 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001984 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001986 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 if (rettv != NULL
1988 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001989 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001990 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001991 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001994 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001995 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001997 }
1998 p = skipwhite(p + 1);
1999 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 else
2002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002004 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002005 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002007 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002010 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002013 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002014 clear_tv(&var2);
2015 return NULL;
2016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002020 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002022
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00002026 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002027 clear_tv(&var1);
2028 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 }
2031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 ++p;
2034 }
Ernie Rael4c8da022023-10-11 21:35:11 +02002035#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02002036 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02002037 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
2038 empty1 ? ":" : (char*)tv_get_string(&var1));
2039 else
2040 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02002041#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042
Bram Moolenaard505d172022-12-18 21:42:55 +00002043 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002045 glv_status_T glv_status;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002046
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002047 glv_status = get_lval_dict_item(name, lp, key, len, &p, &var1,
2048 flags, unlet, rettv);
2049 if (glv_status == GLV_FAIL)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002050 return NULL;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002051 if (glv_status == GLV_STOP)
2052 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002054 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002055 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002056 if (get_lval_blob(lp, &var1, &var2, empty1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002057 return NULL;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002058
Bram Moolenaar61be3762019-03-19 23:04:17 +01002059 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002060 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002061 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002062 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002063 if (get_lval_list(lp, &var1, &var2, empty1, flags, quiet) == FAIL)
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002064 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002065 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002066 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002067 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002068 if (get_lval_class_or_obj(cl_exec, v_type, lp, key, p, flags,
2069 quiet) == FAIL)
2070 return FAIL;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
2073
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02002074 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
2075 {
2076 where_T where = WHERE_INIT;
2077
2078 // In a vim9 script, do type check and make sure the variable is
2079 // writable.
2080 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
2081 return NULL;
2082 }
2083
2084
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002085 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 return p;
2088}
2089
2090/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002094clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095{
2096 vim_free(lp->ll_exp_name);
2097 vim_free(lp->ll_newkey);
2098}
2099
2100/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002101 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002103 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2104 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002107set_var_lval(
2108 lval_T *lp,
2109 char_u *endp,
2110 typval_T *rettv,
2111 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002112 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
2113 char_u *op,
2114 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115{
2116 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118
2119 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002121 cc = *endp;
2122 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01002123 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02002124 return;
2125
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002126 if (lp->ll_blob != NULL)
2127 {
2128 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002129
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002130 if (op != NULL && *op != '=')
2131 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002132 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002133 return;
2134 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002135 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02002136 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002137
2138 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2139 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002140 if (lp->ll_empty2)
2141 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2142
Bram Moolenaar68452172021-04-12 21:21:02 +02002143 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
2144 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002145 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002146 }
2147 else
2148 {
2149 val = (int)tv_get_number_chk(rettv, &error);
2150 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02002151 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002152 }
2153 }
2154 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002156 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002158 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2159 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002160 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002161 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002162 *endp = cc;
2163 return;
2164 }
2165
Bram Moolenaarff697e62019-02-12 22:28:33 +01002166 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002167 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002168 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002169 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01002170 {
Ernie Raele75fde62023-12-21 17:18:54 +01002171 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002172 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002173 clear_tv(&tv);
2174 return;
2175 }
2176
Bram Moolenaar79518e22017-02-17 16:31:35 +01002177 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002178 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2179 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002180 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002181 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002182 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002183 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002186 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002187 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002188 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2189 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002190 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002191 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002192 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002193 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002194 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002196 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002197 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002198 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002199 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 else if (lp->ll_range)
2201 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002202 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2203 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002204 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002205 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002206 return;
2207 }
2208
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002209 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2210 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 }
2212 else
2213 {
2214 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002215 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002217 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2218 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002219 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002220 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002221 return;
2222 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002223
2224 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002225 && check_typval_arg_type(lp->ll_valtype, rettv,
2226 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002227 return;
2228
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 if (lp->ll_newkey != NULL)
2230 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002231 if (op != NULL && *op != '=')
2232 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002233 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 return;
2235 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002236 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2237 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002238 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002240 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002241 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 if (di == NULL)
2243 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002244 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2245 {
2246 vim_free(di);
2247 return;
2248 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002250 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251 else if (op != NULL && *op != '=')
2252 {
2253 tv_op(lp->ll_tv, rettv, op);
2254 return;
2255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 /*
2260 * Assign the value to the variable or list item.
2261 */
2262 if (copy)
2263 copy_tv(rettv, lp->ll_tv);
2264 else
2265 {
2266 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002267 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271}
2272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002273/*
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002274 * Handle "blob1 += blob2".
2275 * Returns OK or FAIL.
2276 */
2277 static int
2278tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
2279{
2280 if (*op != '+' || tv2->v_type != VAR_BLOB)
2281 return FAIL;
2282
2283 // Blob += Blob
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002284 if (tv2->vval.v_blob == NULL)
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002285 return OK;
2286
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002287 if (tv1->vval.v_blob == NULL)
2288 {
2289 tv1->vval.v_blob = tv2->vval.v_blob;
2290 ++tv1->vval.v_blob->bv_refcount;
2291 return OK;
2292 }
2293
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002294 blob_T *b1 = tv1->vval.v_blob;
2295 blob_T *b2 = tv2->vval.v_blob;
2296 int len = blob_len(b2);
2297
2298 for (int i = 0; i < len; i++)
2299 ga_append(&b1->bv_ga, blob_get(b2, i));
2300
2301 return OK;
2302}
2303
2304/*
2305 * Handle "list1 += list2".
2306 * Returns OK or FAIL.
2307 */
2308 static int
2309tv_op_list(typval_T *tv1, typval_T *tv2, char_u *op)
2310{
2311 if (*op != '+' || tv2->v_type != VAR_LIST)
2312 return FAIL;
2313
2314 // List += List
2315 if (tv2->vval.v_list == NULL)
2316 return OK;
2317
2318 if (tv1->vval.v_list == NULL)
2319 {
2320 tv1->vval.v_list = tv2->vval.v_list;
2321 ++tv1->vval.v_list->lv_refcount;
2322 }
2323 else
2324 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2325
2326 return OK;
2327}
2328
2329/*
2330 * Handle number operations:
2331 * nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
2332 *
2333 * Returns OK or FAIL.
2334 */
2335 static int
2336tv_op_number(typval_T *tv1, typval_T *tv2, char_u *op)
2337{
2338 varnumber_T n;
2339 int failed = FALSE;
2340
2341 n = tv_get_number(tv1);
2342 if (tv2->v_type == VAR_FLOAT)
2343 {
2344 float_T f = n;
2345
2346 if (*op == '%')
2347 return FAIL;
2348 switch (*op)
2349 {
2350 case '+': f += tv2->vval.v_float; break;
2351 case '-': f -= tv2->vval.v_float; break;
2352 case '*': f *= tv2->vval.v_float; break;
2353 case '/': f /= tv2->vval.v_float; break;
2354 }
2355 clear_tv(tv1);
2356 tv1->v_type = VAR_FLOAT;
2357 tv1->vval.v_float = f;
2358 }
2359 else
2360 {
2361 switch (*op)
2362 {
2363 case '+': n += tv_get_number(tv2); break;
2364 case '-': n -= tv_get_number(tv2); break;
2365 case '*': n *= tv_get_number(tv2); break;
2366 case '/': n = num_divide(n, tv_get_number(tv2), &failed); break;
2367 case '%': n = num_modulus(n, tv_get_number(tv2), &failed); break;
2368 }
2369 clear_tv(tv1);
2370 tv1->v_type = VAR_NUMBER;
2371 tv1->vval.v_number = n;
2372 }
2373
2374 return failed ? FAIL : OK;
2375}
2376
2377/*
2378 * Handle "str1 .= str2"
2379 * Returns OK or FAIL.
2380 */
2381 static int
2382tv_op_string(typval_T *tv1, typval_T *tv2, char_u *op UNUSED)
2383{
2384 char_u numbuf[NUMBUFLEN];
2385 char_u *s;
2386
2387 if (tv2->v_type == VAR_FLOAT)
2388 return FAIL;
2389
2390 // str .= str
2391 s = tv_get_string(tv1);
2392 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
2393 clear_tv(tv1);
2394 tv1->v_type = VAR_STRING;
2395 tv1->vval.v_string = s;
2396
2397 return OK;
2398}
2399
2400/*
2401 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2402 * and "tv1 .= tv2"
2403 * Returns OK or FAIL.
2404 */
2405 static int
2406tv_op_nr_or_string(typval_T *tv1, typval_T *tv2, char_u *op)
2407{
2408 if (tv2->v_type == VAR_LIST)
2409 return FAIL;
2410
2411 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
2412 return tv_op_number(tv1, tv2, op);
2413
2414 return tv_op_string(tv1, tv2, op);
2415}
2416
2417/*
2418 * Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
2419 * Returns OK or FAIL.
2420 */
2421 static int
2422tv_op_float(typval_T *tv1, typval_T *tv2, char_u *op)
2423{
2424 float_T f;
2425
2426 if (*op == '%' || *op == '.'
2427 || (tv2->v_type != VAR_FLOAT
2428 && tv2->v_type != VAR_NUMBER
2429 && tv2->v_type != VAR_STRING))
2430 return FAIL;
2431
2432 if (tv2->v_type == VAR_FLOAT)
2433 f = tv2->vval.v_float;
2434 else
2435 f = tv_get_number(tv2);
2436 switch (*op)
2437 {
2438 case '+': tv1->vval.v_float += f; break;
2439 case '-': tv1->vval.v_float -= f; break;
2440 case '*': tv1->vval.v_float *= f; break;
2441 case '/': tv1->vval.v_float /= f; break;
2442 }
2443
2444 return OK;
2445}
2446
2447/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002448 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2449 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 * Returns OK or FAIL.
2451 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454{
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002455 // Can't do anything with a Funcref or Dict on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002456 // v:true and friends only work with "..=".
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002457 if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
2458 || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL)
2459 && *op != '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002461 semsg(_(e_wrong_variable_type_for_str_equal), op);
2462 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 }
2464
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002465 int retval = FAIL;
2466 switch (tv1->v_type)
2467 {
2468 case VAR_UNKNOWN:
2469 case VAR_ANY:
2470 case VAR_VOID:
2471 case VAR_DICT:
2472 case VAR_FUNC:
2473 case VAR_PARTIAL:
2474 case VAR_BOOL:
2475 case VAR_SPECIAL:
2476 case VAR_JOB:
2477 case VAR_CHANNEL:
2478 case VAR_INSTR:
2479 case VAR_OBJECT:
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002480 case VAR_CLASS:
2481 case VAR_TYPEALIAS:
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02002482 break;
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002483
2484 case VAR_BLOB:
2485 retval = tv_op_blob(tv1, tv2, op);
2486 break;
2487
2488 case VAR_LIST:
2489 retval = tv_op_list(tv1, tv2, op);
2490 break;
2491
2492 case VAR_NUMBER:
2493 case VAR_STRING:
2494 retval = tv_op_nr_or_string(tv1, tv2, op);
2495 break;
2496
2497 case VAR_FLOAT:
2498 retval = tv_op_float(tv1, tv2, op);
2499 break;
2500 }
2501
2502 if (retval != OK)
Ernie Raele75fde62023-12-21 17:18:54 +01002503 semsg(_(e_wrong_variable_type_for_str_equal), op);
Yegappan Lakshmanan4ceb4dc2024-05-12 09:24:35 +02002504
2505 return retval;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506}
2507
2508/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509 * Evaluate the expression used in a ":for var in expr" command.
2510 * "arg" points to "var".
2511 * Set "*errp" to TRUE for an error, FALSE otherwise;
2512 * Return a pointer that holds the info. Null when there is an error.
2513 */
2514 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002515eval_for_line(
2516 char_u *arg,
2517 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002518 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002519 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002520{
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002522 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T tv;
2525 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002526 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002528 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002530 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531 if (fi == NULL)
2532 return NULL;
2533
Bram Moolenaar404557e2021-07-05 21:41:48 +02002534 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2535 &fi->fi_semicolon, FALSE);
2536 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002537 return fi;
2538
Bram Moolenaar404557e2021-07-05 21:41:48 +02002539 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002540 if (expr[0] != 'i' || expr[1] != 'n'
2541 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002542 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002543 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2544 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2545 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002546 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547 return fi;
2548 }
2549
2550 if (skip)
2551 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002552 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2553 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 {
2555 *errp = FALSE;
2556 if (!skip)
2557 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002558 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002559 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002560 l = tv.vval.v_list;
2561 if (l == NULL)
2562 {
2563 // a null list is like an empty list: do nothing
2564 clear_tv(&tv);
2565 }
2566 else
2567 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002569 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002571 // No need to increment the refcount, it's already set for
2572 // the list being used in "tv".
2573 fi->fi_list = l;
2574 list_add_watch(l, &fi->fi_lw);
2575 fi->fi_lw.lw_item = l->lv_first;
2576 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002577 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002578 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002579 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002580 fi->fi_bi = 0;
2581 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002582 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002583 typval_T btv;
2584
2585 // Make a copy, so that the iteration still works when the
2586 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002588 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002589 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002590 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002591 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002592 else if (tv.v_type == VAR_STRING)
2593 {
2594 fi->fi_byte_idx = 0;
2595 fi->fi_string = tv.vval.v_string;
2596 tv.vval.v_string = NULL;
2597 if (fi->fi_string == NULL)
2598 fi->fi_string = vim_strsave((char_u *)"");
2599 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002600 else
2601 {
Sean Dewar80d73952021-08-04 19:25:54 +02002602 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002603 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002604 }
2605 }
2606 }
2607 if (skip)
2608 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002609 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002610
2611 return fi;
2612}
2613
2614/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002615 * Used when looping over a :for line, skip the "in expr" part.
2616 */
2617 void
2618skip_for_lines(void *fi_void, evalarg_T *evalarg)
2619{
2620 forinfo_T *fi = (forinfo_T *)fi_void;
2621 int i;
2622
2623 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002624 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002625}
2626
2627/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002628 * Use the first item in a ":for" list. Advance to the next.
2629 * Assign the values to the variable (list). "arg" points to the first one.
2630 * Return TRUE when a valid item was found, FALSE when at end of list or
2631 * something wrong.
2632 */
2633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002634next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002635{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002636 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002637 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002638 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002639 ? (ASSIGN_FINAL
2640 // first round: error if variable exists
2641 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002642 | ASSIGN_NO_MEMBER_TYPE
2643 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002644 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002645 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002646 int skip_assign = in_vim9script() && arg[0] == '_'
2647 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002648
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002649 if (fi->fi_blob != NULL)
2650 {
2651 typval_T tv;
2652
2653 if (fi->fi_bi >= blob_len(fi->fi_blob))
2654 return FALSE;
2655 tv.v_type = VAR_NUMBER;
2656 tv.v_lock = VAR_FIXED;
2657 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2658 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002659 if (skip_assign)
2660 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002661 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002662 fi->fi_varcount, flag, NULL) == OK;
2663 }
2664
2665 if (fi->fi_string != NULL)
2666 {
2667 typval_T tv;
2668 int len;
2669
2670 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2671 if (len == 0)
2672 return FALSE;
2673 tv.v_type = VAR_STRING;
2674 tv.v_lock = VAR_FIXED;
2675 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2676 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002677 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002678 if (skip_assign)
2679 result = TRUE;
2680 else
2681 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002682 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002683 vim_free(tv.vval.v_string);
2684 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002685 }
2686
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002687 item = fi->fi_lw.lw_item;
2688 if (item == NULL)
2689 result = FALSE;
2690 else
2691 {
2692 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002693 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002694 if (skip_assign)
2695 result = TRUE;
2696 else
2697 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002698 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002699 }
2700 return result;
2701}
2702
2703/*
2704 * Free the structure used to store info used by ":for".
2705 */
2706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002707free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002708{
Bram Moolenaar33570922005-01-25 22:26:29 +00002709 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002711 if (fi == NULL)
2712 return;
2713 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002714 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002715 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002716 list_unref(fi->fi_list);
2717 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002718 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002719 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002720 else
2721 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002722 vim_free(fi);
2723}
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002726set_context_for_expression(
2727 expand_T *xp,
2728 char_u *arg,
2729 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002731 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002733 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002735 if (cmdidx == CMD_let || cmdidx == CMD_var
2736 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737 {
2738 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002739 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002741 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002742 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002743 {
2744 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002745 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002746 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 break;
2748 }
2749 return;
2750 }
2751 }
2752 else
2753 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2754 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 while ((xp->xp_pattern = vim_strpbrk(arg,
2756 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2757 {
2758 c = *xp->xp_pattern;
2759 if (c == '&')
2760 {
2761 c = xp->xp_pattern[1];
2762 if (c == '&')
2763 {
2764 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002765 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002770 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2771 xp->xp_pattern += 2;
2772
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 else if (c == '$')
2776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 xp->xp_context = EXPAND_ENV_VARS;
2779 }
2780 else if (c == '=')
2781 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002782 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 xp->xp_context = EXPAND_EXPRESSION;
2784 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002785 else if (c == '#'
2786 && xp->xp_context == EXPAND_EXPRESSION)
2787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002788 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002789 break;
2790 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002791 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 && xp->xp_context == EXPAND_FUNCTIONS
2793 && vim_strchr(xp->xp_pattern, '(') == NULL)
2794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002795 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 break;
2797 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002798 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
2802 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2803 if (c == '\\' && xp->xp_pattern[1] != NUL)
2804 ++xp->xp_pattern;
2805 xp->xp_context = EXPAND_NOTHING;
2806 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002807 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002809 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2811 /* skip */ ;
2812 xp->xp_context = EXPAND_NOTHING;
2813 }
2814 else if (c == '|')
2815 {
2816 if (xp->xp_pattern[1] == '|')
2817 {
2818 ++xp->xp_pattern;
2819 xp->xp_context = EXPAND_EXPRESSION;
2820 }
2821 else
2822 xp->xp_context = EXPAND_COMMANDS;
2823 }
2824 else
2825 xp->xp_context = EXPAND_EXPRESSION;
2826 }
2827 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002828 // Doesn't look like something valid, expand as an expression
2829 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 arg = xp->xp_pattern;
2832 if (*arg != NUL)
2833 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2834 /* skip */ ;
2835 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002836
2837 // ":exe one two" completes "two"
2838 if ((cmdidx == CMD_execute
2839 || cmdidx == CMD_echo
2840 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002841 || cmdidx == CMD_echomsg
2842 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002843 && xp->xp_context == EXPAND_EXPRESSION)
2844 {
2845 for (;;)
2846 {
2847 char_u *n = skiptowhite(arg);
2848
2849 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2850 break;
2851 arg = skipwhite(n);
2852 }
2853 }
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 xp->xp_pattern = arg;
2856}
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002859 * Return TRUE if "pat" matches "text".
2860 * Does not use 'cpo' and always uses 'magic'.
2861 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002862 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002863pattern_match(char_u *pat, char_u *text, int ic)
2864{
2865 int matches = FALSE;
2866 char_u *save_cpo;
2867 regmatch_T regmatch;
2868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002869 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002870 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002871 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002872 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2873 if (regmatch.regprog != NULL)
2874 {
2875 regmatch.rm_ic = ic;
2876 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2877 vim_regfree(regmatch.regprog);
2878 }
2879 p_cpo = save_cpo;
2880 return matches;
2881}
2882
2883/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002884 * Handle a name followed by "(". Both for just "name(arg)" and for
2885 * "expr->name(arg)".
2886 * Returns OK or FAIL.
2887 */
2888 static int
2889eval_func(
2890 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002891 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002892 char_u *name,
2893 int name_len,
2894 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002895 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002896 typval_T *basetv) // "expr" for "expr->name(arg)"
2897{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002898 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002899 char_u *s = name;
2900 int len = name_len;
2901 partial_T *partial;
2902 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002903 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002904 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002905
2906 if (!evaluate)
2907 check_vars(s, len);
2908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002909 // If "s" is the name of a variable of type VAR_FUNC
2910 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002911 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002912 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002914 // Need to make a copy, in case evaluating the arguments makes
2915 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002916 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002917 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002918 ret = FAIL;
2919 else
2920 {
2921 funcexe_T funcexe;
2922
2923 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002924 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002925 funcexe.fe_firstline = curwin->w_cursor.lnum;
2926 funcexe.fe_lastline = curwin->w_cursor.lnum;
2927 funcexe.fe_evaluate = evaluate;
2928 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002929 if (partial != NULL)
2930 {
2931 funcexe.fe_object = partial->pt_obj;
2932 if (funcexe.fe_object != NULL)
2933 ++funcexe.fe_object->obj_refcount;
2934 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002935 funcexe.fe_basetv = basetv;
2936 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002937 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002938 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002939 }
2940 vim_free(s);
2941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002942 // If evaluate is FALSE rettv->v_type was not set in
2943 // get_func_tv, but it's needed in handle_subscript() to parse
2944 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002945 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2946 {
2947 rettv->vval.v_string = NULL;
2948 rettv->v_type = VAR_FUNC;
2949 }
2950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002951 // Stop the expression evaluation when immediately
2952 // aborting on error, or when an interrupt occurred or
2953 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002954 if (evaluate && aborting())
2955 {
2956 if (ret == OK)
2957 clear_tv(rettv);
2958 ret = FAIL;
2959 }
2960 return ret;
2961}
2962
2963/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002964 * After a NL, skip over empty lines and comment-only lines.
2965 */
2966 static char_u *
2967newline_skip_comments(char_u *arg)
2968{
2969 char_u *p = arg + 1;
2970
2971 for (;;)
2972 {
2973 p = skipwhite(p);
2974
2975 if (*p == NUL)
2976 break;
2977 if (vim9_comment_start(p))
2978 {
2979 char_u *nl = vim_strchr(p, NL);
2980
2981 if (nl == NULL)
2982 break;
2983 p = nl;
2984 }
2985 if (*p != NL)
2986 break;
2987 ++p; // skip another NL
2988 }
2989 return p;
2990}
2991
2992/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002993 * Get the next line source line without advancing. But do skip over comment
2994 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002995 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002996 */
2997 static char_u *
2998getline_peek_skip_comments(evalarg_T *evalarg)
2999{
3000 for (;;)
3001 {
3002 char_u *next = getline_peek(evalarg->eval_getline,
3003 evalarg->eval_cookie);
3004 char_u *p;
3005
3006 if (next == NULL)
3007 break;
3008 p = skipwhite(next);
3009 if (*p != NUL && !vim9_comment_start(p))
3010 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01003011 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00003012 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02003013 }
3014 return NULL;
3015}
3016
3017/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02003018 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
3019 * comment) and there is a next line, return the next line (skipping blanks)
3020 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02003021 * Otherwise return the next non-white at or after "arg" and set "getnext" to
3022 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003023 * "arg" must point somewhere inside a line, not at the start.
3024 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003025 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003026eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
3027{
Bram Moolenaar9d489562020-07-30 20:08:50 +02003028 char_u *p = skipwhite(arg);
3029
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003030 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003031 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003032 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01003033 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
3034 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01003035 && (*p == NUL || *p == NL
3036 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003037 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02003038 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003039
Bram Moolenaare442d592022-05-05 12:20:28 +01003040 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003041 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01003042 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02003043 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003044 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02003045 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046
Bram Moolenaar9d489562020-07-30 20:08:50 +02003047 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003048 {
Bram Moolenaar69082912022-09-22 21:35:19 +01003049 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003050 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003051 }
3052 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003053 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003054}
3055
3056/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003057 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003058 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003059 *
3060 * If "arg" is not NULL, then the caller should assign the return value to
3061 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003062 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003063 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01003064eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003065{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003066 garray_T *gap = &evalarg->eval_ga;
3067 char_u *line;
3068
Bram Moolenaar39be4982022-05-06 21:51:50 +01003069 if (arg != NULL)
3070 {
3071 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003072 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01003073 // Truncate before a trailing comment, so that concatenating the lines
3074 // won't turn the rest into a comment.
3075 if (*skipwhite(arg) == '#')
3076 *arg = NUL;
3077 }
Bram Moolenaare442d592022-05-05 12:20:28 +01003078
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003079 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02003080 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
3081 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003082 else
3083 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00003084 if (line == NULL)
3085 return NULL;
3086
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02003087 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003088 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
3089 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02003090 char_u *p = skipwhite(line);
3091
3092 // Going to concatenate the lines after parsing. For an empty or
3093 // comment line use an empty string.
3094 if (*p == NUL || vim9_comment_start(p))
3095 {
3096 vim_free(line);
3097 line = vim_strsave((char_u *)"");
3098 }
3099
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003100 ((char_u **)gap->ga_data)[gap->ga_len] = line;
3101 ++gap->ga_len;
3102 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003103 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003104 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01003105 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003106 evalarg->eval_tofree = line;
3107 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02003108
3109 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01003110 // line. The caller assigns the return value to "arg".
3111 // If "arg" is NULL, then the return value is discarded. In that case,
3112 // "arg" still points to the previous line. So don't reset
3113 // "eval_using_cmdline".
3114 if (arg != NULL)
3115 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003116 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003117}
3118
Bram Moolenaare6b53242020-07-01 17:28:33 +02003119/*
3120 * Call eval_next_non_blank() and get the next line if needed.
3121 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02003122 char_u *
3123skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
3124{
3125 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00003126 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003127
Bram Moolenaar962d7212020-07-04 14:15:00 +02003128 if (evalarg == NULL)
3129 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003130 eval_next_non_blank(p, evalarg, &getnext);
3131 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003132 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02003133 return p;
3134}
3135
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003136/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003137 * The "eval" functions have an "evalarg" argument: When NULL or
3138 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
3139 * parsed but not executed. The functions may return OK, but the rettv will be
3140 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 */
3142
3143/*
3144 * Handle zero level expression.
3145 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003147 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003148 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * Return OK or FAIL.
3150 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003152eval0(
3153 char_u *arg,
3154 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003155 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003156 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003158 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
3159}
3160
3161/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01003162 * If "arg" is a simple function call without arguments then call it and return
3163 * the result. Otherwise return NOTDONE.
3164 */
3165 int
3166may_call_simple_func(
3167 char_u *arg,
3168 typval_T *rettv)
3169{
3170 char_u *parens = (char_u *)strstr((char *)arg, "()");
3171 int r = NOTDONE;
3172
3173 // If the expression is "FuncName()" then we can skip a lot of overhead.
3174 if (parens != NULL && *skipwhite(parens + 2) == NUL)
3175 {
3176 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
3177
3178 if (to_name_end(p, TRUE) == parens)
3179 r = call_simple_func(arg, (int)(parens - arg), rettv);
3180 }
3181 return r;
3182}
3183
3184/*
3185 * Handle zero level expression with optimization for a simple function call.
3186 * Same arguments and return value as eval0().
3187 */
3188 int
3189eval0_simple_funccal(
3190 char_u *arg,
3191 typval_T *rettv,
3192 exarg_T *eap,
3193 evalarg_T *evalarg)
3194{
3195 int r = may_call_simple_func(arg, rettv);
3196
3197 if (r == NOTDONE)
3198 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
3199 return r;
3200}
3201
3202/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003203 * Like eval0() but when "retarg" is not NULL store the pointer to after the
3204 * expression and don't check what comes after the expression.
3205 */
3206 int
3207eval0_retarg(
3208 char_u *arg,
3209 typval_T *rettv,
3210 exarg_T *eap,
3211 evalarg_T *evalarg,
3212 char_u **retarg)
3213{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 int ret;
3215 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00003216 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003217 int did_emsg_before = did_emsg;
3218 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003219 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003220 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003223 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003224
Bram Moolenaar79481362022-06-27 20:15:10 +01003225 if (ret != FAIL)
3226 {
3227 expr_end = p;
3228 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003229
Bram Moolenaar79481362022-06-27 20:15:10 +01003230 // In Vim9 script a command block is not split at NL characters for
3231 // commands using an expression argument. Skip over a '#' comment to
3232 // check for a following NL. Require white space before the '#'.
3233 if (in_vim9script() && p > expr_end && retarg == NULL)
3234 while (*p == '#')
3235 {
3236 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00003237
Bram Moolenaar79481362022-06-27 20:15:10 +01003238 if (nl == NULL)
3239 break;
3240 p = skipwhite(nl + 1);
3241 if (eap != NULL && *p != NUL)
3242 eap->nextcmd = p;
3243 check_for_end = FALSE;
3244 }
3245
3246 if (check_for_end)
3247 end_error = !ends_excmd2(arg, p);
3248 }
3249
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003250 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
3252 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003253 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 /*
3255 * Report the invalid expression unless the expression evaluation has
3256 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003257 * exception, or we already gave a more specific error.
3258 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003260 if (!aborting()
3261 && did_emsg == did_emsg_before
3262 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003263 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003264 {
3265 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003266 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003267 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003268 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003269 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003270
zeertzjqf2588b62023-05-05 17:22:35 +01003271 if (eap != NULL && p != NULL)
3272 {
3273 // Some of the expression may not have been consumed.
3274 // Only execute a next command if it cannot be a "||" operator.
3275 // The next command may be "catch".
3276 char_u *nextcmd = check_nextcmd(p);
3277 if (nextcmd != NULL && *nextcmd != '|')
3278 eap->nextcmd = nextcmd;
3279 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003282
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003283 if (retarg != NULL)
3284 *retarg = p;
3285 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003286 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return ret;
3289}
3290
3291/*
3292 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003293 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003294 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 *
3296 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003297 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003299 * Note: "rettv.v_lock" is not set.
3300 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 * Return OK or FAIL.
3302 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003303 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003304eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003306 char_u *p;
3307 int getnext;
3308
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003309 CLEAR_POINTER(rettv);
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 /*
3312 * Get the first variable.
3313 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003314 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 return FAIL;
3316
Bram Moolenaar793648f2020-06-26 21:28:25 +02003317 p = eval_next_non_blank(*arg, evalarg, &getnext);
3318 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003320 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003321 int result;
3322 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003323 evalarg_T *evalarg_used = evalarg;
3324 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003325 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003326 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003327 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003328
3329 if (evalarg == NULL)
3330 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003331 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003332 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003333 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003334 orig_flags = evalarg_used->eval_flags;
3335 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003336
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003337 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003338 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003339 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003340 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003341 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003342 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003343 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003344 clear_tv(rettv);
3345 return FAIL;
3346 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003347 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003348 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003351 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 int error = FALSE;
3354
Bram Moolenaar13106602020-10-04 16:06:05 +02003355 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003356 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003357 else if (vim9script)
3358 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003359 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003361 if (error || !op_falsy || !result)
3362 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 if (error)
3364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366
3367 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003368 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003370 if (op_falsy)
3371 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003372 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003373 {
mityu4ac198c2021-05-28 17:52:40 +02003374 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003375 clear_tv(rettv);
3376 return FAIL;
3377 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003378 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003379 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003380 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003381 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003382 {
3383 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003385 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003386 if (!op_falsy || !result)
3387 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003389 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003391 /*
3392 * Check for the ":".
3393 */
3394 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3395 if (*p != ':')
3396 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003397 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003398 if (evaluate && result)
3399 clear_tv(rettv);
3400 evalarg_used->eval_flags = orig_flags;
3401 return FAIL;
3402 }
3403 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003404 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003405 else
3406 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003407 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003408 {
3409 error_white_both(p, 1);
3410 clear_tv(rettv);
3411 evalarg_used->eval_flags = orig_flags;
3412 return FAIL;
3413 }
3414 *arg = p;
3415 }
3416
3417 /*
3418 * Get the third variable. Recursive!
3419 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003420 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003421 {
mityu4ac198c2021-05-28 17:52:40 +02003422 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003423 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003424 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003425 return FAIL;
3426 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003427 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3428 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003429 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003430 if (eval1(arg, &var2, evalarg_used) == FAIL)
3431 {
3432 if (evaluate && result)
3433 clear_tv(rettv);
3434 evalarg_used->eval_flags = orig_flags;
3435 return FAIL;
3436 }
3437 if (evaluate && !result)
3438 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003440
3441 if (evalarg == NULL)
3442 clear_evalarg(&local_evalarg, NULL);
3443 else
3444 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447 return OK;
3448}
3449
3450/*
3451 * Handle first level expression:
3452 * expr2 || expr2 || expr2 logical OR
3453 *
3454 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003455 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 *
3457 * Return OK or FAIL.
3458 */
3459 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003460eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003462 char_u *p;
3463 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003466 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003468 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 return FAIL;
3470
3471 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003472 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003474 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003475 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003477 evalarg_T *evalarg_used = evalarg;
3478 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003479 int evaluate;
3480 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003481 long result = FALSE;
3482 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003483 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003484 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003485
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003486 if (evalarg == NULL)
3487 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003488 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003489 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003490 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003491 orig_flags = evalarg_used->eval_flags;
3492 evaluate = orig_flags & EVAL_EVALUATE;
3493 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003494 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003495 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003496 result = tv_get_bool_chk(rettv, &error);
3497 else if (tv_get_number_chk(rettv, &error) != 0)
3498 result = TRUE;
3499 clear_tv(rettv);
3500 if (error)
3501 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503
3504 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003505 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003507 while (p[0] == '|' && p[1] == '|')
3508 {
3509 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003510 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003511 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003512 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003513 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003514 {
3515 error_white_both(p, 2);
3516 clear_tv(rettv);
3517 return FAIL;
3518 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003519 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003520 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003521
3522 /*
3523 * Get the second variable.
3524 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003525 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003526 {
mityu4ac198c2021-05-28 17:52:40 +02003527 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003528 clear_tv(rettv);
3529 return FAIL;
3530 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003531 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3532 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003533 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003534 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003535 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003536
3537 /*
3538 * Compute the result.
3539 */
3540 if (evaluate && !result)
3541 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003542 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003543 result = tv_get_bool_chk(&var2, &error);
3544 else if (tv_get_number_chk(&var2, &error) != 0)
3545 result = TRUE;
3546 clear_tv(&var2);
3547 if (error)
3548 return FAIL;
3549 }
3550 if (evaluate)
3551 {
3552 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003553 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003554 rettv->v_type = VAR_BOOL;
3555 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003556 }
3557 else
3558 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003559 rettv->v_type = VAR_NUMBER;
3560 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003561 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003562 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003563
3564 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003566
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003567 if (evalarg == NULL)
3568 clear_evalarg(&local_evalarg, NULL);
3569 else
3570 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572
3573 return OK;
3574}
3575
3576/*
3577 * Handle second level expression:
3578 * expr3 && expr3 && expr3 logical AND
3579 *
3580 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003581 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 *
3583 * Return OK or FAIL.
3584 */
3585 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003586eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003588 char_u *p;
3589 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003592 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003594 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 return FAIL;
3596
3597 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003598 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003600 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003601 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003603 evalarg_T *evalarg_used = evalarg;
3604 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003605 int orig_flags;
3606 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003607 long result = TRUE;
3608 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003609 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003610 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003611
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003612 if (evalarg == NULL)
3613 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003614 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003615 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003616 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003617 orig_flags = evalarg_used->eval_flags;
3618 evaluate = orig_flags & EVAL_EVALUATE;
3619 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003620 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003621 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003622 result = tv_get_bool_chk(rettv, &error);
3623 else if (tv_get_number_chk(rettv, &error) == 0)
3624 result = FALSE;
3625 clear_tv(rettv);
3626 if (error)
3627 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629
3630 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003631 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003633 while (p[0] == '&' && p[1] == '&')
3634 {
3635 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003636 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003637 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003638 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003639 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003640 {
3641 error_white_both(p, 2);
3642 clear_tv(rettv);
3643 return FAIL;
3644 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003645 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003647
3648 /*
3649 * Get the second variable.
3650 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003651 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003652 {
mityu4ac198c2021-05-28 17:52:40 +02003653 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003654 clear_tv(rettv);
3655 return FAIL;
3656 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003657 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3658 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003659 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003660 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003661 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003662 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003663
3664 /*
3665 * Compute the result.
3666 */
3667 if (evaluate && result)
3668 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003669 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003670 result = tv_get_bool_chk(&var2, &error);
3671 else if (tv_get_number_chk(&var2, &error) == 0)
3672 result = FALSE;
3673 clear_tv(&var2);
3674 if (error)
3675 return FAIL;
3676 }
3677 if (evaluate)
3678 {
3679 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003680 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003681 rettv->v_type = VAR_BOOL;
3682 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003683 }
3684 else
3685 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003686 rettv->v_type = VAR_NUMBER;
3687 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003688 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003689 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003690
3691 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003693
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003694 if (evalarg == NULL)
3695 clear_evalarg(&local_evalarg, NULL);
3696 else
3697 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699
3700 return OK;
3701}
3702
3703/*
3704 * Handle third level expression:
3705 * var1 == var2
3706 * var1 =~ var2
3707 * var1 != var2
3708 * var1 !~ var2
3709 * var1 > var2
3710 * var1 >= var2
3711 * var1 < var2
3712 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003713 * var1 is var2
3714 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 *
3716 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003717 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 *
3719 * Return OK or FAIL.
3720 */
3721 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003722eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003725 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003726 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003728 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
3730 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003731 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003733 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 return FAIL;
3735
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003736 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003737
Bram Moolenaar696ba232020-07-29 21:20:41 +02003738 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
3740 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003741 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003743 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003745 typval_T var2;
3746 int ic;
3747 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003748 int evaluate = evalarg == NULL
3749 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003750 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003751
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003752 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003753 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003754 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003755 p = *arg;
3756 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003757 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3758 {
mityu4ac198c2021-05-28 17:52:40 +02003759 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003760 clear_tv(rettv);
3761 return FAIL;
3762 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003763
Bram Moolenaar696ba232020-07-29 21:20:41 +02003764 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3765 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003766 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003767 clear_tv(rettv);
3768 return FAIL;
3769 }
3770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003771 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (p[len] == '?')
3773 {
3774 ic = TRUE;
3775 ++len;
3776 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 else if (p[len] == '#')
3779 {
3780 ic = FALSE;
3781 ++len;
3782 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003783 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003785 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 /*
3788 * Get the second variable.
3789 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003790 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3791 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003792 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003793 clear_tv(rettv);
3794 return FAIL;
3795 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003796 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003797 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 return FAIL;
3801 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003802 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003803 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003804 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003805
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003806 // use the line of the comparison for messages
3807 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003808 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003809 {
3810 ret = FAIL;
3811 clear_tv(rettv);
3812 }
3813 else
3814 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003815 clear_tv(&var2);
3816 return ret;
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819
3820 return OK;
3821}
3822
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003823/*
3824 * Make a copy of blob "tv1" and append blob "tv2".
3825 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 void
3827eval_addblob(typval_T *tv1, typval_T *tv2)
3828{
3829 blob_T *b1 = tv1->vval.v_blob;
3830 blob_T *b2 = tv2->vval.v_blob;
3831 blob_T *b = blob_alloc();
3832 int i;
3833
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003834 if (b == NULL)
3835 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003837 for (i = 0; i < blob_len(b1); i++)
3838 ga_append(&b->bv_ga, blob_get(b1, i));
3839 for (i = 0; i < blob_len(b2); i++)
3840 ga_append(&b->bv_ga, blob_get(b2, i));
3841
3842 clear_tv(tv1);
3843 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844}
3845
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003846/*
3847 * Make a copy of list "tv1" and append list "tv2".
3848 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 int
3850eval_addlist(typval_T *tv1, typval_T *tv2)
3851{
3852 typval_T var3;
3853
3854 // concatenate Lists
3855 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3856 {
3857 clear_tv(tv1);
3858 clear_tv(tv2);
3859 return FAIL;
3860 }
3861 clear_tv(tv1);
3862 *tv1 = var3;
3863 return OK;
3864}
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003867 * Handle the bitwise left/right shift operator expression:
3868 * var1 << var2
3869 * var1 >> var2
3870 *
3871 * "arg" must point to the first non-white of the expression.
3872 * "arg" is advanced to just after the recognized expression.
3873 *
3874 * Return OK or FAIL.
3875 */
3876 static int
3877eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3878{
3879 /*
3880 * Get the first expression.
3881 */
3882 if (eval6(arg, rettv, evalarg) == FAIL)
3883 return FAIL;
3884
3885 /*
3886 * Repeat computing, until no '<<' or '>>' is following.
3887 */
3888 for (;;)
3889 {
3890 char_u *p;
3891 int getnext;
3892 exprtype_T type;
3893 int evaluate;
3894 typval_T var2;
3895 int vim9script;
3896
3897 p = eval_next_non_blank(*arg, evalarg, &getnext);
3898 if (p[0] == '<' && p[1] == '<')
3899 type = EXPR_LSHIFT;
3900 else if (p[0] == '>' && p[1] == '>')
3901 type = EXPR_RSHIFT;
3902 else
3903 return OK;
3904
3905 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003906 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3907 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003908 {
3909 // left operand should be a number
3910 emsg(_(e_bitshift_ops_must_be_number));
3911 clear_tv(rettv);
3912 return FAIL;
3913 }
3914
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003915 vim9script = in_vim9script();
3916 if (getnext)
3917 {
3918 *arg = eval_next_line(*arg, evalarg);
3919 p = *arg;
3920 }
3921 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3922 {
3923 error_white_both(*arg, 2);
3924 clear_tv(rettv);
3925 return FAIL;
3926 }
3927
3928 /*
3929 * Get the second variable.
3930 */
3931 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3932 {
3933 error_white_both(p, 2);
3934 clear_tv(rettv);
3935 return FAIL;
3936 }
3937 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3938 if (eval6(arg, &var2, evalarg) == FAIL)
3939 {
3940 clear_tv(rettv);
3941 return FAIL;
3942 }
3943
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003944 if (evaluate)
3945 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003946 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3947 {
3948 // right operand should be a positive number
3949 if (var2.v_type != VAR_NUMBER)
3950 emsg(_(e_bitshift_ops_must_be_number));
3951 else
3952 emsg(_(e_bitshift_ops_must_be_positive));
3953 clear_tv(rettv);
3954 clear_tv(&var2);
3955 return FAIL;
3956 }
3957
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003958 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3959 // shifting more bits than we have always results in zero
3960 rettv->vval.v_number = 0;
3961 else if (type == EXPR_LSHIFT)
3962 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003963 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003964 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003965 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003966 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003967 }
3968
3969 clear_tv(&var2);
3970 }
3971
3972 return OK;
3973}
3974
3975/*
3976 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003977 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003979 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003980 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 *
3982 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003983 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *
3985 * Return OK or FAIL.
3986 */
3987 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003988eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003991 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003993 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 return FAIL;
3995
3996 /*
3997 * Repeat computing, until no '+', '-' or '.' is following.
3998 */
3999 for (;;)
4000 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004001 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004002 int getnext;
4003 char_u *p;
4004 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004005 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004006 int concat;
4007 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02004008 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004009
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004010 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004011 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004012 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004013 p = eval_next_non_blank(*arg, evalarg, &getnext);
4014 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02004015 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004016 if ((op != '+' && op != '-' && !concat) || p[1] == '='
4017 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02004019 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
4020 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004021
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004022 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004023 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004024 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004025 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004026 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004027 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02004028 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004029 {
mityu4ac198c2021-05-28 17:52:40 +02004030 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004031 clear_tv(rettv);
4032 return FAIL;
4033 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004034 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004035 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004036 if ((op != '+' || (rettv->v_type != VAR_LIST
4037 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004038 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004039 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004040 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004041 int error = FALSE;
4042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004043 // For "list + ...", an illegal use of the first operand as
4044 // a number cannot be determined before evaluating the 2nd
4045 // operand: if this is also a list, all is ok.
4046 // For "something . ...", "something - ..." or "non-list + ...",
4047 // we know that the first operand needs to be a string or number
4048 // without evaluating the 2nd operand. So check before to avoid
4049 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02004050 if (op != '.')
4051 tv_get_number_chk(rettv, &error);
4052 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004053 {
4054 clear_tv(rettv);
4055 return FAIL;
4056 }
4057 }
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 /*
4060 * Get the second variable.
4061 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02004062 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004063 {
mityu89dcb4d2021-05-28 13:50:17 +02004064 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004065 clear_tv(rettv);
4066 return FAIL;
4067 }
4068 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004069 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004071 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 return FAIL;
4073 }
4074
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004075 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
4077 /*
4078 * Compute the result.
4079 */
4080 if (op == '.')
4081 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004082 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4083 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004084 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004085
Bram Moolenaar2e086612020-08-25 22:37:48 +02004086 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004087 || var2.v_type == VAR_CHANNEL
4088 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02004089 semsg(_(e_using_invalid_value_as_string_str),
4090 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02004091 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004092 {
4093 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
4094 var2.vval.v_float);
4095 s2 = buf2;
4096 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004097 else
4098 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 {
4101 clear_tv(rettv);
4102 clear_tv(&var2);
4103 return FAIL;
4104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004105 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(rettv);
4107 rettv->v_type = VAR_STRING;
4108 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004110 else if (op == '+' && rettv->v_type == VAR_BLOB
4111 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00004113 else if (op == '+' && rettv->v_type == VAR_LIST
4114 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004115 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004117 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else
4120 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004121 int error = FALSE;
4122 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004123 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004124
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004125 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004126 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004127 f1 = rettv->vval.v_float;
4128 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004132 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004133 if (error)
4134 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004135 // This can only happen for "list + non-list" or
4136 // "blob + non-blob". For "non-list + ..." or
4137 // "something - ...", we returned before evaluating the
4138 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004139 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02004140 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004141 return FAIL;
4142 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004143 if (var2.v_type == VAR_FLOAT)
4144 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004145 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146 if (var2.v_type == VAR_FLOAT)
4147 {
4148 f2 = var2.vval.v_float;
4149 n2 = 0;
4150 }
4151 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004153 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154 if (error)
4155 {
4156 clear_tv(rettv);
4157 clear_tv(&var2);
4158 return FAIL;
4159 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004160 if (rettv->v_type == VAR_FLOAT)
4161 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4167 {
4168 if (op == '+')
4169 f1 = f1 + f2;
4170 else
4171 f1 = f1 - f2;
4172 rettv->v_type = VAR_FLOAT;
4173 rettv->vval.v_float = f1;
4174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004176 {
4177 if (op == '+')
4178 n1 = n1 + n2;
4179 else
4180 n1 = n1 - n2;
4181 rettv->v_type = VAR_NUMBER;
4182 rettv->vval.v_number = n1;
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 }
4188 return OK;
4189}
4190
4191/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004192 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 * * number multiplication
4194 * / number division
4195 * % number modulo
4196 *
4197 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004198 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 *
4200 * Return OK or FAIL.
4201 */
4202 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004203eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004204 char_u **arg,
4205 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004206 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004207 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004209 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004212 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004214 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return FAIL;
4216
4217 /*
4218 * Repeat computing, until no '*', '/' or '%' is following.
4219 */
4220 for (;;)
4221 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004222 int evaluate;
4223 int getnext;
4224 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02004225 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004226 int op;
4227 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004228 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004229 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004230
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004231 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02004232 p = eval_next_non_blank(*arg, evalarg, &getnext);
4233 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01004234 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004236
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004237 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004238 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01004239 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02004240 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004241 {
4242 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
4243 {
mityu4ac198c2021-05-28 17:52:40 +02004244 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004245 clear_tv(rettv);
4246 return FAIL;
4247 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02004248 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004251 f1 = 0;
4252 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004253 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004254 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004256 if (rettv->v_type == VAR_FLOAT)
4257 {
4258 f1 = rettv->vval.v_float;
4259 use_float = TRUE;
4260 n1 = 0;
4261 }
4262 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004263 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (error)
4266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 else
4269 n1 = 0;
4270
4271 /*
4272 * Get the second variable.
4273 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004274 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4275 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004276 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004277 clear_tv(rettv);
4278 return FAIL;
4279 }
4280 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004281 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return FAIL;
4283
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004284 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004286 if (var2.v_type == VAR_FLOAT)
4287 {
4288 if (!use_float)
4289 {
4290 f1 = n1;
4291 use_float = TRUE;
4292 }
4293 f2 = var2.vval.v_float;
4294 n2 = 0;
4295 }
4296 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004298 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004299 clear_tv(&var2);
4300 if (error)
4301 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004302 if (use_float)
4303 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004310 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004312 if (op == '*')
4313 f1 = f1 * f2;
4314 else if (op == '/')
4315 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004316#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004317 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004318 if (f2 == 0.0)
4319 {
4320 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004321 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004322 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004323 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004324 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004325 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004326 }
4327 else
4328 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004329#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004330 // We rely on the floating point library to handle divide
4331 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004332 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004333#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004336 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004337 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004338 return FAIL;
4339 }
4340 rettv->v_type = VAR_FLOAT;
4341 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 else
4344 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004345 int failed = FALSE;
4346
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004347 if (op == '*')
4348 n1 = n1 * n2;
4349 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004350 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004352 n1 = num_modulus(n1, n2, &failed);
4353 if (failed)
4354 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004355
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 }
4361
4362 return OK;
4363}
4364
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004365/*
4366 * Handle a type cast before a base level expression.
4367 * "arg" must point to the first non-white of the expression.
4368 * "arg" is advanced to just after the recognized expression.
4369 * Return OK or FAIL.
4370 */
4371 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004372eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004373 char_u **arg,
4374 typval_T *rettv,
4375 evalarg_T *evalarg,
4376 int want_string) // after "." operator
4377{
4378 type_T *want_type = NULL;
4379 garray_T type_list; // list of pointers to allocated types
4380 int res;
4381 int evaluate = evalarg == NULL ? 0
4382 : (evalarg->eval_flags & EVAL_EVALUATE);
4383
4384 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004385 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4386 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004387 {
4388 ++*arg;
4389 ga_init2(&type_list, sizeof(type_T *), 10);
4390 want_type = parse_type(arg, &type_list, TRUE);
4391 if (want_type == NULL && (evaluate || **arg != '>'))
4392 {
4393 clear_type_list(&type_list);
4394 return FAIL;
4395 }
4396
4397 if (**arg != '>')
4398 {
4399 if (*skipwhite(*arg) == '>')
4400 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4401 else
4402 emsg(_(e_missing_gt));
4403 clear_type_list(&type_list);
4404 return FAIL;
4405 }
4406 ++*arg;
4407 *arg = skipwhite_and_linebreak(*arg, evalarg);
4408 }
4409
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004410 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004411
4412 if (want_type != NULL && evaluate)
4413 {
4414 if (res == OK)
4415 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004416 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4417 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004418
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004419 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004420 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004421 if (want_type->tt_type == VAR_BOOL
4422 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004423 && (actual->tt_flags & TTFLAG_BOOL_OK))
4424 {
4425 int n = tv2bool(rettv);
4426
4427 // can use "0" and "1" for boolean in some places
4428 clear_tv(rettv);
4429 rettv->v_type = VAR_BOOL;
4430 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4431 }
4432 else
4433 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004434 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004435
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004436 res = check_type(want_type, actual, TRUE, where);
4437 }
4438 }
4439 }
4440 clear_type_list(&type_list);
4441 }
4442
4443 return res;
4444}
4445
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004446 int
4447eval_leader(char_u **arg, int vim9)
4448{
4449 char_u *s = *arg;
4450 char_u *p = *arg;
4451
4452 while (*p == '!' || *p == '-' || *p == '+')
4453 {
4454 char_u *n = skipwhite(p + 1);
4455
4456 // ++, --, -+ and +- are not accepted in Vim9 script
4457 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4458 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004459 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004460 return FAIL;
4461 }
4462 p = n;
4463 }
4464 *arg = p;
4465 return OK;
4466}
4467
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004469 * Check for a predefined value "true", "false" and "null.*".
4470 * Return OK when recognized.
4471 */
4472 int
4473handle_predefined(char_u *s, int len, typval_T *rettv)
4474{
4475 switch (len)
4476 {
4477 case 4: if (STRNCMP(s, "true", 4) == 0)
4478 {
4479 rettv->v_type = VAR_BOOL;
4480 rettv->vval.v_number = VVAL_TRUE;
4481 return OK;
4482 }
4483 if (STRNCMP(s, "null", 4) == 0)
4484 {
4485 rettv->v_type = VAR_SPECIAL;
4486 rettv->vval.v_number = VVAL_NULL;
4487 return OK;
4488 }
4489 break;
4490 case 5: if (STRNCMP(s, "false", 5) == 0)
4491 {
4492 rettv->v_type = VAR_BOOL;
4493 rettv->vval.v_number = VVAL_FALSE;
4494 return OK;
4495 }
4496 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004497 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4498 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004500 rettv->v_type = VAR_JOB;
4501 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004502#else
4503 rettv->v_type = VAR_SPECIAL;
4504 rettv->vval.v_number = VVAL_NULL;
4505#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004506 return OK;
4507 }
4508 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004509 case 9:
4510 if (STRNCMP(s, "null_", 5) != 0)
4511 break;
4512 if (STRNCMP(s + 5, "list", 4) == 0)
4513 {
4514 rettv->v_type = VAR_LIST;
4515 rettv->vval.v_list = NULL;
4516 return OK;
4517 }
4518 if (STRNCMP(s + 5, "dict", 4) == 0)
4519 {
4520 rettv->v_type = VAR_DICT;
4521 rettv->vval.v_dict = NULL;
4522 return OK;
4523 }
4524 if (STRNCMP(s + 5, "blob", 4) == 0)
4525 {
4526 rettv->v_type = VAR_BLOB;
4527 rettv->vval.v_blob = NULL;
4528 return OK;
4529 }
4530 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004531 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4532 {
4533 rettv->v_type = VAR_CLASS;
4534 rettv->vval.v_class = NULL;
4535 return OK;
4536 }
4537 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004538 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4539 {
4540 rettv->v_type = VAR_STRING;
4541 rettv->vval.v_string = NULL;
4542 return OK;
4543 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004544 if (STRNCMP(s, "null_object", 11) == 0)
4545 {
4546 rettv->v_type = VAR_OBJECT;
4547 rettv->vval.v_object = NULL;
4548 return OK;
4549 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004550 break;
4551 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004552 if (STRNCMP(s, "null_channel", 12) == 0)
4553 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004554#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004555 rettv->v_type = VAR_CHANNEL;
4556 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004557#else
4558 rettv->v_type = VAR_SPECIAL;
4559 rettv->vval.v_number = VVAL_NULL;
4560#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004561 return OK;
4562 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004563 if (STRNCMP(s, "null_partial", 12) == 0)
4564 {
4565 rettv->v_type = VAR_PARTIAL;
4566 rettv->vval.v_partial = NULL;
4567 return OK;
4568 }
4569 break;
4570 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4571 {
4572 rettv->v_type = VAR_FUNC;
4573 rettv->vval.v_string = NULL;
4574 return OK;
4575 }
4576 break;
4577 }
4578 return FAIL;
4579}
4580
4581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 * Handle sixth level expression:
4583 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004584 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004585 * "string" string constant
4586 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 * &option-name option value
4588 * @r register contents
4589 * identifier variable value
4590 * function() function call
4591 * $VAR environment variable
4592 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004593 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004595 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004596 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 *
4598 * Also handle:
4599 * ! in front logical NOT
4600 * - in front unary minus
4601 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 * trailing [] subscript in String or List
4603 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004604 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 *
4606 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004607 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 *
4609 * Return OK or FAIL.
4610 */
4611 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004612eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004613 char_u **arg,
4614 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004615 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004618 int evaluate = evalarg != NULL
4619 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int len;
4621 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004622 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 char_u *start_leader, *end_leader;
4624 int ret = OK;
4625 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004626 static int recurse = 0;
4627 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004636 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 */
4638 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004639 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 end_leader = *arg;
4642
Keith Thompson184f71c2024-01-04 21:19:04 +01004643 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004644 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004645 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004646 ++*arg;
4647 return FAIL;
4648 }
4649
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004650 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004651 // and crash. With MSVC the stack is smaller.
4652 if (recurse ==
4653#ifdef _MSC_VER
4654 300
4655#else
4656 1000
4657#endif
4658 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004659 {
4660 semsg(_(e_expression_too_recursive_str), *arg);
4661 return FAIL;
4662 }
4663 ++recurse;
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 switch (**arg)
4666 {
4667 /*
4668 * Number constant.
4669 */
4670 case '0':
4671 case '1':
4672 case '2':
4673 case '3':
4674 case '4':
4675 case '5':
4676 case '6':
4677 case '7':
4678 case '8':
4679 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004680 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004681
4682 // Apply prefixed "-" and "+" now. Matters especially when
4683 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004684 if (ret == OK && evaluate && end_leader > start_leader
4685 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004686 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 break;
4688
4689 /*
4690 * String constant: "string".
4691 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004692 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 break;
4694
4695 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004698 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 break;
4700
4701 /*
4702 * List: [expr, expr]
4703 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004704 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 break;
4706
4707 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004708 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004709 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004710 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004711 {
4712 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4713 }
4714 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004715 {
4716 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004717 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004718 }
4719 else
4720 ret = NOTDONE;
4721 break;
4722
4723 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004724 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004725 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004727 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004728 ret = NOTDONE;
4729 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004730 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004731 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004732 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 break;
4734
4735 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004736 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004738 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 break;
4740
4741 /*
4742 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004743 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 */
LemonBoy2eaef102022-05-06 13:14:50 +01004745 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4746 ret = eval_interp_string(arg, rettv, evaluate);
4747 else
4748 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 break;
4750
4751 /*
4752 * Register contents: @r.
4753 */
4754 case '@': ++*arg;
4755 if (evaluate)
4756 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004757 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004758 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004759 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004760 emsg_invreg(**arg);
4761 else
4762 {
4763 rettv->v_type = VAR_STRING;
4764 rettv->vval.v_string = get_reg_contents(**arg,
4765 GREG_EXPR_SRC);
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 if (**arg != NUL)
4769 ++*arg;
4770 break;
4771
4772 /*
4773 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004774 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004776 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004777 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004778 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004779 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004780 if (ret == OK && evaluate)
4781 {
4782 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4783
Bram Moolenaara9931532021-06-12 15:58:16 +02004784 // Compile it here to get the return type. The return
4785 // type is optional, when it's missing use t_unknown.
4786 // This is recognized in compile_return().
4787 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4788 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004789 if (compile_def_function(ufunc, FALSE,
4790 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004791 {
4792 clear_tv(rettv);
4793 ret = FAIL;
4794 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004795 }
4796 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004797 if (ret == NOTDONE)
4798 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004799 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004800 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004801
Bram Moolenaar9215f012020-06-27 21:18:00 +02004802 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004803 if (**arg == ')')
4804 ++*arg;
4805 else if (ret == OK)
4806 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004807 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004808 clear_tv(rettv);
4809 ret = FAIL;
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 break;
4813
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 break;
4816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817
4818 if (ret == NOTDONE)
4819 {
4820 /*
4821 * Must be a variable or function name.
4822 * Can also be a curly-braces kind of name: {expr}.
4823 */
4824 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004825 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 if (alias != NULL)
4827 s = alias;
4828
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004829 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004830 ret = FAIL;
4831 else
4832 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004833 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4834
Bram Moolenaar4525a572022-02-13 11:57:33 +00004835 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004836 {
4837 emsg(_(e_cannot_use_underscore_here));
4838 ret = FAIL;
4839 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004840 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004841 && s[0] == 's' && s[1] == ':')
4842 {
4843 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4844 ret = FAIL;
4845 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004846 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004847 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004848 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004849 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004850 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004851 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004853 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004854 // get the value of "true", "false", etc. or a variable
4855 ret = FAIL;
4856 if (vim9script)
4857 ret = handle_predefined(s, len, rettv);
4858 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004859 {
4860 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004861 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004862 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004863 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004864 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004865 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004866 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004867 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004868 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004869 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004870 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004871 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004872 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004873 }
4874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004875 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4876 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004877 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004878 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4882 */
4883 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004884 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004885
4886 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004887 return ret;
4888}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004889
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004890/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004891 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004892 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004893 * Adjusts "end_leaderp" until it is at "start_leader".
4894 */
4895 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004896eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004897 typval_T *rettv,
4898 int numeric_only,
4899 char_u *start_leader,
4900 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004901{
4902 char_u *end_leader = *end_leaderp;
4903 int ret = OK;
4904 int error = FALSE;
4905 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004906 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004907 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004908 float_T f = 0.0;
4909
4910 if (rettv->v_type == VAR_FLOAT)
4911 f = rettv->vval.v_float;
4912 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004913 {
4914 while (VIM_ISWHITE(end_leader[-1]))
4915 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004916 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004917 val = tv2bool(rettv);
4918 else
4919 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004920 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004921 if (error)
4922 {
4923 clear_tv(rettv);
4924 ret = FAIL;
4925 }
4926 else
4927 {
4928 while (end_leader > start_leader)
4929 {
4930 --end_leader;
4931 if (*end_leader == '!')
4932 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004933 if (numeric_only)
4934 {
4935 ++end_leader;
4936 break;
4937 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004938 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004939 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004940 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004941 {
4942 rettv->v_type = VAR_BOOL;
4943 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4944 }
4945 else
4946 f = !f;
4947 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004948 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004949 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004950 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004951 type = VAR_BOOL;
4952 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004953 }
4954 else if (*end_leader == '-')
4955 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004956 if (rettv->v_type == VAR_FLOAT)
4957 f = -f;
4958 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004959 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004960 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004961 type = VAR_NUMBER;
4962 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004963 }
4964 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004965 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004967 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004968 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004970 else
4971 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004972 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004973 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004974 rettv->v_type = type;
4975 else
4976 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004977 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004980 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return ret;
4982}
4983
4984/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004985 * Call the function referred to in "rettv".
4986 */
4987 static int
4988call_func_rettv(
4989 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004990 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004991 typval_T *rettv,
4992 int evaluate,
4993 dict_T *selfdict,
4994 typval_T *basetv)
4995{
4996 partial_T *pt = NULL;
4997 funcexe_T funcexe;
4998 typval_T functv;
4999 char_u *s;
5000 int ret;
5001
5002 // need to copy the funcref so that we can clear rettv
5003 if (evaluate)
5004 {
5005 functv = *rettv;
5006 rettv->v_type = VAR_UNKNOWN;
5007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005009 if (functv.v_type == VAR_PARTIAL)
5010 {
5011 pt = functv.vval.v_partial;
5012 s = partial_name(pt);
5013 }
5014 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005015 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005016 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005017 if (s == NULL || *s == NUL)
5018 {
5019 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005020 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005021 goto theend;
5022 }
5023 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005024 }
5025 else
5026 s = (char_u *)"";
5027
Bram Moolenaara80faa82020-04-12 19:37:17 +02005028 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005029 funcexe.fe_firstline = curwin->w_cursor.lnum;
5030 funcexe.fe_lastline = curwin->w_cursor.lnum;
5031 funcexe.fe_evaluate = evaluate;
5032 funcexe.fe_partial = pt;
5033 funcexe.fe_selfdict = selfdict;
5034 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005035 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005036
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005037theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // Clear the funcref afterwards, so that deleting it while
5039 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005040 if (evaluate)
5041 clear_tv(&functv);
5042
5043 return ret;
5044}
5045
5046/*
5047 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005048 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005049 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5050 */
5051 static int
5052eval_lambda(
5053 char_u **arg,
5054 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005055 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005057{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005058 int evaluate = evalarg != NULL
5059 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005060 typval_T base = *rettv;
5061 int ret;
5062
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005063 rettv->v_type = VAR_UNKNOWN;
5064
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005065 if (**arg == '{')
5066 {
5067 // ->{lambda}()
5068 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5069 }
5070 else
5071 {
5072 // ->(lambda)()
5073 ++*arg;
5074 ret = eval1(arg, rettv, evalarg);
5075 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005076 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005077 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005078 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005079 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005080 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005081 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5082 && rettv->v_type != VAR_PARTIAL)
5083 {
5084 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5085 return FAIL;
5086 }
5087 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005088 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005089 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005090 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005091
5092 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005093 {
5094 if (verbose)
5095 {
5096 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005097 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005098 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005099 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005100 }
5101 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005102 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005103 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005104 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005105 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005106
5107 // Clear the funcref afterwards, so that deleting it while
5108 // evaluating the arguments is possible (see test55).
5109 if (evaluate)
5110 clear_tv(&base);
5111
5112 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005113}
5114
5115/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005116 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005117 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005118 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5119 */
5120 static int
5121eval_method(
5122 char_u **arg,
5123 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005124 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005125 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005126{
5127 char_u *name;
5128 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005129 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005130 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005131 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005132 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005133 int evaluate = evalarg != NULL
5134 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005135
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005136 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005137
Bram Moolenaarac92e252019-08-03 21:58:38 +02005138 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005139 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005140 if (alias != NULL)
5141 name = alias;
5142
5143 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005144 {
5145 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005146 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005147 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005148 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005149 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005150 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005151 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005152
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005153 // If there is no "(" immediately following, but there is further on,
5154 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5155 // Does not handle anything where "(" is part of the expression.
5156 *arg = skipwhite(*arg);
5157
5158 if (**arg != '(' && alias == NULL
5159 && (paren = vim_strchr(*arg, '(')) != NULL)
5160 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005161 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005162
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005163 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005164 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005165 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005166 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5167 if (evalarg != NULL)
5168 {
5169 getline = evalarg->eval_getline;
5170 evalarg->eval_getline = NULL;
5171 }
5172
5173 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005174 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005175 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005176 *arg = name + len;
5177 ret = FAIL;
5178 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005179 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005180 {
5181 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005182 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005183 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005184
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005185 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005186 if (getline != NULL)
5187 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005188 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005189
5190 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005191 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005192 *arg = skipwhite(*arg);
5193
5194 if (**arg != '(')
5195 {
5196 if (verbose)
5197 semsg(_(e_missing_parenthesis_str), name);
5198 ret = FAIL;
5199 }
5200 else if (VIM_ISWHITE((*arg)[-1]))
5201 {
5202 if (verbose)
5203 emsg(_(e_no_white_space_allowed_before_parenthesis));
5204 ret = FAIL;
5205 }
5206 else
5207 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005208 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005209 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005210 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005211
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005212 // Clear the funcref afterwards, so that deleting it while
5213 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005214 if (evaluate)
5215 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005216 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005217
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005218 if (alias != NULL)
5219 vim_free(alias);
5220
Bram Moolenaarac92e252019-08-03 21:58:38 +02005221 return ret;
5222}
5223
5224/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005225 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5226 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5228 */
5229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005230eval_index(
5231 char_u **arg,
5232 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005233 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005234 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005236 int evaluate = evalarg != NULL
5237 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005242 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005243 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005245 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5246 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005248 init_tv(&var1);
5249 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /*
5253 * dict.name
5254 */
5255 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005256 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005258 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005260 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 }
5262 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 /*
5265 * something[idx]
5266 *
5267 * Get the (first) variable from inside the [].
5268 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005269 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 if (**arg == ':')
5271 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005272 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005274 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005275 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005276 semsg(_(e_white_space_required_before_and_after_str_at_str),
5277 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005278 clear_tv(&var1);
5279 return FAIL;
5280 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005281 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005283 int error = FALSE;
5284
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005285 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005286 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005287 && var1.v_type == VAR_FLOAT)
5288 {
5289 var1.vval.v_string = typval_tostring(&var1, TRUE);
5290 var1.v_type = VAR_STRING;
5291 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005292
Bram Moolenaar4525a572022-02-13 11:57:33 +00005293 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005294 tv_get_number_chk(&var1, &error);
5295 else
5296 error = tv_get_string_chk(&var1) == NULL;
5297 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005298 {
5299 // not a number or string
5300 clear_tv(&var1);
5301 return FAIL;
5302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304
5305 /*
5306 * Get the second variable from inside the [:].
5307 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005308 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 if (**arg == ':')
5310 {
5311 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005312 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005313 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005314 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005315 semsg(_(e_white_space_required_before_and_after_str_at_str),
5316 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005317 if (!empty1)
5318 clear_tv(&var1);
5319 return FAIL;
5320 }
5321 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 if (**arg == ']')
5323 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005324 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 if (!empty1)
5327 clear_tv(&var1);
5328 return FAIL;
5329 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005330 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 if (!empty1)
5334 clear_tv(&var1);
5335 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336 return FAIL;
5337 }
5338 }
5339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005340 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005341 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 if (**arg != ']')
5343 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005345 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 clear_tv(&var1);
5347 if (range)
5348 clear_tv(&var2);
5349 return FAIL;
5350 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005351 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353
5354 if (evaluate)
5355 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005356 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005357 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005358 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005359
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005360 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005363 clear_tv(&var2);
5364 return res;
5365 }
5366 return OK;
5367}
5368
5369/*
5370 * Check if "rettv" can have an [index] or [sli:ce]
5371 */
5372 int
5373check_can_index(typval_T *rettv, int evaluate, int verbose)
5374{
5375 switch (rettv->v_type)
5376 {
5377 case VAR_FUNC:
5378 case VAR_PARTIAL:
5379 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005380 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005381 return FAIL;
5382 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005383 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005384 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005385 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005386 case VAR_BOOL:
5387 case VAR_SPECIAL:
5388 case VAR_JOB:
5389 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005390 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005391 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005392 if (verbose)
5393 emsg(_(e_cannot_index_special_variable));
5394 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005395 case VAR_CLASS:
5396 case VAR_TYPEALIAS:
5397 if (verbose)
5398 check_typval_is_value(rettv);
5399 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005400 case VAR_UNKNOWN:
5401 case VAR_ANY:
5402 case VAR_VOID:
5403 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005405 emsg(_(e_cannot_index_special_variable));
5406 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005408 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005410 case VAR_STRING:
5411 case VAR_LIST:
5412 case VAR_DICT:
5413 case VAR_BLOB:
5414 break;
5415 case VAR_NUMBER:
5416 if (in_vim9script())
5417 emsg(_(e_cannot_index_number));
5418 break;
5419 }
5420 return OK;
5421}
5422
5423/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005424 * slice() function
5425 */
5426 void
5427f_slice(typval_T *argvars, typval_T *rettv)
5428{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005429 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005430 && ((argvars[0].v_type != VAR_STRING
5431 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005432 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005433 && check_for_list_arg(argvars, 0) == FAIL)
5434 || check_for_number_arg(argvars, 1) == FAIL
5435 || check_for_opt_number_arg(argvars, 2) == FAIL))
5436 return;
5437
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005438 if (check_can_index(&argvars[0], TRUE, FALSE) != OK)
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005439 return;
5440
5441 copy_tv(argvars, rettv);
5442 eval_index_inner(rettv, TRUE, argvars + 1,
5443 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5444 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005445}
5446
5447/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005448 * Apply index or range to "rettv".
5449 * "var1" is the first index, NULL for [:expr].
5450 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005451 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5452 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005453 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5454 */
5455 int
5456eval_index_inner(
5457 typval_T *rettv,
5458 int is_range,
5459 typval_T *var1,
5460 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005461 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005462 char_u *key,
5463 int keylen,
5464 int verbose)
5465{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005466 varnumber_T n1, n2 = 0;
5467 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005468
5469 n1 = 0;
5470 if (var1 != NULL && rettv->v_type != VAR_DICT)
5471 n1 = tv_get_number(var1);
5472
5473 if (is_range)
5474 {
5475 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005477 if (verbose)
5478 emsg(_(e_cannot_slice_dictionary));
5479 return FAIL;
5480 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005481 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005482 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005483 else
5484 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005485 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005486
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005487 switch (rettv->v_type)
5488 {
5489 case VAR_UNKNOWN:
5490 case VAR_ANY:
5491 case VAR_VOID:
5492 case VAR_FUNC:
5493 case VAR_PARTIAL:
5494 case VAR_FLOAT:
5495 case VAR_BOOL:
5496 case VAR_SPECIAL:
5497 case VAR_JOB:
5498 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005499 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005500 case VAR_CLASS:
5501 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005502 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005503 break; // not evaluating, skipping over subscript
5504
5505 case VAR_NUMBER:
5506 case VAR_STRING:
5507 {
5508 char_u *s = tv_get_string(rettv);
5509
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005511 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005512 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005513 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005514 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005515 else
5516 s = char_from_string(s, n1);
5517 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005518 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // The resulting variable is a substring. If the indexes
5521 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 if (n1 < 0)
5523 {
5524 n1 = len + n1;
5525 if (n1 < 0)
5526 n1 = 0;
5527 }
5528 if (n2 < 0)
5529 n2 = len + n2;
5530 else if (n2 >= len)
5531 n2 = len;
5532 if (n1 >= len || n2 < 0 || n1 > n2)
5533 s = NULL;
5534 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005535 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 else
5538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005539 // The resulting variable is a string of a single
5540 // character. If the index is too big or negative the
5541 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 if (n1 >= len || n1 < 0)
5543 s = NULL;
5544 else
5545 s = vim_strnsave(s + n1, 1);
5546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 clear_tv(rettv);
5548 rettv->v_type = VAR_STRING;
5549 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005550 }
5551 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005553 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005554 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5555 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005556 break;
5557
5558 case VAR_LIST:
5559 if (var1 == NULL)
5560 n1 = 0;
5561 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005562 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005563 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005564 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005565 return FAIL;
5566 break;
5567
5568 case VAR_DICT:
5569 {
5570 dictitem_T *item;
5571 typval_T tmp;
5572
5573 if (key == NULL)
5574 {
5575 key = tv_get_string_chk(var1);
5576 if (key == NULL)
5577 return FAIL;
5578 }
5579
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005580 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005581
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005582 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005583 {
5584 if (verbose)
5585 {
5586 if (keylen > 0)
5587 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005588 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005589 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005590 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005591 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005592
5593 copy_tv(&item->di_tv, &tmp);
5594 clear_tv(rettv);
5595 *rettv = tmp;
5596 }
5597 break;
5598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 return OK;
5600}
5601
5602/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005603 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005604 */
5605 char_u *
5606partial_name(partial_T *pt)
5607{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005608 if (pt != NULL)
5609 {
5610 if (pt->pt_name != NULL)
5611 return pt->pt_name;
5612 if (pt->pt_func != NULL)
5613 return pt->pt_func->uf_name;
5614 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005615 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005616}
5617
Bram Moolenaarddecc252016-04-06 22:59:37 +02005618 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005619partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005620{
5621 int i;
5622
5623 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005624 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005625 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005626 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005627 if (pt->pt_name != NULL)
5628 {
5629 func_unref(pt->pt_name);
5630 vim_free(pt->pt_name);
5631 }
5632 else
5633 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005634 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005635
Bram Moolenaar54656012021-06-09 20:50:46 +02005636 // "out_up" is no longer used, decrement refcount on partial that owns it.
5637 partial_unref(pt->pt_outer.out_up_partial);
5638
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005639 // Using pt_outer from another partial.
5640 partial_unref(pt->pt_outer_partial);
5641
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005642 // Decrease the reference count for the context of a closure. If down
5643 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005644 if (pt->pt_funcstack != NULL)
5645 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005646 --pt->pt_funcstack->fs_refcount;
5647 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005648 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005649 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005650 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5651 if (pt->pt_loopvars[i] != NULL)
5652 {
5653 --pt->pt_loopvars[i]->lvs_refcount;
5654 loopvars_check_refcount(pt->pt_loopvars[i]);
5655 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005656
Bram Moolenaarddecc252016-04-06 22:59:37 +02005657 vim_free(pt);
5658}
5659
5660/*
5661 * Unreference a closure: decrement the reference count and free it when it
5662 * becomes zero.
5663 */
5664 void
5665partial_unref(partial_T *pt)
5666{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005667 if (pt == NULL)
5668 return;
5669
5670 int done = FALSE;
5671
5672 if (--pt->pt_refcount <= 0)
5673 partial_free(pt);
5674
5675 // If the reference count goes down to one, the funcstack may be the
5676 // only reference and can be freed if no other partials reference it.
5677 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005678 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005679 // careful: if the funcstack is freed it may contain this partial
5680 // and it gets freed as well
5681 if (pt->pt_funcstack != NULL)
5682 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005683
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005684 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005685 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005686 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005687
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005688 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5689 if (pt->pt_loopvars[depth] != NULL
5690 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005691 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005692 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005693 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005694}
5695
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005697 * Return the next (unique) copy ID.
5698 * Used for serializing nested structures.
5699 */
5700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005701get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005702{
5703 current_copyID += COPYID_INC;
5704 return current_copyID;
5705}
5706
5707/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005708 * Garbage collection for lists and dictionaries.
5709 *
5710 * We use reference counts to be able to free most items right away when they
5711 * are no longer used. But for composite items it's possible that it becomes
5712 * unused while the reference count is > 0: When there is a recursive
5713 * reference. Example:
5714 * :let l = [1, 2, 3]
5715 * :let d = {9: l}
5716 * :let l[1] = d
5717 *
5718 * Since this is quite unusual we handle this with garbage collection: every
5719 * once in a while find out which lists and dicts are not referenced from any
5720 * variable.
5721 *
5722 * Here is a good reference text about garbage collection (refers to Python
5723 * but it applies to all reference-counting mechanisms):
5724 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005725 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005726
5727/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005728 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005729 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005730 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005731 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005732 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005733garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005734{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005735 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005737 buf_T *buf;
5738 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005739 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005740 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005741
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005742 if (!testing)
5743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005744 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005745 want_garbage_collect = FALSE;
5746 may_garbage_collect = FALSE;
5747 garbage_collect_at_exit = FALSE;
5748 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005749
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005750 // The execution stack can grow big, limit the size.
5751 if (exestack.ga_maxlen - exestack.ga_len > 500)
5752 {
5753 size_t new_len;
5754 char_u *pp;
5755 int n;
5756
5757 // Keep 150% of the current size, with a minimum of the growth size.
5758 n = exestack.ga_len / 2;
5759 if (n < exestack.ga_growsize)
5760 n = exestack.ga_growsize;
5761
5762 // Don't make it bigger though.
5763 if (exestack.ga_len + n < exestack.ga_maxlen)
5764 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005765 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005766 pp = vim_realloc(exestack.ga_data, new_len);
5767 if (pp == NULL)
5768 return FAIL;
5769 exestack.ga_maxlen = exestack.ga_len + n;
5770 exestack.ga_data = pp;
5771 }
5772 }
5773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005774 // We advance by two because we add one for items referenced through
5775 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005776 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005777
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005778 /*
5779 * 1. Go through all accessible variables and mark all lists and dicts
5780 * with copyID.
5781 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005783 // Don't free variables in the previous_funccal list unless they are only
5784 // referenced through previous_funccal. This must be first, because if
5785 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005786 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005787
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005788 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005789 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005791 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005792 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005793 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5794 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005797 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005798 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5799 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005800 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005801 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005802 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005803 abort = abort || set_ref_in_item(
5804 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005805#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005806 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005807 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5808 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005809 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005810 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005811 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5812 NULL, NULL);
5813#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005815 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005816 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005817 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5818 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005819 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005820 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005823 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005826 abort = abort || set_ref_in_functions(copyID);
5827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005828 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005829 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005830
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005831 // funcstacks keep variables for closures
5832 abort = abort || set_ref_in_funcstacks(copyID);
5833
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005834 // loopvars keep variables for loop blocks
5835 abort = abort || set_ref_in_loopvars(copyID);
5836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005837 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005838 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005839
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005840 // callbacks in buffers
5841 abort = abort || set_ref_in_buffers(copyID);
5842
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005843 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5844 abort = abort || set_ref_in_insexpand_funcs(copyID);
5845
5846 // 'operatorfunc' callback
5847 abort = abort || set_ref_in_opfunc(copyID);
5848
5849 // 'tagfunc' callback
5850 abort = abort || set_ref_in_tagfunc(copyID);
5851
5852 // 'imactivatefunc' and 'imstatusfunc' callbacks
5853 abort = abort || set_ref_in_im_funcs(copyID);
5854
Bram Moolenaar1dced572012-04-05 16:54:08 +02005855#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005857#endif
5858
Bram Moolenaardb913952012-06-29 12:54:53 +02005859#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005860 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005861#endif
5862
5863#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005864 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005865#endif
5866
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005867#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005868 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005869 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005870#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005871#ifdef FEAT_NETBEANS_INTG
5872 abort = abort || set_ref_in_nb_channel(copyID);
5873#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005874
Bram Moolenaare3188e22016-05-31 21:13:04 +02005875#ifdef FEAT_TIMERS
5876 abort = abort || set_ref_in_timer(copyID);
5877#endif
5878
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005879#ifdef FEAT_QUICKFIX
5880 abort = abort || set_ref_in_quickfix(copyID);
5881#endif
5882
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005883#ifdef FEAT_TERMINAL
5884 abort = abort || set_ref_in_term(copyID);
5885#endif
5886
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005887#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005888 abort = abort || set_ref_in_popups(copyID);
5889#endif
5890
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005891 abort = abort || set_ref_in_classes(copyID);
5892
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005893 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005894 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005895 /*
5896 * 2. Free lists and dictionaries that are not referenced.
5897 */
5898 did_free = free_unref_items(copyID);
5899
5900 /*
5901 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005902 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005903 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005904 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005905 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005906 else if (p_verbose > 0)
5907 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005908 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005909 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005910
5911 return did_free;
5912}
5913
5914/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005915 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005916 */
5917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005918free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005919{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005920 int did_free = FALSE;
5921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 // Let all "free" functions know that we are here. This means no
5923 // dictionaries, lists, channels or jobs are to be freed, because we will
5924 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005925 in_free_unref_items = TRUE;
5926
5927 /*
5928 * PASS 1: free the contents of the items. We don't free the items
5929 * themselves yet, so that it is possible to decrement refcount counters
5930 */
5931
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005932 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005933 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005934
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005935 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005936 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005937
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005938 // Go through the list of objects and free items without this copyID.
5939 did_free |= object_free_nonref(copyID);
5940
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005941 // Go through the list of classes and free items without this copyID.
5942 did_free |= class_free_nonref(copyID);
5943
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005944#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 // Go through the list of jobs and free items without the copyID. This
5946 // must happen before doing channels, because jobs refer to channels, but
5947 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005948 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005950 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005951 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5952#endif
5953
5954 /*
5955 * PASS 2: free the items themselves.
5956 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005957 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005958 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005959 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005960
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005961#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005962 // Go through the list of jobs and free items without the copyID. This
5963 // must happen before doing channels, because jobs refer to channels, but
5964 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 free_unused_jobs(copyID, COPYID_MASK);
5966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005967 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005968 free_unused_channels(copyID, COPYID_MASK);
5969#endif
5970
5971 in_free_unref_items = FALSE;
5972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 return did_free;
5974}
5975
5976/*
5977 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005978 * "list_stack" is used to add lists to be marked. Can be NULL.
5979 *
5980 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005981 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005983set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984{
5985 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005986 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005988 hashtab_T *cur_ht;
5989 ht_stack_T *ht_stack = NULL;
5990 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005991
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005992 cur_ht = ht;
5993 for (;;)
5994 {
5995 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005997 // Mark each item in the hashtab. If the item contains a hashtab
5998 // it is added to ht_stack, if it contains a list it is added to
5999 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006000 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00006001 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006002 if (!HASHITEM_EMPTY(hi))
6003 {
6004 --todo;
6005 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6006 &ht_stack, list_stack);
6007 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006008 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006009
6010 if (ht_stack == NULL)
6011 break;
6012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006013 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006014 cur_ht = ht_stack->ht;
6015 tempitem = ht_stack;
6016 ht_stack = ht_stack->prev;
6017 free(tempitem);
6018 }
6019
6020 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006021}
6022
Dominique Pelle748b3082022-01-08 12:41:16 +00006023#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6024 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006025/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006026 * Mark a dict and its items with "copyID".
6027 * Returns TRUE if setting references failed somehow.
6028 */
6029 int
6030set_ref_in_dict(dict_T *d, int copyID)
6031{
6032 if (d != NULL && d->dv_copyID != copyID)
6033 {
6034 d->dv_copyID = copyID;
6035 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6036 }
6037 return FALSE;
6038}
Dominique Pelle748b3082022-01-08 12:41:16 +00006039#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006040
6041/*
6042 * Mark a list and its items with "copyID".
6043 * Returns TRUE if setting references failed somehow.
6044 */
6045 int
6046set_ref_in_list(list_T *ll, int copyID)
6047{
6048 if (ll != NULL && ll->lv_copyID != copyID)
6049 {
6050 ll->lv_copyID = copyID;
6051 return set_ref_in_list_items(ll, copyID, NULL);
6052 }
6053 return FALSE;
6054}
6055
6056/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006057 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006058 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6059 *
6060 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006061 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006062 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006063set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006065 listitem_T *li;
6066 int abort = FALSE;
6067 list_T *cur_l;
6068 list_stack_T *list_stack = NULL;
6069 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006070
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006071 cur_l = l;
6072 for (;;)
6073 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006074 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 // Mark each item in the list. If the item contains a hashtab
6076 // it is added to ht_stack, if it contains a list it is added to
6077 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006078 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6079 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6080 ht_stack, &list_stack);
6081 if (list_stack == NULL)
6082 break;
6083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006084 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006085 cur_l = list_stack->list;
6086 tempitem = list_stack;
6087 list_stack = list_stack->prev;
6088 free(tempitem);
6089 }
6090
6091 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092}
6093
6094/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00006095 * Mark the partial in callback 'cb' with "copyID".
6096 */
6097 int
6098set_ref_in_callback(callback_T *cb, int copyID)
6099{
6100 typval_T tv;
6101
6102 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
6103 return FALSE;
6104
6105 tv.v_type = VAR_PARTIAL;
6106 tv.vval.v_partial = cb->cb_partial;
6107 return set_ref_in_item(&tv, copyID, NULL, NULL);
6108}
6109
6110/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006111 * Mark the dict "dd" with "copyID".
6112 * Also see set_ref_in_item().
6113 */
6114 static int
6115set_ref_in_item_dict(
6116 dict_T *dd,
6117 int copyID,
6118 ht_stack_T **ht_stack,
6119 list_stack_T **list_stack)
6120{
6121 if (dd == NULL || dd->dv_copyID == copyID)
6122 return FALSE;
6123
6124 // Didn't see this dict yet.
6125 dd->dv_copyID = copyID;
6126 if (ht_stack == NULL)
6127 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6128
6129 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
6130 if (newitem == NULL)
6131 return TRUE;
6132
6133 newitem->ht = &dd->dv_hashtab;
6134 newitem->prev = *ht_stack;
6135 *ht_stack = newitem;
6136
6137 return FALSE;
6138}
6139
6140/*
6141 * Mark the list "ll" with "copyID".
6142 * Also see set_ref_in_item().
6143 */
6144 static int
6145set_ref_in_item_list(
6146 list_T *ll,
6147 int copyID,
6148 ht_stack_T **ht_stack,
6149 list_stack_T **list_stack)
6150{
6151 if (ll == NULL || ll->lv_copyID == copyID)
6152 return FALSE;
6153
6154 // Didn't see this list yet.
6155 ll->lv_copyID = copyID;
6156 if (list_stack == NULL)
6157 return set_ref_in_list_items(ll, copyID, ht_stack);
6158
6159 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
6160 if (newitem == NULL)
6161 return TRUE;
6162
6163 newitem->list = ll;
6164 newitem->prev = *list_stack;
6165 *list_stack = newitem;
6166
6167 return FALSE;
6168}
6169
6170/*
6171 * Mark the partial "pt" with "copyID".
6172 * Also see set_ref_in_item().
6173 */
6174 static int
6175set_ref_in_item_partial(
6176 partial_T *pt,
6177 int copyID,
6178 ht_stack_T **ht_stack,
6179 list_stack_T **list_stack)
6180{
6181 if (pt == NULL || pt->pt_copyID == copyID)
6182 return FALSE;
6183
6184 // Didn't see this partial yet.
6185 pt->pt_copyID = copyID;
6186
6187 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
6188
6189 if (pt->pt_dict != NULL)
6190 {
6191 typval_T dtv;
6192
6193 dtv.v_type = VAR_DICT;
6194 dtv.vval.v_dict = pt->pt_dict;
6195 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6196 }
6197
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02006198 if (pt->pt_obj != NULL)
6199 {
6200 typval_T objtv;
6201
6202 objtv.v_type = VAR_OBJECT;
6203 objtv.vval.v_object = pt->pt_obj;
6204 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
6205 }
6206
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006207 for (int i = 0; i < pt->pt_argc; ++i)
6208 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6209 ht_stack, list_stack);
6210 // pt_funcstack is handled in set_ref_in_funcstacks()
6211 // pt_loopvars is handled in set_ref_in_loopvars()
6212
6213 return abort;
6214}
6215
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006216#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006217/*
6218 * Mark the job "pt" with "copyID".
6219 * Also see set_ref_in_item().
6220 */
6221 static int
6222set_ref_in_item_job(
6223 job_T *job,
6224 int copyID,
6225 ht_stack_T **ht_stack,
6226 list_stack_T **list_stack)
6227{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006228 typval_T dtv;
6229
6230 if (job == NULL || job->jv_copyID == copyID)
6231 return FALSE;
6232
6233 job->jv_copyID = copyID;
6234 if (job->jv_channel != NULL)
6235 {
6236 dtv.v_type = VAR_CHANNEL;
6237 dtv.vval.v_channel = job->jv_channel;
6238 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6239 }
6240 if (job->jv_exit_cb.cb_partial != NULL)
6241 {
6242 dtv.v_type = VAR_PARTIAL;
6243 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
6244 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6245 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006246
6247 return FALSE;
6248}
6249
6250/*
6251 * Mark the channel "ch" with "copyID".
6252 * Also see set_ref_in_item().
6253 */
6254 static int
6255set_ref_in_item_channel(
6256 channel_T *ch,
6257 int copyID,
6258 ht_stack_T **ht_stack,
6259 list_stack_T **list_stack)
6260{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006261 typval_T dtv;
6262
6263 if (ch == NULL || ch->ch_copyID == copyID)
6264 return FALSE;
6265
6266 ch->ch_copyID = copyID;
6267 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
6268 {
6269 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
6270 jq != NULL; jq = jq->jq_next)
6271 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6272 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6273 cq = cq->cq_next)
6274 if (cq->cq_callback.cb_partial != NULL)
6275 {
6276 dtv.v_type = VAR_PARTIAL;
6277 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6278 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6279 }
6280 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6281 {
6282 dtv.v_type = VAR_PARTIAL;
6283 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6284 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6285 }
6286 }
6287 if (ch->ch_callback.cb_partial != NULL)
6288 {
6289 dtv.v_type = VAR_PARTIAL;
6290 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6291 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6292 }
6293 if (ch->ch_close_cb.cb_partial != NULL)
6294 {
6295 dtv.v_type = VAR_PARTIAL;
6296 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6297 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6298 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006299
6300 return FALSE;
6301}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006302#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006303
6304/*
6305 * Mark the class "cl" with "copyID".
6306 * Also see set_ref_in_item().
6307 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006308 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006309set_ref_in_item_class(
6310 class_T *cl,
6311 int copyID,
6312 ht_stack_T **ht_stack,
6313 list_stack_T **list_stack)
6314{
6315 int abort = FALSE;
6316
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006317 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006318 return FALSE;
6319
6320 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006321 if (cl->class_members_tv != NULL)
6322 {
6323 // The "class_members_tv" table is allocated only for regular classes
6324 // and not for interfaces.
6325 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6326 abort = abort || set_ref_in_item(
6327 &cl->class_members_tv[i],
6328 copyID, ht_stack, list_stack);
6329 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006330
6331 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6332 abort = abort || set_ref_in_func(NULL,
6333 cl->class_class_functions[i], copyID);
6334
6335 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6336 abort = abort || set_ref_in_func(NULL,
6337 cl->class_obj_methods[i], copyID);
6338
6339 return abort;
6340}
6341
6342/*
6343 * Mark the object "cl" with "copyID".
6344 * Also see set_ref_in_item().
6345 */
6346 static int
6347set_ref_in_item_object(
6348 object_T *obj,
6349 int copyID,
6350 ht_stack_T **ht_stack,
6351 list_stack_T **list_stack)
6352{
6353 int abort = FALSE;
6354
6355 if (obj == NULL || obj->obj_copyID == copyID)
6356 return FALSE;
6357
6358 obj->obj_copyID = copyID;
6359
6360 // The typval_T array is right after the object_T.
6361 typval_T *mtv = (typval_T *)(obj + 1);
6362 for (int i = 0; !abort
6363 && i < obj->obj_class->class_obj_member_count; ++i)
6364 abort = abort || set_ref_in_item(mtv + i, copyID,
6365 ht_stack, list_stack);
6366
6367 return abort;
6368}
6369
6370/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006371 * Mark all lists, dicts and other container types referenced through typval
6372 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006373 * "list_stack" is used to add lists to be marked. Can be NULL.
6374 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6375 *
6376 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006377 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006379set_ref_in_item(
6380 typval_T *tv,
6381 int copyID,
6382 ht_stack_T **ht_stack,
6383 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006384{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006385 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006386
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006387 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006388 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006389 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006390 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6391 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006392
6393 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006394 return set_ref_in_item_list(tv->vval.v_list, copyID,
6395 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006396
6397 case VAR_FUNC:
6398 {
6399 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6400 break;
6401 }
6402
6403 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006404 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6405 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006406
6407 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006408#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006409 return set_ref_in_item_job(tv->vval.v_job, copyID,
6410 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006411#else
6412 break;
6413#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006414
6415 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006416#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006417 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006418 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006419#else
6420 break;
6421#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006422
6423 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006424 return set_ref_in_item_class(tv->vval.v_class, copyID,
6425 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006426
6427 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006428 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006429 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006430
6431 case VAR_UNKNOWN:
6432 case VAR_ANY:
6433 case VAR_VOID:
6434 case VAR_BOOL:
6435 case VAR_SPECIAL:
6436 case VAR_NUMBER:
6437 case VAR_FLOAT:
6438 case VAR_STRING:
6439 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006440 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006441 case VAR_INSTR:
6442 // Types that do not contain any other item
6443 break;
6444 }
6445
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006446 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006447}
6448
Bram Moolenaar8c711452005-01-14 21:53:12 +00006449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 * Return a string with the string representation of a variable.
6451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006453 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006454 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006455 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006456 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006457 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6458 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006459 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006461 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006462echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463 typval_T *tv,
6464 char_u **tofree,
6465 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006466 int copyID,
6467 int echo_style,
6468 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006469 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006471 static int recurse = 0;
6472 char_u *r = NULL;
6473
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006475 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006476 if (!did_echo_string_emsg)
6477 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 // Only give this message once for a recursive call to avoid
6479 // flooding the user with errors. And stop iterating over lists
6480 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006481 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006482 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006485 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 }
6487 ++recurse;
6488
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489 switch (tv->v_type)
6490 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006491 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006492 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006493 {
6494 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006495 r = tv->vval.v_string;
6496 if (r == NULL)
6497 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006498 }
6499 else
6500 {
6501 *tofree = string_quote(tv->vval.v_string, FALSE);
6502 r = *tofree;
6503 }
6504 break;
6505
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006507 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006508 char_u buf[MAX_FUNC_NAME_LEN];
6509
6510 if (echo_style)
6511 {
LemonBoya5d35902022-04-29 21:15:02 +01006512 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6513 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006514 buf, MAX_FUNC_NAME_LEN);
6515 if (r == buf)
6516 {
6517 r = vim_strsave(buf);
6518 *tofree = r;
6519 }
6520 else
6521 *tofree = NULL;
6522 }
6523 else
6524 {
6525 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6526 : make_ufunc_name_readable(
6527 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6528 TRUE);
6529 r = *tofree;
6530 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006531 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006532 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006533
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006534 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006535 {
6536 partial_T *pt = tv->vval.v_partial;
6537 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006538 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006539 garray_T ga;
6540 int i;
6541 char_u *tf;
6542
6543 ga_init2(&ga, 1, 100);
6544 ga_concat(&ga, (char_u *)"function(");
6545 if (fname != NULL)
6546 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006547 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006548 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006549 && vim_isupper(fname[1]))
6550 {
6551 ga_concat(&ga, (char_u *)"'g:");
6552 ga_concat(&ga, fname + 1);
6553 }
6554 else
6555 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006556 vim_free(fname);
6557 }
6558 if (pt != NULL && pt->pt_argc > 0)
6559 {
6560 ga_concat(&ga, (char_u *)", [");
6561 for (i = 0; i < pt->pt_argc; ++i)
6562 {
6563 if (i > 0)
6564 ga_concat(&ga, (char_u *)", ");
6565 ga_concat(&ga,
6566 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6567 vim_free(tf);
6568 }
6569 ga_concat(&ga, (char_u *)"]");
6570 }
6571 if (pt != NULL && pt->pt_dict != NULL)
6572 {
6573 typval_T dtv;
6574
6575 ga_concat(&ga, (char_u *)", ");
6576 dtv.v_type = VAR_DICT;
6577 dtv.vval.v_dict = pt->pt_dict;
6578 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6579 vim_free(tf);
6580 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006581 // terminate with ')' and a NUL
6582 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006583
6584 *tofree = ga.ga_data;
6585 r = *tofree;
6586 break;
6587 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006588
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006589 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006590 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006591 break;
6592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006594 if (tv->vval.v_list == NULL)
6595 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006596 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006598 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006599 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006600 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6601 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006602 {
6603 *tofree = NULL;
6604 r = (char_u *)"[...]";
6605 }
6606 else
6607 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006608 int old_copyID = tv->vval.v_list->lv_copyID;
6609
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006610 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006611 *tofree = list2string(tv, copyID, restore_copyID);
6612 if (restore_copyID)
6613 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 r = *tofree;
6615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006616 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006617
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006619 if (tv->vval.v_dict == NULL)
6620 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006621 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006622 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006623 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006625 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6626 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006627 {
6628 *tofree = NULL;
6629 r = (char_u *)"{...}";
6630 }
6631 else
6632 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006633 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006634
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006635 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006636 *tofree = dict2string(tv, copyID, restore_copyID);
6637 if (restore_copyID)
6638 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006639 r = *tofree;
6640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006641 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006643 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006644 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006645 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006647 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006648 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006649 break;
6650
Bram Moolenaar835dc632016-02-07 14:27:38 +01006651 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006652 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006653#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006654 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006655 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6656 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006657 if (composite_val)
6658 {
6659 *tofree = string_quote(r, FALSE);
6660 r = *tofree;
6661 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006662#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006664
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006665 case VAR_INSTR:
6666 *tofree = NULL;
6667 r = (char_u *)"instructions";
6668 break;
6669
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006670 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006671 {
6672 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006673 char *s = "class";
Yegappan Lakshmananda9d3452024-05-02 13:02:36 +02006674 if (cl != NULL && IS_INTERFACE(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006675 s = "interface";
Yegappan Lakshmananda9d3452024-05-02 13:02:36 +02006676 else if (cl != NULL && IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006677 s = "enum";
6678 size_t len = STRLEN(s) + 1 +
6679 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006680 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006681 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006682 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6683 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006684 break;
6685
6686 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006687 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6688 echo_style, restore_copyID,
6689 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006690 break;
6691
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006692 case VAR_FLOAT:
6693 *tofree = NULL;
6694 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6695 r = numbuf;
6696 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006697
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006698 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006699 case VAR_SPECIAL:
6700 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006701 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006702 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006703
6704 case VAR_TYPEALIAS:
6705 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6706 r = *tofree;
6707 if (r == NULL)
6708 r = (char_u *)"";
6709 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711
Bram Moolenaar8502c702014-06-17 12:51:16 +02006712 if (--recurse == 0)
6713 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006714 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715}
6716
6717/*
6718 * Return a string with the string representation of a variable.
6719 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6720 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006721 * Does not put quotes around strings, as ":echo" displays values.
6722 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6723 * May return NULL.
6724 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006725 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006726echo_string(
6727 typval_T *tv,
6728 char_u **tofree,
6729 char_u *numbuf,
6730 int copyID)
6731{
6732 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6733}
6734
6735/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006736 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6737 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006738 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006739 */
6740 int
6741buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6742{
6743 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006744 char_u *t;
6745 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006746
6747 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6748 return -1;
6749
6750 if (lnum > buf->b_ml.ml_line_count)
6751 lnum = buf->b_ml.ml_line_count;
6752
6753 str = ml_get_buf(buf, lnum, FALSE);
6754 if (str == NULL)
6755 return -1;
6756
6757 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006758 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006759
Bram Moolenaar91458462021-01-13 20:08:38 +01006760 // count the number of characters
6761 t = str;
6762 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6763 t += mb_ptr2len(t);
6764
6765 // In insert mode, when the cursor is at the end of a non-empty line,
6766 // byteidx points to the NUL character immediately past the end of the
6767 // string. In this case, add one to the character count.
6768 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6769 count++;
6770
6771 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006772}
6773
6774/*
6775 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006776 * byte index. Works only for loaded buffers. Returns -1 on failure.
6777 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006778 */
6779 int
6780buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6781{
6782 char_u *str;
6783 char_u *t;
6784
6785 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6786 return -1;
6787
6788 if (lnum > buf->b_ml.ml_line_count)
6789 lnum = buf->b_ml.ml_line_count;
6790
6791 str = ml_get_buf(buf, lnum, FALSE);
6792 if (str == NULL)
6793 return -1;
6794
6795 // Convert the character offset to a byte offset
6796 t = str;
6797 while (*t != NUL && --charidx > 0)
6798 t += mb_ptr2len(t);
6799
Bram Moolenaar91458462021-01-13 20:08:38 +01006800 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006801}
6802
6803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006805 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808var2fpos(
6809 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006810 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006811 int *fnum, // set to fnum for '0, 'A, etc.
6812 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006814 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006816 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006818 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006819 if (varp->v_type == VAR_LIST)
6820 {
6821 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006822 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006823 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006824 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006825
6826 l = varp->vval.v_list;
6827 if (l == NULL)
6828 return NULL;
6829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006830 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006831 pos.lnum = list_find_nr(l, 0L, &error);
6832 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006833 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006834 if (charcol)
6835 len = (long)mb_charlen(ml_get(pos.lnum));
6836 else
John Marriottbfcc8952024-03-11 22:04:45 +01006837 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006838
Bram Moolenaarec65d772020-08-20 22:29:12 +02006839 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006841 li = list_find(l, 1L);
6842 if (li != NULL && li->li_tv.v_type == VAR_STRING
6843 && li->li_tv.vval.v_string != NULL
6844 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006845 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006846 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006847 }
6848 else
6849 {
6850 pos.col = list_find_nr(l, 1L, &error);
6851 if (error)
6852 return NULL;
6853 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006854
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006855 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006856 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006857 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006858 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006860 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006861 pos.coladd = list_find_nr(l, 2L, &error);
6862 if (error)
6863 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006864
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006865 return &pos;
6866 }
6867
Bram Moolenaarc5809432021-03-27 21:23:30 +01006868 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6869 return NULL;
6870
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006872 if (name == NULL)
6873 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006874
6875 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006876 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006877 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006878 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006879 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006880 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006881 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006882 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006883 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006884 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006885 pos = VIsual;
6886 else
6887 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006888 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006889 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006890 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006892 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006893 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6895 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006896 pos = *pp;
6897 }
6898 if (pos.lnum != 0)
6899 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006900 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006901 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6902 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006904
Bram Moolenaara5525202006-03-02 22:52:09 +00006905 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006906
Bram Moolenaar477933c2007-07-17 14:32:23 +00006907 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006908 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006909 // the "w_valid" flags are not reset when moving the cursor, but they
6910 // do matter for update_topline() and validate_botline().
6911 check_cursor_moved(curwin);
6912
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006913 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006914 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006915 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006916 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006917 // In silent Ex mode topline is zero, but that's not a valid line
6918 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006919 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006920 return &pos;
6921 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006922 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006923 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006924 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006925 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006926 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006927 return &pos;
6928 }
6929 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006930 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006932 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 pos.lnum = curbuf->b_ml.ml_line_count;
6935 pos.col = 0;
6936 }
6937 else
6938 {
6939 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006940 if (charcol)
6941 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6942 else
John Marriottbfcc8952024-03-11 22:04:45 +01006943 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945 return &pos;
6946 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006947 if (in_vim9script())
6948 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 return NULL;
6950}
6951
6952/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006953 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006954 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006955 * Note that the column is passed on as-is, the caller may want to decrement
6956 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006957 * If "charcol" is TRUE use the column as the character index instead of the
6958 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006959 * Return FAIL when conversion is not possible, doesn't check the position for
6960 * validity.
6961 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963list2fpos(
6964 typval_T *arg,
6965 pos_T *posp,
6966 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006967 colnr_T *curswantp,
6968 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006969{
6970 list_T *l = arg->vval.v_list;
6971 long i = 0;
6972 long n;
6973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006974 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6975 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006976 if (arg->v_type != VAR_LIST
6977 || l == NULL
6978 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006979 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006980 return FAIL;
6981
6982 if (fnump != NULL)
6983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006984 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006985 if (n < 0)
6986 return FAIL;
6987 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006988 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006989 *fnump = n;
6990 }
6991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006992 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006993 if (n < 0)
6994 return FAIL;
6995 posp->lnum = n;
6996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006997 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006998 if (n < 0)
6999 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007000 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01007001 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007002 if (charcol)
7003 {
7004 buf_T *buf;
7005
7006 // Get the text for the specified line in a loaded buffer
7007 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
7008 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
7009 return FAIL;
7010
Bram Moolenaar79f23442022-10-10 12:42:57 +01007011 n = buf_charidx_to_byteidx(buf,
7012 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007013 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007014 posp->col = n;
7015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007016 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007017 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00007018 posp->coladd = 0;
7019 else
7020 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007021
Bram Moolenaar493c1782014-05-28 14:34:46 +02007022 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007023 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02007024
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007025 return OK;
7026}
7027
7028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 * Get the length of an environment variable name.
7030 * Advance "arg" to the first character after the name.
7031 * Return 0 for error.
7032 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007033 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 char_u *p;
7037 int len;
7038
7039 for (p = *arg; vim_isIDc(*p); ++p)
7040 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007041 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 return 0;
7043
7044 len = (int)(p - *arg);
7045 *arg = p;
7046 return len;
7047}
7048
7049/*
7050 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007051 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 * Return 0 if something is wrong.
7053 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007055get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 char_u *p;
7058 int len;
7059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007060 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007062 {
7063 if (*p == ':')
7064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007065 // "s:" is start of "s:var", but "n:" is not and can be used in
7066 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007067 len = (int)(p - *arg);
7068 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
7069 || len > 1)
7070 break;
7071 }
7072 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007073 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 return 0;
7075
7076 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007077 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 return len;
7080}
7081
7082/*
Bram Moolenaara7043832005-01-21 11:56:39 +00007083 * Get the length of the name of a variable or function.
7084 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007086 * Return -1 if curly braces expansion failed.
7087 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 * If the name contains 'magic' {}'s, expand them and return the
7089 * expanded name in an allocated string via 'alias' - caller must free.
7090 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007092get_name_len(
7093 char_u **arg,
7094 char_u **alias,
7095 int evaluate,
7096 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
7098 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 char_u *p;
7100 char_u *expr_start;
7101 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007103 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
7106 && (*arg)[2] == (int)KE_SNR)
7107 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007108 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 *arg += 3;
7110 return get_id_len(arg) + 3;
7111 }
7112 len = eval_fname_script(*arg);
7113 if (len > 0)
7114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007115 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 *arg += len;
7117 }
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007120 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007122 p = find_name_end(*arg, &expr_start, &expr_end,
7123 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 if (expr_start != NULL)
7125 {
7126 char_u *temp_string;
7127
7128 if (!evaluate)
7129 {
7130 len += (int)(p - *arg);
7131 *arg = skipwhite(p);
7132 return len;
7133 }
7134
7135 /*
7136 * Include any <SID> etc in the expanded string:
7137 * Thus the -len here.
7138 */
7139 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
7140 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007141 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 *alias = temp_string;
7143 *arg = skipwhite(p);
7144 return (int)STRLEN(temp_string);
7145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01007148 // Only give an error when there is something, otherwise it will be
7149 // reported at a higher level.
7150 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007151 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 return len;
7154}
7155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007156/*
7157 * Find the end of a variable or function name, taking care of magic braces.
7158 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7159 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007160 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161 * Return a pointer to just after the name. Equal to "arg" if there is no
7162 * valid name.
7163 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007164 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165find_name_end(
7166 char_u *arg,
7167 char_u **expr_start,
7168 char_u **expr_end,
7169 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007171 int mb_nest = 0;
7172 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007174 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02007175 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007177 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007179 *expr_start = NULL;
7180 *expr_end = NULL;
7181 }
7182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007183 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007184 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007185 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007186 return arg;
7187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188 for (p = arg; *p != NUL
7189 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007190 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02007191 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007192 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007193 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007194 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007195 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007196 if (*p == '\'')
7197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007198 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007199 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007200 ;
7201 if (*p == NUL)
7202 break;
7203 }
7204 else if (*p == '"')
7205 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007206 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007207 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007208 if (*p == '\\' && p[1] != NUL)
7209 ++p;
7210 if (*p == NUL)
7211 break;
7212 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007213 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007215 // "s:" is start of "s:var", but "n:" is not and can be used in
7216 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007217 len = (int)(p - arg);
7218 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007219 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007220 break;
7221 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225 if (*p == '[')
7226 ++br_nest;
7227 else if (*p == ']')
7228 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007230
zeertzjqa93d9cd2023-05-02 16:25:47 +01007231 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007233 if (*p == '{')
7234 {
7235 mb_nest++;
7236 if (expr_start != NULL && *expr_start == NULL)
7237 *expr_start = p;
7238 }
7239 else if (*p == '}')
7240 {
7241 mb_nest--;
7242 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7243 *expr_end = p;
7244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 }
7247
7248 return p;
7249}
7250
7251/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007252 * Expands out the 'magic' {}'s in a variable/function name.
7253 * Note that this can call itself recursively, to deal with
7254 * constructs like foo{bar}{baz}{bam}
7255 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7256 * "in_start" ^
7257 * "expr_start" ^
7258 * "expr_end" ^
7259 * "in_end" ^
7260 *
7261 * Returns a new allocated string, which the caller must free.
7262 * Returns NULL for failure.
7263 */
7264 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007265make_expanded_name(
7266 char_u *in_start,
7267 char_u *expr_start,
7268 char_u *expr_end,
7269 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007270{
7271 char_u c1;
7272 char_u *retval = NULL;
7273 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007274
7275 if (expr_end == NULL || in_end == NULL)
7276 return NULL;
7277 *expr_start = NUL;
7278 *expr_end = NUL;
7279 c1 = *in_end;
7280 *in_end = NUL;
7281
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007282 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007283 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007284 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007285 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7286 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007287 if (retval != NULL)
7288 {
7289 STRCPY(retval, in_start);
7290 STRCAT(retval, temp_result);
7291 STRCAT(retval, expr_end + 1);
7292 }
7293 }
7294 vim_free(temp_result);
7295
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007296 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007297 *expr_start = '{';
7298 *expr_end = '}';
7299
7300 if (retval != NULL)
7301 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007302 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007303 if (expr_start != NULL)
7304 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007305 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007306 temp_result = make_expanded_name(retval, expr_start,
7307 expr_end, temp_result);
7308 vim_free(retval);
7309 retval = temp_result;
7310 }
7311 }
7312
7313 return retval;
7314}
7315
7316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007323 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007324}
7325
7326/*
7327 * Return TRUE if character "c" can be used as the first character in a
7328 * variable or function name (excluding '{' and '}').
7329 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007330 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007332{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007333 return ASCII_ISALPHA(c) || c == '_';
7334}
7335
7336/*
7337 * Return TRUE if character "c" can be used as the first character of a
7338 * dictionary key.
7339 */
7340 int
7341eval_isdictc(int c)
7342{
7343 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007347 * Handle:
7348 * - expr[expr], expr[expr:expr] subscript
7349 * - ".name" lookup
7350 * - function call with Funcref variable: func(expr)
7351 * - method call: var->method()
7352 *
7353 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007354 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007355 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007356 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007357handle_subscript(
7358 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007359 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007360 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007361 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007362 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007363{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007364 int evaluate = evalarg != NULL
7365 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007366 int ret = OK;
7367 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007368 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007369 int getnext;
7370 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007371
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007372 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007373 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007374 // When at the end of the line and ".name" or "->{" or "->X" follows in
7375 // the next line then consume the line break.
7376 p = eval_next_non_blank(*arg, evalarg, &getnext);
7377 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007378 && ((*p == '.'
7379 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7380 || rettv->v_type == VAR_CLASS
7381 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007382 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7383 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7384 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007385 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007386 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007387 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007388 check_white = FALSE;
7389 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007390
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007391 if (rettv->v_type == VAR_ANY)
7392 {
7393 char_u *exp_name;
7394 int cc;
7395 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007396 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007397 type_T *type;
7398
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007399 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007400 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007401 if (**arg != '.')
7402 {
7403 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007404 semsg(_(e_expected_dot_after_name_str),
7405 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007406 ret = FAIL;
7407 break;
7408 }
7409 ++*arg;
7410 if (IS_WHITE_OR_NUL(**arg))
7411 {
7412 if (verbose)
7413 emsg(_(e_no_white_space_allowed_after_dot));
7414 ret = FAIL;
7415 break;
7416 }
7417
7418 // isolate the name
7419 exp_name = *arg;
7420 while (eval_isnamec(**arg))
7421 ++*arg;
7422 cc = **arg;
7423 **arg = NUL;
7424
7425 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007426 evalarg == NULL ? NULL : evalarg->eval_cctx,
7427 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007428 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007429
7430 if (idx < 0 && ufunc == NULL)
7431 {
7432 ret = FAIL;
7433 break;
7434 }
7435 if (idx >= 0)
7436 {
7437 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7438 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7439
7440 copy_tv(sv->sv_tv, rettv);
7441 }
7442 else
7443 {
7444 rettv->v_type = VAR_FUNC;
7445 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7446 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007447 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007448 }
7449
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007450 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7451 || rettv->v_type == VAR_PARTIAL))
7452 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007453 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007454 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7455 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007456
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007457 // Stop the expression evaluation when immediately aborting on
7458 // error, or when an interrupt occurred or an exception was thrown
7459 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007460 if (aborting())
7461 {
7462 if (ret == OK)
7463 clear_tv(rettv);
7464 ret = FAIL;
7465 }
7466 dict_unref(selfdict);
7467 selfdict = NULL;
7468 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007469 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007470 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007471 if (in_vim9script())
7472 *arg = skipwhite(p + 2);
7473 else
7474 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007475 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007476 {
zeertzjqc481ad32023-03-11 16:18:51 +00007477 emsg(_(e_no_white_space_allowed_before_parenthesis));
7478 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007479 }
zeertzjqc481ad32023-03-11 16:18:51 +00007480 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7481 // expr->{lambda}() or expr->(lambda)()
7482 ret = eval_lambda(arg, rettv, evalarg, verbose);
7483 else
7484 // expr->name()
7485 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007486 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007487 // "." is ".name" lookup when we found a dict or when evaluating and
7488 // scriptversion is at least 2, where string concatenation is "..".
7489 else if (**arg == '['
7490 || (**arg == '.' && (rettv->v_type == VAR_DICT
7491 || (!evaluate
7492 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007493 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007494 {
7495 dict_unref(selfdict);
7496 if (rettv->v_type == VAR_DICT)
7497 {
7498 selfdict = rettv->vval.v_dict;
7499 if (selfdict != NULL)
7500 ++selfdict->dv_refcount;
7501 }
7502 else
7503 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007504 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007505 {
7506 clear_tv(rettv);
7507 ret = FAIL;
7508 }
7509 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007510 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7511 || rettv->v_type == VAR_OBJECT))
7512 {
7513 // class member: SomeClass.varname
7514 // class method: SomeClass.SomeMethod()
7515 // class constructor: SomeClass.new()
7516 // object member: someObject.varname
7517 // object method: someObject.SomeMethod()
7518 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7519 {
7520 clear_tv(rettv);
7521 ret = FAIL;
7522 }
7523 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007524 else
7525 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007526 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007528 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7529 // Don't do this when "Func" is already a partial that was bound
7530 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007531 if (selfdict != NULL
7532 && (rettv->v_type == VAR_FUNC
7533 || (rettv->v_type == VAR_PARTIAL
7534 && (rettv->vval.v_partial->pt_auto
7535 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007536 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007537
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007538 dict_unref(selfdict);
7539 return ret;
7540}
7541
7542/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543 * Make a copy of an item.
7544 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007545 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007546 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7547 * reference to an already copied list/dict can be used.
7548 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007551item_copy(
7552 typval_T *from,
7553 typval_T *to,
7554 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007555 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007557{
7558 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007559 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007562 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007563 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007564 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565 }
7566 ++recurse;
7567
7568 switch (from->v_type)
7569 {
7570 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 case VAR_STRING:
7573 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007574 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007575 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007576 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007577 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007578 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007579 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007580 case VAR_CLASS:
7581 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007582 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 copy_tv(from, to);
7584 break;
7585 case VAR_LIST:
7586 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007587 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007588 if (from->vval.v_list == NULL)
7589 to->vval.v_list = NULL;
7590 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007592 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007593 to->vval.v_list = from->vval.v_list->lv_copylist;
7594 ++to->vval.v_list->lv_refcount;
7595 }
7596 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007597 to->vval.v_list = list_copy(from->vval.v_list,
7598 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007599 if (to->vval.v_list == NULL)
7600 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007602 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007604 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007605 case VAR_DICT:
7606 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007607 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (from->vval.v_dict == NULL)
7609 to->vval.v_dict = NULL;
7610 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007612 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007613 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7614 ++to->vval.v_dict->dv_refcount;
7615 }
7616 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007617 to->vval.v_dict = dict_copy(from->vval.v_dict,
7618 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007619 if (to->vval.v_dict == NULL)
7620 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007622 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007623 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007625 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007627 }
7628 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007629 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630}
7631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007632 void
7633echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7634{
7635 char_u *tofree;
7636 char_u numbuf[NUMBUFLEN];
7637 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7638
7639 if (*atstart)
7640 {
7641 *atstart = FALSE;
7642 // Call msg_start() after eval1(), evaluating the expression
7643 // may cause a message to appear.
7644 if (with_space)
7645 {
7646 // Mark the saved text as finishing the line, so that what
7647 // follows is displayed on a new line when scrolling back
7648 // at the more prompt.
7649 msg_sb_eol();
7650 msg_start();
7651 }
7652 }
7653 else if (with_space)
7654 msg_puts_attr(" ", echo_attr);
7655
7656 if (p != NULL)
7657 for ( ; *p != NUL && !got_int; ++p)
7658 {
7659 if (*p == '\n' || *p == '\r' || *p == TAB)
7660 {
7661 if (*p != TAB && *needclr)
7662 {
7663 // remove any text still there from the command
7664 msg_clr_eos();
7665 *needclr = FALSE;
7666 }
7667 msg_putchar_attr(*p, echo_attr);
7668 }
7669 else
7670 {
7671 if (has_mbyte)
7672 {
7673 int i = (*mb_ptr2len)(p);
7674
7675 (void)msg_outtrans_len_attr(p, i, echo_attr);
7676 p += i - 1;
7677 }
7678 else
7679 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7680 }
7681 }
7682 vim_free(tofree);
7683}
7684
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 * ":echo expr1 ..." print each argument separated with a space, add a
7687 * newline at the end.
7688 * ":echon expr1 ..." print each argument plain.
7689 */
7690 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007691ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007695 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 int needclr = TRUE;
7697 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007698 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007699 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007700 evalarg_T evalarg;
7701
Bram Moolenaare707c882020-07-01 13:07:37 +02007702 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703
7704 if (eap->skip)
7705 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007706 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007708 // If eval1() causes an error message the text from the command may
7709 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007710 need_clr_eos = needclr;
7711
Bram Moolenaar68db9962021-05-09 23:19:22 +02007712 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007713 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
7715 /*
7716 * Report the invalid expression unless the expression evaluation
7717 * has been cancelled due to an aborting error, an interrupt, or an
7718 * exception.
7719 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007720 if (!aborting() && did_emsg == did_emsg_before
7721 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007722 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007723 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 break;
7725 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007726 need_clr_eos = FALSE;
7727
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007729 {
7730 if (rettv.v_type == VAR_VOID)
7731 {
7732 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7733 break;
7734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007736 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 arg = skipwhite(arg);
7740 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007741 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007742 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
7744 if (eap->skip)
7745 --emsg_skip;
7746 else
7747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007748 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 if (needclr)
7750 msg_clr_eos();
7751 if (eap->cmdidx == CMD_echo)
7752 msg_end();
7753 }
7754}
7755
7756/*
7757 * ":echohl {name}".
7758 */
7759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007760ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007762 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764
7765/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007766 * Returns the :echo attribute
7767 */
7768 int
7769get_echo_attr(void)
7770{
7771 return echo_attr;
7772}
7773
7774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 * ":execute expr1 ..." execute the result of an expression.
7776 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007777 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007779 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 * Each gets spaces around each argument and a newline at the end for
7781 * echo commands
7782 */
7783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007784ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 int ret = OK;
7789 char_u *p;
7790 garray_T ga;
7791 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007792 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 ga_init2(&ga, 1, 80);
7795
7796 if (eap->skip)
7797 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007798 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007800 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007801 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
7804 if (!eap->skip)
7805 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007806 char_u buf[NUMBUFLEN];
7807
7808 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007809 {
7810 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7811 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007812 semsg(_(e_using_invalid_value_as_string_str),
7813 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007814 p = NULL;
7815 }
7816 else
7817 p = tv_get_string_buf(&rettv, buf);
7818 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007819 else
7820 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007821 if (p == NULL)
7822 {
7823 clear_tv(&rettv);
7824 ret = FAIL;
7825 break;
7826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 len = (int)STRLEN(p);
7828 if (ga_grow(&ga, len + 2) == FAIL)
7829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 ret = FAIL;
7832 break;
7833 }
7834 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 ga.ga_len += len;
7838 }
7839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 arg = skipwhite(arg);
7842 }
7843
7844 if (ret != FAIL && ga.ga_data != NULL)
7845 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007846 // use the first line of continuation lines for messages
7847 SOURCING_LNUM = start_lnum;
7848
Bram Moolenaar37fef162022-08-29 18:16:32 +01007849 if (eap->cmdidx == CMD_echomsg
7850 || eap->cmdidx == CMD_echowindow
7851 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007853 // Mark the already saved text as finishing the line, so that what
7854 // follows is displayed on a new line when scrolling back at the
7855 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007856 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007857 }
7858
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007859 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007860 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007861 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007862 out_flush();
7863 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007864 else if (eap->cmdidx == CMD_echowindow)
7865 {
7866#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007867 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007868#endif
7869 msg_attr(ga.ga_data, echo_attr);
7870#ifdef HAS_MESSAGE_WINDOW
7871 end_echowindow();
7872#endif
7873 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007874 else if (eap->cmdidx == CMD_echoconsole)
7875 {
7876 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7877 ui_write((char_u *)"\r\n", 2, TRUE);
7878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 else if (eap->cmdidx == CMD_echoerr)
7880 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007881 int save_did_emsg = did_emsg;
7882
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007883 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007884 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (!force_abort)
7886 did_emsg = save_did_emsg;
7887 }
7888 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007889 {
7890 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7891
7892 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7893 sticky_cmdmod_flags = cmdmod.cmod_flags
7894 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007896 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007897 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900
7901 ga_clear(&ga);
7902
7903 if (eap->skip)
7904 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007905 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906}
7907
7908/*
7909 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7910 * "arg" points to the "&" or '+' when called, to "option" when returning.
7911 * Returns NULL when no option name found. Otherwise pointer to the char
7912 * after the option name.
7913 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007914 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007915find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 char_u *p = *arg;
7918
7919 ++p;
7920 if (*p == 'g' && p[1] == ':')
7921 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007922 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 p += 2;
7924 }
7925 else if (*p == 'l' && p[1] == ':')
7926 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007927 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 p += 2;
7929 }
7930 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007931 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933 if (!ASCII_ISALPHA(*p))
7934 return NULL;
7935 *arg = p;
7936
7937 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007938 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 else
7940 while (ASCII_ISALPHA(*p))
7941 ++p;
7942 return p;
7943}
7944
7945/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007946 * Display script name where an item was last set.
7947 * Should only be invoked when 'verbose' is non-zero.
7948 */
7949 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007950last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007951{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007952 char_u *p;
7953
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007954 if (script_ctx.sc_sid == 0)
7955 return;
7956
7957 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7958 if (p == NULL)
7959 return;
7960
7961 verbose_enter();
7962 msg_puts(_("\n\tLast set from "));
7963 msg_puts((char *)p);
7964 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007965 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007966 msg_puts(_(line_msg));
7967 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007968 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007969 verbose_leave();
7970 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007971}
7972
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007973#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974
7975/*
7976 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007977 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 * "flags" can be "g" to do a global substitute.
7979 * Returns an allocated string, NULL for error.
7980 */
7981 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007982do_string_sub(
7983 char_u *str,
7984 char_u *pat,
7985 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007986 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 int sublen;
7990 regmatch_T regmatch;
7991 int i;
7992 int do_all;
7993 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007994 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 garray_T ga;
7996 char_u *ret;
7997 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007998 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008000 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008002 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 ga_init2(&ga, 1, 200);
8005
8006 do_all = (flags[0] == 'g');
8007
8008 regmatch.rm_ic = p_ic;
8009 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8010 if (regmatch.regprog != NULL)
8011 {
8012 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01008013 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
8015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008016 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01008017 if (regmatch.startp[0] == regmatch.endp[0])
8018 {
8019 if (zero_width == regmatch.startp[0])
8020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008021 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02008022 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02008023 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
8024 (size_t)i);
8025 ga.ga_len += i;
8026 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01008027 continue;
8028 }
8029 zero_width = regmatch.startp[0];
8030 }
8031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 /*
8033 * Get some space for a temporary buffer to do the substitution
8034 * into. It will contain:
8035 * - The text up to where the match is.
8036 * - The substituted text.
8037 * - The text after the match.
8038 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008039 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00008040 if (sublen <= 0)
8041 {
8042 ga_clear(&ga);
8043 break;
8044 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01008045 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
8047 {
8048 ga_clear(&ga);
8049 break;
8050 }
8051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008052 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 i = (int)(regmatch.startp[0] - tail);
8054 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008055 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008056 (void)vim_regsub(&regmatch, sub, expr,
8057 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
8058 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02008060 tail = regmatch.endp[0];
8061 if (*tail == NUL)
8062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (!do_all)
8064 break;
8065 }
8066
8067 if (ga.ga_data != NULL)
8068 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
8069
Bram Moolenaar473de612013-06-08 18:19:48 +02008070 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 }
8072
8073 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
8074 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008075 if (p_cpo == empty_option)
8076 p_cpo = save_cpo;
8077 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008079 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008080 // If it's still empty it was changed and restored, need to restore in
8081 // the complicated way.
8082 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008083 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008084 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086
8087 return ret;
8088}