blob: 833a230f9d096792b3426d1307b4f0823571ae2e [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)
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02002982 break;
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002983 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;
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004512 // null_list
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004513 if (STRNCMP(s + 5, "list", 4) == 0)
4514 {
4515 rettv->v_type = VAR_LIST;
4516 rettv->vval.v_list = NULL;
4517 return OK;
4518 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004519 // null_dict
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004520 if (STRNCMP(s + 5, "dict", 4) == 0)
4521 {
4522 rettv->v_type = VAR_DICT;
4523 rettv->vval.v_dict = NULL;
4524 return OK;
4525 }
Yegappan Lakshmanan4776e642024-05-19 09:06:50 +02004526 // null_blob
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004527 if (STRNCMP(s + 5, "blob", 4) == 0)
4528 {
4529 rettv->v_type = VAR_BLOB;
4530 rettv->vval.v_blob = NULL;
4531 return OK;
4532 }
4533 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004534 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4535 {
4536 rettv->v_type = VAR_CLASS;
4537 rettv->vval.v_class = NULL;
4538 return OK;
4539 }
4540 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004541 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4542 {
4543 rettv->v_type = VAR_STRING;
4544 rettv->vval.v_string = NULL;
4545 return OK;
4546 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004547 if (STRNCMP(s, "null_object", 11) == 0)
4548 {
4549 rettv->v_type = VAR_OBJECT;
4550 rettv->vval.v_object = NULL;
4551 return OK;
4552 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004553 break;
4554 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004555 if (STRNCMP(s, "null_channel", 12) == 0)
4556 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004557#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004558 rettv->v_type = VAR_CHANNEL;
4559 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004560#else
4561 rettv->v_type = VAR_SPECIAL;
4562 rettv->vval.v_number = VVAL_NULL;
4563#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004564 return OK;
4565 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004566 if (STRNCMP(s, "null_partial", 12) == 0)
4567 {
4568 rettv->v_type = VAR_PARTIAL;
4569 rettv->vval.v_partial = NULL;
4570 return OK;
4571 }
4572 break;
4573 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4574 {
4575 rettv->v_type = VAR_FUNC;
4576 rettv->vval.v_string = NULL;
4577 return OK;
4578 }
4579 break;
4580 }
4581 return FAIL;
4582}
4583
4584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 * Handle sixth level expression:
4586 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004587 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004588 * "string" string constant
4589 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 * &option-name option value
4591 * @r register contents
4592 * identifier variable value
4593 * function() function call
4594 * $VAR environment variable
4595 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004596 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004598 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004599 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 *
4601 * Also handle:
4602 * ! in front logical NOT
4603 * - in front unary minus
4604 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 * trailing [] subscript in String or List
4606 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004607 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 *
4609 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004610 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 *
4612 * Return OK or FAIL.
4613 */
4614 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004615eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004616 char_u **arg,
4617 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004618 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004621 int evaluate = evalarg != NULL
4622 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 int len;
4624 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004625 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 char_u *start_leader, *end_leader;
4627 int ret = OK;
4628 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004629 static int recurse = 0;
4630 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
4632 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
4638 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004639 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 */
4641 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004642 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004643 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 end_leader = *arg;
4645
Keith Thompson184f71c2024-01-04 21:19:04 +01004646 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004647 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004648 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004649 ++*arg;
4650 return FAIL;
4651 }
4652
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004653 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004654 // and crash. With MSVC the stack is smaller.
4655 if (recurse ==
4656#ifdef _MSC_VER
4657 300
4658#else
4659 1000
4660#endif
4661 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004662 {
4663 semsg(_(e_expression_too_recursive_str), *arg);
4664 return FAIL;
4665 }
4666 ++recurse;
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 switch (**arg)
4669 {
4670 /*
4671 * Number constant.
4672 */
4673 case '0':
4674 case '1':
4675 case '2':
4676 case '3':
4677 case '4':
4678 case '5':
4679 case '6':
4680 case '7':
4681 case '8':
4682 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004683 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004684
4685 // Apply prefixed "-" and "+" now. Matters especially when
4686 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004687 if (ret == OK && evaluate && end_leader > start_leader
4688 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004689 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 break;
4691
4692 /*
4693 * String constant: "string".
4694 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004695 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 break;
4697
4698 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004701 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 break;
4703
4704 /*
4705 * List: [expr, expr]
4706 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004707 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 break;
4709
4710 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004711 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004712 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004713 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004714 {
4715 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4716 }
4717 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004718 {
4719 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004720 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004721 }
4722 else
4723 ret = NOTDONE;
4724 break;
4725
4726 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004727 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004728 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004730 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004731 ret = NOTDONE;
4732 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004733 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004734 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004735 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 break;
4737
4738 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004739 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004741 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 break;
4743
4744 /*
4745 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004746 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 */
LemonBoy2eaef102022-05-06 13:14:50 +01004748 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4749 ret = eval_interp_string(arg, rettv, evaluate);
4750 else
4751 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 break;
4753
4754 /*
4755 * Register contents: @r.
4756 */
4757 case '@': ++*arg;
4758 if (evaluate)
4759 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004760 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004761 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004762 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004763 emsg_invreg(**arg);
4764 else
4765 {
4766 rettv->v_type = VAR_STRING;
4767 rettv->vval.v_string = get_reg_contents(**arg,
4768 GREG_EXPR_SRC);
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 if (**arg != NUL)
4772 ++*arg;
4773 break;
4774
4775 /*
4776 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004777 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004779 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004780 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004781 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004782 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004783 if (ret == OK && evaluate)
4784 {
4785 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4786
Bram Moolenaara9931532021-06-12 15:58:16 +02004787 // Compile it here to get the return type. The return
4788 // type is optional, when it's missing use t_unknown.
4789 // This is recognized in compile_return().
4790 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4791 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004792 if (compile_def_function(ufunc, FALSE,
4793 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004794 {
4795 clear_tv(rettv);
4796 ret = FAIL;
4797 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004798 }
4799 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004800 if (ret == NOTDONE)
4801 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004802 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004803 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004804
Bram Moolenaar9215f012020-06-27 21:18:00 +02004805 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004806 if (**arg == ')')
4807 ++*arg;
4808 else if (ret == OK)
4809 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004810 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004811 clear_tv(rettv);
4812 ret = FAIL;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 break;
4816
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 break;
4819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820
4821 if (ret == NOTDONE)
4822 {
4823 /*
4824 * Must be a variable or function name.
4825 * Can also be a curly-braces kind of name: {expr}.
4826 */
4827 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004828 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004829 if (alias != NULL)
4830 s = alias;
4831
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004832 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004833 ret = FAIL;
4834 else
4835 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004836 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4837
Bram Moolenaar4525a572022-02-13 11:57:33 +00004838 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004839 {
4840 emsg(_(e_cannot_use_underscore_here));
4841 ret = FAIL;
4842 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004843 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004844 && s[0] == 's' && s[1] == ':')
4845 {
4846 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4847 ret = FAIL;
4848 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004849 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004850 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004851 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004852 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004853 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004856 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004857 // get the value of "true", "false", etc. or a variable
4858 ret = FAIL;
4859 if (vim9script)
4860 ret = handle_predefined(s, len, rettv);
4861 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004862 {
4863 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004864 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004865 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004866 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004867 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004868 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004869 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004870 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004871 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004872 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004874 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004875 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004876 }
4877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004878 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4879 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004880 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004881 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882
4883 /*
4884 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4885 */
4886 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004887 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004888
4889 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004890 return ret;
4891}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004892
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004893/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004894 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004895 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004896 * Adjusts "end_leaderp" until it is at "start_leader".
4897 */
4898 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004899eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004900 typval_T *rettv,
4901 int numeric_only,
4902 char_u *start_leader,
4903 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004904{
4905 char_u *end_leader = *end_leaderp;
4906 int ret = OK;
4907 int error = FALSE;
4908 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004909 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004910 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004911 float_T f = 0.0;
4912
4913 if (rettv->v_type == VAR_FLOAT)
4914 f = rettv->vval.v_float;
4915 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004916 {
4917 while (VIM_ISWHITE(end_leader[-1]))
4918 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004919 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004920 val = tv2bool(rettv);
4921 else
4922 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004923 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004924 if (error)
4925 {
4926 clear_tv(rettv);
4927 ret = FAIL;
4928 }
4929 else
4930 {
4931 while (end_leader > start_leader)
4932 {
4933 --end_leader;
4934 if (*end_leader == '!')
4935 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004936 if (numeric_only)
4937 {
4938 ++end_leader;
4939 break;
4940 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004941 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004942 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004943 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004944 {
4945 rettv->v_type = VAR_BOOL;
4946 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4947 }
4948 else
4949 f = !f;
4950 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004951 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004952 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004953 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004954 type = VAR_BOOL;
4955 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004956 }
4957 else if (*end_leader == '-')
4958 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004959 if (rettv->v_type == VAR_FLOAT)
4960 f = -f;
4961 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004962 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004963 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004964 type = VAR_NUMBER;
4965 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004966 }
4967 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004968 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004970 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004971 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004973 else
4974 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004975 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004976 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004977 rettv->v_type = type;
4978 else
4979 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004980 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004983 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 return ret;
4985}
4986
4987/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004988 * Call the function referred to in "rettv".
4989 */
4990 static int
4991call_func_rettv(
4992 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004993 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004994 typval_T *rettv,
4995 int evaluate,
4996 dict_T *selfdict,
4997 typval_T *basetv)
4998{
4999 partial_T *pt = NULL;
5000 funcexe_T funcexe;
5001 typval_T functv;
5002 char_u *s;
5003 int ret;
5004
5005 // need to copy the funcref so that we can clear rettv
5006 if (evaluate)
5007 {
5008 functv = *rettv;
5009 rettv->v_type = VAR_UNKNOWN;
5010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005012 if (functv.v_type == VAR_PARTIAL)
5013 {
5014 pt = functv.vval.v_partial;
5015 s = partial_name(pt);
5016 }
5017 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005018 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005019 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005020 if (s == NULL || *s == NUL)
5021 {
5022 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02005023 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005024 goto theend;
5025 }
5026 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005027 }
5028 else
5029 s = (char_u *)"";
5030
Bram Moolenaara80faa82020-04-12 19:37:17 +02005031 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005032 funcexe.fe_firstline = curwin->w_cursor.lnum;
5033 funcexe.fe_lastline = curwin->w_cursor.lnum;
5034 funcexe.fe_evaluate = evaluate;
5035 funcexe.fe_partial = pt;
5036 funcexe.fe_selfdict = selfdict;
5037 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005038 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005039
Bram Moolenaar22db0d52021-06-12 12:16:55 +02005040theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // Clear the funcref afterwards, so that deleting it while
5042 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005043 if (evaluate)
5044 clear_tv(&functv);
5045
5046 return ret;
5047}
5048
5049/*
5050 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005051 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005052 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5053 */
5054 static int
5055eval_lambda(
5056 char_u **arg,
5057 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005058 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005059 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005060{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005061 int evaluate = evalarg != NULL
5062 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005063 typval_T base = *rettv;
5064 int ret;
5065
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005066 rettv->v_type = VAR_UNKNOWN;
5067
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005068 if (**arg == '{')
5069 {
5070 // ->{lambda}()
5071 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
5072 }
5073 else
5074 {
5075 // ->(lambda)()
5076 ++*arg;
5077 ret = eval1(arg, rettv, evalarg);
5078 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005079 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005080 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005081 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005082 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005083 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005084 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
5085 && rettv->v_type != VAR_PARTIAL)
5086 {
5087 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
5088 return FAIL;
5089 }
5090 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005091 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01005092 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005093 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01005094
5095 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005096 {
5097 if (verbose)
5098 {
5099 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005100 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005101 else
Bram Moolenaare1242042021-12-16 20:56:57 +00005102 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005103 }
5104 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02005105 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005106 }
Bram Moolenaar86173482019-10-01 17:02:16 +02005107 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02005108 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02005109
5110 // Clear the funcref afterwards, so that deleting it while
5111 // evaluating the arguments is possible (see test55).
5112 if (evaluate)
5113 clear_tv(&base);
5114
5115 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005116}
5117
5118/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005119 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005120 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02005121 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
5122 */
5123 static int
5124eval_method(
5125 char_u **arg,
5126 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02005127 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02005129{
5130 char_u *name;
5131 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005132 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005133 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005134 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005135 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005136 int evaluate = evalarg != NULL
5137 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005138
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005139 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005140
Bram Moolenaarac92e252019-08-03 21:58:38 +02005141 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01005142 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005143 if (alias != NULL)
5144 name = alias;
5145
5146 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02005147 {
5148 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00005149 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005150 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02005151 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005152 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02005153 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005154 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005155
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005156 // If there is no "(" immediately following, but there is further on,
5157 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
5158 // Does not handle anything where "(" is part of the expression.
5159 *arg = skipwhite(*arg);
5160
5161 if (**arg != '(' && alias == NULL
5162 && (paren = vim_strchr(*arg, '(')) != NULL)
5163 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005164 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00005165
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005166 // Truncate the name at the "(". Avoid trying to get another line
Bram Moolenaar34820942022-12-19 20:28:38 +00005167 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005168 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00005169 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
5170 if (evalarg != NULL)
5171 {
5172 getline = evalarg->eval_getline;
5173 evalarg->eval_getline = NULL;
5174 }
5175
5176 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005177 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005178 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005179 *arg = name + len;
5180 ret = FAIL;
5181 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005182 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00005183 {
5184 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00005185 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00005186 }
Bram Moolenaar34820942022-12-19 20:28:38 +00005187
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005188 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00005189 if (getline != NULL)
5190 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02005191 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005192
5193 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02005194 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005195 *arg = skipwhite(*arg);
5196
5197 if (**arg != '(')
5198 {
5199 if (verbose)
5200 semsg(_(e_missing_parenthesis_str), name);
5201 ret = FAIL;
5202 }
5203 else if (VIM_ISWHITE((*arg)[-1]))
5204 {
5205 if (verbose)
5206 emsg(_(e_no_white_space_allowed_before_parenthesis));
5207 ret = FAIL;
5208 }
5209 else
5210 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005211 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00005212 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005213 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005214
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005215 // Clear the funcref afterwards, so that deleting it while
5216 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02005217 if (evaluate)
5218 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00005219 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02005220
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005221 if (alias != NULL)
5222 vim_free(alias);
5223
Bram Moolenaarac92e252019-08-03 21:58:38 +02005224 return ret;
5225}
5226
5227/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005228 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5229 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5231 */
5232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005233eval_index(
5234 char_u **arg,
5235 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005236 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005239 int evaluate = evalarg != NULL
5240 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005245 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005246 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005248 if (check_can_index(rettv, evaluate, verbose) == FAIL)
5249 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005251 init_tv(&var1);
5252 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 /*
5256 * dict.name
5257 */
5258 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005259 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005261 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02005263 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 }
5265 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 /*
5268 * something[idx]
5269 *
5270 * Get the (first) variable from inside the [].
5271 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005272 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 if (**arg == ':')
5274 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005275 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005277 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005278 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005279 semsg(_(e_white_space_required_before_and_after_str_at_str),
5280 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005281 clear_tv(&var1);
5282 return FAIL;
5283 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005284 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005286 int error = FALSE;
5287
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005288 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005289 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005290 && var1.v_type == VAR_FLOAT)
5291 {
5292 var1.vval.v_string = typval_tostring(&var1, TRUE);
5293 var1.v_type = VAR_STRING;
5294 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005295
Bram Moolenaar4525a572022-02-13 11:57:33 +00005296 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005297 tv_get_number_chk(&var1, &error);
5298 else
5299 error = tv_get_string_chk(&var1) == NULL;
5300 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005301 {
5302 // not a number or string
5303 clear_tv(&var1);
5304 return FAIL;
5305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307
5308 /*
5309 * Get the second variable from inside the [:].
5310 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005311 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 if (**arg == ':')
5313 {
5314 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005315 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005316 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005317 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005318 semsg(_(e_white_space_required_before_and_after_str_at_str),
5319 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005320 if (!empty1)
5321 clear_tv(&var1);
5322 return FAIL;
5323 }
5324 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 if (**arg == ']')
5326 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005327 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 if (!empty1)
5330 clear_tv(&var1);
5331 return FAIL;
5332 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005333 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 if (!empty1)
5337 clear_tv(&var1);
5338 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 return FAIL;
5340 }
5341 }
5342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005344 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005345 if (**arg != ']')
5346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005347 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005348 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 clear_tv(&var1);
5350 if (range)
5351 clear_tv(&var2);
5352 return FAIL;
5353 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005354 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 }
5356
5357 if (evaluate)
5358 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005359 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005360 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005361 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005362
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005363 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005366 clear_tv(&var2);
5367 return res;
5368 }
5369 return OK;
5370}
5371
5372/*
5373 * Check if "rettv" can have an [index] or [sli:ce]
5374 */
5375 int
5376check_can_index(typval_T *rettv, int evaluate, int verbose)
5377{
5378 switch (rettv->v_type)
5379 {
5380 case VAR_FUNC:
5381 case VAR_PARTIAL:
5382 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005383 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005384 return FAIL;
5385 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005386 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005387 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005388 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005389 case VAR_BOOL:
5390 case VAR_SPECIAL:
5391 case VAR_JOB:
5392 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005393 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005394 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005395 if (verbose)
5396 emsg(_(e_cannot_index_special_variable));
5397 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005398 case VAR_CLASS:
5399 case VAR_TYPEALIAS:
5400 if (verbose)
5401 check_typval_is_value(rettv);
5402 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005403 case VAR_UNKNOWN:
5404 case VAR_ANY:
5405 case VAR_VOID:
5406 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005408 emsg(_(e_cannot_index_special_variable));
5409 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005411 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005413 case VAR_STRING:
5414 case VAR_LIST:
5415 case VAR_DICT:
5416 case VAR_BLOB:
5417 break;
5418 case VAR_NUMBER:
5419 if (in_vim9script())
5420 emsg(_(e_cannot_index_number));
5421 break;
5422 }
5423 return OK;
5424}
5425
5426/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005427 * slice() function
5428 */
5429 void
5430f_slice(typval_T *argvars, typval_T *rettv)
5431{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005432 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005433 && ((argvars[0].v_type != VAR_STRING
5434 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005435 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005436 && check_for_list_arg(argvars, 0) == FAIL)
5437 || check_for_number_arg(argvars, 1) == FAIL
5438 || check_for_opt_number_arg(argvars, 2) == FAIL))
5439 return;
5440
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02005441 if (check_can_index(&argvars[0], TRUE, FALSE) != OK)
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005442 return;
5443
5444 copy_tv(argvars, rettv);
5445 eval_index_inner(rettv, TRUE, argvars + 1,
5446 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5447 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005448}
5449
5450/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005451 * Apply index or range to "rettv".
5452 * "var1" is the first index, NULL for [:expr].
5453 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005454 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5455 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005456 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5457 */
5458 int
5459eval_index_inner(
5460 typval_T *rettv,
5461 int is_range,
5462 typval_T *var1,
5463 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005464 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005465 char_u *key,
5466 int keylen,
5467 int verbose)
5468{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005469 varnumber_T n1, n2 = 0;
5470 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005471
5472 n1 = 0;
5473 if (var1 != NULL && rettv->v_type != VAR_DICT)
5474 n1 = tv_get_number(var1);
5475
5476 if (is_range)
5477 {
5478 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005480 if (verbose)
5481 emsg(_(e_cannot_slice_dictionary));
5482 return FAIL;
5483 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005484 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005485 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005486 else
5487 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005488 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005489
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005490 switch (rettv->v_type)
5491 {
5492 case VAR_UNKNOWN:
5493 case VAR_ANY:
5494 case VAR_VOID:
5495 case VAR_FUNC:
5496 case VAR_PARTIAL:
5497 case VAR_FLOAT:
5498 case VAR_BOOL:
5499 case VAR_SPECIAL:
5500 case VAR_JOB:
5501 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005502 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005503 case VAR_CLASS:
5504 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005505 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005506 break; // not evaluating, skipping over subscript
5507
5508 case VAR_NUMBER:
5509 case VAR_STRING:
5510 {
5511 char_u *s = tv_get_string(rettv);
5512
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005514 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005515 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005516 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005517 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005518 else
5519 s = char_from_string(s, n1);
5520 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005521 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005523 // The resulting variable is a substring. If the indexes
5524 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 if (n1 < 0)
5526 {
5527 n1 = len + n1;
5528 if (n1 < 0)
5529 n1 = 0;
5530 }
5531 if (n2 < 0)
5532 n2 = len + n2;
5533 else if (n2 >= len)
5534 n2 = len;
5535 if (n1 >= len || n2 < 0 || n1 > n2)
5536 s = NULL;
5537 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005538 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 }
5540 else
5541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005542 // The resulting variable is a string of a single
5543 // character. If the index is too big or negative the
5544 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 if (n1 >= len || n1 < 0)
5546 s = NULL;
5547 else
5548 s = vim_strnsave(s + n1, 1);
5549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 rettv->v_type = VAR_STRING;
5552 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005553 }
5554 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005556 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005557 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5558 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005559 break;
5560
5561 case VAR_LIST:
5562 if (var1 == NULL)
5563 n1 = 0;
5564 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005565 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005566 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005567 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005568 return FAIL;
5569 break;
5570
5571 case VAR_DICT:
5572 {
5573 dictitem_T *item;
5574 typval_T tmp;
5575
5576 if (key == NULL)
5577 {
5578 key = tv_get_string_chk(var1);
5579 if (key == NULL)
5580 return FAIL;
5581 }
5582
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005583 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005584
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005585 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005586 {
5587 if (verbose)
5588 {
5589 if (keylen > 0)
5590 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005591 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005592 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005593 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005594 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005595
5596 copy_tv(&item->di_tv, &tmp);
5597 clear_tv(rettv);
5598 *rettv = tmp;
5599 }
5600 break;
5601 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 return OK;
5603}
5604
5605/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005606 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005607 */
5608 char_u *
5609partial_name(partial_T *pt)
5610{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005611 if (pt != NULL)
5612 {
5613 if (pt->pt_name != NULL)
5614 return pt->pt_name;
5615 if (pt->pt_func != NULL)
5616 return pt->pt_func->uf_name;
5617 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005618 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005619}
5620
Bram Moolenaarddecc252016-04-06 22:59:37 +02005621 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005622partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005623{
5624 int i;
5625
5626 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005627 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005628 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005629 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005630 if (pt->pt_name != NULL)
5631 {
5632 func_unref(pt->pt_name);
5633 vim_free(pt->pt_name);
5634 }
5635 else
5636 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005637 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005638
Bram Moolenaar54656012021-06-09 20:50:46 +02005639 // "out_up" is no longer used, decrement refcount on partial that owns it.
5640 partial_unref(pt->pt_outer.out_up_partial);
5641
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005642 // Using pt_outer from another partial.
5643 partial_unref(pt->pt_outer_partial);
5644
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005645 // Decrease the reference count for the context of a closure. If down
5646 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005647 if (pt->pt_funcstack != NULL)
5648 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005649 --pt->pt_funcstack->fs_refcount;
5650 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005651 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005652 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005653 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5654 if (pt->pt_loopvars[i] != NULL)
5655 {
5656 --pt->pt_loopvars[i]->lvs_refcount;
5657 loopvars_check_refcount(pt->pt_loopvars[i]);
5658 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005659
Bram Moolenaarddecc252016-04-06 22:59:37 +02005660 vim_free(pt);
5661}
5662
5663/*
5664 * Unreference a closure: decrement the reference count and free it when it
5665 * becomes zero.
5666 */
5667 void
5668partial_unref(partial_T *pt)
5669{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005670 if (pt == NULL)
5671 return;
5672
5673 int done = FALSE;
5674
5675 if (--pt->pt_refcount <= 0)
5676 partial_free(pt);
5677
5678 // If the reference count goes down to one, the funcstack may be the
5679 // only reference and can be freed if no other partials reference it.
5680 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005681 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005682 // careful: if the funcstack is freed it may contain this partial
5683 // and it gets freed as well
5684 if (pt->pt_funcstack != NULL)
5685 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005686
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005687 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005688 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005689 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005690
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005691 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5692 if (pt->pt_loopvars[depth] != NULL
5693 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005694 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005695 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005696 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005697}
5698
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005699/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005700 * Return the next (unique) copy ID.
5701 * Used for serializing nested structures.
5702 */
5703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005705{
5706 current_copyID += COPYID_INC;
5707 return current_copyID;
5708}
5709
5710/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005711 * Garbage collection for lists and dictionaries.
5712 *
5713 * We use reference counts to be able to free most items right away when they
5714 * are no longer used. But for composite items it's possible that it becomes
5715 * unused while the reference count is > 0: When there is a recursive
5716 * reference. Example:
5717 * :let l = [1, 2, 3]
5718 * :let d = {9: l}
5719 * :let l[1] = d
5720 *
5721 * Since this is quite unusual we handle this with garbage collection: every
5722 * once in a while find out which lists and dicts are not referenced from any
5723 * variable.
5724 *
5725 * Here is a good reference text about garbage collection (refers to Python
5726 * but it applies to all reference-counting mechanisms):
5727 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005728 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005729
5730/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005731 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005732 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005733 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005734 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005735 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005736garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005737{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005738 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005739 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005740 buf_T *buf;
5741 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005742 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005743 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005744
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005745 if (!testing)
5746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005747 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005748 want_garbage_collect = FALSE;
5749 may_garbage_collect = FALSE;
5750 garbage_collect_at_exit = FALSE;
5751 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005752
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005753 // The execution stack can grow big, limit the size.
5754 if (exestack.ga_maxlen - exestack.ga_len > 500)
5755 {
5756 size_t new_len;
5757 char_u *pp;
5758 int n;
5759
5760 // Keep 150% of the current size, with a minimum of the growth size.
5761 n = exestack.ga_len / 2;
5762 if (n < exestack.ga_growsize)
5763 n = exestack.ga_growsize;
5764
5765 // Don't make it bigger though.
5766 if (exestack.ga_len + n < exestack.ga_maxlen)
5767 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005768 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005769 pp = vim_realloc(exestack.ga_data, new_len);
5770 if (pp == NULL)
5771 return FAIL;
5772 exestack.ga_maxlen = exestack.ga_len + n;
5773 exestack.ga_data = pp;
5774 }
5775 }
5776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005777 // We advance by two because we add one for items referenced through
5778 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005779 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005780
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005781 /*
5782 * 1. Go through all accessible variables and mark all lists and dicts
5783 * with copyID.
5784 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005786 // Don't free variables in the previous_funccal list unless they are only
5787 // referenced through previous_funccal. This must be first, because if
5788 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005791 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005792 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005794 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005795 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005796 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5797 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005799 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005800 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005801 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5802 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005803 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005804 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005805 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005806 abort = abort || set_ref_in_item(
5807 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005808#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005809 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005810 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5811 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005812 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005813 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005814 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5815 NULL, NULL);
5816#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005819 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005820 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5821 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005823 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005826 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005828 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005829 abort = abort || set_ref_in_functions(copyID);
5830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005831 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005832 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005833
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005834 // funcstacks keep variables for closures
5835 abort = abort || set_ref_in_funcstacks(copyID);
5836
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005837 // loopvars keep variables for loop blocks
5838 abort = abort || set_ref_in_loopvars(copyID);
5839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005840 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005841 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005842
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005843 // callbacks in buffers
5844 abort = abort || set_ref_in_buffers(copyID);
5845
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005846 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5847 abort = abort || set_ref_in_insexpand_funcs(copyID);
5848
5849 // 'operatorfunc' callback
5850 abort = abort || set_ref_in_opfunc(copyID);
5851
5852 // 'tagfunc' callback
5853 abort = abort || set_ref_in_tagfunc(copyID);
5854
5855 // 'imactivatefunc' and 'imstatusfunc' callbacks
5856 abort = abort || set_ref_in_im_funcs(copyID);
5857
Bram Moolenaar1dced572012-04-05 16:54:08 +02005858#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005860#endif
5861
Bram Moolenaardb913952012-06-29 12:54:53 +02005862#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005864#endif
5865
5866#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005867 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005868#endif
5869
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005870#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005871 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005872 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005873#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005874#ifdef FEAT_NETBEANS_INTG
5875 abort = abort || set_ref_in_nb_channel(copyID);
5876#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005877
Bram Moolenaare3188e22016-05-31 21:13:04 +02005878#ifdef FEAT_TIMERS
5879 abort = abort || set_ref_in_timer(copyID);
5880#endif
5881
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005882#ifdef FEAT_QUICKFIX
5883 abort = abort || set_ref_in_quickfix(copyID);
5884#endif
5885
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005886#ifdef FEAT_TERMINAL
5887 abort = abort || set_ref_in_term(copyID);
5888#endif
5889
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005890#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005891 abort = abort || set_ref_in_popups(copyID);
5892#endif
5893
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005894 abort = abort || set_ref_in_classes(copyID);
5895
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005896 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005897 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005898 /*
5899 * 2. Free lists and dictionaries that are not referenced.
5900 */
5901 did_free = free_unref_items(copyID);
5902
5903 /*
5904 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005905 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005906 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005907 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005908 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005909 else if (p_verbose > 0)
5910 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005911 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005912 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005913
5914 return did_free;
5915}
5916
5917/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005918 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005919 */
5920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005922{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005923 int did_free = FALSE;
5924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005925 // Let all "free" functions know that we are here. This means no
5926 // dictionaries, lists, channels or jobs are to be freed, because we will
5927 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005928 in_free_unref_items = TRUE;
5929
5930 /*
5931 * PASS 1: free the contents of the items. We don't free the items
5932 * themselves yet, so that it is possible to decrement refcount counters
5933 */
5934
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005935 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005936 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005937
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005938 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005939 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005940
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005941 // Go through the list of objects and free items without this copyID.
5942 did_free |= object_free_nonref(copyID);
5943
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005944 // Go through the list of classes and free items without this copyID.
5945 did_free |= class_free_nonref(copyID);
5946
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005947#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005948 // Go through the list of jobs and free items without the copyID. This
5949 // must happen before doing channels, because jobs refer to channels, but
5950 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005951 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005954 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5955#endif
5956
5957 /*
5958 * PASS 2: free the items themselves.
5959 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005960 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005961 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005962 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005964#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005965 // Go through the list of jobs and free items without the copyID. This
5966 // must happen before doing channels, because jobs refer to channels, but
5967 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005968 free_unused_jobs(copyID, COPYID_MASK);
5969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005970 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005971 free_unused_channels(copyID, COPYID_MASK);
5972#endif
5973
5974 in_free_unref_items = FALSE;
5975
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005976 return did_free;
5977}
5978
5979/*
5980 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005981 * "list_stack" is used to add lists to be marked. Can be NULL.
5982 *
5983 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987{
5988 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005989 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005990 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005991 hashtab_T *cur_ht;
5992 ht_stack_T *ht_stack = NULL;
5993 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005994
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005995 cur_ht = ht;
5996 for (;;)
5997 {
5998 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006000 // Mark each item in the hashtab. If the item contains a hashtab
6001 // it is added to ht_stack, if it contains a list it is added to
6002 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006003 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00006004 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006005 if (!HASHITEM_EMPTY(hi))
6006 {
6007 --todo;
6008 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6009 &ht_stack, list_stack);
6010 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006011 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006012
6013 if (ht_stack == NULL)
6014 break;
6015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006016 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006017 cur_ht = ht_stack->ht;
6018 tempitem = ht_stack;
6019 ht_stack = ht_stack->prev;
6020 free(tempitem);
6021 }
6022
6023 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024}
6025
Dominique Pelle748b3082022-01-08 12:41:16 +00006026#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6027 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006029 * Mark a dict and its items with "copyID".
6030 * Returns TRUE if setting references failed somehow.
6031 */
6032 int
6033set_ref_in_dict(dict_T *d, int copyID)
6034{
6035 if (d != NULL && d->dv_copyID != copyID)
6036 {
6037 d->dv_copyID = copyID;
6038 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6039 }
6040 return FALSE;
6041}
Dominique Pelle748b3082022-01-08 12:41:16 +00006042#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006043
6044/*
6045 * Mark a list and its items with "copyID".
6046 * Returns TRUE if setting references failed somehow.
6047 */
6048 int
6049set_ref_in_list(list_T *ll, int copyID)
6050{
6051 if (ll != NULL && ll->lv_copyID != copyID)
6052 {
6053 ll->lv_copyID = copyID;
6054 return set_ref_in_list_items(ll, copyID, NULL);
6055 }
6056 return FALSE;
6057}
6058
6059/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006060 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006061 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6062 *
6063 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006065 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006066set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006067{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006068 listitem_T *li;
6069 int abort = FALSE;
6070 list_T *cur_l;
6071 list_stack_T *list_stack = NULL;
6072 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006073
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006074 cur_l = l;
6075 for (;;)
6076 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006077 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006078 // Mark each item in the list. If the item contains a hashtab
6079 // it is added to ht_stack, if it contains a list it is added to
6080 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006081 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6082 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6083 ht_stack, &list_stack);
6084 if (list_stack == NULL)
6085 break;
6086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006087 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006088 cur_l = list_stack->list;
6089 tempitem = list_stack;
6090 list_stack = list_stack->prev;
6091 free(tempitem);
6092 }
6093
6094 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006095}
6096
6097/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00006098 * Mark the partial in callback 'cb' with "copyID".
6099 */
6100 int
6101set_ref_in_callback(callback_T *cb, int copyID)
6102{
6103 typval_T tv;
6104
6105 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
6106 return FALSE;
6107
6108 tv.v_type = VAR_PARTIAL;
6109 tv.vval.v_partial = cb->cb_partial;
6110 return set_ref_in_item(&tv, copyID, NULL, NULL);
6111}
6112
6113/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006114 * Mark the dict "dd" with "copyID".
6115 * Also see set_ref_in_item().
6116 */
6117 static int
6118set_ref_in_item_dict(
6119 dict_T *dd,
6120 int copyID,
6121 ht_stack_T **ht_stack,
6122 list_stack_T **list_stack)
6123{
6124 if (dd == NULL || dd->dv_copyID == copyID)
6125 return FALSE;
6126
6127 // Didn't see this dict yet.
6128 dd->dv_copyID = copyID;
6129 if (ht_stack == NULL)
6130 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6131
6132 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
6133 if (newitem == NULL)
6134 return TRUE;
6135
6136 newitem->ht = &dd->dv_hashtab;
6137 newitem->prev = *ht_stack;
6138 *ht_stack = newitem;
6139
6140 return FALSE;
6141}
6142
6143/*
6144 * Mark the list "ll" with "copyID".
6145 * Also see set_ref_in_item().
6146 */
6147 static int
6148set_ref_in_item_list(
6149 list_T *ll,
6150 int copyID,
6151 ht_stack_T **ht_stack,
6152 list_stack_T **list_stack)
6153{
6154 if (ll == NULL || ll->lv_copyID == copyID)
6155 return FALSE;
6156
6157 // Didn't see this list yet.
6158 ll->lv_copyID = copyID;
6159 if (list_stack == NULL)
6160 return set_ref_in_list_items(ll, copyID, ht_stack);
6161
6162 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
6163 if (newitem == NULL)
6164 return TRUE;
6165
6166 newitem->list = ll;
6167 newitem->prev = *list_stack;
6168 *list_stack = newitem;
6169
6170 return FALSE;
6171}
6172
6173/*
6174 * Mark the partial "pt" with "copyID".
6175 * Also see set_ref_in_item().
6176 */
6177 static int
6178set_ref_in_item_partial(
6179 partial_T *pt,
6180 int copyID,
6181 ht_stack_T **ht_stack,
6182 list_stack_T **list_stack)
6183{
6184 if (pt == NULL || pt->pt_copyID == copyID)
6185 return FALSE;
6186
6187 // Didn't see this partial yet.
6188 pt->pt_copyID = copyID;
6189
6190 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
6191
6192 if (pt->pt_dict != NULL)
6193 {
6194 typval_T dtv;
6195
6196 dtv.v_type = VAR_DICT;
6197 dtv.vval.v_dict = pt->pt_dict;
6198 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6199 }
6200
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02006201 if (pt->pt_obj != NULL)
6202 {
6203 typval_T objtv;
6204
6205 objtv.v_type = VAR_OBJECT;
6206 objtv.vval.v_object = pt->pt_obj;
6207 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
6208 }
6209
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006210 for (int i = 0; i < pt->pt_argc; ++i)
6211 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6212 ht_stack, list_stack);
6213 // pt_funcstack is handled in set_ref_in_funcstacks()
6214 // pt_loopvars is handled in set_ref_in_loopvars()
6215
6216 return abort;
6217}
6218
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006219#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006220/*
6221 * Mark the job "pt" with "copyID".
6222 * Also see set_ref_in_item().
6223 */
6224 static int
6225set_ref_in_item_job(
6226 job_T *job,
6227 int copyID,
6228 ht_stack_T **ht_stack,
6229 list_stack_T **list_stack)
6230{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006231 typval_T dtv;
6232
6233 if (job == NULL || job->jv_copyID == copyID)
6234 return FALSE;
6235
6236 job->jv_copyID = copyID;
6237 if (job->jv_channel != NULL)
6238 {
6239 dtv.v_type = VAR_CHANNEL;
6240 dtv.vval.v_channel = job->jv_channel;
6241 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6242 }
6243 if (job->jv_exit_cb.cb_partial != NULL)
6244 {
6245 dtv.v_type = VAR_PARTIAL;
6246 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
6247 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6248 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006249
6250 return FALSE;
6251}
6252
6253/*
6254 * Mark the channel "ch" with "copyID".
6255 * Also see set_ref_in_item().
6256 */
6257 static int
6258set_ref_in_item_channel(
6259 channel_T *ch,
6260 int copyID,
6261 ht_stack_T **ht_stack,
6262 list_stack_T **list_stack)
6263{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006264 typval_T dtv;
6265
6266 if (ch == NULL || ch->ch_copyID == copyID)
6267 return FALSE;
6268
6269 ch->ch_copyID = copyID;
6270 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
6271 {
6272 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
6273 jq != NULL; jq = jq->jq_next)
6274 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6275 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6276 cq = cq->cq_next)
6277 if (cq->cq_callback.cb_partial != NULL)
6278 {
6279 dtv.v_type = VAR_PARTIAL;
6280 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6281 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6282 }
6283 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6284 {
6285 dtv.v_type = VAR_PARTIAL;
6286 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6287 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6288 }
6289 }
6290 if (ch->ch_callback.cb_partial != NULL)
6291 {
6292 dtv.v_type = VAR_PARTIAL;
6293 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6294 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6295 }
6296 if (ch->ch_close_cb.cb_partial != NULL)
6297 {
6298 dtv.v_type = VAR_PARTIAL;
6299 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6300 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6301 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006302
6303 return FALSE;
6304}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006305#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006306
6307/*
6308 * Mark the class "cl" with "copyID".
6309 * Also see set_ref_in_item().
6310 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006311 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006312set_ref_in_item_class(
6313 class_T *cl,
6314 int copyID,
6315 ht_stack_T **ht_stack,
6316 list_stack_T **list_stack)
6317{
6318 int abort = FALSE;
6319
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006320 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006321 return FALSE;
6322
6323 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006324 if (cl->class_members_tv != NULL)
6325 {
6326 // The "class_members_tv" table is allocated only for regular classes
6327 // and not for interfaces.
6328 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6329 abort = abort || set_ref_in_item(
6330 &cl->class_members_tv[i],
6331 copyID, ht_stack, list_stack);
6332 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006333
6334 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6335 abort = abort || set_ref_in_func(NULL,
6336 cl->class_class_functions[i], copyID);
6337
6338 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6339 abort = abort || set_ref_in_func(NULL,
6340 cl->class_obj_methods[i], copyID);
6341
6342 return abort;
6343}
6344
6345/*
6346 * Mark the object "cl" with "copyID".
6347 * Also see set_ref_in_item().
6348 */
6349 static int
6350set_ref_in_item_object(
6351 object_T *obj,
6352 int copyID,
6353 ht_stack_T **ht_stack,
6354 list_stack_T **list_stack)
6355{
6356 int abort = FALSE;
6357
6358 if (obj == NULL || obj->obj_copyID == copyID)
6359 return FALSE;
6360
6361 obj->obj_copyID = copyID;
6362
6363 // The typval_T array is right after the object_T.
6364 typval_T *mtv = (typval_T *)(obj + 1);
6365 for (int i = 0; !abort
6366 && i < obj->obj_class->class_obj_member_count; ++i)
6367 abort = abort || set_ref_in_item(mtv + i, copyID,
6368 ht_stack, list_stack);
6369
6370 return abort;
6371}
6372
6373/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006374 * Mark all lists, dicts and other container types referenced through typval
6375 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006376 * "list_stack" is used to add lists to be marked. Can be NULL.
6377 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6378 *
6379 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006380 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006381 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006382set_ref_in_item(
6383 typval_T *tv,
6384 int copyID,
6385 ht_stack_T **ht_stack,
6386 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006387{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006388 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006389
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006390 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006391 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006392 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006393 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6394 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006395
6396 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006397 return set_ref_in_item_list(tv->vval.v_list, copyID,
6398 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006399
6400 case VAR_FUNC:
6401 {
6402 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6403 break;
6404 }
6405
6406 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006407 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6408 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006409
6410 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006411#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006412 return set_ref_in_item_job(tv->vval.v_job, copyID,
6413 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006414#else
6415 break;
6416#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006417
6418 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006419#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006420 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006421 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006422#else
6423 break;
6424#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006425
6426 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006427 return set_ref_in_item_class(tv->vval.v_class, copyID,
6428 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006429
6430 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006431 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006432 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006433
6434 case VAR_UNKNOWN:
6435 case VAR_ANY:
6436 case VAR_VOID:
6437 case VAR_BOOL:
6438 case VAR_SPECIAL:
6439 case VAR_NUMBER:
6440 case VAR_FLOAT:
6441 case VAR_STRING:
6442 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006443 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006444 case VAR_INSTR:
6445 // Types that do not contain any other item
6446 break;
6447 }
6448
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006449 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006450}
6451
Bram Moolenaar8c711452005-01-14 21:53:12 +00006452/*
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006453 * Return a textual representation of a string in "tv".
6454 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6455 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6456 * strings as "string()", otherwise does not put quotes around strings.
6457 * May return NULL.
6458 */
6459 static char_u *
6460string_tv2string(
6461 typval_T *tv,
6462 char_u **tofree,
6463 int echo_style,
6464 int composite_val)
6465{
6466 char_u *r = NULL;
6467
6468 if (echo_style && !composite_val)
6469 {
6470 *tofree = NULL;
6471 r = tv->vval.v_string;
6472 if (r == NULL)
6473 r = (char_u *)"";
6474 }
6475 else
6476 {
6477 *tofree = string_quote(tv->vval.v_string, FALSE);
6478 r = *tofree;
6479 }
6480
6481 return r;
6482}
6483
6484/*
6485 * Return a textual representation of a function in "tv".
6486 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6487 * When "echo_style" is FALSE, put quotes around the function name as
6488 * "function()", otherwise does not put quotes around function name.
6489 * May return NULL.
6490 */
6491 static char_u *
6492func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
6493{
6494 char_u *r = NULL;
6495 char_u buf[MAX_FUNC_NAME_LEN];
6496
6497 if (echo_style)
6498 {
6499 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6500 : make_ufunc_name_readable(tv->vval.v_string,
6501 buf, MAX_FUNC_NAME_LEN);
6502 if (r == buf)
6503 {
6504 r = vim_strsave(buf);
6505 *tofree = r;
6506 }
6507 else
6508 *tofree = NULL;
6509 }
6510 else
6511 {
6512 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6513 : make_ufunc_name_readable(tv->vval.v_string,
6514 buf, MAX_FUNC_NAME_LEN), TRUE);
6515 r = *tofree;
6516 }
6517
6518 return r;
6519}
6520
6521/*
6522 * Return a textual representation of a partial in "tv".
6523 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6524 * "numbuf" is used for a number. May return NULL.
6525 */
6526 static char_u *
6527partial_tv2string(
6528 typval_T *tv,
6529 char_u **tofree,
6530 char_u *numbuf,
6531 int copyID)
6532{
6533 char_u *r = NULL;
6534 partial_T *pt;
6535 char_u *fname;
6536 garray_T ga;
6537 int i;
6538 char_u *tf;
6539
6540 pt = tv->vval.v_partial;
6541 fname = string_quote(pt == NULL ? NULL : partial_name(pt), FALSE);
6542
6543 ga_init2(&ga, 1, 100);
6544 ga_concat(&ga, (char_u *)"function(");
6545 if (fname != NULL)
6546 {
6547 // When using uf_name prepend "g:" for a global function.
6548 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
6549 && 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);
6556 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, tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6566 vim_free(tf);
6567 }
6568 ga_concat(&ga, (char_u *)"]");
6569 }
6570 if (pt != NULL && pt->pt_dict != NULL)
6571 {
6572 typval_T dtv;
6573
6574 ga_concat(&ga, (char_u *)", ");
6575 dtv.v_type = VAR_DICT;
6576 dtv.vval.v_dict = pt->pt_dict;
6577 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6578 vim_free(tf);
6579 }
6580 // terminate with ')' and a NUL
6581 ga_concat_len(&ga, (char_u *)")", 2);
6582
6583 *tofree = ga.ga_data;
6584 r = *tofree;
6585
6586 return r;
6587}
6588
6589/*
6590 * Return a textual representation of a List in "tv".
6591 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6592 * When "copyID" is not zero replace recursive lists with "...". When
6593 * "restore_copyID" is FALSE, repeated items in lists are replaced with "...".
6594 * May return NULL.
6595 */
6596 static char_u *
6597list_tv2string(
6598 typval_T *tv,
6599 char_u **tofree,
6600 int copyID,
6601 int restore_copyID)
6602{
6603 char_u *r = NULL;
6604
6605 if (tv->vval.v_list == NULL)
6606 {
6607 // NULL list is equivalent to empty list.
6608 *tofree = NULL;
6609 r = (char_u *)"[]";
6610 }
6611 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6612 && tv->vval.v_list->lv_len > 0)
6613 {
6614 *tofree = NULL;
6615 r = (char_u *)"[...]";
6616 }
6617 else
6618 {
6619 int old_copyID = tv->vval.v_list->lv_copyID;
6620
6621 tv->vval.v_list->lv_copyID = copyID;
6622 *tofree = list2string(tv, copyID, restore_copyID);
6623 if (restore_copyID)
6624 tv->vval.v_list->lv_copyID = old_copyID;
6625 r = *tofree;
6626 }
6627
6628 return r;
6629}
6630
6631/*
6632 * Return a textual representation of a Dict in "tv".
6633 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6634 * When "copyID" is not zero replace recursive dicts with "...".
6635 * When "restore_copyID" is FALSE, repeated items in the dictionary are
6636 * replaced with "...". May return NULL.
6637 */
6638 static char_u *
6639dict_tv2string(
6640 typval_T *tv,
6641 char_u **tofree,
6642 int copyID,
6643 int restore_copyID)
6644{
6645 char_u *r = NULL;
6646
6647 if (tv->vval.v_dict == NULL)
6648 {
6649 // NULL dict is equivalent to empty dict.
6650 *tofree = NULL;
6651 r = (char_u *)"{}";
6652 }
6653 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6654 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
6655 {
6656 *tofree = NULL;
6657 r = (char_u *)"{...}";
6658 }
6659 else
6660 {
6661 int old_copyID = tv->vval.v_dict->dv_copyID;
6662
6663 tv->vval.v_dict->dv_copyID = copyID;
6664 *tofree = dict2string(tv, copyID, restore_copyID);
6665 if (restore_copyID)
6666 tv->vval.v_dict->dv_copyID = old_copyID;
6667 r = *tofree;
6668 }
6669
6670 return r;
6671}
6672
6673/*
6674 * Return a textual representation of a job or a channel in "tv".
6675 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6676 * "numbuf" is used for a number.
6677 * When "composite_val" is FALSE, put quotes around strings as "string()",
6678 * otherwise does not put quotes around strings.
6679 * May return NULL.
6680 */
6681 static char_u *
6682jobchan_tv2string(
6683 typval_T *tv,
6684 char_u **tofree,
6685 char_u *numbuf,
6686 int composite_val)
6687{
6688 char_u *r = NULL;
6689
6690#ifdef FEAT_JOB_CHANNEL
6691 *tofree = NULL;
6692
6693 if (tv->v_type == VAR_JOB)
6694 r = job_to_string_buf(tv, numbuf);
6695 else
6696 r = channel_to_string_buf(tv, numbuf);
6697
6698 if (composite_val)
6699 {
6700 *tofree = string_quote(r, FALSE);
6701 r = *tofree;
6702 }
6703#endif
6704
6705 return r;
6706}
6707
6708/*
6709 * Return a textual representation of a class in "tv".
6710 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6711 * May return NULL.
6712 */
6713 static char_u *
6714class_tv2string(typval_T *tv, char_u **tofree)
6715{
6716 char_u *r = NULL;
6717 class_T *cl = tv->vval.v_class;
6718 char *s = "class";
6719
6720 if (cl != NULL && IS_INTERFACE(cl))
6721 s = "interface";
6722 else if (cl != NULL && IS_ENUM(cl))
6723 s = "enum";
6724 size_t len = STRLEN(s) + 1 +
6725 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6726 r = *tofree = alloc(len);
6727 vim_snprintf((char *)r, len, "%s %s", s,
6728 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6729
6730 return r;
6731}
6732
6733/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006734 * Return a string with the string representation of a variable.
6735 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006736 * "numbuf" is used for a number.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006737 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006738 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006739 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006740 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006741 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6742 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006743 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006745 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006746echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747 typval_T *tv,
6748 char_u **tofree,
6749 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006750 int copyID,
6751 int echo_style,
6752 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006753 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755 static int recurse = 0;
6756 char_u *r = NULL;
6757
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006760 if (!did_echo_string_emsg)
6761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006762 // Only give this message once for a recursive call to avoid
6763 // flooding the user with errors. And stop iterating over lists
6764 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006765 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006766 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006768 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006769 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006770 }
6771 ++recurse;
6772
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006773 switch (tv->v_type)
6774 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006775 case VAR_STRING:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006776 r = string_tv2string(tv, tofree, echo_style, composite_val);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006777 break;
6778
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779 case VAR_FUNC:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006780 r = func_tv2string(tv, tofree, echo_style);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006782
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006783 case VAR_PARTIAL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006784 r = partial_tv2string(tv, tofree, numbuf, copyID);
6785 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006786
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006787 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006788 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006789 break;
6790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 case VAR_LIST:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006792 r = list_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006793 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006794
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795 case VAR_DICT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006796 r = dict_tv2string(tv, tofree, copyID, restore_copyID);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006797 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006798
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006799 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006800 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006801 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006803 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006804 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006805 break;
6806
Bram Moolenaar835dc632016-02-07 14:27:38 +01006807 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006808 case VAR_CHANNEL:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006809 r = jobchan_tv2string(tv, tofree, numbuf, composite_val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006810 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006811
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006812 case VAR_INSTR:
6813 *tofree = NULL;
6814 r = (char_u *)"instructions";
6815 break;
6816
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006817 case VAR_CLASS:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006818 r = class_tv2string(tv, tofree);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006819 break;
6820
6821 case VAR_OBJECT:
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006822 *tofree = r = object2string(tv->vval.v_object, numbuf, copyID,
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006823 echo_style, restore_copyID,
6824 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006825 break;
6826
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006827 case VAR_FLOAT:
6828 *tofree = NULL;
6829 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6830 r = numbuf;
6831 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006832
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006833 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006834 case VAR_SPECIAL:
6835 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006836 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006837 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006838
6839 case VAR_TYPEALIAS:
6840 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6841 r = *tofree;
6842 if (r == NULL)
6843 r = (char_u *)"";
6844 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846
Bram Moolenaar8502c702014-06-17 12:51:16 +02006847 if (--recurse == 0)
6848 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850}
6851
6852/*
6853 * Return a string with the string representation of a variable.
6854 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6855 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006856 * Does not put quotes around strings, as ":echo" displays values.
Yegappan Lakshmanan22029ed2024-05-20 13:57:11 +02006857 * When "copyID" is not zero replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006858 * May return NULL.
6859 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006861echo_string(
6862 typval_T *tv,
6863 char_u **tofree,
6864 char_u *numbuf,
6865 int copyID)
6866{
6867 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6868}
6869
6870/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006871 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6872 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006873 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006874 */
6875 int
6876buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6877{
6878 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006879 char_u *t;
6880 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006881
6882 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6883 return -1;
6884
6885 if (lnum > buf->b_ml.ml_line_count)
6886 lnum = buf->b_ml.ml_line_count;
6887
6888 str = ml_get_buf(buf, lnum, FALSE);
6889 if (str == NULL)
6890 return -1;
6891
6892 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006893 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006894
Bram Moolenaar91458462021-01-13 20:08:38 +01006895 // count the number of characters
6896 t = str;
6897 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6898 t += mb_ptr2len(t);
6899
6900 // In insert mode, when the cursor is at the end of a non-empty line,
6901 // byteidx points to the NUL character immediately past the end of the
6902 // string. In this case, add one to the character count.
6903 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6904 count++;
6905
6906 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006907}
6908
6909/*
6910 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006911 * byte index. Works only for loaded buffers. Returns -1 on failure.
6912 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006913 */
6914 int
6915buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6916{
6917 char_u *str;
6918 char_u *t;
6919
6920 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6921 return -1;
6922
6923 if (lnum > buf->b_ml.ml_line_count)
6924 lnum = buf->b_ml.ml_line_count;
6925
6926 str = ml_get_buf(buf, lnum, FALSE);
6927 if (str == NULL)
6928 return -1;
6929
6930 // Convert the character offset to a byte offset
6931 t = str;
6932 while (*t != NUL && --charidx > 0)
6933 t += mb_ptr2len(t);
6934
Bram Moolenaar91458462021-01-13 20:08:38 +01006935 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006936}
6937
6938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006940 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006943var2fpos(
6944 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006945 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006946 int *fnum, // set to fnum for '0, 'A, etc.
6947 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006949 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006951 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006953 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006954 if (varp->v_type == VAR_LIST)
6955 {
6956 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006957 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006958 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006959 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006960
6961 l = varp->vval.v_list;
6962 if (l == NULL)
6963 return NULL;
6964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006965 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006966 pos.lnum = list_find_nr(l, 0L, &error);
6967 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006968 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006969 if (charcol)
6970 len = (long)mb_charlen(ml_get(pos.lnum));
6971 else
John Marriottbfcc8952024-03-11 22:04:45 +01006972 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006973
Bram Moolenaarec65d772020-08-20 22:29:12 +02006974 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006975 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006976 li = list_find(l, 1L);
6977 if (li != NULL && li->li_tv.v_type == VAR_STRING
6978 && li->li_tv.vval.v_string != NULL
6979 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006980 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006981 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006982 }
6983 else
6984 {
6985 pos.col = list_find_nr(l, 1L, &error);
6986 if (error)
6987 return NULL;
6988 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006990 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006991 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006992 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006993 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006995 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006996 pos.coladd = list_find_nr(l, 2L, &error);
6997 if (error)
6998 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006999
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007000 return &pos;
7001 }
7002
Bram Moolenaarc5809432021-03-27 21:23:30 +01007003 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
7004 return NULL;
7005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007007 if (name == NULL)
7008 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01007009
7010 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00007011 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007012 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00007013 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007014 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007015 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01007016 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00007017 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01007018 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00007019 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007020 pos = VIsual;
7021 else
7022 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00007023 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01007024 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00007025 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00007027 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01007028 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
7030 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01007031 pos = *pp;
7032 }
7033 if (pos.lnum != 0)
7034 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007035 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01007036 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
7037 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
Bram Moolenaara5525202006-03-02 22:52:09 +00007039
Bram Moolenaara5525202006-03-02 22:52:09 +00007040 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00007041
Bram Moolenaar477933c2007-07-17 14:32:23 +00007042 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00007043 {
Bram Moolenaar85090142023-06-01 19:27:08 +01007044 // the "w_valid" flags are not reset when moving the cursor, but they
7045 // do matter for update_topline() and validate_botline().
7046 check_cursor_moved(curwin);
7047
Bram Moolenaarf52c7252006-02-10 23:23:57 +00007048 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007049 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00007050 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007051 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007052 // In silent Ex mode topline is zero, but that's not a valid line
7053 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02007054 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00007055 return &pos;
7056 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007057 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00007058 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007059 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007060 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02007061 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00007062 return &pos;
7063 }
7064 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007065 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00007067 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {
7069 pos.lnum = curbuf->b_ml.ml_line_count;
7070 pos.col = 0;
7071 }
7072 else
7073 {
7074 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007075 if (charcol)
7076 pos.col = (colnr_T)mb_charlen(ml_get_curline());
7077 else
John Marriottbfcc8952024-03-11 22:04:45 +01007078 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
7080 return &pos;
7081 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02007082 if (in_vim9script())
7083 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 return NULL;
7085}
7086
7087/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01007088 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01007089 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007090 * Note that the column is passed on as-is, the caller may want to decrement
7091 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01007092 * If "charcol" is TRUE use the column as the character index instead of the
7093 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007094 * Return FAIL when conversion is not possible, doesn't check the position for
7095 * validity.
7096 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007098list2fpos(
7099 typval_T *arg,
7100 pos_T *posp,
7101 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007102 colnr_T *curswantp,
7103 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007104{
7105 list_T *l = arg->vval.v_list;
7106 long i = 0;
7107 long n;
7108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007109 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
7110 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00007111 if (arg->v_type != VAR_LIST
7112 || l == NULL
7113 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02007114 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007115 return FAIL;
7116
7117 if (fnump != NULL)
7118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007119 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007120 if (n < 0)
7121 return FAIL;
7122 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007123 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007124 *fnump = n;
7125 }
7126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007127 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007128 if (n < 0)
7129 return FAIL;
7130 posp->lnum = n;
7131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007132 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007133 if (n < 0)
7134 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007135 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01007136 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007137 if (charcol)
7138 {
7139 buf_T *buf;
7140
7141 // Get the text for the specified line in a loaded buffer
7142 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
7143 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
7144 return FAIL;
7145
Bram Moolenaar79f23442022-10-10 12:42:57 +01007146 n = buf_charidx_to_byteidx(buf,
7147 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007148 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007149 posp->col = n;
7150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007151 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007152 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00007153 posp->coladd = 0;
7154 else
7155 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007156
Bram Moolenaar493c1782014-05-28 14:34:46 +02007157 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007158 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02007159
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007160 return OK;
7161}
7162
7163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 * Get the length of an environment variable name.
7165 * Advance "arg" to the first character after the name.
7166 * Return 0 for error.
7167 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 char_u *p;
7172 int len;
7173
7174 for (p = *arg; vim_isIDc(*p); ++p)
7175 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007176 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 return 0;
7178
7179 len = (int)(p - *arg);
7180 *arg = p;
7181 return len;
7182}
7183
7184/*
7185 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007186 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 * Return 0 if something is wrong.
7188 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 char_u *p;
7193 int len;
7194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007195 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007197 {
7198 if (*p == ':')
7199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007200 // "s:" is start of "s:var", but "n:" is not and can be used in
7201 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007202 len = (int)(p - *arg);
7203 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
7204 || len > 1)
7205 break;
7206 }
7207 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007208 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 return 0;
7210
7211 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02007212 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214 return len;
7215}
7216
7217/*
Bram Moolenaara7043832005-01-21 11:56:39 +00007218 * Get the length of the name of a variable or function.
7219 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007221 * Return -1 if curly braces expansion failed.
7222 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 * If the name contains 'magic' {}'s, expand them and return the
7224 * expanded name in an allocated string via 'alias' - caller must free.
7225 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007227get_name_len(
7228 char_u **arg,
7229 char_u **alias,
7230 int evaluate,
7231 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 char_u *p;
7235 char_u *expr_start;
7236 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007238 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
7241 && (*arg)[2] == (int)KE_SNR)
7242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007243 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 *arg += 3;
7245 return get_id_len(arg) + 3;
7246 }
7247 len = eval_fname_script(*arg);
7248 if (len > 0)
7249 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007250 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 *arg += len;
7252 }
7253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007255 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007257 p = find_name_end(*arg, &expr_start, &expr_end,
7258 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (expr_start != NULL)
7260 {
7261 char_u *temp_string;
7262
7263 if (!evaluate)
7264 {
7265 len += (int)(p - *arg);
7266 *arg = skipwhite(p);
7267 return len;
7268 }
7269
7270 /*
7271 * Include any <SID> etc in the expanded string:
7272 * Thus the -len here.
7273 */
7274 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
7275 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007276 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 *alias = temp_string;
7278 *arg = skipwhite(p);
7279 return (int)STRLEN(temp_string);
7280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
7282 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01007283 // Only give an error when there is something, otherwise it will be
7284 // reported at a higher level.
7285 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007286 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
7288 return len;
7289}
7290
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007291/*
7292 * Find the end of a variable or function name, taking care of magic braces.
7293 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7294 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007295 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007296 * Return a pointer to just after the name. Equal to "arg" if there is no
7297 * valid name.
7298 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300find_name_end(
7301 char_u *arg,
7302 char_u **expr_start,
7303 char_u **expr_end,
7304 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007306 int mb_nest = 0;
7307 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007309 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02007310 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007314 *expr_start = NULL;
7315 *expr_end = NULL;
7316 }
7317
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007318 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007319 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007320 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007321 return arg;
7322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007323 for (p = arg; *p != NUL
7324 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01007325 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02007326 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007327 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007328 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007329 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007330 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007331 if (*p == '\'')
7332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007333 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007334 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007335 ;
7336 if (*p == NUL)
7337 break;
7338 }
7339 else if (*p == '"')
7340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007341 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007342 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007343 if (*p == '\\' && p[1] != NUL)
7344 ++p;
7345 if (*p == NUL)
7346 break;
7347 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007348 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007350 // "s:" is start of "s:var", but "n:" is not and can be used in
7351 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007352 len = (int)(p - arg);
7353 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007354 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007355 break;
7356 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007360 if (*p == '[')
7361 ++br_nest;
7362 else if (*p == ']')
7363 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007365
zeertzjqa93d9cd2023-05-02 16:25:47 +01007366 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007368 if (*p == '{')
7369 {
7370 mb_nest++;
7371 if (expr_start != NULL && *expr_start == NULL)
7372 *expr_start = p;
7373 }
7374 else if (*p == '}')
7375 {
7376 mb_nest--;
7377 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7378 *expr_end = p;
7379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 }
7382
7383 return p;
7384}
7385
7386/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007387 * Expands out the 'magic' {}'s in a variable/function name.
7388 * Note that this can call itself recursively, to deal with
7389 * constructs like foo{bar}{baz}{bam}
7390 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7391 * "in_start" ^
7392 * "expr_start" ^
7393 * "expr_end" ^
7394 * "in_end" ^
7395 *
7396 * Returns a new allocated string, which the caller must free.
7397 * Returns NULL for failure.
7398 */
7399 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007400make_expanded_name(
7401 char_u *in_start,
7402 char_u *expr_start,
7403 char_u *expr_end,
7404 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007405{
7406 char_u c1;
7407 char_u *retval = NULL;
7408 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007409
7410 if (expr_end == NULL || in_end == NULL)
7411 return NULL;
7412 *expr_start = NUL;
7413 *expr_end = NUL;
7414 c1 = *in_end;
7415 *in_end = NUL;
7416
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007417 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007418 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007419 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007420 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7421 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007422 if (retval != NULL)
7423 {
7424 STRCPY(retval, in_start);
7425 STRCAT(retval, temp_result);
7426 STRCAT(retval, expr_end + 1);
7427 }
7428 }
7429 vim_free(temp_result);
7430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007431 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007432 *expr_start = '{';
7433 *expr_end = '}';
7434
7435 if (retval != NULL)
7436 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007437 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007438 if (expr_start != NULL)
7439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007440 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007441 temp_result = make_expanded_name(retval, expr_start,
7442 expr_end, temp_result);
7443 vim_free(retval);
7444 retval = temp_result;
7445 }
7446 }
7447
7448 return retval;
7449}
7450
7451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007456eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007458 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007459}
7460
7461/*
7462 * Return TRUE if character "c" can be used as the first character in a
7463 * variable or function name (excluding '{' and '}').
7464 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007466eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007467{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007468 return ASCII_ISALPHA(c) || c == '_';
7469}
7470
7471/*
7472 * Return TRUE if character "c" can be used as the first character of a
7473 * dictionary key.
7474 */
7475 int
7476eval_isdictc(int c)
7477{
7478 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479}
7480
7481/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007482 * Handle:
7483 * - expr[expr], expr[expr:expr] subscript
7484 * - ".name" lookup
7485 * - function call with Funcref variable: func(expr)
7486 * - method call: var->method()
7487 *
7488 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007489 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007490 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007492handle_subscript(
7493 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007494 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007495 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007496 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007497 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007498{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007499 int evaluate = evalarg != NULL
7500 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007501 int ret = OK;
7502 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007503 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007504 int getnext;
7505 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007506
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007507 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007508 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007509 // When at the end of the line and ".name" or "->{" or "->X" follows in
7510 // the next line then consume the line break.
7511 p = eval_next_non_blank(*arg, evalarg, &getnext);
7512 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007513 && ((*p == '.'
7514 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7515 || rettv->v_type == VAR_CLASS
7516 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007517 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7518 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7519 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007520 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007521 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007522 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007523 check_white = FALSE;
7524 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007525
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007526 if (rettv->v_type == VAR_ANY)
7527 {
7528 char_u *exp_name;
7529 int cc;
7530 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007531 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007532 type_T *type;
7533
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007534 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007535 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007536 if (**arg != '.')
7537 {
7538 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007539 semsg(_(e_expected_dot_after_name_str),
7540 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007541 ret = FAIL;
7542 break;
7543 }
7544 ++*arg;
7545 if (IS_WHITE_OR_NUL(**arg))
7546 {
7547 if (verbose)
7548 emsg(_(e_no_white_space_allowed_after_dot));
7549 ret = FAIL;
7550 break;
7551 }
7552
7553 // isolate the name
7554 exp_name = *arg;
7555 while (eval_isnamec(**arg))
7556 ++*arg;
7557 cc = **arg;
7558 **arg = NUL;
7559
7560 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007561 evalarg == NULL ? NULL : evalarg->eval_cctx,
7562 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007563 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007564
7565 if (idx < 0 && ufunc == NULL)
7566 {
7567 ret = FAIL;
7568 break;
7569 }
7570 if (idx >= 0)
7571 {
7572 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7573 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7574
7575 copy_tv(sv->sv_tv, rettv);
7576 }
7577 else
7578 {
7579 rettv->v_type = VAR_FUNC;
7580 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7581 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007582 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007583 }
7584
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007585 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7586 || rettv->v_type == VAR_PARTIAL))
7587 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007588 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007589 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7590 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007591
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007592 // Stop the expression evaluation when immediately aborting on
7593 // error, or when an interrupt occurred or an exception was thrown
7594 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007595 if (aborting())
7596 {
7597 if (ret == OK)
7598 clear_tv(rettv);
7599 ret = FAIL;
7600 }
7601 dict_unref(selfdict);
7602 selfdict = NULL;
7603 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007604 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007605 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007606 if (in_vim9script())
7607 *arg = skipwhite(p + 2);
7608 else
7609 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007610 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007611 {
zeertzjqc481ad32023-03-11 16:18:51 +00007612 emsg(_(e_no_white_space_allowed_before_parenthesis));
7613 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007614 }
zeertzjqc481ad32023-03-11 16:18:51 +00007615 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7616 // expr->{lambda}() or expr->(lambda)()
7617 ret = eval_lambda(arg, rettv, evalarg, verbose);
7618 else
7619 // expr->name()
7620 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007621 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007622 // "." is ".name" lookup when we found a dict or when evaluating and
7623 // scriptversion is at least 2, where string concatenation is "..".
7624 else if (**arg == '['
7625 || (**arg == '.' && (rettv->v_type == VAR_DICT
7626 || (!evaluate
7627 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007628 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007629 {
7630 dict_unref(selfdict);
7631 if (rettv->v_type == VAR_DICT)
7632 {
7633 selfdict = rettv->vval.v_dict;
7634 if (selfdict != NULL)
7635 ++selfdict->dv_refcount;
7636 }
7637 else
7638 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007639 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007640 {
7641 clear_tv(rettv);
7642 ret = FAIL;
7643 }
7644 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007645 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7646 || rettv->v_type == VAR_OBJECT))
7647 {
7648 // class member: SomeClass.varname
7649 // class method: SomeClass.SomeMethod()
7650 // class constructor: SomeClass.new()
7651 // object member: someObject.varname
7652 // object method: someObject.SomeMethod()
7653 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7654 {
7655 clear_tv(rettv);
7656 ret = FAIL;
7657 }
7658 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007659 else
7660 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007661 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007663 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7664 // Don't do this when "Func" is already a partial that was bound
7665 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007666 if (selfdict != NULL
7667 && (rettv->v_type == VAR_FUNC
7668 || (rettv->v_type == VAR_PARTIAL
7669 && (rettv->vval.v_partial->pt_auto
7670 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007671 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007672
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007673 dict_unref(selfdict);
7674 return ret;
7675}
7676
7677/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 * Make a copy of an item.
7679 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007680 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007681 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7682 * reference to an already copied list/dict can be used.
7683 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686item_copy(
7687 typval_T *from,
7688 typval_T *to,
7689 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007690 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007691 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692{
7693 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007694 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007698 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007699 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 }
7701 ++recurse;
7702
7703 switch (from->v_type)
7704 {
7705 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007706 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007707 case VAR_STRING:
7708 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007709 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007710 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007711 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007712 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007713 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007714 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007715 case VAR_CLASS:
7716 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007717 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007718 copy_tv(from, to);
7719 break;
7720 case VAR_LIST:
7721 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007722 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007723 if (from->vval.v_list == NULL)
7724 to->vval.v_list = NULL;
7725 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007727 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007728 to->vval.v_list = from->vval.v_list->lv_copylist;
7729 ++to->vval.v_list->lv_refcount;
7730 }
7731 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007732 to->vval.v_list = list_copy(from->vval.v_list,
7733 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007734 if (to->vval.v_list == NULL)
7735 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007736 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007737 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007739 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 case VAR_DICT:
7741 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007742 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007743 if (from->vval.v_dict == NULL)
7744 to->vval.v_dict = NULL;
7745 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007747 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007748 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7749 ++to->vval.v_dict->dv_refcount;
7750 }
7751 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007752 to->vval.v_dict = dict_copy(from->vval.v_dict,
7753 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007754 if (to->vval.v_dict == NULL)
7755 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007757 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007758 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007760 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007761 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007762 }
7763 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007764 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765}
7766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 void
7768echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7769{
7770 char_u *tofree;
7771 char_u numbuf[NUMBUFLEN];
7772 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7773
7774 if (*atstart)
7775 {
7776 *atstart = FALSE;
7777 // Call msg_start() after eval1(), evaluating the expression
7778 // may cause a message to appear.
7779 if (with_space)
7780 {
7781 // Mark the saved text as finishing the line, so that what
7782 // follows is displayed on a new line when scrolling back
7783 // at the more prompt.
7784 msg_sb_eol();
7785 msg_start();
7786 }
7787 }
7788 else if (with_space)
7789 msg_puts_attr(" ", echo_attr);
7790
7791 if (p != NULL)
7792 for ( ; *p != NUL && !got_int; ++p)
7793 {
7794 if (*p == '\n' || *p == '\r' || *p == TAB)
7795 {
7796 if (*p != TAB && *needclr)
7797 {
7798 // remove any text still there from the command
7799 msg_clr_eos();
7800 *needclr = FALSE;
7801 }
7802 msg_putchar_attr(*p, echo_attr);
7803 }
7804 else
7805 {
7806 if (has_mbyte)
7807 {
7808 int i = (*mb_ptr2len)(p);
7809
7810 (void)msg_outtrans_len_attr(p, i, echo_attr);
7811 p += i - 1;
7812 }
7813 else
7814 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7815 }
7816 }
7817 vim_free(tofree);
7818}
7819
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 * ":echo expr1 ..." print each argument separated with a space, add a
7822 * newline at the end.
7823 * ":echon expr1 ..." print each argument plain.
7824 */
7825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007826ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007830 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 int needclr = TRUE;
7832 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007833 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007834 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007835 evalarg_T evalarg;
7836
Bram Moolenaare707c882020-07-01 13:07:37 +02007837 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838
7839 if (eap->skip)
7840 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007841 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007843 // If eval1() causes an error message the text from the command may
7844 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845 need_clr_eos = needclr;
7846
Bram Moolenaar68db9962021-05-09 23:19:22 +02007847 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007848 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {
7850 /*
7851 * Report the invalid expression unless the expression evaluation
7852 * has been cancelled due to an aborting error, an interrupt, or an
7853 * exception.
7854 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007855 if (!aborting() && did_emsg == did_emsg_before
7856 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007857 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007858 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 break;
7860 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861 need_clr_eos = FALSE;
7862
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007864 {
7865 if (rettv.v_type == VAR_VOID)
7866 {
7867 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7868 break;
7869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007871 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 arg = skipwhite(arg);
7875 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007876 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007877 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
7879 if (eap->skip)
7880 --emsg_skip;
7881 else
7882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007883 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (needclr)
7885 msg_clr_eos();
7886 if (eap->cmdidx == CMD_echo)
7887 msg_end();
7888 }
7889}
7890
7891/*
7892 * ":echohl {name}".
7893 */
7894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007897 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898}
7899
7900/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007901 * Returns the :echo attribute
7902 */
7903 int
7904get_echo_attr(void)
7905{
7906 return echo_attr;
7907}
7908
7909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 * ":execute expr1 ..." execute the result of an expression.
7911 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007912 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007914 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * Each gets spaces around each argument and a newline at the end for
7916 * echo commands
7917 */
7918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007919ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 int ret = OK;
7924 char_u *p;
7925 garray_T ga;
7926 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007927 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 ga_init2(&ga, 1, 80);
7930
7931 if (eap->skip)
7932 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007933 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007935 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007936 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938
7939 if (!eap->skip)
7940 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007941 char_u buf[NUMBUFLEN];
7942
7943 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007944 {
7945 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7946 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007947 semsg(_(e_using_invalid_value_as_string_str),
7948 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007949 p = NULL;
7950 }
7951 else
7952 p = tv_get_string_buf(&rettv, buf);
7953 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007954 else
7955 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007956 if (p == NULL)
7957 {
7958 clear_tv(&rettv);
7959 ret = FAIL;
7960 break;
7961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 len = (int)STRLEN(p);
7963 if (ga_grow(&ga, len + 2) == FAIL)
7964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 ret = FAIL;
7967 break;
7968 }
7969 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 ga.ga_len += len;
7973 }
7974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 arg = skipwhite(arg);
7977 }
7978
7979 if (ret != FAIL && ga.ga_data != NULL)
7980 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007981 // use the first line of continuation lines for messages
7982 SOURCING_LNUM = start_lnum;
7983
Bram Moolenaar37fef162022-08-29 18:16:32 +01007984 if (eap->cmdidx == CMD_echomsg
7985 || eap->cmdidx == CMD_echowindow
7986 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007988 // Mark the already saved text as finishing the line, so that what
7989 // follows is displayed on a new line when scrolling back at the
7990 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007991 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007992 }
7993
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007994 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007995 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007996 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007997 out_flush();
7998 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007999 else if (eap->cmdidx == CMD_echowindow)
8000 {
8001#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01008002 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01008003#endif
8004 msg_attr(ga.ga_data, echo_attr);
8005#ifdef HAS_MESSAGE_WINDOW
8006 end_echowindow();
8007#endif
8008 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01008009 else if (eap->cmdidx == CMD_echoconsole)
8010 {
8011 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
8012 ui_write((char_u *)"\r\n", 2, TRUE);
8013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 else if (eap->cmdidx == CMD_echoerr)
8015 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008016 int save_did_emsg = did_emsg;
8017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008018 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008019 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 if (!force_abort)
8021 did_emsg = save_did_emsg;
8022 }
8023 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00008024 {
8025 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
8026
8027 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
8028 sticky_cmdmod_flags = cmdmod.cmod_flags
8029 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01008031 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00008032 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
8033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 }
8035
8036 ga_clear(&ga);
8037
8038 if (eap->skip)
8039 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02008040 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041}
8042
8043/*
8044 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8045 * "arg" points to the "&" or '+' when called, to "option" when returning.
8046 * Returns NULL when no option name found. Otherwise pointer to the char
8047 * after the option name.
8048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02008049 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008050find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
8052 char_u *p = *arg;
8053
8054 ++p;
8055 if (*p == 'g' && p[1] == ':')
8056 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008057 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 p += 2;
8059 }
8060 else if (*p == 'l' && p[1] == ':')
8061 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008062 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 p += 2;
8064 }
8065 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008066 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
8068 if (!ASCII_ISALPHA(*p))
8069 return NULL;
8070 *arg = p;
8071
8072 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008073 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 else
8075 while (ASCII_ISALPHA(*p))
8076 ++p;
8077 return p;
8078}
8079
8080/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00008081 * Display script name where an item was last set.
8082 * Should only be invoked when 'verbose' is non-zero.
8083 */
8084 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008085last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008086{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008087 char_u *p;
8088
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00008089 if (script_ctx.sc_sid == 0)
8090 return;
8091
8092 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
8093 if (p == NULL)
8094 return;
8095
8096 verbose_enter();
8097 msg_puts(_("\n\tLast set from "));
8098 msg_puts((char *)p);
8099 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008100 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00008101 msg_puts(_(line_msg));
8102 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00008103 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00008104 verbose_leave();
8105 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00008106}
8107
Bram Moolenaarb005cd82019-09-04 15:54:55 +02008108#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109
8110/*
8111 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008112 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 * "flags" can be "g" to do a global substitute.
8114 * Returns an allocated string, NULL for error.
8115 */
8116 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008117do_string_sub(
8118 char_u *str,
8119 char_u *pat,
8120 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008121 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008122 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 int sublen;
8125 regmatch_T regmatch;
8126 int i;
8127 int do_all;
8128 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01008129 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 garray_T ga;
8131 char_u *ret;
8132 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01008133 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008135 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008137 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138
8139 ga_init2(&ga, 1, 200);
8140
8141 do_all = (flags[0] == 'g');
8142
8143 regmatch.rm_ic = p_ic;
8144 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8145 if (regmatch.regprog != NULL)
8146 {
8147 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01008148 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
8150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008151 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01008152 if (regmatch.startp[0] == regmatch.endp[0])
8153 {
8154 if (zero_width == regmatch.startp[0])
8155 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008156 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02008157 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02008158 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
8159 (size_t)i);
8160 ga.ga_len += i;
8161 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01008162 continue;
8163 }
8164 zero_width = regmatch.startp[0];
8165 }
8166
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 /*
8168 * Get some space for a temporary buffer to do the substitution
8169 * into. It will contain:
8170 * - The text up to where the match is.
8171 * - The substituted text.
8172 * - The text after the match.
8173 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008174 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00008175 if (sublen <= 0)
8176 {
8177 ga_clear(&ga);
8178 break;
8179 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01008180 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
8182 {
8183 ga_clear(&ga);
8184 break;
8185 }
8186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008187 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 i = (int)(regmatch.startp[0] - tail);
8189 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008190 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01008191 (void)vim_regsub(&regmatch, sub, expr,
8192 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
8193 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02008195 tail = regmatch.endp[0];
8196 if (*tail == NUL)
8197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (!do_all)
8199 break;
8200 }
8201
8202 if (ga.ga_data != NULL)
8203 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
8204
Bram Moolenaar473de612013-06-08 18:19:48 +02008205 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 }
8207
8208 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
8209 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008210 if (p_cpo == empty_option)
8211 p_cpo = save_cpo;
8212 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008213 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008214 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008215 // If it's still empty it was changed and restored, need to restore in
8216 // the complicated way.
8217 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008218 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008219 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
8222 return ret;
8223}