blob: a4fde0ef292e67cabe7e3368d9f9333392580c9c [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/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200971 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Ernie Rael64885642023-10-04 20:16:22 +0200988#ifdef LOG_LOCKVAR
989typedef struct flag_string_S
990{
991 int flag;
992 char *str;
993} flag_string_T;
994
995 static char *
996flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
997{
998 char *p = buf;
999 *p = NUL;
1000 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1001 {
1002 if ((fstring->flag & flags) != 0)
1003 {
1004 size_t len = STRLEN(fstring->str);
1005 if (n > p - buf + len + 7)
1006 {
1007 STRCAT(p, fstring->str);
1008 p += len;
1009 STRCAT(p, " ");
1010 ++p;
1011 }
1012 else
1013 {
1014 STRCAT(buf, "...");
1015 break;
1016 }
1017 }
1018 }
1019 return buf;
1020}
1021
1022flag_string_T glv_flag_strings[] = {
1023 { GLV_QUIET, "QUIET" },
1024 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1025 { GLV_READ_ONLY, "READ_ONLY" },
1026 { GLV_NO_DECL, "NO_DECL" },
1027 { GLV_COMPILING, "COMPILING" },
1028 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1029 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1030 { 0, NULL }
1031};
1032#endif
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
Ernie Raelee865f32023-09-29 19:53:55 +02001035 * Fill in "lp" using "root". This is used in a special case when
1036 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1037 *
1038 * This is typically called with "lval_root" as "root". For a class, find
1039 * the name from lp in the class from root, fill in lval_T if found. For a
1040 * complex type, list/dict use it as the result; just put the root into
1041 * ll_tv.
1042 *
1043 * "lval_root" is a hack used during run-time/instr-execution to provide the
1044 * starting point for "get_lval()" to traverse a chain of indexes. In some
1045 * cases get_lval sees a bare name and uses this function to populate the
1046 * lval_T.
1047 *
1048 * For setting up "lval_root" (currently only used with lockvar)
1049 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1050 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1051 */
1052 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001053fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001054{
1055#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001056 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1057 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001058#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001059 if (lr->lr_tv == NULL)
1060 return;
Ernie Rael64885642023-10-04 20:16:22 +02001061 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001062 {
Ernie Rael64885642023-10-04 20:16:22 +02001063 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001064 {
1065 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001066 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001067 int m_idx;
1068 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1069 lp->ll_name_end - lp->ll_name, &m_idx);
1070 if (m != NULL)
1071 {
1072 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001073 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001074 lp->ll_oi = m_idx;
1075 lp->ll_valtype = m->ocm_type;
1076 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1077#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001078 ch_log(NULL, "LKVAR: ... class member %s.%s",
1079 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001080#endif
1081 return;
1082 }
1083 }
1084 }
1085
1086#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001087 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001088#endif
Ernie Rael64885642023-10-04 20:16:22 +02001089 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001090 lp->ll_is_root = TRUE;
1091}
1092
1093/*
Ernie Rael64885642023-10-04 20:16:22 +02001094 * Check if the class has permission to access the member.
1095 * Returns OK or FAIL.
1096 */
1097 static int
1098get_lval_check_access(
1099 class_T *cl_exec, // executing class, NULL if :def or script level
1100 class_T *cl, // class which contains the member
1101 ocmember_T *om, // member being accessed
1102 char_u *p, // char after member name
1103 int flags) // GLV flags to check if writing to lval
1104{
1105#ifdef LOG_LOCKVAR
1106 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1107 (void*)cl_exec, (void*)cl, *p);
1108#endif
1109 if (cl_exec == NULL || cl_exec != cl)
1110 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001111 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001112 switch (om->ocm_access)
1113 {
1114 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001115 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001116 break;
Ernie Rael64885642023-10-04 20:16:22 +02001117 case VIM_ACCESS_READ:
1118 // If [idx] or .key following, read only OK.
1119 if (*p == '[' || *p == '.')
1120 break;
1121 if ((flags & GLV_READ_ONLY) == 0)
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001122 {
1123 if (IS_ENUM(cl))
1124 {
1125 if (om->ocm_type->tt_type == VAR_OBJECT)
1126 semsg(_(e_enumvalue_str_cannot_be_modified),
1127 cl->class_name, om->ocm_name);
1128 else
1129 msg = e_variable_is_not_writable_str;
1130 }
1131 else
1132 msg = e_variable_is_not_writable_str;
1133 }
Ernie Rael64885642023-10-04 20:16:22 +02001134 break;
1135 case VIM_ACCESS_ALL:
1136 break;
1137 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001138 if (msg != NULL)
1139 {
1140 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1141 return FAIL;
1142 }
1143
Ernie Rael64885642023-10-04 20:16:22 +02001144 }
1145 return OK;
1146}
1147
1148/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001149 * Get lval information for a variable imported from script "imp_sid". On
1150 * success, updates "lp" with the variable name, type, script ID and typval.
1151 * The variable name starts at or after "p".
1152 * If "rettv" is not NULL it points to the value to be assigned. This used to
1153 * match the rhs and lhs types.
1154 * Returns a pointer to the character after the variable name if the imported
1155 * variable is valid and writable.
1156 * Returns NULL if the variable is not exported or typval is not found or the
1157 * rhs type doesn't match the lhs type or the variable is not writable.
1158 */
1159 static char_u *
1160get_lval_imported(
1161 lval_T *lp,
1162 typval_T *rettv,
1163 scid_T imp_sid,
1164 char_u *p,
1165 dictitem_T **dip,
1166 int fne_flags,
1167 int vim9script)
1168{
1169 ufunc_T *ufunc;
1170 type_T *type = NULL;
1171 int cc;
1172 int rc = FAIL;
1173
1174 p = skipwhite(p);
1175
1176 import_check_sourced_sid(&imp_sid);
1177 lp->ll_sid = imp_sid;
1178 lp->ll_name = p;
1179 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1180 lp->ll_name_end = p;
1181
1182 // check the item is exported
1183 cc = *p;
1184 *p = NUL;
1185 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1186 TRUE) == -1)
1187 goto failed;
1188
1189 if (vim9script && type != NULL)
1190 {
1191 where_T where = WHERE_INIT;
1192
1193 // In a vim9 script, do type check and make sure the variable is
1194 // writable.
1195 if (check_typval_type(type, rettv, where) == FAIL)
1196 goto failed;
1197 }
1198
1199 // Get the typval for the exported item
1200 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1201 if (ht == NULL)
1202 goto failed;
1203
1204 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1205 if (di == NULL)
1206 // variable is not found
1207 goto success;
1208
1209 *dip = di;
1210
1211 // Check whether the variable is writable.
1212 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1213 if (sv != NULL && sv->sv_const != 0)
1214 {
1215 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1216 goto failed;
1217 }
1218
1219 // check whether variable is locked
1220 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1221 goto failed;
1222
1223 lp->ll_tv = &di->di_tv;
1224
1225success:
1226 rc = OK;
1227
1228failed:
1229 *p = cc;
1230 return rc == OK ? p : NULL;
1231}
1232
1233/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 * Get an lval: variable, Dict item or List item that can be assigned a value
1235 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1236 * "name.key", "name.key[expr]" etc.
1237 * Indexing only works if "name" is an existing List or Dictionary.
1238 * "name" points to the start of the name.
1239 * If "rettv" is not NULL it points to the value to be assigned.
1240 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1241 * wrong; must end in space or cmd separator.
1242 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001243 * flags:
1244 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001245 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001246 * GLV_NO_AUTOLOAD: do not use script autoloading
1247 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001249 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001251 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001252 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253get_lval(
1254 char_u *name,
1255 typval_T *rettv,
1256 lval_T *lp,
1257 int unlet,
1258 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001259 int flags, // GLV_ values
1260 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 char_u *expr_start, *expr_end;
1264 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001265 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T var1;
1267 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001271 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001272 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001273 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001274 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001275 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276
Ernie Raelee865f32023-09-29 19:53:55 +02001277#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001278 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001279 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001280 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001281 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1282 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001283 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001284 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001285 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001286#endif
1287
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001288 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001289 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001291 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001293 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001295 lp->ll_name_end = find_name_end(name, NULL, NULL,
1296 FNE_INCL_BR | fne_flags);
1297 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 }
1299
Bram Moolenaara749a422022-02-12 19:52:25 +00001300 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001301 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001302 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1303 {
1304 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1305 return NULL;
1306 }
1307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001309 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 if (expr_start != NULL)
1312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001313 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 && *p != '[' && *p != '.')
1316 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001317 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 return NULL;
1319 }
1320
1321 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1322 if (lp->ll_exp_name == NULL)
1323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // Report an invalid expression in braces, unless the
1325 // expression evaluation has been cancelled due to an
1326 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 if (!aborting() && !quiet)
1328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001329 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001330 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 return NULL;
1332 }
1333 }
1334 lp->ll_name = lp->ll_exp_name;
1335 }
1336 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 lp->ll_name = name;
1339
Bram Moolenaar4525a572022-02-13 11:57:33 +00001340 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001342 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001343 // However, "g:[key]" is indexing a dictionary.
1344 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001345 {
1346 --p;
1347 lp->ll_name_end = p;
1348 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001349 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001350 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001351 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001352
Bram Moolenaar9510d222022-09-11 15:14:05 +01001353 if (is_scoped_variable(name))
1354 {
1355 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1356 return NULL;
1357 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001358 if (VIM_ISWHITE(*p))
1359 {
1360 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1361 return NULL;
1362 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001363 if (tp == p + 1 && !quiet)
1364 {
1365 semsg(_(e_white_space_required_after_str_str), ":", p);
1366 return NULL;
1367 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001368 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001369 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001370 semsg(_(e_using_type_not_in_script_context_str), p);
1371 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001372 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001373 if (vim9script && (flags & GLV_NO_DECL) &&
1374 !(flags & GLV_FOR_LOOP))
1375 {
1376 // Using a type and not in a "var" declaration.
1377 semsg(_(e_trailing_characters_str), p);
1378 return NULL;
1379 }
1380
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001381
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001382 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001383 lp->ll_type = parse_type(&tp,
1384 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1385 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001386 if (lp->ll_type == NULL && !quiet)
1387 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001388 lp->ll_name_end = tp;
1389 }
Ernie Raelee865f32023-09-29 19:53:55 +02001390 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 }
1392 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001393 if (lp->ll_name == NULL)
1394 return p;
1395
Bram Moolenaar62aec932022-01-29 21:45:34 +00001396 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001397 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001398 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001399 if (import != NULL)
1400 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001401 p++; // skip '.'
1402 p = get_lval_imported(lp, rettv, import->imp_sid, p, &v,
1403 fne_flags, vim9script);
1404 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001405 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001406 }
1407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
Ernie Rael0f058d12023-10-14 11:25:04 +02001409 // Without [idx] or .key we are done.
1410 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001411 {
1412 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001413 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001415 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416
Ernie Rael0f058d12023-10-14 11:25:04 +02001417 if (vim9script && lval_root != NULL)
1418 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001419 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001420 {
1421 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001422 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001423 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001424 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001425 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001426 {
1427 cc = *p;
1428 *p = NUL;
1429 // When we would write to the variable pass &ht and prevent autoload.
1430 writing = !(flags & GLV_READ_ONLY);
1431 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001432 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001433 if (v == NULL && !quiet)
1434 semsg(_(e_undefined_variable_str), lp->ll_name);
1435 *p = cc;
1436 if (v == NULL)
1437 return NULL;
1438 lp->ll_tv = &v->di_tv;
1439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001440
Bram Moolenaar4525a572022-02-13 11:57:33 +00001441 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001442 {
1443 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001444 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001445 return NULL;
1446 }
1447
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 /*
1449 * Loop until no more [idx] or .key is following.
1450 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001451 var1.v_type = VAR_UNKNOWN;
1452 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001453 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001454 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001455 vartype_T v_type = lp->ll_tv->v_type;
1456
1457 if (*p == '.' && v_type != VAR_DICT
1458 && v_type != VAR_OBJECT
1459 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001460 {
1461 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001462 semsg(_(e_dot_not_allowed_after_str_str),
1463 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001464 return NULL;
1465 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001466 if (v_type != VAR_LIST
1467 && v_type != VAR_DICT
1468 && v_type != VAR_BLOB
1469 && v_type != VAR_OBJECT
1470 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001473 semsg(_(e_index_not_allowed_after_str_str),
1474 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001476 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001477
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001478 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001479 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001480 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001481 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001482 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001483 r = rettv_blob_alloc(lp->ll_tv);
1484 if (r == FAIL)
1485 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001486
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001490 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001493#ifdef LOG_LOCKVAR
1494 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1495 vartype_name(v_type));
1496#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001497
Bram Moolenaar4525a572022-02-13 11:57:33 +00001498 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001499 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001500 && lp->ll_tv == &v->di_tv
1501 && ht != NULL && ht == get_script_local_ht())
1502 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001503 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001504
1505 // Vim9 script local variable: get the type
1506 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001507 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001508 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001509#ifdef LOG_LOCKVAR
1510 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1511 vartype_name(lp->ll_valtype->tt_type));
1512#endif
1513 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001514 }
1515
Bram Moolenaar8c711452005-01-14 21:53:12 +00001516 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001517 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001518 {
1519 key = p + 1;
1520 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1521 ;
1522 if (len == 0)
1523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001525 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001527 }
1528 p = key + len;
1529 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001530 else
1531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001532 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001533 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001534 if (*p == ':')
1535 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001536 else
1537 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001538 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001539 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001540 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001541 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001543 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001544 clear_tv(&var1);
1545 return NULL;
1546 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001547 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001548 }
1549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001550 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001551 if (*p == ':')
1552 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001553 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001555 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001556 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001558 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001559 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 if (rettv != NULL
1561 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001562 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001563 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001564 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001566 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001567 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001568 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001569 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001570 }
1571 p = skipwhite(p + 1);
1572 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001573 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001574 else
1575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001576 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001577 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001578 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001579 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001580 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001581 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001582 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001583 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001585 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001586 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001587 clear_tv(&var2);
1588 return NULL;
1589 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001590 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001591 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001593 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001594 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001595
Bram Moolenaar8c711452005-01-14 21:53:12 +00001596 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001599 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001600 clear_tv(&var1);
1601 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001602 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001603 }
1604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001605 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001606 ++p;
1607 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001608#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001609 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001610 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1611 empty1 ? ":" : (char*)tv_get_string(&var1));
1612 else
1613 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001614#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001615
Bram Moolenaard505d172022-12-18 21:42:55 +00001616 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001617 {
1618 if (len == -1)
1619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001620 // "[key]": get key from "var1"
1621 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001622 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001624 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001625 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001626 }
1627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001629 lp->ll_object = NULL;
1630 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001631
1632 // a NULL dict is equivalent with an empty dict
1633 if (lp->ll_tv->vval.v_dict == NULL)
1634 {
1635 lp->ll_tv->vval.v_dict = dict_alloc();
1636 if (lp->ll_tv->vval.v_dict == NULL)
1637 {
1638 clear_tv(&var1);
1639 return NULL;
1640 }
1641 ++lp->ll_tv->vval.v_dict->dv_refcount;
1642 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001644
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001645 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001647 // When assigning to a scope dictionary check that a function and
1648 // variable name is valid (only variable name unless it is l: or
1649 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001650 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001651 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001652 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001653
1654 if (len != -1)
1655 {
1656 prevval = key[len];
1657 key[len] = NUL;
1658 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001659 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001660 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001661 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001662 && (rettv->v_type == VAR_FUNC
1663 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001664 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001665 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001666 if (len != -1)
1667 key[len] = prevval;
1668 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001669 {
1670 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001671 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001672 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001673 }
1674
Bram Moolenaar10c65862020-10-08 21:16:42 +02001675 if (lp->ll_valtype != NULL)
1676 // use the type of the member
1677 lp->ll_valtype = lp->ll_valtype->tt_member;
1678
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001679 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001680 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001681 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001682 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001683 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001684 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001685 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001686 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001687 return NULL;
1688 }
1689
Bram Moolenaar31b81602019-02-10 22:14:27 +01001690 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001693 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001694 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 }
1698 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001699 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001700 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001701 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001704 p = NULL;
1705 break;
1706 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001707 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001708 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001709 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1710 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001711 {
1712 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001713 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001714 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001715
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001717 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001718 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001719 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001721 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1722
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 /*
1724 * Get the number and item for the only or first index of the List.
1725 */
1726 if (empty1)
1727 lp->ll_n1 = 0;
1728 else
1729 // is number or string
1730 lp->ll_n1 = (long)tv_get_number(&var1);
1731 clear_tv(&var1);
1732
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001733 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001735 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 return NULL;
1737 }
1738 if (lp->ll_range && !lp->ll_empty2)
1739 {
1740 lp->ll_n2 = (long)tv_get_number(&var2);
1741 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001742 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1743 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001744 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 }
1746 lp->ll_blob = lp->ll_tv->vval.v_blob;
1747 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001748 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001749 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001750 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001751 {
1752 /*
1753 * Get the number and item for the only or first index of the List.
1754 */
1755 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001756 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001757 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001758 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001760 clear_tv(&var1);
1761
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001763 lp->ll_object = NULL;
1764 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001765 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001766 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1767 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001768 if (lp->ll_li == NULL)
1769 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001770 clear_tv(&var2);
1771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001772 }
1773
Bram Moolenaar10c65862020-10-08 21:16:42 +02001774 if (lp->ll_valtype != NULL)
1775 // use the type of the member
1776 lp->ll_valtype = lp->ll_valtype->tt_member;
1777
Bram Moolenaar8c711452005-01-14 21:53:12 +00001778 /*
1779 * May need to find the item or absolute index for the second
1780 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001781 * When no index given: "lp->ll_empty2" is TRUE.
1782 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001783 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001784 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001786 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001787 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001788 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001789 if (check_range_index_two(lp->ll_list,
1790 &lp->ll_n1, lp->ll_li,
1791 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001793 }
1794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001795 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001796 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001797 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001798 {
Ernie Raelee865f32023-09-29 19:53:55 +02001799 lp->ll_dict = NULL;
1800 lp->ll_list = NULL;
1801
1802 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001803 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001804 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001805 if (lp->ll_tv->vval.v_object == NULL)
1806 {
1807 if (!quiet)
1808 emsg(_(e_using_null_object));
1809 return NULL;
1810 }
Ernie Raelee865f32023-09-29 19:53:55 +02001811 cl = lp->ll_tv->vval.v_object->obj_class;
1812 lp->ll_object = lp->ll_tv->vval.v_object;
1813 }
1814 else
1815 {
1816 cl = lp->ll_tv->vval.v_class;
1817 lp->ll_object = NULL;
1818 }
1819 lp->ll_class = cl;
1820
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001821 // TODO: what if class is NULL?
1822 if (cl != NULL)
1823 {
1824 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001825
1826 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001827 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001828 // First look for a function with this name.
1829 // round 1: class functions (skipped for an object)
1830 // round 2: object methods
1831 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001832 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001833 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001834 int m_idx;
1835 ufunc_T *fp;
1836
1837 fp = method_lookup(cl,
1838 round == 1 ? VAR_CLASS : VAR_OBJECT,
1839 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001840 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001841 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001842 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001843 lp->ll_ufunc = fp;
1844 lp->ll_valtype = fp->uf_func_type;
1845 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001846 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001847 }
1848 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001849
1850 if (lp->ll_valtype == NULL)
1851 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001852 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001853 ocmember_T *om
1854 = member_lookup(cl, v_type, key, p - key, &m_idx);
1855 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001856 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001857 {
Ernie Rael64885642023-10-04 20:16:22 +02001858 if (get_lval_check_access(cl_exec, cl, om,
1859 p, flags) == FAIL)
1860 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001861
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001862 // When lhs is used to modify the variable, check it is
1863 // not a read-only variable.
1864 if ((flags & GLV_READ_ONLY) == 0
1865 && (*p != '.' && *p != '[')
1866 && oc_var_check_ro(cl, om))
1867 return NULL;
1868
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001869 lp->ll_valtype = om->ocm_type;
1870
1871 if (v_type == VAR_OBJECT)
1872 lp->ll_tv = ((typval_T *)(
1873 lp->ll_tv->vval.v_object + 1)) + m_idx;
1874 else
1875 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001876 }
1877 }
1878
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001879 if (lp->ll_valtype == NULL)
1880 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001881 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001882 return NULL;
1883 }
1884 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 }
1887
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001888 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001890 return p;
1891}
1892
1893/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001897clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898{
1899 vim_free(lp->ll_exp_name);
1900 vim_free(lp->ll_newkey);
1901}
1902
1903/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001904 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001906 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1907 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001910set_var_lval(
1911 lval_T *lp,
1912 char_u *endp,
1913 typval_T *rettv,
1914 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001915 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1916 char_u *op,
1917 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918{
1919 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921
1922 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001924 cc = *endp;
1925 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001926 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001927 return;
1928
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001929 if (lp->ll_blob != NULL)
1930 {
1931 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001932
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001933 if (op != NULL && *op != '=')
1934 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001935 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001936 return;
1937 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001938 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001939 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940
1941 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1942 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001943 if (lp->ll_empty2)
1944 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1945
Bram Moolenaar68452172021-04-12 21:21:02 +02001946 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1947 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001948 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001949 }
1950 else
1951 {
1952 val = (int)tv_get_number_chk(rettv, &error);
1953 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001954 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001955 }
1956 }
1957 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001959 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001961 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1962 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001963 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001964 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001965 *endp = cc;
1966 return;
1967 }
1968
Bram Moolenaarff697e62019-02-12 22:28:33 +01001969 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001970 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001971 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001972 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001973 {
Ernie Raele75fde62023-12-21 17:18:54 +01001974 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001975 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001976 clear_tv(&tv);
1977 return;
1978 }
1979
Bram Moolenaar79518e22017-02-17 16:31:35 +01001980 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001981 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1982 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001983 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001984 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01001985 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001986 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001989 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001990 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001991 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1992 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001993 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001994 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001995 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001996 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001997 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001999 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002000 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002001 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002002 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 else if (lp->ll_range)
2004 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002005 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2006 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002007 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002008 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002009 return;
2010 }
2011
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002012 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2013 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 }
2015 else
2016 {
2017 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002018 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002019 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002020 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2021 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002022 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002023 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002024 return;
2025 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002026
2027 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002028 && check_typval_arg_type(lp->ll_valtype, rettv,
2029 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002030 return;
2031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (lp->ll_newkey != NULL)
2033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034 if (op != NULL && *op != '=')
2035 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002036 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037 return;
2038 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002039 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2040 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002041 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002044 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 if (di == NULL)
2046 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002047 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2048 {
2049 vim_free(di);
2050 return;
2051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002053 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002054 else if (op != NULL && *op != '=')
2055 {
2056 tv_op(lp->ll_tv, rettv, op);
2057 return;
2058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 /*
2063 * Assign the value to the variable or list item.
2064 */
2065 if (copy)
2066 copy_tv(rettv, lp->ll_tv);
2067 else
2068 {
2069 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002070 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
2073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074}
2075
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002077 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2078 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002079 * Returns OK or FAIL.
2080 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002082tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002084 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 char_u numbuf[NUMBUFLEN];
2086 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002087 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002088
Ernie Raele75fde62023-12-21 17:18:54 +01002089 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002090 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002091 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002092 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002093 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2094 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002095 {
2096 switch (tv1->v_type)
2097 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002098 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002099 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 case VAR_DICT:
2102 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002103 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002104 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002105 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002106 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002107 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002108 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002109 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002110 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002111 case VAR_CLASS:
2112 case VAR_TYPEALIAS:
2113 check_typval_is_value(tv1);
2114 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002116 case VAR_BLOB:
2117 if (*op != '+' || tv2->v_type != VAR_BLOB)
2118 break;
2119 // BLOB += BLOB
2120 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2121 {
2122 blob_T *b1 = tv1->vval.v_blob;
2123 blob_T *b2 = tv2->vval.v_blob;
2124 int i, len = blob_len(b2);
2125 for (i = 0; i < len; i++)
2126 ga_append(&b1->bv_ga, blob_get(b2, i));
2127 }
2128 return OK;
2129
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 case VAR_LIST:
2131 if (*op != '+' || tv2->v_type != VAR_LIST)
2132 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002133 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002134 if (tv2->vval.v_list != NULL)
2135 {
2136 if (tv1->vval.v_list == NULL)
2137 {
2138 tv1->vval.v_list = tv2->vval.v_list;
2139 ++tv1->vval.v_list->lv_refcount;
2140 }
2141 else
2142 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2143 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002144 return OK;
2145
2146 case VAR_NUMBER:
2147 case VAR_STRING:
2148 if (tv2->v_type == VAR_LIST)
2149 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002150 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002152 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002153 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002154 if (tv2->v_type == VAR_FLOAT)
2155 {
2156 float_T f = n;
2157
Bram Moolenaarff697e62019-02-12 22:28:33 +01002158 if (*op == '%')
2159 break;
2160 switch (*op)
2161 {
2162 case '+': f += tv2->vval.v_float; break;
2163 case '-': f -= tv2->vval.v_float; break;
2164 case '*': f *= tv2->vval.v_float; break;
2165 case '/': f /= tv2->vval.v_float; break;
2166 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002167 clear_tv(tv1);
2168 tv1->v_type = VAR_FLOAT;
2169 tv1->vval.v_float = f;
2170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002172 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002173 switch (*op)
2174 {
2175 case '+': n += tv_get_number(tv2); break;
2176 case '-': n -= tv_get_number(tv2); break;
2177 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002178 case '/': n = num_divide(n, tv_get_number(tv2),
2179 &failed); break;
2180 case '%': n = num_modulus(n, tv_get_number(tv2),
2181 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002182 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002183 clear_tv(tv1);
2184 tv1->v_type = VAR_NUMBER;
2185 tv1->vval.v_number = n;
2186 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 }
2188 else
2189 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002190 if (tv2->v_type == VAR_FLOAT)
2191 break;
2192
Bram Moolenaarff697e62019-02-12 22:28:33 +01002193 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002194 s = tv_get_string(tv1);
2195 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 clear_tv(tv1);
2197 tv1->v_type = VAR_STRING;
2198 tv1->vval.v_string = s;
2199 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002200 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002202 case VAR_FLOAT:
2203 {
2204 float_T f;
2205
Bram Moolenaarff697e62019-02-12 22:28:33 +01002206 if (*op == '%' || *op == '.'
2207 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002208 && tv2->v_type != VAR_NUMBER
2209 && tv2->v_type != VAR_STRING))
2210 break;
2211 if (tv2->v_type == VAR_FLOAT)
2212 f = tv2->vval.v_float;
2213 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002214 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002215 switch (*op)
2216 {
2217 case '+': tv1->vval.v_float += f; break;
2218 case '-': tv1->vval.v_float -= f; break;
2219 case '*': tv1->vval.v_float *= f; break;
2220 case '/': tv1->vval.v_float /= f; break;
2221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002222 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002223 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 }
2225 }
2226
Ernie Raele75fde62023-12-21 17:18:54 +01002227 if (check_typval_is_value(tv2) == OK)
2228 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 return FAIL;
2230}
2231
2232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002233 * Evaluate the expression used in a ":for var in expr" command.
2234 * "arg" points to "var".
2235 * Set "*errp" to TRUE for an error, FALSE otherwise;
2236 * Return a pointer that holds the info. Null when there is an error.
2237 */
2238 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239eval_for_line(
2240 char_u *arg,
2241 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002242 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002243 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002244{
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002246 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002247 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 typval_T tv;
2249 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002250 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002252 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002253
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002254 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002255 if (fi == NULL)
2256 return NULL;
2257
Bram Moolenaar404557e2021-07-05 21:41:48 +02002258 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2259 &fi->fi_semicolon, FALSE);
2260 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002261 return fi;
2262
Bram Moolenaar404557e2021-07-05 21:41:48 +02002263 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002264 if (expr[0] != 'i' || expr[1] != 'n'
2265 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002266 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002267 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2268 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2269 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002270 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 return fi;
2272 }
2273
2274 if (skip)
2275 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002276 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2277 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002278 {
2279 *errp = FALSE;
2280 if (!skip)
2281 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002283 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002284 l = tv.vval.v_list;
2285 if (l == NULL)
2286 {
2287 // a null list is like an empty list: do nothing
2288 clear_tv(&tv);
2289 }
2290 else
2291 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002293 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002295 // No need to increment the refcount, it's already set for
2296 // the list being used in "tv".
2297 fi->fi_list = l;
2298 list_add_watch(l, &fi->fi_lw);
2299 fi->fi_lw.lw_item = l->lv_first;
2300 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002301 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002302 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002303 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002304 fi->fi_bi = 0;
2305 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002306 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002307 typval_T btv;
2308
2309 // Make a copy, so that the iteration still works when the
2310 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002312 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002313 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002314 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002315 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002316 else if (tv.v_type == VAR_STRING)
2317 {
2318 fi->fi_byte_idx = 0;
2319 fi->fi_string = tv.vval.v_string;
2320 tv.vval.v_string = NULL;
2321 if (fi->fi_string == NULL)
2322 fi->fi_string = vim_strsave((char_u *)"");
2323 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002324 else
2325 {
Sean Dewar80d73952021-08-04 19:25:54 +02002326 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002327 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 }
2329 }
2330 }
2331 if (skip)
2332 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002333 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002334
2335 return fi;
2336}
2337
2338/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002339 * Used when looping over a :for line, skip the "in expr" part.
2340 */
2341 void
2342skip_for_lines(void *fi_void, evalarg_T *evalarg)
2343{
2344 forinfo_T *fi = (forinfo_T *)fi_void;
2345 int i;
2346
2347 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002348 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002349}
2350
2351/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002352 * Use the first item in a ":for" list. Advance to the next.
2353 * Assign the values to the variable (list). "arg" points to the first one.
2354 * Return TRUE when a valid item was found, FALSE when at end of list or
2355 * something wrong.
2356 */
2357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002359{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002360 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002361 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002362 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002363 ? (ASSIGN_FINAL
2364 // first round: error if variable exists
2365 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002366 | ASSIGN_NO_MEMBER_TYPE
2367 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002368 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002369 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002370 int skip_assign = in_vim9script() && arg[0] == '_'
2371 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002373 if (fi->fi_blob != NULL)
2374 {
2375 typval_T tv;
2376
2377 if (fi->fi_bi >= blob_len(fi->fi_blob))
2378 return FALSE;
2379 tv.v_type = VAR_NUMBER;
2380 tv.v_lock = VAR_FIXED;
2381 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2382 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002383 if (skip_assign)
2384 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002385 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002386 fi->fi_varcount, flag, NULL) == OK;
2387 }
2388
2389 if (fi->fi_string != NULL)
2390 {
2391 typval_T tv;
2392 int len;
2393
2394 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2395 if (len == 0)
2396 return FALSE;
2397 tv.v_type = VAR_STRING;
2398 tv.v_lock = VAR_FIXED;
2399 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2400 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002401 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002402 if (skip_assign)
2403 result = TRUE;
2404 else
2405 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002406 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002407 vim_free(tv.vval.v_string);
2408 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002409 }
2410
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002411 item = fi->fi_lw.lw_item;
2412 if (item == NULL)
2413 result = FALSE;
2414 else
2415 {
2416 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002417 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002418 if (skip_assign)
2419 result = TRUE;
2420 else
2421 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002422 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002423 }
2424 return result;
2425}
2426
2427/*
2428 * Free the structure used to store info used by ":for".
2429 */
2430 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002431free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432{
Bram Moolenaar33570922005-01-25 22:26:29 +00002433 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002435 if (fi == NULL)
2436 return;
2437 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002438 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002440 list_unref(fi->fi_list);
2441 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002442 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002443 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002444 else
2445 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002446 vim_free(fi);
2447}
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002450set_context_for_expression(
2451 expand_T *xp,
2452 char_u *arg,
2453 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002455 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002457 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002459 if (cmdidx == CMD_let || cmdidx == CMD_var
2460 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 {
2462 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002463 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002466 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 {
2468 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002469 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002470 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002471 break;
2472 }
2473 return;
2474 }
2475 }
2476 else
2477 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2478 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 while ((xp->xp_pattern = vim_strpbrk(arg,
2480 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2481 {
2482 c = *xp->xp_pattern;
2483 if (c == '&')
2484 {
2485 c = xp->xp_pattern[1];
2486 if (c == '&')
2487 {
2488 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002489 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002494 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2495 xp->xp_pattern += 2;
2496
2497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 }
2499 else if (c == '$')
2500 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002501 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 xp->xp_context = EXPAND_ENV_VARS;
2503 }
2504 else if (c == '=')
2505 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002506 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 xp->xp_context = EXPAND_EXPRESSION;
2508 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002509 else if (c == '#'
2510 && xp->xp_context == EXPAND_EXPRESSION)
2511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002512 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002513 break;
2514 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002515 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 && xp->xp_context == EXPAND_FUNCTIONS
2517 && vim_strchr(xp->xp_pattern, '(') == NULL)
2518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002519 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 break;
2521 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002524 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2527 if (c == '\\' && xp->xp_pattern[1] != NUL)
2528 ++xp->xp_pattern;
2529 xp->xp_context = EXPAND_NOTHING;
2530 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002531 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002533 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2535 /* skip */ ;
2536 xp->xp_context = EXPAND_NOTHING;
2537 }
2538 else if (c == '|')
2539 {
2540 if (xp->xp_pattern[1] == '|')
2541 {
2542 ++xp->xp_pattern;
2543 xp->xp_context = EXPAND_EXPRESSION;
2544 }
2545 else
2546 xp->xp_context = EXPAND_COMMANDS;
2547 }
2548 else
2549 xp->xp_context = EXPAND_EXPRESSION;
2550 }
2551 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002552 // Doesn't look like something valid, expand as an expression
2553 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 arg = xp->xp_pattern;
2556 if (*arg != NUL)
2557 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2558 /* skip */ ;
2559 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002560
2561 // ":exe one two" completes "two"
2562 if ((cmdidx == CMD_execute
2563 || cmdidx == CMD_echo
2564 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002565 || cmdidx == CMD_echomsg
2566 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002567 && xp->xp_context == EXPAND_EXPRESSION)
2568 {
2569 for (;;)
2570 {
2571 char_u *n = skiptowhite(arg);
2572
2573 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2574 break;
2575 arg = skipwhite(n);
2576 }
2577 }
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 xp->xp_pattern = arg;
2580}
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002583 * Return TRUE if "pat" matches "text".
2584 * Does not use 'cpo' and always uses 'magic'.
2585 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002586 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002587pattern_match(char_u *pat, char_u *text, int ic)
2588{
2589 int matches = FALSE;
2590 char_u *save_cpo;
2591 regmatch_T regmatch;
2592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002593 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002594 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002595 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002596 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2597 if (regmatch.regprog != NULL)
2598 {
2599 regmatch.rm_ic = ic;
2600 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2601 vim_regfree(regmatch.regprog);
2602 }
2603 p_cpo = save_cpo;
2604 return matches;
2605}
2606
2607/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002608 * Handle a name followed by "(". Both for just "name(arg)" and for
2609 * "expr->name(arg)".
2610 * Returns OK or FAIL.
2611 */
2612 static int
2613eval_func(
2614 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002615 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002616 char_u *name,
2617 int name_len,
2618 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002619 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002620 typval_T *basetv) // "expr" for "expr->name(arg)"
2621{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002622 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002623 char_u *s = name;
2624 int len = name_len;
2625 partial_T *partial;
2626 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002627 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002628 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002629
2630 if (!evaluate)
2631 check_vars(s, len);
2632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002633 // If "s" is the name of a variable of type VAR_FUNC
2634 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002635 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002636 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002638 // Need to make a copy, in case evaluating the arguments makes
2639 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002640 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002641 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002642 ret = FAIL;
2643 else
2644 {
2645 funcexe_T funcexe;
2646
2647 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002648 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002649 funcexe.fe_firstline = curwin->w_cursor.lnum;
2650 funcexe.fe_lastline = curwin->w_cursor.lnum;
2651 funcexe.fe_evaluate = evaluate;
2652 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002653 if (partial != NULL)
2654 {
2655 funcexe.fe_object = partial->pt_obj;
2656 if (funcexe.fe_object != NULL)
2657 ++funcexe.fe_object->obj_refcount;
2658 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002659 funcexe.fe_basetv = basetv;
2660 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002661 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002662 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002663 }
2664 vim_free(s);
2665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002666 // If evaluate is FALSE rettv->v_type was not set in
2667 // get_func_tv, but it's needed in handle_subscript() to parse
2668 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002669 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2670 {
2671 rettv->vval.v_string = NULL;
2672 rettv->v_type = VAR_FUNC;
2673 }
2674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002675 // Stop the expression evaluation when immediately
2676 // aborting on error, or when an interrupt occurred or
2677 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002678 if (evaluate && aborting())
2679 {
2680 if (ret == OK)
2681 clear_tv(rettv);
2682 ret = FAIL;
2683 }
2684 return ret;
2685}
2686
2687/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002688 * After a NL, skip over empty lines and comment-only lines.
2689 */
2690 static char_u *
2691newline_skip_comments(char_u *arg)
2692{
2693 char_u *p = arg + 1;
2694
2695 for (;;)
2696 {
2697 p = skipwhite(p);
2698
2699 if (*p == NUL)
2700 break;
2701 if (vim9_comment_start(p))
2702 {
2703 char_u *nl = vim_strchr(p, NL);
2704
2705 if (nl == NULL)
2706 break;
2707 p = nl;
2708 }
2709 if (*p != NL)
2710 break;
2711 ++p; // skip another NL
2712 }
2713 return p;
2714}
2715
2716/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002717 * Get the next line source line without advancing. But do skip over comment
2718 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002719 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002720 */
2721 static char_u *
2722getline_peek_skip_comments(evalarg_T *evalarg)
2723{
2724 for (;;)
2725 {
2726 char_u *next = getline_peek(evalarg->eval_getline,
2727 evalarg->eval_cookie);
2728 char_u *p;
2729
2730 if (next == NULL)
2731 break;
2732 p = skipwhite(next);
2733 if (*p != NUL && !vim9_comment_start(p))
2734 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002735 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002736 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002737 }
2738 return NULL;
2739}
2740
2741/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002742 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2743 * comment) and there is a next line, return the next line (skipping blanks)
2744 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002745 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2746 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002747 * "arg" must point somewhere inside a line, not at the start.
2748 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002749 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2751{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002752 char_u *p = skipwhite(arg);
2753
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002755 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002756 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002757 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2758 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002759 && (*p == NUL || *p == NL
2760 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002761 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002762 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002763
Bram Moolenaare442d592022-05-05 12:20:28 +01002764 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002765 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002766 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002767 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002768 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002769 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002770
Bram Moolenaar9d489562020-07-30 20:08:50 +02002771 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002772 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002773 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002774 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002775 }
2776 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002777 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002778}
2779
2780/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002781 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002782 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002783 *
2784 * If "arg" is not NULL, then the caller should assign the return value to
2785 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002786 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002787 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002788eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002789{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002790 garray_T *gap = &evalarg->eval_ga;
2791 char_u *line;
2792
Bram Moolenaar39be4982022-05-06 21:51:50 +01002793 if (arg != NULL)
2794 {
2795 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002796 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002797 // Truncate before a trailing comment, so that concatenating the lines
2798 // won't turn the rest into a comment.
2799 if (*skipwhite(arg) == '#')
2800 *arg = NUL;
2801 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002802
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002803 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002804 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2805 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002806 else
2807 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002808 if (line == NULL)
2809 return NULL;
2810
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002811 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002812 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2813 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002814 char_u *p = skipwhite(line);
2815
2816 // Going to concatenate the lines after parsing. For an empty or
2817 // comment line use an empty string.
2818 if (*p == NUL || vim9_comment_start(p))
2819 {
2820 vim_free(line);
2821 line = vim_strsave((char_u *)"");
2822 }
2823
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002824 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2825 ++gap->ga_len;
2826 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002827 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002828 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002829 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002830 evalarg->eval_tofree = line;
2831 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002832
2833 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002834 // line. The caller assigns the return value to "arg".
2835 // If "arg" is NULL, then the return value is discarded. In that case,
2836 // "arg" still points to the previous line. So don't reset
2837 // "eval_using_cmdline".
2838 if (arg != NULL)
2839 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002840 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002841}
2842
Bram Moolenaare6b53242020-07-01 17:28:33 +02002843/*
2844 * Call eval_next_non_blank() and get the next line if needed.
2845 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002846 char_u *
2847skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2848{
2849 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002850 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002851
Bram Moolenaar962d7212020-07-04 14:15:00 +02002852 if (evalarg == NULL)
2853 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002854 eval_next_non_blank(p, evalarg, &getnext);
2855 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002856 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002857 return p;
2858}
2859
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002860/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002861 * The "eval" functions have an "evalarg" argument: When NULL or
2862 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2863 * parsed but not executed. The functions may return OK, but the rettv will be
2864 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 */
2866
2867/*
2868 * Handle zero level expression.
2869 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002870 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002871 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 * Return OK or FAIL.
2874 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002876eval0(
2877 char_u *arg,
2878 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002879 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002882 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2883}
2884
2885/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002886 * If "arg" is a simple function call without arguments then call it and return
2887 * the result. Otherwise return NOTDONE.
2888 */
2889 int
2890may_call_simple_func(
2891 char_u *arg,
2892 typval_T *rettv)
2893{
2894 char_u *parens = (char_u *)strstr((char *)arg, "()");
2895 int r = NOTDONE;
2896
2897 // If the expression is "FuncName()" then we can skip a lot of overhead.
2898 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2899 {
2900 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2901
2902 if (to_name_end(p, TRUE) == parens)
2903 r = call_simple_func(arg, (int)(parens - arg), rettv);
2904 }
2905 return r;
2906}
2907
2908/*
2909 * Handle zero level expression with optimization for a simple function call.
2910 * Same arguments and return value as eval0().
2911 */
2912 int
2913eval0_simple_funccal(
2914 char_u *arg,
2915 typval_T *rettv,
2916 exarg_T *eap,
2917 evalarg_T *evalarg)
2918{
2919 int r = may_call_simple_func(arg, rettv);
2920
2921 if (r == NOTDONE)
2922 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2923 return r;
2924}
2925
2926/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002927 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2928 * expression and don't check what comes after the expression.
2929 */
2930 int
2931eval0_retarg(
2932 char_u *arg,
2933 typval_T *rettv,
2934 exarg_T *eap,
2935 evalarg_T *evalarg,
2936 char_u **retarg)
2937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 int ret;
2939 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002940 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002941 int did_emsg_before = did_emsg;
2942 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002943 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002944 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002948
Bram Moolenaar79481362022-06-27 20:15:10 +01002949 if (ret != FAIL)
2950 {
2951 expr_end = p;
2952 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002953
Bram Moolenaar79481362022-06-27 20:15:10 +01002954 // In Vim9 script a command block is not split at NL characters for
2955 // commands using an expression argument. Skip over a '#' comment to
2956 // check for a following NL. Require white space before the '#'.
2957 if (in_vim9script() && p > expr_end && retarg == NULL)
2958 while (*p == '#')
2959 {
2960 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002961
Bram Moolenaar79481362022-06-27 20:15:10 +01002962 if (nl == NULL)
2963 break;
2964 p = skipwhite(nl + 1);
2965 if (eap != NULL && *p != NUL)
2966 eap->nextcmd = p;
2967 check_for_end = FALSE;
2968 }
2969
2970 if (check_for_end)
2971 end_error = !ends_excmd2(arg, p);
2972 }
2973
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002974 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /*
2979 * Report the invalid expression unless the expression evaluation has
2980 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002981 * exception, or we already gave a more specific error.
2982 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002984 if (!aborting()
2985 && did_emsg == did_emsg_before
2986 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002987 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002988 {
2989 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002990 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002991 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002992 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002993 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002994
zeertzjqf2588b62023-05-05 17:22:35 +01002995 if (eap != NULL && p != NULL)
2996 {
2997 // Some of the expression may not have been consumed.
2998 // Only execute a next command if it cannot be a "||" operator.
2999 // The next command may be "catch".
3000 char_u *nextcmd = check_nextcmd(p);
3001 if (nextcmd != NULL && *nextcmd != '|')
3002 eap->nextcmd = nextcmd;
3003 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003004 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003006
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003007 if (retarg != NULL)
3008 *retarg = p;
3009 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003010 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 return ret;
3013}
3014
3015/*
3016 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003017 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003018 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 *
3020 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003021 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003023 * Note: "rettv.v_lock" is not set.
3024 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 * Return OK or FAIL.
3026 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003027 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003030 char_u *p;
3031 int getnext;
3032
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003033 CLEAR_POINTER(rettv);
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 /*
3036 * Get the first variable.
3037 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003038 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 return FAIL;
3040
Bram Moolenaar793648f2020-06-26 21:28:25 +02003041 p = eval_next_non_blank(*arg, evalarg, &getnext);
3042 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003044 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003045 int result;
3046 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003047 evalarg_T *evalarg_used = evalarg;
3048 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003050 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003051 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003052
3053 if (evalarg == NULL)
3054 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003055 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003056 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003057 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003058 orig_flags = evalarg_used->eval_flags;
3059 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003060
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003061 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003062 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003063 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003064 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003065 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003066 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003067 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003068 clear_tv(rettv);
3069 return FAIL;
3070 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003071 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003072 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003077 int error = FALSE;
3078
Bram Moolenaar13106602020-10-04 16:06:05 +02003079 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003080 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003081 else if (vim9script)
3082 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003083 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003085 if (error || !op_falsy || !result)
3086 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003087 if (error)
3088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090
3091 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003092 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003094 if (op_falsy)
3095 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003096 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003097 {
mityu4ac198c2021-05-28 17:52:40 +02003098 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003099 clear_tv(rettv);
3100 return FAIL;
3101 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003102 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003103 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003104 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003105 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003106 {
3107 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003109 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003110 if (!op_falsy || !result)
3111 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003113 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003115 /*
3116 * Check for the ":".
3117 */
3118 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3119 if (*p != ':')
3120 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003121 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003122 if (evaluate && result)
3123 clear_tv(rettv);
3124 evalarg_used->eval_flags = orig_flags;
3125 return FAIL;
3126 }
3127 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003128 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003129 else
3130 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003131 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003132 {
3133 error_white_both(p, 1);
3134 clear_tv(rettv);
3135 evalarg_used->eval_flags = orig_flags;
3136 return FAIL;
3137 }
3138 *arg = p;
3139 }
3140
3141 /*
3142 * Get the third variable. Recursive!
3143 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003144 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003145 {
mityu4ac198c2021-05-28 17:52:40 +02003146 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003147 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003148 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003149 return FAIL;
3150 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003151 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3152 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003153 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003154 if (eval1(arg, &var2, evalarg_used) == FAIL)
3155 {
3156 if (evaluate && result)
3157 clear_tv(rettv);
3158 evalarg_used->eval_flags = orig_flags;
3159 return FAIL;
3160 }
3161 if (evaluate && !result)
3162 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003164
3165 if (evalarg == NULL)
3166 clear_evalarg(&local_evalarg, NULL);
3167 else
3168 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170
3171 return OK;
3172}
3173
3174/*
3175 * Handle first level expression:
3176 * expr2 || expr2 || expr2 logical OR
3177 *
3178 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003179 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 *
3181 * Return OK or FAIL.
3182 */
3183 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003184eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003186 char_u *p;
3187 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003190 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003192 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 return FAIL;
3194
3195 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003196 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003198 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003199 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003201 evalarg_T *evalarg_used = evalarg;
3202 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003203 int evaluate;
3204 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003205 long result = FALSE;
3206 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003207 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003208 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003209
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003210 if (evalarg == NULL)
3211 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003212 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003213 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003214 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003215 orig_flags = evalarg_used->eval_flags;
3216 evaluate = orig_flags & EVAL_EVALUATE;
3217 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003218 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003219 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003220 result = tv_get_bool_chk(rettv, &error);
3221 else if (tv_get_number_chk(rettv, &error) != 0)
3222 result = TRUE;
3223 clear_tv(rettv);
3224 if (error)
3225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227
3228 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003229 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003231 while (p[0] == '|' && p[1] == '|')
3232 {
3233 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003234 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003235 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003236 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003237 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003238 {
3239 error_white_both(p, 2);
3240 clear_tv(rettv);
3241 return FAIL;
3242 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003243 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003244 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003245
3246 /*
3247 * Get the second variable.
3248 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003249 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003250 {
mityu4ac198c2021-05-28 17:52:40 +02003251 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003252 clear_tv(rettv);
3253 return FAIL;
3254 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003255 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3256 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003257 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003258 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003259 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003260
3261 /*
3262 * Compute the result.
3263 */
3264 if (evaluate && !result)
3265 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003266 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003267 result = tv_get_bool_chk(&var2, &error);
3268 else if (tv_get_number_chk(&var2, &error) != 0)
3269 result = TRUE;
3270 clear_tv(&var2);
3271 if (error)
3272 return FAIL;
3273 }
3274 if (evaluate)
3275 {
3276 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003277 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003278 rettv->v_type = VAR_BOOL;
3279 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003280 }
3281 else
3282 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003283 rettv->v_type = VAR_NUMBER;
3284 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003285 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003286 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003287
3288 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003290
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003291 if (evalarg == NULL)
3292 clear_evalarg(&local_evalarg, NULL);
3293 else
3294 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296
3297 return OK;
3298}
3299
3300/*
3301 * Handle second level expression:
3302 * expr3 && expr3 && expr3 logical AND
3303 *
3304 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003305 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 *
3307 * Return OK or FAIL.
3308 */
3309 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003310eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003312 char_u *p;
3313 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
3315 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003316 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003318 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return FAIL;
3320
3321 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003322 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003324 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003325 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003327 evalarg_T *evalarg_used = evalarg;
3328 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003329 int orig_flags;
3330 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003331 long result = TRUE;
3332 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003333 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003334 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003335
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003336 if (evalarg == NULL)
3337 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003338 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003339 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003340 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003341 orig_flags = evalarg_used->eval_flags;
3342 evaluate = orig_flags & EVAL_EVALUATE;
3343 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003344 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003345 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003346 result = tv_get_bool_chk(rettv, &error);
3347 else if (tv_get_number_chk(rettv, &error) == 0)
3348 result = FALSE;
3349 clear_tv(rettv);
3350 if (error)
3351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353
3354 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003355 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003357 while (p[0] == '&' && p[1] == '&')
3358 {
3359 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003360 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003361 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003362 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003363 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003364 {
3365 error_white_both(p, 2);
3366 clear_tv(rettv);
3367 return FAIL;
3368 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003369 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003370 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003371
3372 /*
3373 * Get the second variable.
3374 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003375 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003376 {
mityu4ac198c2021-05-28 17:52:40 +02003377 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003378 clear_tv(rettv);
3379 return FAIL;
3380 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003381 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3382 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003383 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003384 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003385 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003386 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003387
3388 /*
3389 * Compute the result.
3390 */
3391 if (evaluate && result)
3392 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003393 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003394 result = tv_get_bool_chk(&var2, &error);
3395 else if (tv_get_number_chk(&var2, &error) == 0)
3396 result = FALSE;
3397 clear_tv(&var2);
3398 if (error)
3399 return FAIL;
3400 }
3401 if (evaluate)
3402 {
3403 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003404 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003405 rettv->v_type = VAR_BOOL;
3406 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003407 }
3408 else
3409 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003410 rettv->v_type = VAR_NUMBER;
3411 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003412 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003413 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003414
3415 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003417
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003418 if (evalarg == NULL)
3419 clear_evalarg(&local_evalarg, NULL);
3420 else
3421 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423
3424 return OK;
3425}
3426
3427/*
3428 * Handle third level expression:
3429 * var1 == var2
3430 * var1 =~ var2
3431 * var1 != var2
3432 * var1 !~ var2
3433 * var1 > var2
3434 * var1 >= var2
3435 * var1 < var2
3436 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003437 * var1 is var2
3438 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 *
3440 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003441 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 *
3443 * Return OK or FAIL.
3444 */
3445 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003446eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003449 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003450 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003452 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003455 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003457 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 return FAIL;
3459
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003460 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003461
Bram Moolenaar696ba232020-07-29 21:20:41 +02003462 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003465 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003467 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003469 typval_T var2;
3470 int ic;
3471 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003472 int evaluate = evalarg == NULL
3473 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003474 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003475
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003476 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003477 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003478 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003479 p = *arg;
3480 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003481 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3482 {
mityu4ac198c2021-05-28 17:52:40 +02003483 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003484 clear_tv(rettv);
3485 return FAIL;
3486 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003487
Bram Moolenaar696ba232020-07-29 21:20:41 +02003488 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3489 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003490 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003491 clear_tv(rettv);
3492 return FAIL;
3493 }
3494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003495 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (p[len] == '?')
3497 {
3498 ic = TRUE;
3499 ++len;
3500 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003501 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 else if (p[len] == '#')
3503 {
3504 ic = FALSE;
3505 ++len;
3506 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003507 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003509 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 /*
3512 * Get the second variable.
3513 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003514 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3515 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003516 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003517 clear_tv(rettv);
3518 return FAIL;
3519 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003520 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003521 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return FAIL;
3525 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003526 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003527 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003528 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003529
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003530 // use the line of the comparison for messages
3531 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003532 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003533 {
3534 ret = FAIL;
3535 clear_tv(rettv);
3536 }
3537 else
3538 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003539 clear_tv(&var2);
3540 return ret;
3541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544 return OK;
3545}
3546
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003547/*
3548 * Make a copy of blob "tv1" and append blob "tv2".
3549 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 void
3551eval_addblob(typval_T *tv1, typval_T *tv2)
3552{
3553 blob_T *b1 = tv1->vval.v_blob;
3554 blob_T *b2 = tv2->vval.v_blob;
3555 blob_T *b = blob_alloc();
3556 int i;
3557
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003558 if (b == NULL)
3559 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003561 for (i = 0; i < blob_len(b1); i++)
3562 ga_append(&b->bv_ga, blob_get(b1, i));
3563 for (i = 0; i < blob_len(b2); i++)
3564 ga_append(&b->bv_ga, blob_get(b2, i));
3565
3566 clear_tv(tv1);
3567 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568}
3569
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003570/*
3571 * Make a copy of list "tv1" and append list "tv2".
3572 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 int
3574eval_addlist(typval_T *tv1, typval_T *tv2)
3575{
3576 typval_T var3;
3577
3578 // concatenate Lists
3579 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3580 {
3581 clear_tv(tv1);
3582 clear_tv(tv2);
3583 return FAIL;
3584 }
3585 clear_tv(tv1);
3586 *tv1 = var3;
3587 return OK;
3588}
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003591 * Handle the bitwise left/right shift operator expression:
3592 * var1 << var2
3593 * var1 >> var2
3594 *
3595 * "arg" must point to the first non-white of the expression.
3596 * "arg" is advanced to just after the recognized expression.
3597 *
3598 * Return OK or FAIL.
3599 */
3600 static int
3601eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3602{
3603 /*
3604 * Get the first expression.
3605 */
3606 if (eval6(arg, rettv, evalarg) == FAIL)
3607 return FAIL;
3608
3609 /*
3610 * Repeat computing, until no '<<' or '>>' is following.
3611 */
3612 for (;;)
3613 {
3614 char_u *p;
3615 int getnext;
3616 exprtype_T type;
3617 int evaluate;
3618 typval_T var2;
3619 int vim9script;
3620
3621 p = eval_next_non_blank(*arg, evalarg, &getnext);
3622 if (p[0] == '<' && p[1] == '<')
3623 type = EXPR_LSHIFT;
3624 else if (p[0] == '>' && p[1] == '>')
3625 type = EXPR_RSHIFT;
3626 else
3627 return OK;
3628
3629 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003630 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3631 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003632 {
3633 // left operand should be a number
3634 emsg(_(e_bitshift_ops_must_be_number));
3635 clear_tv(rettv);
3636 return FAIL;
3637 }
3638
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003639 vim9script = in_vim9script();
3640 if (getnext)
3641 {
3642 *arg = eval_next_line(*arg, evalarg);
3643 p = *arg;
3644 }
3645 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3646 {
3647 error_white_both(*arg, 2);
3648 clear_tv(rettv);
3649 return FAIL;
3650 }
3651
3652 /*
3653 * Get the second variable.
3654 */
3655 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3656 {
3657 error_white_both(p, 2);
3658 clear_tv(rettv);
3659 return FAIL;
3660 }
3661 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3662 if (eval6(arg, &var2, evalarg) == FAIL)
3663 {
3664 clear_tv(rettv);
3665 return FAIL;
3666 }
3667
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003668 if (evaluate)
3669 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003670 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3671 {
3672 // right operand should be a positive number
3673 if (var2.v_type != VAR_NUMBER)
3674 emsg(_(e_bitshift_ops_must_be_number));
3675 else
3676 emsg(_(e_bitshift_ops_must_be_positive));
3677 clear_tv(rettv);
3678 clear_tv(&var2);
3679 return FAIL;
3680 }
3681
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003682 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3683 // shifting more bits than we have always results in zero
3684 rettv->vval.v_number = 0;
3685 else if (type == EXPR_LSHIFT)
3686 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003687 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003688 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003689 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003690 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003691 }
3692
3693 clear_tv(&var2);
3694 }
3695
3696 return OK;
3697}
3698
3699/*
3700 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003701 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003703 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003704 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 *
3706 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003707 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 *
3709 * Return OK or FAIL.
3710 */
3711 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003712eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003715 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003717 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 return FAIL;
3719
3720 /*
3721 * Repeat computing, until no '+', '-' or '.' is following.
3722 */
3723 for (;;)
3724 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003725 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003726 int getnext;
3727 char_u *p;
3728 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003729 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003730 int concat;
3731 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003732 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003733
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003734 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003735 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003736 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003737 p = eval_next_non_blank(*arg, evalarg, &getnext);
3738 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003739 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003740 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3741 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003743 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3744 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003745
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003746 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003747 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003748 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003749 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003750 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003751 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003752 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003753 {
mityu4ac198c2021-05-28 17:52:40 +02003754 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003755 clear_tv(rettv);
3756 return FAIL;
3757 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003758 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003759 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003760 if ((op != '+' || (rettv->v_type != VAR_LIST
3761 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003762 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003763 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003765 int error = FALSE;
3766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // For "list + ...", an illegal use of the first operand as
3768 // a number cannot be determined before evaluating the 2nd
3769 // operand: if this is also a list, all is ok.
3770 // For "something . ...", "something - ..." or "non-list + ...",
3771 // we know that the first operand needs to be a string or number
3772 // without evaluating the 2nd operand. So check before to avoid
3773 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003774 if (op != '.')
3775 tv_get_number_chk(rettv, &error);
3776 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 {
3778 clear_tv(rettv);
3779 return FAIL;
3780 }
3781 }
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 /*
3784 * Get the second variable.
3785 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003786 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003787 {
mityu89dcb4d2021-05-28 13:50:17 +02003788 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003789 clear_tv(rettv);
3790 return FAIL;
3791 }
3792 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003793 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797 }
3798
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003799 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
3801 /*
3802 * Compute the result.
3803 */
3804 if (op == '.')
3805 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003806 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3807 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003808 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003809
Bram Moolenaar2e086612020-08-25 22:37:48 +02003810 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003811 || var2.v_type == VAR_CHANNEL
3812 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003813 semsg(_(e_using_invalid_value_as_string_str),
3814 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003815 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003816 {
3817 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3818 var2.vval.v_float);
3819 s2 = buf2;
3820 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003821 else
3822 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 {
3825 clear_tv(rettv);
3826 clear_tv(&var2);
3827 return FAIL;
3828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003829 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 clear_tv(rettv);
3831 rettv->v_type = VAR_STRING;
3832 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003834 else if (op == '+' && rettv->v_type == VAR_BLOB
3835 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003837 else if (op == '+' && rettv->v_type == VAR_LIST
3838 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003839 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003841 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 else
3844 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003845 int error = FALSE;
3846 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003847 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003848
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003849 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003851 f1 = rettv->vval.v_float;
3852 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003854 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003856 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003857 if (error)
3858 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003859 // This can only happen for "list + non-list" or
3860 // "blob + non-blob". For "non-list + ..." or
3861 // "something - ...", we returned before evaluating the
3862 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003863 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003864 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 return FAIL;
3866 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 if (var2.v_type == VAR_FLOAT)
3868 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003869 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003870 if (var2.v_type == VAR_FLOAT)
3871 {
3872 f2 = var2.vval.v_float;
3873 n2 = 0;
3874 }
3875 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003877 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003878 if (error)
3879 {
3880 clear_tv(rettv);
3881 clear_tv(&var2);
3882 return FAIL;
3883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003884 if (rettv->v_type == VAR_FLOAT)
3885 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003890 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3891 {
3892 if (op == '+')
3893 f1 = f1 + f2;
3894 else
3895 f1 = f1 - f2;
3896 rettv->v_type = VAR_FLOAT;
3897 rettv->vval.v_float = f1;
3898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003900 {
3901 if (op == '+')
3902 n1 = n1 + n2;
3903 else
3904 n1 = n1 - n2;
3905 rettv->v_type = VAR_NUMBER;
3906 rettv->vval.v_number = n1;
3907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 }
3912 return OK;
3913}
3914
3915/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003916 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 * * number multiplication
3918 * / number division
3919 * % number modulo
3920 *
3921 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003922 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 *
3924 * Return OK or FAIL.
3925 */
3926 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003927eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003928 char_u **arg,
3929 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003930 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003931 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003933 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003936 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003938 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 return FAIL;
3940
3941 /*
3942 * Repeat computing, until no '*', '/' or '%' is following.
3943 */
3944 for (;;)
3945 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003946 int evaluate;
3947 int getnext;
3948 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003949 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003950 int op;
3951 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003952 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003953 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003954
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003955 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003956 p = eval_next_non_blank(*arg, evalarg, &getnext);
3957 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003958 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003960
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003961 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003962 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003963 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003964 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003965 {
3966 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3967 {
mityu4ac198c2021-05-28 17:52:40 +02003968 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003969 clear_tv(rettv);
3970 return FAIL;
3971 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003972 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003975 f1 = 0;
3976 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003977 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003978 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003980 if (rettv->v_type == VAR_FLOAT)
3981 {
3982 f1 = rettv->vval.v_float;
3983 use_float = TRUE;
3984 n1 = 0;
3985 }
3986 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003987 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989 if (error)
3990 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 else
3993 n1 = 0;
3994
3995 /*
3996 * Get the second variable.
3997 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003998 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3999 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004000 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004001 clear_tv(rettv);
4002 return FAIL;
4003 }
4004 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004005 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return FAIL;
4007
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004008 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004010 if (var2.v_type == VAR_FLOAT)
4011 {
4012 if (!use_float)
4013 {
4014 f1 = n1;
4015 use_float = TRUE;
4016 }
4017 f2 = var2.vval.v_float;
4018 n2 = 0;
4019 }
4020 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004022 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004023 clear_tv(&var2);
4024 if (error)
4025 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004026 if (use_float)
4027 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030 /*
4031 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004034 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004036 if (op == '*')
4037 f1 = f1 * f2;
4038 else if (op == '/')
4039 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004040#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004042 if (f2 == 0.0)
4043 {
4044 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004047 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004049 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004050 }
4051 else
4052 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004053#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // We rely on the floating point library to handle divide
4055 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004057#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004061 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 return FAIL;
4063 }
4064 rettv->v_type = VAR_FLOAT;
4065 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
4068 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004069 int failed = FALSE;
4070
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004071 if (op == '*')
4072 n1 = n1 * n2;
4073 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004074 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004076 n1 = num_modulus(n1, n2, &failed);
4077 if (failed)
4078 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004079
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004080 rettv->v_type = VAR_NUMBER;
4081 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 }
4085
4086 return OK;
4087}
4088
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004089/*
4090 * Handle a type cast before a base level expression.
4091 * "arg" must point to the first non-white of the expression.
4092 * "arg" is advanced to just after the recognized expression.
4093 * Return OK or FAIL.
4094 */
4095 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004096eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004097 char_u **arg,
4098 typval_T *rettv,
4099 evalarg_T *evalarg,
4100 int want_string) // after "." operator
4101{
4102 type_T *want_type = NULL;
4103 garray_T type_list; // list of pointers to allocated types
4104 int res;
4105 int evaluate = evalarg == NULL ? 0
4106 : (evalarg->eval_flags & EVAL_EVALUATE);
4107
4108 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004109 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4110 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004111 {
4112 ++*arg;
4113 ga_init2(&type_list, sizeof(type_T *), 10);
4114 want_type = parse_type(arg, &type_list, TRUE);
4115 if (want_type == NULL && (evaluate || **arg != '>'))
4116 {
4117 clear_type_list(&type_list);
4118 return FAIL;
4119 }
4120
4121 if (**arg != '>')
4122 {
4123 if (*skipwhite(*arg) == '>')
4124 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4125 else
4126 emsg(_(e_missing_gt));
4127 clear_type_list(&type_list);
4128 return FAIL;
4129 }
4130 ++*arg;
4131 *arg = skipwhite_and_linebreak(*arg, evalarg);
4132 }
4133
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004134 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004135
4136 if (want_type != NULL && evaluate)
4137 {
4138 if (res == OK)
4139 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004140 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4141 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004142
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004143 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004144 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004145 if (want_type->tt_type == VAR_BOOL
4146 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004147 && (actual->tt_flags & TTFLAG_BOOL_OK))
4148 {
4149 int n = tv2bool(rettv);
4150
4151 // can use "0" and "1" for boolean in some places
4152 clear_tv(rettv);
4153 rettv->v_type = VAR_BOOL;
4154 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4155 }
4156 else
4157 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004158 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004159
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004160 res = check_type(want_type, actual, TRUE, where);
4161 }
4162 }
4163 }
4164 clear_type_list(&type_list);
4165 }
4166
4167 return res;
4168}
4169
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004170 int
4171eval_leader(char_u **arg, int vim9)
4172{
4173 char_u *s = *arg;
4174 char_u *p = *arg;
4175
4176 while (*p == '!' || *p == '-' || *p == '+')
4177 {
4178 char_u *n = skipwhite(p + 1);
4179
4180 // ++, --, -+ and +- are not accepted in Vim9 script
4181 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4182 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004183 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004184 return FAIL;
4185 }
4186 p = n;
4187 }
4188 *arg = p;
4189 return OK;
4190}
4191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004193 * Check for a predefined value "true", "false" and "null.*".
4194 * Return OK when recognized.
4195 */
4196 int
4197handle_predefined(char_u *s, int len, typval_T *rettv)
4198{
4199 switch (len)
4200 {
4201 case 4: if (STRNCMP(s, "true", 4) == 0)
4202 {
4203 rettv->v_type = VAR_BOOL;
4204 rettv->vval.v_number = VVAL_TRUE;
4205 return OK;
4206 }
4207 if (STRNCMP(s, "null", 4) == 0)
4208 {
4209 rettv->v_type = VAR_SPECIAL;
4210 rettv->vval.v_number = VVAL_NULL;
4211 return OK;
4212 }
4213 break;
4214 case 5: if (STRNCMP(s, "false", 5) == 0)
4215 {
4216 rettv->v_type = VAR_BOOL;
4217 rettv->vval.v_number = VVAL_FALSE;
4218 return OK;
4219 }
4220 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004221 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4222 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004223#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004224 rettv->v_type = VAR_JOB;
4225 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004226#else
4227 rettv->v_type = VAR_SPECIAL;
4228 rettv->vval.v_number = VVAL_NULL;
4229#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004230 return OK;
4231 }
4232 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004233 case 9:
4234 if (STRNCMP(s, "null_", 5) != 0)
4235 break;
4236 if (STRNCMP(s + 5, "list", 4) == 0)
4237 {
4238 rettv->v_type = VAR_LIST;
4239 rettv->vval.v_list = NULL;
4240 return OK;
4241 }
4242 if (STRNCMP(s + 5, "dict", 4) == 0)
4243 {
4244 rettv->v_type = VAR_DICT;
4245 rettv->vval.v_dict = NULL;
4246 return OK;
4247 }
4248 if (STRNCMP(s + 5, "blob", 4) == 0)
4249 {
4250 rettv->v_type = VAR_BLOB;
4251 rettv->vval.v_blob = NULL;
4252 return OK;
4253 }
4254 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004255 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4256 {
4257 rettv->v_type = VAR_CLASS;
4258 rettv->vval.v_class = NULL;
4259 return OK;
4260 }
4261 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004262 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4263 {
4264 rettv->v_type = VAR_STRING;
4265 rettv->vval.v_string = NULL;
4266 return OK;
4267 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004268 if (STRNCMP(s, "null_object", 11) == 0)
4269 {
4270 rettv->v_type = VAR_OBJECT;
4271 rettv->vval.v_object = NULL;
4272 return OK;
4273 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004274 break;
4275 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004276 if (STRNCMP(s, "null_channel", 12) == 0)
4277 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004278#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004279 rettv->v_type = VAR_CHANNEL;
4280 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004281#else
4282 rettv->v_type = VAR_SPECIAL;
4283 rettv->vval.v_number = VVAL_NULL;
4284#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004285 return OK;
4286 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004287 if (STRNCMP(s, "null_partial", 12) == 0)
4288 {
4289 rettv->v_type = VAR_PARTIAL;
4290 rettv->vval.v_partial = NULL;
4291 return OK;
4292 }
4293 break;
4294 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4295 {
4296 rettv->v_type = VAR_FUNC;
4297 rettv->vval.v_string = NULL;
4298 return OK;
4299 }
4300 break;
4301 }
4302 return FAIL;
4303}
4304
4305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 * Handle sixth level expression:
4307 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004308 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004309 * "string" string constant
4310 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 * &option-name option value
4312 * @r register contents
4313 * identifier variable value
4314 * function() function call
4315 * $VAR environment variable
4316 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004317 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004319 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004320 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 *
4322 * Also handle:
4323 * ! in front logical NOT
4324 * - in front unary minus
4325 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326 * trailing [] subscript in String or List
4327 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004328 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 *
4330 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004331 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 *
4333 * Return OK or FAIL.
4334 */
4335 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004336eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004337 char_u **arg,
4338 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004339 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004342 int evaluate = evalarg != NULL
4343 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 int len;
4345 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004346 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 char_u *start_leader, *end_leader;
4348 int ret = OK;
4349 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004350 static int recurse = 0;
4351 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
4353 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004355 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004360 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
4362 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 end_leader = *arg;
4366
Keith Thompson184f71c2024-01-04 21:19:04 +01004367 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004368 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004369 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004370 ++*arg;
4371 return FAIL;
4372 }
4373
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004374 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004375 // and crash. With MSVC the stack is smaller.
4376 if (recurse ==
4377#ifdef _MSC_VER
4378 300
4379#else
4380 1000
4381#endif
4382 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004383 {
4384 semsg(_(e_expression_too_recursive_str), *arg);
4385 return FAIL;
4386 }
4387 ++recurse;
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 switch (**arg)
4390 {
4391 /*
4392 * Number constant.
4393 */
4394 case '0':
4395 case '1':
4396 case '2':
4397 case '3':
4398 case '4':
4399 case '5':
4400 case '6':
4401 case '7':
4402 case '8':
4403 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004404 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004405
4406 // Apply prefixed "-" and "+" now. Matters especially when
4407 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004408 if (ret == OK && evaluate && end_leader > start_leader
4409 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004410 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 break;
4412
4413 /*
4414 * String constant: "string".
4415 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004416 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 break;
4418
4419 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004420 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004422 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 break;
4424
4425 /*
4426 * List: [expr, expr]
4427 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004428 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 break;
4430
4431 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004432 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004433 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004434 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004435 {
4436 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4437 }
4438 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004439 {
4440 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004441 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004442 }
4443 else
4444 ret = NOTDONE;
4445 break;
4446
4447 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004448 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004449 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004451 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004452 ret = NOTDONE;
4453 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004454 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004455 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004456 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 break;
4458
4459 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004460 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004462 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 break;
4464
4465 /*
4466 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004467 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 */
LemonBoy2eaef102022-05-06 13:14:50 +01004469 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4470 ret = eval_interp_string(arg, rettv, evaluate);
4471 else
4472 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 break;
4474
4475 /*
4476 * Register contents: @r.
4477 */
4478 case '@': ++*arg;
4479 if (evaluate)
4480 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004481 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004482 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004483 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004484 emsg_invreg(**arg);
4485 else
4486 {
4487 rettv->v_type = VAR_STRING;
4488 rettv->vval.v_string = get_reg_contents(**arg,
4489 GREG_EXPR_SRC);
4490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 if (**arg != NUL)
4493 ++*arg;
4494 break;
4495
4496 /*
4497 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004498 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004500 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004501 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004502 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004503 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004504 if (ret == OK && evaluate)
4505 {
4506 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4507
Bram Moolenaara9931532021-06-12 15:58:16 +02004508 // Compile it here to get the return type. The return
4509 // type is optional, when it's missing use t_unknown.
4510 // This is recognized in compile_return().
4511 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4512 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004513 if (compile_def_function(ufunc, FALSE,
4514 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004515 {
4516 clear_tv(rettv);
4517 ret = FAIL;
4518 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004519 }
4520 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004521 if (ret == NOTDONE)
4522 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004523 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004524 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004525
Bram Moolenaar9215f012020-06-27 21:18:00 +02004526 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004527 if (**arg == ')')
4528 ++*arg;
4529 else if (ret == OK)
4530 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004531 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004532 clear_tv(rettv);
4533 ret = FAIL;
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
4536 break;
4537
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 break;
4540 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541
4542 if (ret == NOTDONE)
4543 {
4544 /*
4545 * Must be a variable or function name.
4546 * Can also be a curly-braces kind of name: {expr}.
4547 */
4548 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004549 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 if (alias != NULL)
4551 s = alias;
4552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 ret = FAIL;
4555 else
4556 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004557 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4558
Bram Moolenaar4525a572022-02-13 11:57:33 +00004559 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004560 {
4561 emsg(_(e_cannot_use_underscore_here));
4562 ret = FAIL;
4563 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004564 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004565 && s[0] == 's' && s[1] == ':')
4566 {
4567 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4568 ret = FAIL;
4569 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004570 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004571 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004572 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004573 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004574 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004576 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004577 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004578 // get the value of "true", "false", etc. or a variable
4579 ret = FAIL;
4580 if (vim9script)
4581 ret = handle_predefined(s, len, rettv);
4582 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004583 {
4584 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004585 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004586 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004587 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004588 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004590 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004591 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004592 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004593 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004596 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 }
4598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4600 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004601 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004602 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 /*
4605 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4606 */
4607 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004608 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004609
4610 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004611 return ret;
4612}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004613
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004614/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004615 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004616 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004617 * Adjusts "end_leaderp" until it is at "start_leader".
4618 */
4619 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004620eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004621 typval_T *rettv,
4622 int numeric_only,
4623 char_u *start_leader,
4624 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004625{
4626 char_u *end_leader = *end_leaderp;
4627 int ret = OK;
4628 int error = FALSE;
4629 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004630 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004631 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004632 float_T f = 0.0;
4633
4634 if (rettv->v_type == VAR_FLOAT)
4635 f = rettv->vval.v_float;
4636 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004637 {
4638 while (VIM_ISWHITE(end_leader[-1]))
4639 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004640 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004641 val = tv2bool(rettv);
4642 else
4643 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004644 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004645 if (error)
4646 {
4647 clear_tv(rettv);
4648 ret = FAIL;
4649 }
4650 else
4651 {
4652 while (end_leader > start_leader)
4653 {
4654 --end_leader;
4655 if (*end_leader == '!')
4656 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004657 if (numeric_only)
4658 {
4659 ++end_leader;
4660 break;
4661 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004662 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004663 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004664 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004665 {
4666 rettv->v_type = VAR_BOOL;
4667 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4668 }
4669 else
4670 f = !f;
4671 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004672 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004673 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004674 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004675 type = VAR_BOOL;
4676 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004677 }
4678 else if (*end_leader == '-')
4679 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004680 if (rettv->v_type == VAR_FLOAT)
4681 f = -f;
4682 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004683 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004684 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004685 type = VAR_NUMBER;
4686 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004687 }
4688 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004689 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004692 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 else
4695 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004696 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004697 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004698 rettv->v_type = type;
4699 else
4700 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004701 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004704 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return ret;
4706}
4707
4708/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004709 * Call the function referred to in "rettv".
4710 */
4711 static int
4712call_func_rettv(
4713 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004714 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004715 typval_T *rettv,
4716 int evaluate,
4717 dict_T *selfdict,
4718 typval_T *basetv)
4719{
4720 partial_T *pt = NULL;
4721 funcexe_T funcexe;
4722 typval_T functv;
4723 char_u *s;
4724 int ret;
4725
4726 // need to copy the funcref so that we can clear rettv
4727 if (evaluate)
4728 {
4729 functv = *rettv;
4730 rettv->v_type = VAR_UNKNOWN;
4731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004733 if (functv.v_type == VAR_PARTIAL)
4734 {
4735 pt = functv.vval.v_partial;
4736 s = partial_name(pt);
4737 }
4738 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004739 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004740 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004741 if (s == NULL || *s == NUL)
4742 {
4743 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004744 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004745 goto theend;
4746 }
4747 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004748 }
4749 else
4750 s = (char_u *)"";
4751
Bram Moolenaara80faa82020-04-12 19:37:17 +02004752 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004753 funcexe.fe_firstline = curwin->w_cursor.lnum;
4754 funcexe.fe_lastline = curwin->w_cursor.lnum;
4755 funcexe.fe_evaluate = evaluate;
4756 funcexe.fe_partial = pt;
4757 funcexe.fe_selfdict = selfdict;
4758 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004759 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004760
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004761theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // Clear the funcref afterwards, so that deleting it while
4763 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004764 if (evaluate)
4765 clear_tv(&functv);
4766
4767 return ret;
4768}
4769
4770/*
4771 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004772 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004773 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4774 */
4775 static int
4776eval_lambda(
4777 char_u **arg,
4778 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004779 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004781{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004782 int evaluate = evalarg != NULL
4783 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004784 typval_T base = *rettv;
4785 int ret;
4786
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004787 rettv->v_type = VAR_UNKNOWN;
4788
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004789 if (**arg == '{')
4790 {
4791 // ->{lambda}()
4792 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4793 }
4794 else
4795 {
4796 // ->(lambda)()
4797 ++*arg;
4798 ret = eval1(arg, rettv, evalarg);
4799 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004800 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004801 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004802 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004803 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004804 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004805 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4806 && rettv->v_type != VAR_PARTIAL)
4807 {
4808 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4809 return FAIL;
4810 }
4811 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004812 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004813 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004814 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004815
4816 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004817 {
4818 if (verbose)
4819 {
4820 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004821 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004822 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004823 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004824 }
4825 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004826 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004827 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004828 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004829 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004830
4831 // Clear the funcref afterwards, so that deleting it while
4832 // evaluating the arguments is possible (see test55).
4833 if (evaluate)
4834 clear_tv(&base);
4835
4836 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004837}
4838
4839/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004840 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004841 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004842 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4843 */
4844 static int
4845eval_method(
4846 char_u **arg,
4847 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004848 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004850{
4851 char_u *name;
4852 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004853 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004854 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004855 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004856 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004857 int evaluate = evalarg != NULL
4858 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004859
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004860 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004861
Bram Moolenaarac92e252019-08-03 21:58:38 +02004862 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004863 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004864 if (alias != NULL)
4865 name = alias;
4866
4867 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004868 {
4869 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004870 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004871 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004872 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004873 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004874 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004875 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004876
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004877 // If there is no "(" immediately following, but there is further on,
4878 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4879 // Does not handle anything where "(" is part of the expression.
4880 *arg = skipwhite(*arg);
4881
4882 if (**arg != '(' && alias == NULL
4883 && (paren = vim_strchr(*arg, '(')) != NULL)
4884 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004885 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004886
4887 // Truncate the name a the "(". Avoid trying to get another line
4888 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004889 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004890 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4891 if (evalarg != NULL)
4892 {
4893 getline = evalarg->eval_getline;
4894 evalarg->eval_getline = NULL;
4895 }
4896
4897 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004898 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004899 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004900 *arg = name + len;
4901 ret = FAIL;
4902 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004903 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004904 {
4905 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004906 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004907 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004908
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004909 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004910 if (getline != NULL)
4911 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004912 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004913
4914 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004915 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004916 *arg = skipwhite(*arg);
4917
4918 if (**arg != '(')
4919 {
4920 if (verbose)
4921 semsg(_(e_missing_parenthesis_str), name);
4922 ret = FAIL;
4923 }
4924 else if (VIM_ISWHITE((*arg)[-1]))
4925 {
4926 if (verbose)
4927 emsg(_(e_no_white_space_allowed_before_parenthesis));
4928 ret = FAIL;
4929 }
4930 else
4931 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004932 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004933 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004934 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004935
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004936 // Clear the funcref afterwards, so that deleting it while
4937 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004938 if (evaluate)
4939 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004940 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004941
Bram Moolenaarac92e252019-08-03 21:58:38 +02004942 return ret;
4943}
4944
4945/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004946 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4947 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4949 */
4950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004951eval_index(
4952 char_u **arg,
4953 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004954 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004956{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004957 int evaluate = evalarg != NULL
4958 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004960 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004963 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004964 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004966 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4967 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004968
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004969 init_tv(&var1);
4970 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 /*
4974 * dict.name
4975 */
4976 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004977 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004979 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004980 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004981 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 }
4983 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985 /*
4986 * something[idx]
4987 *
4988 * Get the (first) variable from inside the [].
4989 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004990 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 if (**arg == ':')
4992 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004993 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004995 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004996 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004997 semsg(_(e_white_space_required_before_and_after_str_at_str),
4998 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004999 clear_tv(&var1);
5000 return FAIL;
5001 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005002 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005004 int error = FALSE;
5005
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005006 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005007 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005008 && var1.v_type == VAR_FLOAT)
5009 {
5010 var1.vval.v_string = typval_tostring(&var1, TRUE);
5011 var1.v_type = VAR_STRING;
5012 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005013
Bram Moolenaar4525a572022-02-13 11:57:33 +00005014 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005015 tv_get_number_chk(&var1, &error);
5016 else
5017 error = tv_get_string_chk(&var1) == NULL;
5018 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005019 {
5020 // not a number or string
5021 clear_tv(&var1);
5022 return FAIL;
5023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025
5026 /*
5027 * Get the second variable from inside the [:].
5028 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005029 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 if (**arg == ':')
5031 {
5032 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005033 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005034 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005035 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005036 semsg(_(e_white_space_required_before_and_after_str_at_str),
5037 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005038 if (!empty1)
5039 clear_tv(&var1);
5040 return FAIL;
5041 }
5042 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 if (**arg == ']')
5044 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005045 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005047 if (!empty1)
5048 clear_tv(&var1);
5049 return FAIL;
5050 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005051 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005052 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005053 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005054 if (!empty1)
5055 clear_tv(&var1);
5056 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 return FAIL;
5058 }
5059 }
5060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005062 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 if (**arg != ']')
5064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005065 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005066 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 clear_tv(&var1);
5068 if (range)
5069 clear_tv(&var2);
5070 return FAIL;
5071 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005072 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 }
5074
5075 if (evaluate)
5076 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005077 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005078 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005079 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005080
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005083 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005084 clear_tv(&var2);
5085 return res;
5086 }
5087 return OK;
5088}
5089
5090/*
5091 * Check if "rettv" can have an [index] or [sli:ce]
5092 */
5093 int
5094check_can_index(typval_T *rettv, int evaluate, int verbose)
5095{
5096 switch (rettv->v_type)
5097 {
5098 case VAR_FUNC:
5099 case VAR_PARTIAL:
5100 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005101 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005102 return FAIL;
5103 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005104 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005105 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005106 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005107 case VAR_BOOL:
5108 case VAR_SPECIAL:
5109 case VAR_JOB:
5110 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005111 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005112 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005113 if (verbose)
5114 emsg(_(e_cannot_index_special_variable));
5115 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005116 case VAR_CLASS:
5117 case VAR_TYPEALIAS:
5118 if (verbose)
5119 check_typval_is_value(rettv);
5120 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005121 case VAR_UNKNOWN:
5122 case VAR_ANY:
5123 case VAR_VOID:
5124 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005126 emsg(_(e_cannot_index_special_variable));
5127 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005129 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005131 case VAR_STRING:
5132 case VAR_LIST:
5133 case VAR_DICT:
5134 case VAR_BLOB:
5135 break;
5136 case VAR_NUMBER:
5137 if (in_vim9script())
5138 emsg(_(e_cannot_index_number));
5139 break;
5140 }
5141 return OK;
5142}
5143
5144/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005145 * slice() function
5146 */
5147 void
5148f_slice(typval_T *argvars, typval_T *rettv)
5149{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005150 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005151 && ((argvars[0].v_type != VAR_STRING
5152 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005153 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005154 && check_for_list_arg(argvars, 0) == FAIL)
5155 || check_for_number_arg(argvars, 1) == FAIL
5156 || check_for_opt_number_arg(argvars, 2) == FAIL))
5157 return;
5158
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005159 if (check_can_index(argvars, TRUE, FALSE) != OK)
5160 return;
5161
5162 copy_tv(argvars, rettv);
5163 eval_index_inner(rettv, TRUE, argvars + 1,
5164 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5165 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005166}
5167
5168/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005169 * Apply index or range to "rettv".
5170 * "var1" is the first index, NULL for [:expr].
5171 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005172 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5173 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005174 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5175 */
5176 int
5177eval_index_inner(
5178 typval_T *rettv,
5179 int is_range,
5180 typval_T *var1,
5181 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005182 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005183 char_u *key,
5184 int keylen,
5185 int verbose)
5186{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005187 varnumber_T n1, n2 = 0;
5188 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005189
5190 n1 = 0;
5191 if (var1 != NULL && rettv->v_type != VAR_DICT)
5192 n1 = tv_get_number(var1);
5193
5194 if (is_range)
5195 {
5196 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005198 if (verbose)
5199 emsg(_(e_cannot_slice_dictionary));
5200 return FAIL;
5201 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005202 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005203 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005204 else
5205 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005206 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005207
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005208 switch (rettv->v_type)
5209 {
5210 case VAR_UNKNOWN:
5211 case VAR_ANY:
5212 case VAR_VOID:
5213 case VAR_FUNC:
5214 case VAR_PARTIAL:
5215 case VAR_FLOAT:
5216 case VAR_BOOL:
5217 case VAR_SPECIAL:
5218 case VAR_JOB:
5219 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005220 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005221 case VAR_CLASS:
5222 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005223 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005224 break; // not evaluating, skipping over subscript
5225
5226 case VAR_NUMBER:
5227 case VAR_STRING:
5228 {
5229 char_u *s = tv_get_string(rettv);
5230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005232 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005233 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005234 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005235 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005236 else
5237 s = char_from_string(s, n1);
5238 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005239 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 // The resulting variable is a substring. If the indexes
5242 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 if (n1 < 0)
5244 {
5245 n1 = len + n1;
5246 if (n1 < 0)
5247 n1 = 0;
5248 }
5249 if (n2 < 0)
5250 n2 = len + n2;
5251 else if (n2 >= len)
5252 n2 = len;
5253 if (n1 >= len || n2 < 0 || n1 > n2)
5254 s = NULL;
5255 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005256 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 }
5258 else
5259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005260 // The resulting variable is a string of a single
5261 // character. If the index is too big or negative the
5262 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 if (n1 >= len || n1 < 0)
5264 s = NULL;
5265 else
5266 s = vim_strnsave(s + n1, 1);
5267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005268 clear_tv(rettv);
5269 rettv->v_type = VAR_STRING;
5270 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005271 }
5272 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005274 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005275 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5276 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005277 break;
5278
5279 case VAR_LIST:
5280 if (var1 == NULL)
5281 n1 = 0;
5282 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005283 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005284 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005285 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005286 return FAIL;
5287 break;
5288
5289 case VAR_DICT:
5290 {
5291 dictitem_T *item;
5292 typval_T tmp;
5293
5294 if (key == NULL)
5295 {
5296 key = tv_get_string_chk(var1);
5297 if (key == NULL)
5298 return FAIL;
5299 }
5300
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005301 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005302
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005303 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005304 {
5305 if (verbose)
5306 {
5307 if (keylen > 0)
5308 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005309 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005310 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005311 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005312 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005313
5314 copy_tv(&item->di_tv, &tmp);
5315 clear_tv(rettv);
5316 *rettv = tmp;
5317 }
5318 break;
5319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return OK;
5321}
5322
5323/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005324 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005325 */
5326 char_u *
5327partial_name(partial_T *pt)
5328{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005329 if (pt != NULL)
5330 {
5331 if (pt->pt_name != NULL)
5332 return pt->pt_name;
5333 if (pt->pt_func != NULL)
5334 return pt->pt_func->uf_name;
5335 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005336 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005337}
5338
Bram Moolenaarddecc252016-04-06 22:59:37 +02005339 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005340partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005341{
5342 int i;
5343
5344 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005346 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005347 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005348 if (pt->pt_name != NULL)
5349 {
5350 func_unref(pt->pt_name);
5351 vim_free(pt->pt_name);
5352 }
5353 else
5354 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005355 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005356
Bram Moolenaar54656012021-06-09 20:50:46 +02005357 // "out_up" is no longer used, decrement refcount on partial that owns it.
5358 partial_unref(pt->pt_outer.out_up_partial);
5359
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005360 // Using pt_outer from another partial.
5361 partial_unref(pt->pt_outer_partial);
5362
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005363 // Decrease the reference count for the context of a closure. If down
5364 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005365 if (pt->pt_funcstack != NULL)
5366 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005367 --pt->pt_funcstack->fs_refcount;
5368 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005369 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005370 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005371 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5372 if (pt->pt_loopvars[i] != NULL)
5373 {
5374 --pt->pt_loopvars[i]->lvs_refcount;
5375 loopvars_check_refcount(pt->pt_loopvars[i]);
5376 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005377
Bram Moolenaarddecc252016-04-06 22:59:37 +02005378 vim_free(pt);
5379}
5380
5381/*
5382 * Unreference a closure: decrement the reference count and free it when it
5383 * becomes zero.
5384 */
5385 void
5386partial_unref(partial_T *pt)
5387{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005388 if (pt == NULL)
5389 return;
5390
5391 int done = FALSE;
5392
5393 if (--pt->pt_refcount <= 0)
5394 partial_free(pt);
5395
5396 // If the reference count goes down to one, the funcstack may be the
5397 // only reference and can be freed if no other partials reference it.
5398 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005399 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005400 // careful: if the funcstack is freed it may contain this partial
5401 // and it gets freed as well
5402 if (pt->pt_funcstack != NULL)
5403 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005404
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005405 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005406 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005407 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005408
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005409 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5410 if (pt->pt_loopvars[depth] != NULL
5411 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005412 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005413 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005414 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005415}
5416
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005417/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005418 * Return the next (unique) copy ID.
5419 * Used for serializing nested structures.
5420 */
5421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005423{
5424 current_copyID += COPYID_INC;
5425 return current_copyID;
5426}
5427
5428/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429 * Garbage collection for lists and dictionaries.
5430 *
5431 * We use reference counts to be able to free most items right away when they
5432 * are no longer used. But for composite items it's possible that it becomes
5433 * unused while the reference count is > 0: When there is a recursive
5434 * reference. Example:
5435 * :let l = [1, 2, 3]
5436 * :let d = {9: l}
5437 * :let l[1] = d
5438 *
5439 * Since this is quite unusual we handle this with garbage collection: every
5440 * once in a while find out which lists and dicts are not referenced from any
5441 * variable.
5442 *
5443 * Here is a good reference text about garbage collection (refers to Python
5444 * but it applies to all reference-counting mechanisms):
5445 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005446 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005447
5448/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005449 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005450 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005451 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005452 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005453 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005454garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005455{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005456 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005457 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005458 buf_T *buf;
5459 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005460 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005461 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005463 if (!testing)
5464 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005466 want_garbage_collect = FALSE;
5467 may_garbage_collect = FALSE;
5468 garbage_collect_at_exit = FALSE;
5469 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005470
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005471 // The execution stack can grow big, limit the size.
5472 if (exestack.ga_maxlen - exestack.ga_len > 500)
5473 {
5474 size_t new_len;
5475 char_u *pp;
5476 int n;
5477
5478 // Keep 150% of the current size, with a minimum of the growth size.
5479 n = exestack.ga_len / 2;
5480 if (n < exestack.ga_growsize)
5481 n = exestack.ga_growsize;
5482
5483 // Don't make it bigger though.
5484 if (exestack.ga_len + n < exestack.ga_maxlen)
5485 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005486 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005487 pp = vim_realloc(exestack.ga_data, new_len);
5488 if (pp == NULL)
5489 return FAIL;
5490 exestack.ga_maxlen = exestack.ga_len + n;
5491 exestack.ga_data = pp;
5492 }
5493 }
5494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 // We advance by two because we add one for items referenced through
5496 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005497 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005498
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005499 /*
5500 * 1. Go through all accessible variables and mark all lists and dicts
5501 * with copyID.
5502 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005504 // Don't free variables in the previous_funccal list unless they are only
5505 // referenced through previous_funccal. This must be first, because if
5506 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005507 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005509 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005510 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005512 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005513 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005514 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5515 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005517 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005518 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005519 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5520 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005521 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005522 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005523 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005524 abort = abort || set_ref_in_item(
5525 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005526#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005527 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005528 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5529 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005530 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005531 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005532 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5533 NULL, NULL);
5534#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005537 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005538 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5539 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005541 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005544 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005546 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005547 abort = abort || set_ref_in_functions(copyID);
5548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005549 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005550 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005551
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005552 // funcstacks keep variables for closures
5553 abort = abort || set_ref_in_funcstacks(copyID);
5554
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005555 // loopvars keep variables for loop blocks
5556 abort = abort || set_ref_in_loopvars(copyID);
5557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005558 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005559 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005560
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005561 // callbacks in buffers
5562 abort = abort || set_ref_in_buffers(copyID);
5563
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005564 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5565 abort = abort || set_ref_in_insexpand_funcs(copyID);
5566
5567 // 'operatorfunc' callback
5568 abort = abort || set_ref_in_opfunc(copyID);
5569
5570 // 'tagfunc' callback
5571 abort = abort || set_ref_in_tagfunc(copyID);
5572
5573 // 'imactivatefunc' and 'imstatusfunc' callbacks
5574 abort = abort || set_ref_in_im_funcs(copyID);
5575
Bram Moolenaar1dced572012-04-05 16:54:08 +02005576#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005577 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005578#endif
5579
Bram Moolenaardb913952012-06-29 12:54:53 +02005580#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005581 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005582#endif
5583
5584#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005585 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005586#endif
5587
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005588#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005589 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005590 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005591#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005592#ifdef FEAT_NETBEANS_INTG
5593 abort = abort || set_ref_in_nb_channel(copyID);
5594#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005595
Bram Moolenaare3188e22016-05-31 21:13:04 +02005596#ifdef FEAT_TIMERS
5597 abort = abort || set_ref_in_timer(copyID);
5598#endif
5599
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005600#ifdef FEAT_QUICKFIX
5601 abort = abort || set_ref_in_quickfix(copyID);
5602#endif
5603
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005604#ifdef FEAT_TERMINAL
5605 abort = abort || set_ref_in_term(copyID);
5606#endif
5607
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005608#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005609 abort = abort || set_ref_in_popups(copyID);
5610#endif
5611
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005612 abort = abort || set_ref_in_classes(copyID);
5613
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005614 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005615 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005616 /*
5617 * 2. Free lists and dictionaries that are not referenced.
5618 */
5619 did_free = free_unref_items(copyID);
5620
5621 /*
5622 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005623 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005624 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005625 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005626 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005627 else if (p_verbose > 0)
5628 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005629 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005630 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005631
5632 return did_free;
5633}
5634
5635/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005636 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005637 */
5638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005639free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005640{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005641 int did_free = FALSE;
5642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 // Let all "free" functions know that we are here. This means no
5644 // dictionaries, lists, channels or jobs are to be freed, because we will
5645 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005646 in_free_unref_items = TRUE;
5647
5648 /*
5649 * PASS 1: free the contents of the items. We don't free the items
5650 * themselves yet, so that it is possible to decrement refcount counters
5651 */
5652
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005653 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005654 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005655
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005656 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005657 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005658
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005659 // Go through the list of objects and free items without this copyID.
5660 did_free |= object_free_nonref(copyID);
5661
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005662 // Go through the list of classes and free items without this copyID.
5663 did_free |= class_free_nonref(copyID);
5664
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005665#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005666 // Go through the list of jobs and free items without the copyID. This
5667 // must happen before doing channels, because jobs refer to channels, but
5668 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005669 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005671 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005672 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5673#endif
5674
5675 /*
5676 * PASS 2: free the items themselves.
5677 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005678 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005679 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005680 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005681
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005682#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005683 // Go through the list of jobs and free items without the copyID. This
5684 // must happen before doing channels, because jobs refer to channels, but
5685 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005686 free_unused_jobs(copyID, COPYID_MASK);
5687
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005688 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005689 free_unused_channels(copyID, COPYID_MASK);
5690#endif
5691
5692 in_free_unref_items = FALSE;
5693
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005694 return did_free;
5695}
5696
5697/*
5698 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005699 * "list_stack" is used to add lists to be marked. Can be NULL.
5700 *
5701 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005702 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005705{
5706 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005707 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005708 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005709 hashtab_T *cur_ht;
5710 ht_stack_T *ht_stack = NULL;
5711 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005712
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005713 cur_ht = ht;
5714 for (;;)
5715 {
5716 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005718 // Mark each item in the hashtab. If the item contains a hashtab
5719 // it is added to ht_stack, if it contains a list it is added to
5720 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005721 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005722 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 if (!HASHITEM_EMPTY(hi))
5724 {
5725 --todo;
5726 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5727 &ht_stack, list_stack);
5728 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005729 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005730
5731 if (ht_stack == NULL)
5732 break;
5733
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005734 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005735 cur_ht = ht_stack->ht;
5736 tempitem = ht_stack;
5737 ht_stack = ht_stack->prev;
5738 free(tempitem);
5739 }
5740
5741 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005742}
5743
Dominique Pelle748b3082022-01-08 12:41:16 +00005744#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5745 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005746/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005747 * Mark a dict and its items with "copyID".
5748 * Returns TRUE if setting references failed somehow.
5749 */
5750 int
5751set_ref_in_dict(dict_T *d, int copyID)
5752{
5753 if (d != NULL && d->dv_copyID != copyID)
5754 {
5755 d->dv_copyID = copyID;
5756 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5757 }
5758 return FALSE;
5759}
Dominique Pelle748b3082022-01-08 12:41:16 +00005760#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005761
5762/*
5763 * Mark a list and its items with "copyID".
5764 * Returns TRUE if setting references failed somehow.
5765 */
5766 int
5767set_ref_in_list(list_T *ll, int copyID)
5768{
5769 if (ll != NULL && ll->lv_copyID != copyID)
5770 {
5771 ll->lv_copyID = copyID;
5772 return set_ref_in_list_items(ll, copyID, NULL);
5773 }
5774 return FALSE;
5775}
5776
5777/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005778 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005779 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5780 *
5781 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005782 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005783 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005784set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005785{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005786 listitem_T *li;
5787 int abort = FALSE;
5788 list_T *cur_l;
5789 list_stack_T *list_stack = NULL;
5790 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005791
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005792 cur_l = l;
5793 for (;;)
5794 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005795 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // Mark each item in the list. If the item contains a hashtab
5797 // it is added to ht_stack, if it contains a list it is added to
5798 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005799 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5800 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5801 ht_stack, &list_stack);
5802 if (list_stack == NULL)
5803 break;
5804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005805 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005806 cur_l = list_stack->list;
5807 tempitem = list_stack;
5808 list_stack = list_stack->prev;
5809 free(tempitem);
5810 }
5811
5812 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005813}
5814
5815/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005816 * Mark the partial in callback 'cb' with "copyID".
5817 */
5818 int
5819set_ref_in_callback(callback_T *cb, int copyID)
5820{
5821 typval_T tv;
5822
5823 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5824 return FALSE;
5825
5826 tv.v_type = VAR_PARTIAL;
5827 tv.vval.v_partial = cb->cb_partial;
5828 return set_ref_in_item(&tv, copyID, NULL, NULL);
5829}
5830
5831/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005832 * Mark the dict "dd" with "copyID".
5833 * Also see set_ref_in_item().
5834 */
5835 static int
5836set_ref_in_item_dict(
5837 dict_T *dd,
5838 int copyID,
5839 ht_stack_T **ht_stack,
5840 list_stack_T **list_stack)
5841{
5842 if (dd == NULL || dd->dv_copyID == copyID)
5843 return FALSE;
5844
5845 // Didn't see this dict yet.
5846 dd->dv_copyID = copyID;
5847 if (ht_stack == NULL)
5848 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5849
5850 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5851 if (newitem == NULL)
5852 return TRUE;
5853
5854 newitem->ht = &dd->dv_hashtab;
5855 newitem->prev = *ht_stack;
5856 *ht_stack = newitem;
5857
5858 return FALSE;
5859}
5860
5861/*
5862 * Mark the list "ll" with "copyID".
5863 * Also see set_ref_in_item().
5864 */
5865 static int
5866set_ref_in_item_list(
5867 list_T *ll,
5868 int copyID,
5869 ht_stack_T **ht_stack,
5870 list_stack_T **list_stack)
5871{
5872 if (ll == NULL || ll->lv_copyID == copyID)
5873 return FALSE;
5874
5875 // Didn't see this list yet.
5876 ll->lv_copyID = copyID;
5877 if (list_stack == NULL)
5878 return set_ref_in_list_items(ll, copyID, ht_stack);
5879
5880 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5881 if (newitem == NULL)
5882 return TRUE;
5883
5884 newitem->list = ll;
5885 newitem->prev = *list_stack;
5886 *list_stack = newitem;
5887
5888 return FALSE;
5889}
5890
5891/*
5892 * Mark the partial "pt" with "copyID".
5893 * Also see set_ref_in_item().
5894 */
5895 static int
5896set_ref_in_item_partial(
5897 partial_T *pt,
5898 int copyID,
5899 ht_stack_T **ht_stack,
5900 list_stack_T **list_stack)
5901{
5902 if (pt == NULL || pt->pt_copyID == copyID)
5903 return FALSE;
5904
5905 // Didn't see this partial yet.
5906 pt->pt_copyID = copyID;
5907
5908 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5909
5910 if (pt->pt_dict != NULL)
5911 {
5912 typval_T dtv;
5913
5914 dtv.v_type = VAR_DICT;
5915 dtv.vval.v_dict = pt->pt_dict;
5916 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5917 }
5918
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005919 if (pt->pt_obj != NULL)
5920 {
5921 typval_T objtv;
5922
5923 objtv.v_type = VAR_OBJECT;
5924 objtv.vval.v_object = pt->pt_obj;
5925 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5926 }
5927
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005928 for (int i = 0; i < pt->pt_argc; ++i)
5929 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5930 ht_stack, list_stack);
5931 // pt_funcstack is handled in set_ref_in_funcstacks()
5932 // pt_loopvars is handled in set_ref_in_loopvars()
5933
5934 return abort;
5935}
5936
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005937#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005938/*
5939 * Mark the job "pt" with "copyID".
5940 * Also see set_ref_in_item().
5941 */
5942 static int
5943set_ref_in_item_job(
5944 job_T *job,
5945 int copyID,
5946 ht_stack_T **ht_stack,
5947 list_stack_T **list_stack)
5948{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005949 typval_T dtv;
5950
5951 if (job == NULL || job->jv_copyID == copyID)
5952 return FALSE;
5953
5954 job->jv_copyID = copyID;
5955 if (job->jv_channel != NULL)
5956 {
5957 dtv.v_type = VAR_CHANNEL;
5958 dtv.vval.v_channel = job->jv_channel;
5959 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5960 }
5961 if (job->jv_exit_cb.cb_partial != NULL)
5962 {
5963 dtv.v_type = VAR_PARTIAL;
5964 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5965 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5966 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005967
5968 return FALSE;
5969}
5970
5971/*
5972 * Mark the channel "ch" with "copyID".
5973 * Also see set_ref_in_item().
5974 */
5975 static int
5976set_ref_in_item_channel(
5977 channel_T *ch,
5978 int copyID,
5979 ht_stack_T **ht_stack,
5980 list_stack_T **list_stack)
5981{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005982 typval_T dtv;
5983
5984 if (ch == NULL || ch->ch_copyID == copyID)
5985 return FALSE;
5986
5987 ch->ch_copyID = copyID;
5988 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5989 {
5990 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5991 jq != NULL; jq = jq->jq_next)
5992 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5993 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5994 cq = cq->cq_next)
5995 if (cq->cq_callback.cb_partial != NULL)
5996 {
5997 dtv.v_type = VAR_PARTIAL;
5998 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5999 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6000 }
6001 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6002 {
6003 dtv.v_type = VAR_PARTIAL;
6004 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6005 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6006 }
6007 }
6008 if (ch->ch_callback.cb_partial != NULL)
6009 {
6010 dtv.v_type = VAR_PARTIAL;
6011 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6012 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6013 }
6014 if (ch->ch_close_cb.cb_partial != NULL)
6015 {
6016 dtv.v_type = VAR_PARTIAL;
6017 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6018 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6019 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006020
6021 return FALSE;
6022}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006023#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006024
6025/*
6026 * Mark the class "cl" with "copyID".
6027 * Also see set_ref_in_item().
6028 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006029 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006030set_ref_in_item_class(
6031 class_T *cl,
6032 int copyID,
6033 ht_stack_T **ht_stack,
6034 list_stack_T **list_stack)
6035{
6036 int abort = FALSE;
6037
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006038 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006039 return FALSE;
6040
6041 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006042 if (cl->class_members_tv != NULL)
6043 {
6044 // The "class_members_tv" table is allocated only for regular classes
6045 // and not for interfaces.
6046 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6047 abort = abort || set_ref_in_item(
6048 &cl->class_members_tv[i],
6049 copyID, ht_stack, list_stack);
6050 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006051
6052 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6053 abort = abort || set_ref_in_func(NULL,
6054 cl->class_class_functions[i], copyID);
6055
6056 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6057 abort = abort || set_ref_in_func(NULL,
6058 cl->class_obj_methods[i], copyID);
6059
6060 return abort;
6061}
6062
6063/*
6064 * Mark the object "cl" with "copyID".
6065 * Also see set_ref_in_item().
6066 */
6067 static int
6068set_ref_in_item_object(
6069 object_T *obj,
6070 int copyID,
6071 ht_stack_T **ht_stack,
6072 list_stack_T **list_stack)
6073{
6074 int abort = FALSE;
6075
6076 if (obj == NULL || obj->obj_copyID == copyID)
6077 return FALSE;
6078
6079 obj->obj_copyID = copyID;
6080
6081 // The typval_T array is right after the object_T.
6082 typval_T *mtv = (typval_T *)(obj + 1);
6083 for (int i = 0; !abort
6084 && i < obj->obj_class->class_obj_member_count; ++i)
6085 abort = abort || set_ref_in_item(mtv + i, copyID,
6086 ht_stack, list_stack);
6087
6088 return abort;
6089}
6090
6091/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006092 * Mark all lists, dicts and other container types referenced through typval
6093 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006094 * "list_stack" is used to add lists to be marked. Can be NULL.
6095 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6096 *
6097 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006098 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100set_ref_in_item(
6101 typval_T *tv,
6102 int copyID,
6103 ht_stack_T **ht_stack,
6104 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006105{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006106 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006107
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006108 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006110 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006111 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6112 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006113
6114 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006115 return set_ref_in_item_list(tv->vval.v_list, copyID,
6116 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006117
6118 case VAR_FUNC:
6119 {
6120 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6121 break;
6122 }
6123
6124 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006125 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6126 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006127
6128 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006129#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006130 return set_ref_in_item_job(tv->vval.v_job, copyID,
6131 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006132#else
6133 break;
6134#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006135
6136 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006137#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006138 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006139 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006140#else
6141 break;
6142#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006143
6144 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006145 return set_ref_in_item_class(tv->vval.v_class, copyID,
6146 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006147
6148 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006149 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006150 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006151
6152 case VAR_UNKNOWN:
6153 case VAR_ANY:
6154 case VAR_VOID:
6155 case VAR_BOOL:
6156 case VAR_SPECIAL:
6157 case VAR_NUMBER:
6158 case VAR_FLOAT:
6159 case VAR_STRING:
6160 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006161 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006162 case VAR_INSTR:
6163 // Types that do not contain any other item
6164 break;
6165 }
6166
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006167 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006168}
6169
Bram Moolenaar8c711452005-01-14 21:53:12 +00006170/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 * Return a string with the string representation of a variable.
6172 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006174 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006175 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006176 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006177 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006178 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6179 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006180 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006182 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006183echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006184 typval_T *tv,
6185 char_u **tofree,
6186 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006187 int copyID,
6188 int echo_style,
6189 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006190 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006192 static int recurse = 0;
6193 char_u *r = NULL;
6194
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006197 if (!did_echo_string_emsg)
6198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 // Only give this message once for a recursive call to avoid
6200 // flooding the user with errors. And stop iterating over lists
6201 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006202 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006203 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006206 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 }
6208 ++recurse;
6209
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 switch (tv->v_type)
6211 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006212 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006213 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006214 {
6215 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006216 r = tv->vval.v_string;
6217 if (r == NULL)
6218 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006219 }
6220 else
6221 {
6222 *tofree = string_quote(tv->vval.v_string, FALSE);
6223 r = *tofree;
6224 }
6225 break;
6226
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006228 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006229 char_u buf[MAX_FUNC_NAME_LEN];
6230
6231 if (echo_style)
6232 {
LemonBoya5d35902022-04-29 21:15:02 +01006233 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6234 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006235 buf, MAX_FUNC_NAME_LEN);
6236 if (r == buf)
6237 {
6238 r = vim_strsave(buf);
6239 *tofree = r;
6240 }
6241 else
6242 *tofree = NULL;
6243 }
6244 else
6245 {
6246 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6247 : make_ufunc_name_readable(
6248 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6249 TRUE);
6250 r = *tofree;
6251 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006254
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006255 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006256 {
6257 partial_T *pt = tv->vval.v_partial;
6258 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006259 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006260 garray_T ga;
6261 int i;
6262 char_u *tf;
6263
6264 ga_init2(&ga, 1, 100);
6265 ga_concat(&ga, (char_u *)"function(");
6266 if (fname != NULL)
6267 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006268 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006269 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006270 && vim_isupper(fname[1]))
6271 {
6272 ga_concat(&ga, (char_u *)"'g:");
6273 ga_concat(&ga, fname + 1);
6274 }
6275 else
6276 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006277 vim_free(fname);
6278 }
6279 if (pt != NULL && pt->pt_argc > 0)
6280 {
6281 ga_concat(&ga, (char_u *)", [");
6282 for (i = 0; i < pt->pt_argc; ++i)
6283 {
6284 if (i > 0)
6285 ga_concat(&ga, (char_u *)", ");
6286 ga_concat(&ga,
6287 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6288 vim_free(tf);
6289 }
6290 ga_concat(&ga, (char_u *)"]");
6291 }
6292 if (pt != NULL && pt->pt_dict != NULL)
6293 {
6294 typval_T dtv;
6295
6296 ga_concat(&ga, (char_u *)", ");
6297 dtv.v_type = VAR_DICT;
6298 dtv.vval.v_dict = pt->pt_dict;
6299 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6300 vim_free(tf);
6301 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006302 // terminate with ')' and a NUL
6303 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006304
6305 *tofree = ga.ga_data;
6306 r = *tofree;
6307 break;
6308 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006310 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006311 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006312 break;
6313
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006315 if (tv->vval.v_list == NULL)
6316 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006317 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006318 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006319 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006320 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006321 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6322 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006323 {
6324 *tofree = NULL;
6325 r = (char_u *)"[...]";
6326 }
6327 else
6328 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006329 int old_copyID = tv->vval.v_list->lv_copyID;
6330
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006331 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006332 *tofree = list2string(tv, copyID, restore_copyID);
6333 if (restore_copyID)
6334 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006335 r = *tofree;
6336 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006337 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006338
Bram Moolenaar8c711452005-01-14 21:53:12 +00006339 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006340 if (tv->vval.v_dict == NULL)
6341 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006342 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006343 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006344 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006345 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006346 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6347 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006348 {
6349 *tofree = NULL;
6350 r = (char_u *)"{...}";
6351 }
6352 else
6353 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006354 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006355
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006356 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006357 *tofree = dict2string(tv, copyID, restore_copyID);
6358 if (restore_copyID)
6359 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006360 r = *tofree;
6361 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006362 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006365 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006366 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006368 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006369 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006370 break;
6371
Bram Moolenaar835dc632016-02-07 14:27:38 +01006372 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006373 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006374#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006375 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006376 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6377 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006378 if (composite_val)
6379 {
6380 *tofree = string_quote(r, FALSE);
6381 r = *tofree;
6382 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006383#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006385
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006386 case VAR_INSTR:
6387 *tofree = NULL;
6388 r = (char_u *)"instructions";
6389 break;
6390
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006391 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006392 {
6393 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006394 char *s = "class";
6395 if (IS_INTERFACE(cl))
6396 s = "interface";
6397 else if (IS_ENUM(cl))
6398 s = "enum";
6399 size_t len = STRLEN(s) + 1 +
6400 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006401 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006402 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006403 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6404 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006405 break;
6406
6407 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006408 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6409 echo_style, restore_copyID,
6410 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006411 break;
6412
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006413 case VAR_FLOAT:
6414 *tofree = NULL;
6415 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6416 r = numbuf;
6417 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006418
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006419 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006420 case VAR_SPECIAL:
6421 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006422 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006423 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006424
6425 case VAR_TYPEALIAS:
6426 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6427 r = *tofree;
6428 if (r == NULL)
6429 r = (char_u *)"";
6430 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432
Bram Moolenaar8502c702014-06-17 12:51:16 +02006433 if (--recurse == 0)
6434 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006436}
6437
6438/*
6439 * Return a string with the string representation of a variable.
6440 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6441 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006442 * Does not put quotes around strings, as ":echo" displays values.
6443 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6444 * May return NULL.
6445 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006446 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006447echo_string(
6448 typval_T *tv,
6449 char_u **tofree,
6450 char_u *numbuf,
6451 int copyID)
6452{
6453 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6454}
6455
6456/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006457 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6458 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006459 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006460 */
6461 int
6462buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6463{
6464 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006465 char_u *t;
6466 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006467
6468 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6469 return -1;
6470
6471 if (lnum > buf->b_ml.ml_line_count)
6472 lnum = buf->b_ml.ml_line_count;
6473
6474 str = ml_get_buf(buf, lnum, FALSE);
6475 if (str == NULL)
6476 return -1;
6477
6478 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006479 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006480
Bram Moolenaar91458462021-01-13 20:08:38 +01006481 // count the number of characters
6482 t = str;
6483 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6484 t += mb_ptr2len(t);
6485
6486 // In insert mode, when the cursor is at the end of a non-empty line,
6487 // byteidx points to the NUL character immediately past the end of the
6488 // string. In this case, add one to the character count.
6489 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6490 count++;
6491
6492 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006493}
6494
6495/*
6496 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006497 * byte index. Works only for loaded buffers. Returns -1 on failure.
6498 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006499 */
6500 int
6501buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6502{
6503 char_u *str;
6504 char_u *t;
6505
6506 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6507 return -1;
6508
6509 if (lnum > buf->b_ml.ml_line_count)
6510 lnum = buf->b_ml.ml_line_count;
6511
6512 str = ml_get_buf(buf, lnum, FALSE);
6513 if (str == NULL)
6514 return -1;
6515
6516 // Convert the character offset to a byte offset
6517 t = str;
6518 while (*t != NUL && --charidx > 0)
6519 t += mb_ptr2len(t);
6520
Bram Moolenaar91458462021-01-13 20:08:38 +01006521 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006522}
6523
6524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006526 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006529var2fpos(
6530 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006531 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006532 int *fnum, // set to fnum for '0, 'A, etc.
6533 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006535 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006537 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006539 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006540 if (varp->v_type == VAR_LIST)
6541 {
6542 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006543 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006544 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006545 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006546
6547 l = varp->vval.v_list;
6548 if (l == NULL)
6549 return NULL;
6550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006552 pos.lnum = list_find_nr(l, 0L, &error);
6553 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006554 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006555 if (charcol)
6556 len = (long)mb_charlen(ml_get(pos.lnum));
6557 else
John Marriottbfcc8952024-03-11 22:04:45 +01006558 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006559
Bram Moolenaarec65d772020-08-20 22:29:12 +02006560 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006561 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006562 li = list_find(l, 1L);
6563 if (li != NULL && li->li_tv.v_type == VAR_STRING
6564 && li->li_tv.vval.v_string != NULL
6565 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006566 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006567 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006568 }
6569 else
6570 {
6571 pos.col = list_find_nr(l, 1L, &error);
6572 if (error)
6573 return NULL;
6574 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006576 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006577 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006578 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006579 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006581 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006582 pos.coladd = list_find_nr(l, 2L, &error);
6583 if (error)
6584 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006585
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006586 return &pos;
6587 }
6588
Bram Moolenaarc5809432021-03-27 21:23:30 +01006589 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6590 return NULL;
6591
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006592 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006593 if (name == NULL)
6594 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006595
6596 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006597 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006598 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006599 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006600 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006601 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006602 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006603 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006604 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006605 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006606 pos = VIsual;
6607 else
6608 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006609 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006610 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006611 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006613 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006614 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6616 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006617 pos = *pp;
6618 }
6619 if (pos.lnum != 0)
6620 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006621 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006622 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6623 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006625
Bram Moolenaara5525202006-03-02 22:52:09 +00006626 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006627
Bram Moolenaar477933c2007-07-17 14:32:23 +00006628 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006629 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006630 // the "w_valid" flags are not reset when moving the cursor, but they
6631 // do matter for update_topline() and validate_botline().
6632 check_cursor_moved(curwin);
6633
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006634 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006635 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006636 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006637 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 // In silent Ex mode topline is zero, but that's not a valid line
6639 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006640 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006641 return &pos;
6642 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006644 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006645 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006647 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006648 return &pos;
6649 }
6650 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006651 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006653 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
6655 pos.lnum = curbuf->b_ml.ml_line_count;
6656 pos.col = 0;
6657 }
6658 else
6659 {
6660 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006661 if (charcol)
6662 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6663 else
John Marriottbfcc8952024-03-11 22:04:45 +01006664 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 }
6666 return &pos;
6667 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006668 if (in_vim9script())
6669 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 return NULL;
6671}
6672
6673/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006674 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006675 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006676 * Note that the column is passed on as-is, the caller may want to decrement
6677 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006678 * If "charcol" is TRUE use the column as the character index instead of the
6679 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006680 * Return FAIL when conversion is not possible, doesn't check the position for
6681 * validity.
6682 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006683 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006684list2fpos(
6685 typval_T *arg,
6686 pos_T *posp,
6687 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006688 colnr_T *curswantp,
6689 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006690{
6691 list_T *l = arg->vval.v_list;
6692 long i = 0;
6693 long n;
6694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6696 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006697 if (arg->v_type != VAR_LIST
6698 || l == NULL
6699 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006700 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006701 return FAIL;
6702
6703 if (fnump != NULL)
6704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006705 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006706 if (n < 0)
6707 return FAIL;
6708 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006709 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006710 *fnump = n;
6711 }
6712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006713 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006714 if (n < 0)
6715 return FAIL;
6716 posp->lnum = n;
6717
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006718 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006719 if (n < 0)
6720 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006721 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006722 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006723 if (charcol)
6724 {
6725 buf_T *buf;
6726
6727 // Get the text for the specified line in a loaded buffer
6728 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6729 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6730 return FAIL;
6731
Bram Moolenaar79f23442022-10-10 12:42:57 +01006732 n = buf_charidx_to_byteidx(buf,
6733 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006734 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006735 posp->col = n;
6736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006737 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006738 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006739 posp->coladd = 0;
6740 else
6741 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006742
Bram Moolenaar493c1782014-05-28 14:34:46 +02006743 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006744 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006745
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006746 return OK;
6747}
6748
6749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 * Get the length of an environment variable name.
6751 * Advance "arg" to the first character after the name.
6752 * Return 0 for error.
6753 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 char_u *p;
6758 int len;
6759
6760 for (p = *arg; vim_isIDc(*p); ++p)
6761 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006762 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 return 0;
6764
6765 len = (int)(p - *arg);
6766 *arg = p;
6767 return len;
6768}
6769
6770/*
6771 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006772 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 * Return 0 if something is wrong.
6774 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006776get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 char_u *p;
6779 int len;
6780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006781 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006783 {
6784 if (*p == ':')
6785 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006786 // "s:" is start of "s:var", but "n:" is not and can be used in
6787 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006788 len = (int)(p - *arg);
6789 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6790 || len > 1)
6791 break;
6792 }
6793 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006794 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 return 0;
6796
6797 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006798 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800 return len;
6801}
6802
6803/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006804 * Get the length of the name of a variable or function.
6805 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006807 * Return -1 if curly braces expansion failed.
6808 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 * If the name contains 'magic' {}'s, expand them and return the
6810 * expanded name in an allocated string via 'alias' - caller must free.
6811 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813get_name_len(
6814 char_u **arg,
6815 char_u **alias,
6816 int evaluate,
6817 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 char_u *p;
6821 char_u *expr_start;
6822 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006824 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
6826 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6827 && (*arg)[2] == (int)KE_SNR)
6828 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006829 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 *arg += 3;
6831 return get_id_len(arg) + 3;
6832 }
6833 len = eval_fname_script(*arg);
6834 if (len > 0)
6835 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006836 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 *arg += len;
6838 }
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006843 p = find_name_end(*arg, &expr_start, &expr_end,
6844 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (expr_start != NULL)
6846 {
6847 char_u *temp_string;
6848
6849 if (!evaluate)
6850 {
6851 len += (int)(p - *arg);
6852 *arg = skipwhite(p);
6853 return len;
6854 }
6855
6856 /*
6857 * Include any <SID> etc in the expanded string:
6858 * Thus the -len here.
6859 */
6860 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6861 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006862 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 *alias = temp_string;
6864 *arg = skipwhite(p);
6865 return (int)STRLEN(temp_string);
6866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006869 // Only give an error when there is something, otherwise it will be
6870 // reported at a higher level.
6871 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006872 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 return len;
6875}
6876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006877/*
6878 * Find the end of a variable or function name, taking care of magic braces.
6879 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6880 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006881 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006882 * Return a pointer to just after the name. Equal to "arg" if there is no
6883 * valid name.
6884 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886find_name_end(
6887 char_u *arg,
6888 char_u **expr_start,
6889 char_u **expr_end,
6890 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006892 int mb_nest = 0;
6893 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006895 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006896 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006898 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900 *expr_start = NULL;
6901 *expr_end = NULL;
6902 }
6903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006904 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006905 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006906 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006907 return arg;
6908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909 for (p = arg; *p != NUL
6910 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006911 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006912 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006913 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006915 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006916 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006917 if (*p == '\'')
6918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006920 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006921 ;
6922 if (*p == NUL)
6923 break;
6924 }
6925 else if (*p == '"')
6926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006927 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006928 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006929 if (*p == '\\' && p[1] != NUL)
6930 ++p;
6931 if (*p == NUL)
6932 break;
6933 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006934 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6935 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006936 // "s:" is start of "s:var", but "n:" is not and can be used in
6937 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006938 len = (int)(p - arg);
6939 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006940 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006941 break;
6942 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006944 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006946 if (*p == '[')
6947 ++br_nest;
6948 else if (*p == ']')
6949 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006951
zeertzjqa93d9cd2023-05-02 16:25:47 +01006952 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006954 if (*p == '{')
6955 {
6956 mb_nest++;
6957 if (expr_start != NULL && *expr_start == NULL)
6958 *expr_start = p;
6959 }
6960 else if (*p == '}')
6961 {
6962 mb_nest--;
6963 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6964 *expr_end = p;
6965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968
6969 return p;
6970}
6971
6972/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006973 * Expands out the 'magic' {}'s in a variable/function name.
6974 * Note that this can call itself recursively, to deal with
6975 * constructs like foo{bar}{baz}{bam}
6976 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6977 * "in_start" ^
6978 * "expr_start" ^
6979 * "expr_end" ^
6980 * "in_end" ^
6981 *
6982 * Returns a new allocated string, which the caller must free.
6983 * Returns NULL for failure.
6984 */
6985 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986make_expanded_name(
6987 char_u *in_start,
6988 char_u *expr_start,
6989 char_u *expr_end,
6990 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006991{
6992 char_u c1;
6993 char_u *retval = NULL;
6994 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006995
6996 if (expr_end == NULL || in_end == NULL)
6997 return NULL;
6998 *expr_start = NUL;
6999 *expr_end = NUL;
7000 c1 = *in_end;
7001 *in_end = NUL;
7002
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007003 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007004 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007005 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007006 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7007 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007008 if (retval != NULL)
7009 {
7010 STRCPY(retval, in_start);
7011 STRCAT(retval, temp_result);
7012 STRCAT(retval, expr_end + 1);
7013 }
7014 }
7015 vim_free(temp_result);
7016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007017 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007018 *expr_start = '{';
7019 *expr_end = '}';
7020
7021 if (retval != NULL)
7022 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007023 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007024 if (expr_start != NULL)
7025 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007026 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007027 temp_result = make_expanded_name(retval, expr_start,
7028 expr_end, temp_result);
7029 vim_free(retval);
7030 retval = temp_result;
7031 }
7032 }
7033
7034 return retval;
7035}
7036
7037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007044 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007045}
7046
7047/*
7048 * Return TRUE if character "c" can be used as the first character in a
7049 * variable or function name (excluding '{' and '}').
7050 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007052eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007053{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007054 return ASCII_ISALPHA(c) || c == '_';
7055}
7056
7057/*
7058 * Return TRUE if character "c" can be used as the first character of a
7059 * dictionary key.
7060 */
7061 int
7062eval_isdictc(int c)
7063{
7064 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065}
7066
7067/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007068 * Handle:
7069 * - expr[expr], expr[expr:expr] subscript
7070 * - ".name" lookup
7071 * - function call with Funcref variable: func(expr)
7072 * - method call: var->method()
7073 *
7074 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007075 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007076 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078handle_subscript(
7079 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007080 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007081 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007082 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007083 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007084{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007085 int evaluate = evalarg != NULL
7086 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007087 int ret = OK;
7088 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007089 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007090 int getnext;
7091 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007092
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007093 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007094 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007095 // When at the end of the line and ".name" or "->{" or "->X" follows in
7096 // the next line then consume the line break.
7097 p = eval_next_non_blank(*arg, evalarg, &getnext);
7098 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007099 && ((*p == '.'
7100 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7101 || rettv->v_type == VAR_CLASS
7102 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007103 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7104 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7105 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007106 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007107 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007108 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007109 check_white = FALSE;
7110 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007111
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007112 if (rettv->v_type == VAR_ANY)
7113 {
7114 char_u *exp_name;
7115 int cc;
7116 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007117 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007118 type_T *type;
7119
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007120 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007121 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007122 if (**arg != '.')
7123 {
7124 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007125 semsg(_(e_expected_dot_after_name_str),
7126 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007127 ret = FAIL;
7128 break;
7129 }
7130 ++*arg;
7131 if (IS_WHITE_OR_NUL(**arg))
7132 {
7133 if (verbose)
7134 emsg(_(e_no_white_space_allowed_after_dot));
7135 ret = FAIL;
7136 break;
7137 }
7138
7139 // isolate the name
7140 exp_name = *arg;
7141 while (eval_isnamec(**arg))
7142 ++*arg;
7143 cc = **arg;
7144 **arg = NUL;
7145
7146 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007147 evalarg == NULL ? NULL : evalarg->eval_cctx,
7148 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007149 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007150
7151 if (idx < 0 && ufunc == NULL)
7152 {
7153 ret = FAIL;
7154 break;
7155 }
7156 if (idx >= 0)
7157 {
7158 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7159 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7160
7161 copy_tv(sv->sv_tv, rettv);
7162 }
7163 else
7164 {
7165 rettv->v_type = VAR_FUNC;
7166 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7167 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007168 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007169 }
7170
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007171 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7172 || rettv->v_type == VAR_PARTIAL))
7173 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007174 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007175 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7176 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007177
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007178 // Stop the expression evaluation when immediately aborting on
7179 // error, or when an interrupt occurred or an exception was thrown
7180 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007181 if (aborting())
7182 {
7183 if (ret == OK)
7184 clear_tv(rettv);
7185 ret = FAIL;
7186 }
7187 dict_unref(selfdict);
7188 selfdict = NULL;
7189 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007190 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007191 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007192 if (in_vim9script())
7193 *arg = skipwhite(p + 2);
7194 else
7195 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007196 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007197 {
zeertzjqc481ad32023-03-11 16:18:51 +00007198 emsg(_(e_no_white_space_allowed_before_parenthesis));
7199 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007200 }
zeertzjqc481ad32023-03-11 16:18:51 +00007201 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7202 // expr->{lambda}() or expr->(lambda)()
7203 ret = eval_lambda(arg, rettv, evalarg, verbose);
7204 else
7205 // expr->name()
7206 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007207 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007208 // "." is ".name" lookup when we found a dict or when evaluating and
7209 // scriptversion is at least 2, where string concatenation is "..".
7210 else if (**arg == '['
7211 || (**arg == '.' && (rettv->v_type == VAR_DICT
7212 || (!evaluate
7213 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007214 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007215 {
7216 dict_unref(selfdict);
7217 if (rettv->v_type == VAR_DICT)
7218 {
7219 selfdict = rettv->vval.v_dict;
7220 if (selfdict != NULL)
7221 ++selfdict->dv_refcount;
7222 }
7223 else
7224 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007225 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007226 {
7227 clear_tv(rettv);
7228 ret = FAIL;
7229 }
7230 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007231 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7232 || rettv->v_type == VAR_OBJECT))
7233 {
7234 // class member: SomeClass.varname
7235 // class method: SomeClass.SomeMethod()
7236 // class constructor: SomeClass.new()
7237 // object member: someObject.varname
7238 // object method: someObject.SomeMethod()
7239 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7240 {
7241 clear_tv(rettv);
7242 ret = FAIL;
7243 }
7244 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007245 else
7246 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007247 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007249 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7250 // Don't do this when "Func" is already a partial that was bound
7251 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007252 if (selfdict != NULL
7253 && (rettv->v_type == VAR_FUNC
7254 || (rettv->v_type == VAR_PARTIAL
7255 && (rettv->vval.v_partial->pt_auto
7256 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007257 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007258
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007259 dict_unref(selfdict);
7260 return ret;
7261}
7262
7263/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007264 * Make a copy of an item.
7265 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007266 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007267 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7268 * reference to an already copied list/dict can be used.
7269 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007270 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007272item_copy(
7273 typval_T *from,
7274 typval_T *to,
7275 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007276 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278{
7279 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007280 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007284 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007285 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286 }
7287 ++recurse;
7288
7289 switch (from->v_type)
7290 {
7291 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007292 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 case VAR_STRING:
7294 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007295 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007296 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007297 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007298 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007299 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007300 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007301 case VAR_CLASS:
7302 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007303 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 copy_tv(from, to);
7305 break;
7306 case VAR_LIST:
7307 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007308 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007309 if (from->vval.v_list == NULL)
7310 to->vval.v_list = NULL;
7311 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007313 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007314 to->vval.v_list = from->vval.v_list->lv_copylist;
7315 ++to->vval.v_list->lv_refcount;
7316 }
7317 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007318 to->vval.v_list = list_copy(from->vval.v_list,
7319 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007320 if (to->vval.v_list == NULL)
7321 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007323 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007325 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 case VAR_DICT:
7327 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007328 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007329 if (from->vval.v_dict == NULL)
7330 to->vval.v_dict = NULL;
7331 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007333 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007334 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7335 ++to->vval.v_dict->dv_refcount;
7336 }
7337 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007338 to->vval.v_dict = dict_copy(from->vval.v_dict,
7339 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007340 if (to->vval.v_dict == NULL)
7341 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007343 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007344 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007346 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007347 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 }
7349 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007350 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351}
7352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 void
7354echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7355{
7356 char_u *tofree;
7357 char_u numbuf[NUMBUFLEN];
7358 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7359
7360 if (*atstart)
7361 {
7362 *atstart = FALSE;
7363 // Call msg_start() after eval1(), evaluating the expression
7364 // may cause a message to appear.
7365 if (with_space)
7366 {
7367 // Mark the saved text as finishing the line, so that what
7368 // follows is displayed on a new line when scrolling back
7369 // at the more prompt.
7370 msg_sb_eol();
7371 msg_start();
7372 }
7373 }
7374 else if (with_space)
7375 msg_puts_attr(" ", echo_attr);
7376
7377 if (p != NULL)
7378 for ( ; *p != NUL && !got_int; ++p)
7379 {
7380 if (*p == '\n' || *p == '\r' || *p == TAB)
7381 {
7382 if (*p != TAB && *needclr)
7383 {
7384 // remove any text still there from the command
7385 msg_clr_eos();
7386 *needclr = FALSE;
7387 }
7388 msg_putchar_attr(*p, echo_attr);
7389 }
7390 else
7391 {
7392 if (has_mbyte)
7393 {
7394 int i = (*mb_ptr2len)(p);
7395
7396 (void)msg_outtrans_len_attr(p, i, echo_attr);
7397 p += i - 1;
7398 }
7399 else
7400 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7401 }
7402 }
7403 vim_free(tofree);
7404}
7405
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * ":echo expr1 ..." print each argument separated with a space, add a
7408 * newline at the end.
7409 * ":echon expr1 ..." print each argument plain.
7410 */
7411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007416 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 int needclr = TRUE;
7418 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007419 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007420 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007421 evalarg_T evalarg;
7422
Bram Moolenaare707c882020-07-01 13:07:37 +02007423 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
7425 if (eap->skip)
7426 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007427 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007429 // If eval1() causes an error message the text from the command may
7430 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007431 need_clr_eos = needclr;
7432
Bram Moolenaar68db9962021-05-09 23:19:22 +02007433 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007434 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
7436 /*
7437 * Report the invalid expression unless the expression evaluation
7438 * has been cancelled due to an aborting error, an interrupt, or an
7439 * exception.
7440 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007441 if (!aborting() && did_emsg == did_emsg_before
7442 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007443 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007444 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 break;
7446 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007447 need_clr_eos = FALSE;
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007450 {
7451 if (rettv.v_type == VAR_VOID)
7452 {
7453 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7454 break;
7455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007457 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007459 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 arg = skipwhite(arg);
7461 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007462 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007463 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 if (eap->skip)
7466 --emsg_skip;
7467 else
7468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007469 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 if (needclr)
7471 msg_clr_eos();
7472 if (eap->cmdidx == CMD_echo)
7473 msg_end();
7474 }
7475}
7476
7477/*
7478 * ":echohl {name}".
7479 */
7480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007483 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
7485
7486/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007487 * Returns the :echo attribute
7488 */
7489 int
7490get_echo_attr(void)
7491{
7492 return echo_attr;
7493}
7494
7495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 * ":execute expr1 ..." execute the result of an expression.
7497 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007498 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007500 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 * Each gets spaces around each argument and a newline at the end for
7502 * echo commands
7503 */
7504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007505ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 int ret = OK;
7510 char_u *p;
7511 garray_T ga;
7512 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007513 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 ga_init2(&ga, 1, 80);
7516
7517 if (eap->skip)
7518 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007519 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007521 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007522 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
7525 if (!eap->skip)
7526 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007527 char_u buf[NUMBUFLEN];
7528
7529 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007530 {
7531 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7532 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007533 semsg(_(e_using_invalid_value_as_string_str),
7534 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007535 p = NULL;
7536 }
7537 else
7538 p = tv_get_string_buf(&rettv, buf);
7539 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007540 else
7541 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007542 if (p == NULL)
7543 {
7544 clear_tv(&rettv);
7545 ret = FAIL;
7546 break;
7547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 len = (int)STRLEN(p);
7549 if (ga_grow(&ga, len + 2) == FAIL)
7550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 ret = FAIL;
7553 break;
7554 }
7555 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 ga.ga_len += len;
7559 }
7560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 arg = skipwhite(arg);
7563 }
7564
7565 if (ret != FAIL && ga.ga_data != NULL)
7566 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007567 // use the first line of continuation lines for messages
7568 SOURCING_LNUM = start_lnum;
7569
Bram Moolenaar37fef162022-08-29 18:16:32 +01007570 if (eap->cmdidx == CMD_echomsg
7571 || eap->cmdidx == CMD_echowindow
7572 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007574 // Mark the already saved text as finishing the line, so that what
7575 // follows is displayed on a new line when scrolling back at the
7576 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007577 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007578 }
7579
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007580 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007581 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007582 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007583 out_flush();
7584 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007585 else if (eap->cmdidx == CMD_echowindow)
7586 {
7587#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007588 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007589#endif
7590 msg_attr(ga.ga_data, echo_attr);
7591#ifdef HAS_MESSAGE_WINDOW
7592 end_echowindow();
7593#endif
7594 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007595 else if (eap->cmdidx == CMD_echoconsole)
7596 {
7597 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7598 ui_write((char_u *)"\r\n", 2, TRUE);
7599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 else if (eap->cmdidx == CMD_echoerr)
7601 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007602 int save_did_emsg = did_emsg;
7603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007604 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007605 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (!force_abort)
7607 did_emsg = save_did_emsg;
7608 }
7609 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007610 {
7611 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7612
7613 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7614 sticky_cmdmod_flags = cmdmod.cmod_flags
7615 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007617 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007618 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
7621
7622 ga_clear(&ga);
7623
7624 if (eap->skip)
7625 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007626 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627}
7628
7629/*
7630 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7631 * "arg" points to the "&" or '+' when called, to "option" when returning.
7632 * Returns NULL when no option name found. Otherwise pointer to the char
7633 * after the option name.
7634 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007635 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007636find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637{
7638 char_u *p = *arg;
7639
7640 ++p;
7641 if (*p == 'g' && p[1] == ':')
7642 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007643 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 p += 2;
7645 }
7646 else if (*p == 'l' && p[1] == ':')
7647 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007648 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 p += 2;
7650 }
7651 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007652 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
7654 if (!ASCII_ISALPHA(*p))
7655 return NULL;
7656 *arg = p;
7657
7658 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007659 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 else
7661 while (ASCII_ISALPHA(*p))
7662 ++p;
7663 return p;
7664}
7665
7666/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007667 * Display script name where an item was last set.
7668 * Should only be invoked when 'verbose' is non-zero.
7669 */
7670 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007671last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007672{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007673 char_u *p;
7674
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007675 if (script_ctx.sc_sid == 0)
7676 return;
7677
7678 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7679 if (p == NULL)
7680 return;
7681
7682 verbose_enter();
7683 msg_puts(_("\n\tLast set from "));
7684 msg_puts((char *)p);
7685 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007686 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007687 msg_puts(_(line_msg));
7688 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007689 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007690 verbose_leave();
7691 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007692}
7693
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007694#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
7696/*
7697 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007698 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 * "flags" can be "g" to do a global substitute.
7700 * Returns an allocated string, NULL for error.
7701 */
7702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007703do_string_sub(
7704 char_u *str,
7705 char_u *pat,
7706 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007707 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007708 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
7710 int sublen;
7711 regmatch_T regmatch;
7712 int i;
7713 int do_all;
7714 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007715 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 garray_T ga;
7717 char_u *ret;
7718 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007719 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007721 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007723 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
7725 ga_init2(&ga, 1, 200);
7726
7727 do_all = (flags[0] == 'g');
7728
7729 regmatch.rm_ic = p_ic;
7730 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7731 if (regmatch.regprog != NULL)
7732 {
7733 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007734 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007737 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007738 if (regmatch.startp[0] == regmatch.endp[0])
7739 {
7740 if (zero_width == regmatch.startp[0])
7741 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007742 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007743 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007744 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7745 (size_t)i);
7746 ga.ga_len += i;
7747 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007748 continue;
7749 }
7750 zero_width = regmatch.startp[0];
7751 }
7752
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 /*
7754 * Get some space for a temporary buffer to do the substitution
7755 * into. It will contain:
7756 * - The text up to where the match is.
7757 * - The substituted text.
7758 * - The text after the match.
7759 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007760 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007761 if (sublen <= 0)
7762 {
7763 ga_clear(&ga);
7764 break;
7765 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007766 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7768 {
7769 ga_clear(&ga);
7770 break;
7771 }
7772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007773 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 i = (int)(regmatch.startp[0] - tail);
7775 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007776 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007777 (void)vim_regsub(&regmatch, sub, expr,
7778 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7779 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007781 tail = regmatch.endp[0];
7782 if (*tail == NUL)
7783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (!do_all)
7785 break;
7786 }
7787
7788 if (ga.ga_data != NULL)
7789 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7790
Bram Moolenaar473de612013-06-08 18:19:48 +02007791 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793
7794 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7795 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007796 if (p_cpo == empty_option)
7797 p_cpo = save_cpo;
7798 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007800 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007801 // If it's still empty it was changed and restored, need to restore in
7802 // the complicated way.
7803 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007804 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007805 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807
7808 return ret;
7809}