blob: 2647e7a5911540505c0b2705634abffca8143eb0 [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/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 * Get an lval: variable, Dict item or List item that can be assigned a value
1150 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1151 * "name.key", "name.key[expr]" etc.
1152 * Indexing only works if "name" is an existing List or Dictionary.
1153 * "name" points to the start of the name.
1154 * If "rettv" is not NULL it points to the value to be assigned.
1155 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1156 * wrong; must end in space or cmd separator.
1157 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001158 * flags:
1159 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001160 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 * GLV_NO_AUTOLOAD: do not use script autoloading
1162 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001164 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001167 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001168get_lval(
1169 char_u *name,
1170 typval_T *rettv,
1171 lval_T *lp,
1172 int unlet,
1173 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001174 int flags, // GLV_ values
1175 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001176{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 char_u *expr_start, *expr_end;
1179 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 dictitem_T *v;
1181 typval_T var1;
1182 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001186 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001187 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001188 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001189 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001190 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001191
Ernie Raelee865f32023-09-29 19:53:55 +02001192#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001193 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001194 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001195 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001196 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1197 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001198 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001199 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001200 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001201#endif
1202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001204 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001206 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001208 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001210 lp->ll_name_end = find_name_end(name, NULL, NULL,
1211 FNE_INCL_BR | fne_flags);
1212 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 }
1214
Bram Moolenaara749a422022-02-12 19:52:25 +00001215 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001216 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001217 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1218 {
1219 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1220 return NULL;
1221 }
1222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001223 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001224 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 if (expr_start != NULL)
1227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001229 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 && *p != '[' && *p != '.')
1231 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001232 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 return NULL;
1234 }
1235
1236 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1237 if (lp->ll_exp_name == NULL)
1238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // Report an invalid expression in braces, unless the
1240 // expression evaluation has been cancelled due to an
1241 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (!aborting() && !quiet)
1243 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001244 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001245 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return NULL;
1247 }
1248 }
1249 lp->ll_name = lp->ll_exp_name;
1250 }
1251 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 lp->ll_name = name;
1254
Bram Moolenaar4525a572022-02-13 11:57:33 +00001255 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001257 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001258 // However, "g:[key]" is indexing a dictionary.
1259 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001260 {
1261 --p;
1262 lp->ll_name_end = p;
1263 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001264 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001265 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001266 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001267
Bram Moolenaar9510d222022-09-11 15:14:05 +01001268 if (is_scoped_variable(name))
1269 {
1270 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1271 return NULL;
1272 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001273 if (VIM_ISWHITE(*p))
1274 {
1275 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1276 return NULL;
1277 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001278 if (tp == p + 1 && !quiet)
1279 {
1280 semsg(_(e_white_space_required_after_str_str), ":", p);
1281 return NULL;
1282 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001283 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001284 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001285 semsg(_(e_using_type_not_in_script_context_str), p);
1286 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001287 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001288 if (vim9script && (flags & GLV_NO_DECL) &&
1289 !(flags & GLV_FOR_LOOP))
1290 {
1291 // Using a type and not in a "var" declaration.
1292 semsg(_(e_trailing_characters_str), p);
1293 return NULL;
1294 }
1295
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001296
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001297 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001298 lp->ll_type = parse_type(&tp,
1299 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1300 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001301 if (lp->ll_type == NULL && !quiet)
1302 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001303 lp->ll_name_end = tp;
1304 }
Ernie Raelee865f32023-09-29 19:53:55 +02001305 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 }
1307 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001308 if (lp->ll_name == NULL)
1309 return p;
1310
Bram Moolenaar62aec932022-01-29 21:45:34 +00001311 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001312 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001313 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001314
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001315 if (import != NULL)
1316 {
1317 ufunc_T *ufunc;
1318 type_T *type;
1319
Bram Moolenaar753885b2022-08-24 16:30:36 +01001320 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001321 lp->ll_sid = import->imp_sid;
1322 lp->ll_name = skipwhite(p + 1);
1323 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1324 lp->ll_name_end = p;
1325
1326 // check the item is exported
1327 cc = *p;
1328 *p = NUL;
1329 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001330 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001331 {
1332 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001333 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001334 }
1335 *p = cc;
1336 }
1337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
Ernie Rael0f058d12023-10-14 11:25:04 +02001339 // Without [idx] or .key we are done.
1340 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001341 {
1342 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001343 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346
Ernie Rael0f058d12023-10-14 11:25:04 +02001347 if (vim9script && lval_root != NULL)
1348 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001349 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001350 {
1351 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001352 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001353 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001354 }
1355 else
1356 {
1357 cc = *p;
1358 *p = NUL;
1359 // When we would write to the variable pass &ht and prevent autoload.
1360 writing = !(flags & GLV_READ_ONLY);
1361 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001362 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001363 if (v == NULL && !quiet)
1364 semsg(_(e_undefined_variable_str), lp->ll_name);
1365 *p = cc;
1366 if (v == NULL)
1367 return NULL;
1368 lp->ll_tv = &v->di_tv;
1369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370
Bram Moolenaar4525a572022-02-13 11:57:33 +00001371 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001372 {
1373 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001374 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001375 return NULL;
1376 }
1377
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378 /*
1379 * Loop until no more [idx] or .key is following.
1380 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001381 var1.v_type = VAR_UNKNOWN;
1382 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001383 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001385 vartype_T v_type = lp->ll_tv->v_type;
1386
1387 if (*p == '.' && v_type != VAR_DICT
1388 && v_type != VAR_OBJECT
1389 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001390 {
1391 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001392 semsg(_(e_dot_not_allowed_after_str_str),
1393 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001394 return NULL;
1395 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001396 if (v_type != VAR_LIST
1397 && v_type != VAR_DICT
1398 && v_type != VAR_BLOB
1399 && v_type != VAR_OBJECT
1400 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001402 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001403 semsg(_(e_index_not_allowed_after_str_str),
1404 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001407
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001408 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001409 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001410 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001411 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001412 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001413 r = rettv_blob_alloc(lp->ll_tv);
1414 if (r == FAIL)
1415 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001420 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001423#ifdef LOG_LOCKVAR
1424 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1425 vartype_name(v_type));
1426#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427
Bram Moolenaar4525a572022-02-13 11:57:33 +00001428 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001429 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001430 && lp->ll_tv == &v->di_tv
1431 && ht != NULL && ht == get_script_local_ht())
1432 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001433 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001434
1435 // Vim9 script local variable: get the type
1436 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001437 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001438 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001439#ifdef LOG_LOCKVAR
1440 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1441 vartype_name(lp->ll_valtype->tt_type));
1442#endif
1443 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001444 }
1445
Bram Moolenaar8c711452005-01-14 21:53:12 +00001446 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001447 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001448 {
1449 key = p + 1;
1450 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1451 ;
1452 if (len == 0)
1453 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001455 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001457 }
1458 p = key + len;
1459 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001460 else
1461 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001462 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001463 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001464 if (*p == ':')
1465 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001466 else
1467 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001468 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001469 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001471 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001473 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001474 clear_tv(&var1);
1475 return NULL;
1476 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001477 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001478 }
1479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001480 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001481 if (*p == ':')
1482 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001483 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001486 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001487 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001489 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001490 if (rettv != NULL
1491 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001492 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001493 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001494 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001497 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001498 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001499 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001500 }
1501 p = skipwhite(p + 1);
1502 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001504 else
1505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001507 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001508 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001510 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001511 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001513 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001514 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001515 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001516 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001517 clear_tv(&var2);
1518 return NULL;
1519 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001520 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001523 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001525
Bram Moolenaar8c711452005-01-14 21:53:12 +00001526 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001528 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001529 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001530 clear_tv(&var1);
1531 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001532 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001533 }
1534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001535 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001536 ++p;
1537 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001538#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001539 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001540 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1541 empty1 ? ":" : (char*)tv_get_string(&var1));
1542 else
1543 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001544#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001545
Bram Moolenaard505d172022-12-18 21:42:55 +00001546 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001547 {
1548 if (len == -1)
1549 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001550 // "[key]": get key from "var1"
1551 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001552 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001553 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001554 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001555 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001556 }
1557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001558 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001559 lp->ll_object = NULL;
1560 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001561
1562 // a NULL dict is equivalent with an empty dict
1563 if (lp->ll_tv->vval.v_dict == NULL)
1564 {
1565 lp->ll_tv->vval.v_dict = dict_alloc();
1566 if (lp->ll_tv->vval.v_dict == NULL)
1567 {
1568 clear_tv(&var1);
1569 return NULL;
1570 }
1571 ++lp->ll_tv->vval.v_dict->dv_refcount;
1572 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001575 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // When assigning to a scope dictionary check that a function and
1578 // variable name is valid (only variable name unless it is l: or
1579 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001580 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001581 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001582 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001583
1584 if (len != -1)
1585 {
1586 prevval = key[len];
1587 key[len] = NUL;
1588 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001589 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001590 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001591 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001592 && (rettv->v_type == VAR_FUNC
1593 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001594 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001595 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001596 if (len != -1)
1597 key[len] = prevval;
1598 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001599 {
1600 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001601 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001602 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001603 }
1604
Bram Moolenaar10c65862020-10-08 21:16:42 +02001605 if (lp->ll_valtype != NULL)
1606 // use the type of the member
1607 lp->ll_valtype = lp->ll_valtype->tt_member;
1608
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001609 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001610 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001611 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001612 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001613 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001614 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001615 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001616 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001617 return NULL;
1618 }
1619
Bram Moolenaar31b81602019-02-10 22:14:27 +01001620 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001621 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001623 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001624 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001625 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001627 }
1628 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001629 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001630 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001631 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001632 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001633 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001634 p = NULL;
1635 break;
1636 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001637 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001638 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001639 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1640 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001641 {
1642 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001643 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001644 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001645
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001646 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001647 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001648 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001649 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001650 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001651 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1652
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001653 /*
1654 * Get the number and item for the only or first index of the List.
1655 */
1656 if (empty1)
1657 lp->ll_n1 = 0;
1658 else
1659 // is number or string
1660 lp->ll_n1 = (long)tv_get_number(&var1);
1661 clear_tv(&var1);
1662
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001663 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001665 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001666 return NULL;
1667 }
1668 if (lp->ll_range && !lp->ll_empty2)
1669 {
1670 lp->ll_n2 = (long)tv_get_number(&var2);
1671 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001672 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1673 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001674 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001675 }
1676 lp->ll_blob = lp->ll_tv->vval.v_blob;
1677 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001678 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001680 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001681 {
1682 /*
1683 * Get the number and item for the only or first index of the List.
1684 */
1685 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001686 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001687 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001688 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001689 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001690 clear_tv(&var1);
1691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001693 lp->ll_object = NULL;
1694 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001695 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001696 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1697 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001698 if (lp->ll_li == NULL)
1699 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001700 clear_tv(&var2);
1701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001702 }
1703
Bram Moolenaar10c65862020-10-08 21:16:42 +02001704 if (lp->ll_valtype != NULL)
1705 // use the type of the member
1706 lp->ll_valtype = lp->ll_valtype->tt_member;
1707
Bram Moolenaar8c711452005-01-14 21:53:12 +00001708 /*
1709 * May need to find the item or absolute index for the second
1710 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001711 * When no index given: "lp->ll_empty2" is TRUE.
1712 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001713 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001716 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001717 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001718 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001719 if (check_range_index_two(lp->ll_list,
1720 &lp->ll_n1, lp->ll_li,
1721 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001722 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001723 }
1724
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001725 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001726 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001727 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001728 {
Ernie Raelee865f32023-09-29 19:53:55 +02001729 lp->ll_dict = NULL;
1730 lp->ll_list = NULL;
1731
1732 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001733 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001734 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001735 if (lp->ll_tv->vval.v_object == NULL)
1736 {
1737 if (!quiet)
1738 emsg(_(e_using_null_object));
1739 return NULL;
1740 }
Ernie Raelee865f32023-09-29 19:53:55 +02001741 cl = lp->ll_tv->vval.v_object->obj_class;
1742 lp->ll_object = lp->ll_tv->vval.v_object;
1743 }
1744 else
1745 {
1746 cl = lp->ll_tv->vval.v_class;
1747 lp->ll_object = NULL;
1748 }
1749 lp->ll_class = cl;
1750
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001751 // TODO: what if class is NULL?
1752 if (cl != NULL)
1753 {
1754 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001755
1756 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001757 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001758 // First look for a function with this name.
1759 // round 1: class functions (skipped for an object)
1760 // round 2: object methods
1761 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001762 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001763 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001764 int m_idx;
1765 ufunc_T *fp;
1766
1767 fp = method_lookup(cl,
1768 round == 1 ? VAR_CLASS : VAR_OBJECT,
1769 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001770 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001771 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001772 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001773 lp->ll_ufunc = fp;
1774 lp->ll_valtype = fp->uf_func_type;
1775 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001776 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001777 }
1778 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001779
1780 if (lp->ll_valtype == NULL)
1781 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001782 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001783 ocmember_T *om
1784 = member_lookup(cl, v_type, key, p - key, &m_idx);
1785 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001786 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001787 {
Ernie Rael64885642023-10-04 20:16:22 +02001788 if (get_lval_check_access(cl_exec, cl, om,
1789 p, flags) == FAIL)
1790 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001791
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001792 // When lhs is used to modify the variable, check it is
1793 // not a read-only variable.
1794 if ((flags & GLV_READ_ONLY) == 0
1795 && (*p != '.' && *p != '[')
1796 && oc_var_check_ro(cl, om))
1797 return NULL;
1798
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001799 lp->ll_valtype = om->ocm_type;
1800
1801 if (v_type == VAR_OBJECT)
1802 lp->ll_tv = ((typval_T *)(
1803 lp->ll_tv->vval.v_object + 1)) + m_idx;
1804 else
1805 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001806 }
1807 }
1808
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001809 if (lp->ll_valtype == NULL)
1810 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001811 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001812 return NULL;
1813 }
1814 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 }
1817
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001818 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001820 return p;
1821}
1822
1823/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001824 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001825 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001828{
1829 vim_free(lp->ll_exp_name);
1830 vim_free(lp->ll_newkey);
1831}
1832
1833/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001834 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001836 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1837 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001838 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840set_var_lval(
1841 lval_T *lp,
1842 char_u *endp,
1843 typval_T *rettv,
1844 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001845 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1846 char_u *op,
1847 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848{
1849 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001851
1852 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001854 cc = *endp;
1855 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001856 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001857 return;
1858
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001859 if (lp->ll_blob != NULL)
1860 {
1861 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001862
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001863 if (op != NULL && *op != '=')
1864 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001865 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001866 return;
1867 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001868 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001869 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001870
1871 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1872 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001873 if (lp->ll_empty2)
1874 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1875
Bram Moolenaar68452172021-04-12 21:21:02 +02001876 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1877 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001878 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001879 }
1880 else
1881 {
1882 val = (int)tv_get_number_chk(rettv, &error);
1883 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001884 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001885 }
1886 }
1887 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001889 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001891 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1892 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001893 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001894 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001895 *endp = cc;
1896 return;
1897 }
1898
Bram Moolenaarff697e62019-02-12 22:28:33 +01001899 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001900 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001901 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001902 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001903 {
Ernie Raele75fde62023-12-21 17:18:54 +01001904 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001905 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001906 clear_tv(&tv);
1907 return;
1908 }
1909
Bram Moolenaar79518e22017-02-17 16:31:35 +01001910 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001911 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1912 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001913 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001914 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01001915 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001916 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001919 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001920 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001921 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1922 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001923 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001924 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001925 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001926 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001927 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001929 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001930 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001931 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001932 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 else if (lp->ll_range)
1934 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001935 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1936 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001937 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001938 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001939 return;
1940 }
1941
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001942 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1943 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 }
1945 else
1946 {
1947 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001948 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001950 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1951 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001952 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001953 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001954 return;
1955 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001956
1957 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001958 && check_typval_arg_type(lp->ll_valtype, rettv,
1959 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001960 return;
1961
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (lp->ll_newkey != NULL)
1963 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 if (op != NULL && *op != '=')
1965 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001966 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 return;
1968 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001969 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1970 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001971 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001974 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 if (di == NULL)
1976 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001977 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1978 {
1979 vim_free(di);
1980 return;
1981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 else if (op != NULL && *op != '=')
1985 {
1986 tv_op(lp->ll_tv, rettv, op);
1987 return;
1988 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 /*
1993 * Assign the value to the variable or list item.
1994 */
1995 if (copy)
1996 copy_tv(rettv, lp->ll_tv);
1997 else
1998 {
1999 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002000 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 }
2003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004}
2005
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002007 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2008 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 * Returns OK or FAIL.
2010 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002012tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002014 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 char_u numbuf[NUMBUFLEN];
2016 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002017 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018
Ernie Raele75fde62023-12-21 17:18:54 +01002019 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002020 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002021 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002022 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002023 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2024 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 {
2026 switch (tv1->v_type)
2027 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002028 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002029 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 case VAR_DICT:
2032 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002033 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002034 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002035 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002036 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002037 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002038 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002039 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002041 case VAR_CLASS:
2042 case VAR_TYPEALIAS:
2043 check_typval_is_value(tv1);
2044 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002046 case VAR_BLOB:
2047 if (*op != '+' || tv2->v_type != VAR_BLOB)
2048 break;
2049 // BLOB += BLOB
2050 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2051 {
2052 blob_T *b1 = tv1->vval.v_blob;
2053 blob_T *b2 = tv2->vval.v_blob;
2054 int i, len = blob_len(b2);
2055 for (i = 0; i < len; i++)
2056 ga_append(&b1->bv_ga, blob_get(b2, i));
2057 }
2058 return OK;
2059
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 case VAR_LIST:
2061 if (*op != '+' || tv2->v_type != VAR_LIST)
2062 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002063 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002064 if (tv2->vval.v_list != NULL)
2065 {
2066 if (tv1->vval.v_list == NULL)
2067 {
2068 tv1->vval.v_list = tv2->vval.v_list;
2069 ++tv1->vval.v_list->lv_refcount;
2070 }
2071 else
2072 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002074 return OK;
2075
2076 case VAR_NUMBER:
2077 case VAR_STRING:
2078 if (tv2->v_type == VAR_LIST)
2079 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002080 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002081 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002082 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002083 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002084 if (tv2->v_type == VAR_FLOAT)
2085 {
2086 float_T f = n;
2087
Bram Moolenaarff697e62019-02-12 22:28:33 +01002088 if (*op == '%')
2089 break;
2090 switch (*op)
2091 {
2092 case '+': f += tv2->vval.v_float; break;
2093 case '-': f -= tv2->vval.v_float; break;
2094 case '*': f *= tv2->vval.v_float; break;
2095 case '/': f /= tv2->vval.v_float; break;
2096 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002097 clear_tv(tv1);
2098 tv1->v_type = VAR_FLOAT;
2099 tv1->vval.v_float = f;
2100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002102 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002103 switch (*op)
2104 {
2105 case '+': n += tv_get_number(tv2); break;
2106 case '-': n -= tv_get_number(tv2); break;
2107 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002108 case '/': n = num_divide(n, tv_get_number(tv2),
2109 &failed); break;
2110 case '%': n = num_modulus(n, tv_get_number(tv2),
2111 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002112 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002113 clear_tv(tv1);
2114 tv1->v_type = VAR_NUMBER;
2115 tv1->vval.v_number = n;
2116 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117 }
2118 else
2119 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002120 if (tv2->v_type == VAR_FLOAT)
2121 break;
2122
Bram Moolenaarff697e62019-02-12 22:28:33 +01002123 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002124 s = tv_get_string(tv1);
2125 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 clear_tv(tv1);
2127 tv1->v_type = VAR_STRING;
2128 tv1->vval.v_string = s;
2129 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002130 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002131
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002132 case VAR_FLOAT:
2133 {
2134 float_T f;
2135
Bram Moolenaarff697e62019-02-12 22:28:33 +01002136 if (*op == '%' || *op == '.'
2137 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002138 && tv2->v_type != VAR_NUMBER
2139 && tv2->v_type != VAR_STRING))
2140 break;
2141 if (tv2->v_type == VAR_FLOAT)
2142 f = tv2->vval.v_float;
2143 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002144 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002145 switch (*op)
2146 {
2147 case '+': tv1->vval.v_float += f; break;
2148 case '-': tv1->vval.v_float -= f; break;
2149 case '*': tv1->vval.v_float *= f; break;
2150 case '/': tv1->vval.v_float /= f; break;
2151 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002152 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002153 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002154 }
2155 }
2156
Ernie Raele75fde62023-12-21 17:18:54 +01002157 if (check_typval_is_value(tv2) == OK)
2158 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 return FAIL;
2160}
2161
2162/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163 * Evaluate the expression used in a ":for var in expr" command.
2164 * "arg" points to "var".
2165 * Set "*errp" to TRUE for an error, FALSE otherwise;
2166 * Return a pointer that holds the info. Null when there is an error.
2167 */
2168 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169eval_for_line(
2170 char_u *arg,
2171 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002172 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002173 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174{
Bram Moolenaar33570922005-01-25 22:26:29 +00002175 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002176 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002177 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 typval_T tv;
2179 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002180 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002182 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002184 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002185 if (fi == NULL)
2186 return NULL;
2187
Bram Moolenaar404557e2021-07-05 21:41:48 +02002188 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2189 &fi->fi_semicolon, FALSE);
2190 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002191 return fi;
2192
Bram Moolenaar404557e2021-07-05 21:41:48 +02002193 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002194 if (expr[0] != 'i' || expr[1] != 'n'
2195 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002196 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002197 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2198 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2199 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002200 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002201 return fi;
2202 }
2203
2204 if (skip)
2205 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002206 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2207 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002208 {
2209 *errp = FALSE;
2210 if (!skip)
2211 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002212 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002213 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002214 l = tv.vval.v_list;
2215 if (l == NULL)
2216 {
2217 // a null list is like an empty list: do nothing
2218 clear_tv(&tv);
2219 }
2220 else
2221 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002223 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002225 // No need to increment the refcount, it's already set for
2226 // the list being used in "tv".
2227 fi->fi_list = l;
2228 list_add_watch(l, &fi->fi_lw);
2229 fi->fi_lw.lw_item = l->lv_first;
2230 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002231 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002232 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002233 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002234 fi->fi_bi = 0;
2235 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002236 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002237 typval_T btv;
2238
2239 // Make a copy, so that the iteration still works when the
2240 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002242 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002243 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002244 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002245 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002246 else if (tv.v_type == VAR_STRING)
2247 {
2248 fi->fi_byte_idx = 0;
2249 fi->fi_string = tv.vval.v_string;
2250 tv.vval.v_string = NULL;
2251 if (fi->fi_string == NULL)
2252 fi->fi_string = vim_strsave((char_u *)"");
2253 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002254 else
2255 {
Sean Dewar80d73952021-08-04 19:25:54 +02002256 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002257 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002258 }
2259 }
2260 }
2261 if (skip)
2262 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002263 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002264
2265 return fi;
2266}
2267
2268/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002269 * Used when looping over a :for line, skip the "in expr" part.
2270 */
2271 void
2272skip_for_lines(void *fi_void, evalarg_T *evalarg)
2273{
2274 forinfo_T *fi = (forinfo_T *)fi_void;
2275 int i;
2276
2277 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002278 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002279}
2280
2281/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282 * Use the first item in a ":for" list. Advance to the next.
2283 * Assign the values to the variable (list). "arg" points to the first one.
2284 * Return TRUE when a valid item was found, FALSE when at end of list or
2285 * something wrong.
2286 */
2287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002288next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002289{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002290 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002291 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002292 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002293 ? (ASSIGN_FINAL
2294 // first round: error if variable exists
2295 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002296 | ASSIGN_NO_MEMBER_TYPE
2297 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002298 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002300 int skip_assign = in_vim9script() && arg[0] == '_'
2301 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002302
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002303 if (fi->fi_blob != NULL)
2304 {
2305 typval_T tv;
2306
2307 if (fi->fi_bi >= blob_len(fi->fi_blob))
2308 return FALSE;
2309 tv.v_type = VAR_NUMBER;
2310 tv.v_lock = VAR_FIXED;
2311 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2312 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002313 if (skip_assign)
2314 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002315 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002316 fi->fi_varcount, flag, NULL) == OK;
2317 }
2318
2319 if (fi->fi_string != NULL)
2320 {
2321 typval_T tv;
2322 int len;
2323
2324 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2325 if (len == 0)
2326 return FALSE;
2327 tv.v_type = VAR_STRING;
2328 tv.v_lock = VAR_FIXED;
2329 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2330 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002331 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002332 if (skip_assign)
2333 result = TRUE;
2334 else
2335 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002336 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002337 vim_free(tv.vval.v_string);
2338 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002339 }
2340
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002341 item = fi->fi_lw.lw_item;
2342 if (item == NULL)
2343 result = FALSE;
2344 else
2345 {
2346 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002347 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002348 if (skip_assign)
2349 result = TRUE;
2350 else
2351 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002352 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002353 }
2354 return result;
2355}
2356
2357/*
2358 * Free the structure used to store info used by ":for".
2359 */
2360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002361free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362{
Bram Moolenaar33570922005-01-25 22:26:29 +00002363 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002364
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002365 if (fi == NULL)
2366 return;
2367 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002368 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002370 list_unref(fi->fi_list);
2371 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002372 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002373 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002374 else
2375 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376 vim_free(fi);
2377}
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380set_context_for_expression(
2381 expand_T *xp,
2382 char_u *arg,
2383 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002385 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002387 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002389 if (cmdidx == CMD_let || cmdidx == CMD_var
2390 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002391 {
2392 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002393 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002395 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002396 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002397 {
2398 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002399 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002400 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002401 break;
2402 }
2403 return;
2404 }
2405 }
2406 else
2407 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2408 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 while ((xp->xp_pattern = vim_strpbrk(arg,
2410 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2411 {
2412 c = *xp->xp_pattern;
2413 if (c == '&')
2414 {
2415 c = xp->xp_pattern[1];
2416 if (c == '&')
2417 {
2418 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002419 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002424 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2425 xp->xp_pattern += 2;
2426
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 else if (c == '$')
2430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002431 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 xp->xp_context = EXPAND_ENV_VARS;
2433 }
2434 else if (c == '=')
2435 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002436 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 xp->xp_context = EXPAND_EXPRESSION;
2438 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002439 else if (c == '#'
2440 && xp->xp_context == EXPAND_EXPRESSION)
2441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002442 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002443 break;
2444 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002445 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 && xp->xp_context == EXPAND_FUNCTIONS
2447 && vim_strchr(xp->xp_pattern, '(') == NULL)
2448 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002449 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 break;
2451 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002452 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002454 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2457 if (c == '\\' && xp->xp_pattern[1] != NUL)
2458 ++xp->xp_pattern;
2459 xp->xp_context = EXPAND_NOTHING;
2460 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002461 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002463 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2465 /* skip */ ;
2466 xp->xp_context = EXPAND_NOTHING;
2467 }
2468 else if (c == '|')
2469 {
2470 if (xp->xp_pattern[1] == '|')
2471 {
2472 ++xp->xp_pattern;
2473 xp->xp_context = EXPAND_EXPRESSION;
2474 }
2475 else
2476 xp->xp_context = EXPAND_COMMANDS;
2477 }
2478 else
2479 xp->xp_context = EXPAND_EXPRESSION;
2480 }
2481 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002482 // Doesn't look like something valid, expand as an expression
2483 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002484 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 arg = xp->xp_pattern;
2486 if (*arg != NUL)
2487 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2488 /* skip */ ;
2489 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002490
2491 // ":exe one two" completes "two"
2492 if ((cmdidx == CMD_execute
2493 || cmdidx == CMD_echo
2494 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002495 || cmdidx == CMD_echomsg
2496 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002497 && xp->xp_context == EXPAND_EXPRESSION)
2498 {
2499 for (;;)
2500 {
2501 char_u *n = skiptowhite(arg);
2502
2503 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2504 break;
2505 arg = skipwhite(n);
2506 }
2507 }
2508
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 xp->xp_pattern = arg;
2510}
2511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002513 * Return TRUE if "pat" matches "text".
2514 * Does not use 'cpo' and always uses 'magic'.
2515 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002516 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002517pattern_match(char_u *pat, char_u *text, int ic)
2518{
2519 int matches = FALSE;
2520 char_u *save_cpo;
2521 regmatch_T regmatch;
2522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002523 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002524 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002525 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002526 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2527 if (regmatch.regprog != NULL)
2528 {
2529 regmatch.rm_ic = ic;
2530 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2531 vim_regfree(regmatch.regprog);
2532 }
2533 p_cpo = save_cpo;
2534 return matches;
2535}
2536
2537/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002538 * Handle a name followed by "(". Both for just "name(arg)" and for
2539 * "expr->name(arg)".
2540 * Returns OK or FAIL.
2541 */
2542 static int
2543eval_func(
2544 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002545 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002546 char_u *name,
2547 int name_len,
2548 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002549 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002550 typval_T *basetv) // "expr" for "expr->name(arg)"
2551{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002552 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002553 char_u *s = name;
2554 int len = name_len;
2555 partial_T *partial;
2556 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002557 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002558 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002559
2560 if (!evaluate)
2561 check_vars(s, len);
2562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002563 // If "s" is the name of a variable of type VAR_FUNC
2564 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002565 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002566 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // Need to make a copy, in case evaluating the arguments makes
2569 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002570 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002571 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002572 ret = FAIL;
2573 else
2574 {
2575 funcexe_T funcexe;
2576
2577 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002578 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002579 funcexe.fe_firstline = curwin->w_cursor.lnum;
2580 funcexe.fe_lastline = curwin->w_cursor.lnum;
2581 funcexe.fe_evaluate = evaluate;
2582 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002583 if (partial != NULL)
2584 {
2585 funcexe.fe_object = partial->pt_obj;
2586 if (funcexe.fe_object != NULL)
2587 ++funcexe.fe_object->obj_refcount;
2588 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002589 funcexe.fe_basetv = basetv;
2590 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002591 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002592 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002593 }
2594 vim_free(s);
2595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002596 // If evaluate is FALSE rettv->v_type was not set in
2597 // get_func_tv, but it's needed in handle_subscript() to parse
2598 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002599 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2600 {
2601 rettv->vval.v_string = NULL;
2602 rettv->v_type = VAR_FUNC;
2603 }
2604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002605 // Stop the expression evaluation when immediately
2606 // aborting on error, or when an interrupt occurred or
2607 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002608 if (evaluate && aborting())
2609 {
2610 if (ret == OK)
2611 clear_tv(rettv);
2612 ret = FAIL;
2613 }
2614 return ret;
2615}
2616
2617/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002618 * After a NL, skip over empty lines and comment-only lines.
2619 */
2620 static char_u *
2621newline_skip_comments(char_u *arg)
2622{
2623 char_u *p = arg + 1;
2624
2625 for (;;)
2626 {
2627 p = skipwhite(p);
2628
2629 if (*p == NUL)
2630 break;
2631 if (vim9_comment_start(p))
2632 {
2633 char_u *nl = vim_strchr(p, NL);
2634
2635 if (nl == NULL)
2636 break;
2637 p = nl;
2638 }
2639 if (*p != NL)
2640 break;
2641 ++p; // skip another NL
2642 }
2643 return p;
2644}
2645
2646/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002647 * Get the next line source line without advancing. But do skip over comment
2648 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002649 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002650 */
2651 static char_u *
2652getline_peek_skip_comments(evalarg_T *evalarg)
2653{
2654 for (;;)
2655 {
2656 char_u *next = getline_peek(evalarg->eval_getline,
2657 evalarg->eval_cookie);
2658 char_u *p;
2659
2660 if (next == NULL)
2661 break;
2662 p = skipwhite(next);
2663 if (*p != NUL && !vim9_comment_start(p))
2664 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002665 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002666 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002667 }
2668 return NULL;
2669}
2670
2671/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002672 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2673 * comment) and there is a next line, return the next line (skipping blanks)
2674 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002675 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2676 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002677 * "arg" must point somewhere inside a line, not at the start.
2678 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002679 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2681{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002682 char_u *p = skipwhite(arg);
2683
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002684 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002685 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002687 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2688 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002689 && (*p == NUL || *p == NL
2690 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002692 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002693
Bram Moolenaare442d592022-05-05 12:20:28 +01002694 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002695 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002696 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002697 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002698 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002699 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002700
Bram Moolenaar9d489562020-07-30 20:08:50 +02002701 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002702 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002703 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002704 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 }
2706 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002707 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708}
2709
2710/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002711 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002712 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002713 *
2714 * If "arg" is not NULL, then the caller should assign the return value to
2715 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002716 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002717 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002718eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002719{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002720 garray_T *gap = &evalarg->eval_ga;
2721 char_u *line;
2722
Bram Moolenaar39be4982022-05-06 21:51:50 +01002723 if (arg != NULL)
2724 {
2725 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002726 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002727 // Truncate before a trailing comment, so that concatenating the lines
2728 // won't turn the rest into a comment.
2729 if (*skipwhite(arg) == '#')
2730 *arg = NUL;
2731 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002732
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002733 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002734 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2735 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002736 else
2737 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002738 if (line == NULL)
2739 return NULL;
2740
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002741 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002742 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2743 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002744 char_u *p = skipwhite(line);
2745
2746 // Going to concatenate the lines after parsing. For an empty or
2747 // comment line use an empty string.
2748 if (*p == NUL || vim9_comment_start(p))
2749 {
2750 vim_free(line);
2751 line = vim_strsave((char_u *)"");
2752 }
2753
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002754 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2755 ++gap->ga_len;
2756 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002757 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002758 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002759 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002760 evalarg->eval_tofree = line;
2761 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002762
2763 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002764 // line. The caller assigns the return value to "arg".
2765 // If "arg" is NULL, then the return value is discarded. In that case,
2766 // "arg" still points to the previous line. So don't reset
2767 // "eval_using_cmdline".
2768 if (arg != NULL)
2769 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002770 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002771}
2772
Bram Moolenaare6b53242020-07-01 17:28:33 +02002773/*
2774 * Call eval_next_non_blank() and get the next line if needed.
2775 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002776 char_u *
2777skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2778{
2779 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002780 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002781
Bram Moolenaar962d7212020-07-04 14:15:00 +02002782 if (evalarg == NULL)
2783 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002784 eval_next_non_blank(p, evalarg, &getnext);
2785 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002786 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002787 return p;
2788}
2789
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002790/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002791 * The "eval" functions have an "evalarg" argument: When NULL or
2792 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2793 * parsed but not executed. The functions may return OK, but the rettv will be
2794 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 */
2796
2797/*
2798 * Handle zero level expression.
2799 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002801 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * Return OK or FAIL.
2804 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002806eval0(
2807 char_u *arg,
2808 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002809 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002810 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002812 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2813}
2814
2815/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002816 * If "arg" is a simple function call without arguments then call it and return
2817 * the result. Otherwise return NOTDONE.
2818 */
2819 int
2820may_call_simple_func(
2821 char_u *arg,
2822 typval_T *rettv)
2823{
2824 char_u *parens = (char_u *)strstr((char *)arg, "()");
2825 int r = NOTDONE;
2826
2827 // If the expression is "FuncName()" then we can skip a lot of overhead.
2828 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2829 {
2830 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2831
2832 if (to_name_end(p, TRUE) == parens)
2833 r = call_simple_func(arg, (int)(parens - arg), rettv);
2834 }
2835 return r;
2836}
2837
2838/*
2839 * Handle zero level expression with optimization for a simple function call.
2840 * Same arguments and return value as eval0().
2841 */
2842 int
2843eval0_simple_funccal(
2844 char_u *arg,
2845 typval_T *rettv,
2846 exarg_T *eap,
2847 evalarg_T *evalarg)
2848{
2849 int r = may_call_simple_func(arg, rettv);
2850
2851 if (r == NOTDONE)
2852 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2853 return r;
2854}
2855
2856/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002857 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2858 * expression and don't check what comes after the expression.
2859 */
2860 int
2861eval0_retarg(
2862 char_u *arg,
2863 typval_T *rettv,
2864 exarg_T *eap,
2865 evalarg_T *evalarg,
2866 char_u **retarg)
2867{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 int ret;
2869 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002870 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002871 int did_emsg_before = did_emsg;
2872 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002873 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002874 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002878
Bram Moolenaar79481362022-06-27 20:15:10 +01002879 if (ret != FAIL)
2880 {
2881 expr_end = p;
2882 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002883
Bram Moolenaar79481362022-06-27 20:15:10 +01002884 // In Vim9 script a command block is not split at NL characters for
2885 // commands using an expression argument. Skip over a '#' comment to
2886 // check for a following NL. Require white space before the '#'.
2887 if (in_vim9script() && p > expr_end && retarg == NULL)
2888 while (*p == '#')
2889 {
2890 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002891
Bram Moolenaar79481362022-06-27 20:15:10 +01002892 if (nl == NULL)
2893 break;
2894 p = skipwhite(nl + 1);
2895 if (eap != NULL && *p != NUL)
2896 eap->nextcmd = p;
2897 check_for_end = FALSE;
2898 }
2899
2900 if (check_for_end)
2901 end_error = !ends_excmd2(arg, p);
2902 }
2903
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002904 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002907 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /*
2909 * Report the invalid expression unless the expression evaluation has
2910 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002911 * exception, or we already gave a more specific error.
2912 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002914 if (!aborting()
2915 && did_emsg == did_emsg_before
2916 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002917 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002918 {
2919 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002920 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002921 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002922 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002923 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002924
zeertzjqf2588b62023-05-05 17:22:35 +01002925 if (eap != NULL && p != NULL)
2926 {
2927 // Some of the expression may not have been consumed.
2928 // Only execute a next command if it cannot be a "||" operator.
2929 // The next command may be "catch".
2930 char_u *nextcmd = check_nextcmd(p);
2931 if (nextcmd != NULL && *nextcmd != '|')
2932 eap->nextcmd = nextcmd;
2933 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002936
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002937 if (retarg != NULL)
2938 *retarg = p;
2939 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002940 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 return ret;
2943}
2944
2945/*
2946 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002947 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002948 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 *
2950 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002951 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002953 * Note: "rettv.v_lock" is not set.
2954 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 * Return OK or FAIL.
2956 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002957 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002960 char_u *p;
2961 int getnext;
2962
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002963 CLEAR_POINTER(rettv);
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 /*
2966 * Get the first variable.
2967 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 return FAIL;
2970
Bram Moolenaar793648f2020-06-26 21:28:25 +02002971 p = eval_next_non_blank(*arg, evalarg, &getnext);
2972 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002974 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002975 int result;
2976 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002977 evalarg_T *evalarg_used = evalarg;
2978 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002979 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002980 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002981 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982
2983 if (evalarg == NULL)
2984 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002985 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002986 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002988 orig_flags = evalarg_used->eval_flags;
2989 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002990
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002991 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002992 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002993 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002994 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002995 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002996 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002997 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002998 clear_tv(rettv);
2999 return FAIL;
3000 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003001 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003002 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003007 int error = FALSE;
3008
Bram Moolenaar13106602020-10-04 16:06:05 +02003009 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003010 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003011 else if (vim9script)
3012 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003013 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003015 if (error || !op_falsy || !result)
3016 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003017 if (error)
3018 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
3020
3021 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003022 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003024 if (op_falsy)
3025 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003026 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003027 {
mityu4ac198c2021-05-28 17:52:40 +02003028 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003029 clear_tv(rettv);
3030 return FAIL;
3031 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003032 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003033 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003034 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003035 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003036 {
3037 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003039 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003040 if (!op_falsy || !result)
3041 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003043 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003045 /*
3046 * Check for the ":".
3047 */
3048 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3049 if (*p != ':')
3050 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003051 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003052 if (evaluate && result)
3053 clear_tv(rettv);
3054 evalarg_used->eval_flags = orig_flags;
3055 return FAIL;
3056 }
3057 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003058 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003059 else
3060 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003061 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003062 {
3063 error_white_both(p, 1);
3064 clear_tv(rettv);
3065 evalarg_used->eval_flags = orig_flags;
3066 return FAIL;
3067 }
3068 *arg = p;
3069 }
3070
3071 /*
3072 * Get the third variable. Recursive!
3073 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003074 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003075 {
mityu4ac198c2021-05-28 17:52:40 +02003076 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003077 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003078 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003079 return FAIL;
3080 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003081 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3082 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003083 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003084 if (eval1(arg, &var2, evalarg_used) == FAIL)
3085 {
3086 if (evaluate && result)
3087 clear_tv(rettv);
3088 evalarg_used->eval_flags = orig_flags;
3089 return FAIL;
3090 }
3091 if (evaluate && !result)
3092 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003094
3095 if (evalarg == NULL)
3096 clear_evalarg(&local_evalarg, NULL);
3097 else
3098 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100
3101 return OK;
3102}
3103
3104/*
3105 * Handle first level expression:
3106 * expr2 || expr2 || expr2 logical OR
3107 *
3108 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003109 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 *
3111 * Return OK or FAIL.
3112 */
3113 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003114eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003116 char_u *p;
3117 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003120 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003122 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return FAIL;
3124
3125 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003126 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003128 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003129 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003131 evalarg_T *evalarg_used = evalarg;
3132 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003133 int evaluate;
3134 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003135 long result = FALSE;
3136 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003137 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003138 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003139
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003140 if (evalarg == NULL)
3141 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003142 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003143 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003145 orig_flags = evalarg_used->eval_flags;
3146 evaluate = orig_flags & EVAL_EVALUATE;
3147 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003148 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003149 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003150 result = tv_get_bool_chk(rettv, &error);
3151 else if (tv_get_number_chk(rettv, &error) != 0)
3152 result = TRUE;
3153 clear_tv(rettv);
3154 if (error)
3155 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157
3158 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003159 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003161 while (p[0] == '|' && p[1] == '|')
3162 {
3163 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003164 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003165 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003166 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003167 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003168 {
3169 error_white_both(p, 2);
3170 clear_tv(rettv);
3171 return FAIL;
3172 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003173 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003174 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003175
3176 /*
3177 * Get the second variable.
3178 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003179 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003180 {
mityu4ac198c2021-05-28 17:52:40 +02003181 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003182 clear_tv(rettv);
3183 return FAIL;
3184 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003185 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3186 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003187 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003188 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003189 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003190
3191 /*
3192 * Compute the result.
3193 */
3194 if (evaluate && !result)
3195 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003196 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003197 result = tv_get_bool_chk(&var2, &error);
3198 else if (tv_get_number_chk(&var2, &error) != 0)
3199 result = TRUE;
3200 clear_tv(&var2);
3201 if (error)
3202 return FAIL;
3203 }
3204 if (evaluate)
3205 {
3206 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003207 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003208 rettv->v_type = VAR_BOOL;
3209 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003210 }
3211 else
3212 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003213 rettv->v_type = VAR_NUMBER;
3214 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003215 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003216 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003217
3218 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003220
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003221 if (evalarg == NULL)
3222 clear_evalarg(&local_evalarg, NULL);
3223 else
3224 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
3226
3227 return OK;
3228}
3229
3230/*
3231 * Handle second level expression:
3232 * expr3 && expr3 && expr3 logical AND
3233 *
3234 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003235 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 *
3237 * Return OK or FAIL.
3238 */
3239 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003240eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003242 char_u *p;
3243 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003246 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003248 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 return FAIL;
3250
3251 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003252 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003254 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003255 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003257 evalarg_T *evalarg_used = evalarg;
3258 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003259 int orig_flags;
3260 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003261 long result = TRUE;
3262 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003263 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003264 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003265
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003266 if (evalarg == NULL)
3267 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003268 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003269 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003270 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003271 orig_flags = evalarg_used->eval_flags;
3272 evaluate = orig_flags & EVAL_EVALUATE;
3273 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003274 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003275 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003276 result = tv_get_bool_chk(rettv, &error);
3277 else if (tv_get_number_chk(rettv, &error) == 0)
3278 result = FALSE;
3279 clear_tv(rettv);
3280 if (error)
3281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283
3284 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003285 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003287 while (p[0] == '&' && p[1] == '&')
3288 {
3289 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003290 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003291 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003292 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003293 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003294 {
3295 error_white_both(p, 2);
3296 clear_tv(rettv);
3297 return FAIL;
3298 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003299 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003300 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003301
3302 /*
3303 * Get the second variable.
3304 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003305 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003306 {
mityu4ac198c2021-05-28 17:52:40 +02003307 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003308 clear_tv(rettv);
3309 return FAIL;
3310 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003311 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3312 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003313 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003314 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003315 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003316 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003317
3318 /*
3319 * Compute the result.
3320 */
3321 if (evaluate && result)
3322 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003323 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003324 result = tv_get_bool_chk(&var2, &error);
3325 else if (tv_get_number_chk(&var2, &error) == 0)
3326 result = FALSE;
3327 clear_tv(&var2);
3328 if (error)
3329 return FAIL;
3330 }
3331 if (evaluate)
3332 {
3333 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003334 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003335 rettv->v_type = VAR_BOOL;
3336 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003337 }
3338 else
3339 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003340 rettv->v_type = VAR_NUMBER;
3341 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003342 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003343 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003344
3345 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003347
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003348 if (evalarg == NULL)
3349 clear_evalarg(&local_evalarg, NULL);
3350 else
3351 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353
3354 return OK;
3355}
3356
3357/*
3358 * Handle third level expression:
3359 * var1 == var2
3360 * var1 =~ var2
3361 * var1 != var2
3362 * var1 !~ var2
3363 * var1 > var2
3364 * var1 >= var2
3365 * var1 < var2
3366 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003367 * var1 is var2
3368 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 *
3370 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003371 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 *
3373 * Return OK or FAIL.
3374 */
3375 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003376eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003379 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003380 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003382 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003385 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003387 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return FAIL;
3389
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003390 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003391
Bram Moolenaar696ba232020-07-29 21:20:41 +02003392 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003395 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003397 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003399 typval_T var2;
3400 int ic;
3401 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003402 int evaluate = evalarg == NULL
3403 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003404 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003405
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003406 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003407 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003408 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003409 p = *arg;
3410 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003411 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3412 {
mityu4ac198c2021-05-28 17:52:40 +02003413 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003414 clear_tv(rettv);
3415 return FAIL;
3416 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003417
Bram Moolenaar696ba232020-07-29 21:20:41 +02003418 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3419 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003420 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003421 clear_tv(rettv);
3422 return FAIL;
3423 }
3424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003425 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (p[len] == '?')
3427 {
3428 ic = TRUE;
3429 ++len;
3430 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003431 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 else if (p[len] == '#')
3433 {
3434 ic = FALSE;
3435 ++len;
3436 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003437 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003439 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 /*
3442 * Get the second variable.
3443 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003444 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3445 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003446 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003447 clear_tv(rettv);
3448 return FAIL;
3449 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003450 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003451 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 return FAIL;
3455 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003456 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003457 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003458 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003459
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003460 // use the line of the comparison for messages
3461 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003462 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003463 {
3464 ret = FAIL;
3465 clear_tv(rettv);
3466 }
3467 else
3468 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003469 clear_tv(&var2);
3470 return ret;
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473
3474 return OK;
3475}
3476
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003477/*
3478 * Make a copy of blob "tv1" and append blob "tv2".
3479 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 void
3481eval_addblob(typval_T *tv1, typval_T *tv2)
3482{
3483 blob_T *b1 = tv1->vval.v_blob;
3484 blob_T *b2 = tv2->vval.v_blob;
3485 blob_T *b = blob_alloc();
3486 int i;
3487
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003488 if (b == NULL)
3489 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003491 for (i = 0; i < blob_len(b1); i++)
3492 ga_append(&b->bv_ga, blob_get(b1, i));
3493 for (i = 0; i < blob_len(b2); i++)
3494 ga_append(&b->bv_ga, blob_get(b2, i));
3495
3496 clear_tv(tv1);
3497 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498}
3499
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003500/*
3501 * Make a copy of list "tv1" and append list "tv2".
3502 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 int
3504eval_addlist(typval_T *tv1, typval_T *tv2)
3505{
3506 typval_T var3;
3507
3508 // concatenate Lists
3509 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3510 {
3511 clear_tv(tv1);
3512 clear_tv(tv2);
3513 return FAIL;
3514 }
3515 clear_tv(tv1);
3516 *tv1 = var3;
3517 return OK;
3518}
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003521 * Handle the bitwise left/right shift operator expression:
3522 * var1 << var2
3523 * var1 >> var2
3524 *
3525 * "arg" must point to the first non-white of the expression.
3526 * "arg" is advanced to just after the recognized expression.
3527 *
3528 * Return OK or FAIL.
3529 */
3530 static int
3531eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3532{
3533 /*
3534 * Get the first expression.
3535 */
3536 if (eval6(arg, rettv, evalarg) == FAIL)
3537 return FAIL;
3538
3539 /*
3540 * Repeat computing, until no '<<' or '>>' is following.
3541 */
3542 for (;;)
3543 {
3544 char_u *p;
3545 int getnext;
3546 exprtype_T type;
3547 int evaluate;
3548 typval_T var2;
3549 int vim9script;
3550
3551 p = eval_next_non_blank(*arg, evalarg, &getnext);
3552 if (p[0] == '<' && p[1] == '<')
3553 type = EXPR_LSHIFT;
3554 else if (p[0] == '>' && p[1] == '>')
3555 type = EXPR_RSHIFT;
3556 else
3557 return OK;
3558
3559 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003560 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3561 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003562 {
3563 // left operand should be a number
3564 emsg(_(e_bitshift_ops_must_be_number));
3565 clear_tv(rettv);
3566 return FAIL;
3567 }
3568
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003569 vim9script = in_vim9script();
3570 if (getnext)
3571 {
3572 *arg = eval_next_line(*arg, evalarg);
3573 p = *arg;
3574 }
3575 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3576 {
3577 error_white_both(*arg, 2);
3578 clear_tv(rettv);
3579 return FAIL;
3580 }
3581
3582 /*
3583 * Get the second variable.
3584 */
3585 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3586 {
3587 error_white_both(p, 2);
3588 clear_tv(rettv);
3589 return FAIL;
3590 }
3591 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3592 if (eval6(arg, &var2, evalarg) == FAIL)
3593 {
3594 clear_tv(rettv);
3595 return FAIL;
3596 }
3597
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003598 if (evaluate)
3599 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003600 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3601 {
3602 // right operand should be a positive number
3603 if (var2.v_type != VAR_NUMBER)
3604 emsg(_(e_bitshift_ops_must_be_number));
3605 else
3606 emsg(_(e_bitshift_ops_must_be_positive));
3607 clear_tv(rettv);
3608 clear_tv(&var2);
3609 return FAIL;
3610 }
3611
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003612 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3613 // shifting more bits than we have always results in zero
3614 rettv->vval.v_number = 0;
3615 else if (type == EXPR_LSHIFT)
3616 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003617 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003618 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003619 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003620 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003621 }
3622
3623 clear_tv(&var2);
3624 }
3625
3626 return OK;
3627}
3628
3629/*
3630 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003631 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003633 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003634 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 *
3636 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003637 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 *
3639 * Return OK or FAIL.
3640 */
3641 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003642eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003645 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003647 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 return FAIL;
3649
3650 /*
3651 * Repeat computing, until no '+', '-' or '.' is following.
3652 */
3653 for (;;)
3654 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003655 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003656 int getnext;
3657 char_u *p;
3658 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003659 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003660 int concat;
3661 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003662 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003663
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003664 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003665 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003666 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003667 p = eval_next_non_blank(*arg, evalarg, &getnext);
3668 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003669 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003670 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3671 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003673 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3674 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003675
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003676 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003677 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003678 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003679 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003680 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003681 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003682 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003683 {
mityu4ac198c2021-05-28 17:52:40 +02003684 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003685 clear_tv(rettv);
3686 return FAIL;
3687 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003688 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003689 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003690 if ((op != '+' || (rettv->v_type != VAR_LIST
3691 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003692 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003693 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003694 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003695 int error = FALSE;
3696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003697 // For "list + ...", an illegal use of the first operand as
3698 // a number cannot be determined before evaluating the 2nd
3699 // operand: if this is also a list, all is ok.
3700 // For "something . ...", "something - ..." or "non-list + ...",
3701 // we know that the first operand needs to be a string or number
3702 // without evaluating the 2nd operand. So check before to avoid
3703 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003704 if (op != '.')
3705 tv_get_number_chk(rettv, &error);
3706 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003707 {
3708 clear_tv(rettv);
3709 return FAIL;
3710 }
3711 }
3712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 /*
3714 * Get the second variable.
3715 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003716 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003717 {
mityu89dcb4d2021-05-28 13:50:17 +02003718 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003719 clear_tv(rettv);
3720 return FAIL;
3721 }
3722 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003723 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 return FAIL;
3727 }
3728
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003729 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 /*
3732 * Compute the result.
3733 */
3734 if (op == '.')
3735 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003736 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3737 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003738 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003739
Bram Moolenaar2e086612020-08-25 22:37:48 +02003740 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003741 || var2.v_type == VAR_CHANNEL
3742 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003743 semsg(_(e_using_invalid_value_as_string_str),
3744 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003745 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003746 {
3747 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3748 var2.vval.v_float);
3749 s2 = buf2;
3750 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003751 else
3752 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003753 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754 {
3755 clear_tv(rettv);
3756 clear_tv(&var2);
3757 return FAIL;
3758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003759 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003760 clear_tv(rettv);
3761 rettv->v_type = VAR_STRING;
3762 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003764 else if (op == '+' && rettv->v_type == VAR_BLOB
3765 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003767 else if (op == '+' && rettv->v_type == VAR_LIST
3768 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003769 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003771 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 else
3774 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003775 int error = FALSE;
3776 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003777 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003779 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003781 f1 = rettv->vval.v_float;
3782 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003784 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003786 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003787 if (error)
3788 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003789 // This can only happen for "list + non-list" or
3790 // "blob + non-blob". For "non-list + ..." or
3791 // "something - ...", we returned before evaluating the
3792 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003793 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003794 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003795 return FAIL;
3796 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003797 if (var2.v_type == VAR_FLOAT)
3798 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003799 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800 if (var2.v_type == VAR_FLOAT)
3801 {
3802 f2 = var2.vval.v_float;
3803 n2 = 0;
3804 }
3805 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003807 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003808 if (error)
3809 {
3810 clear_tv(rettv);
3811 clear_tv(&var2);
3812 return FAIL;
3813 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003814 if (rettv->v_type == VAR_FLOAT)
3815 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003819 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003820 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3821 {
3822 if (op == '+')
3823 f1 = f1 + f2;
3824 else
3825 f1 = f1 - f2;
3826 rettv->v_type = VAR_FLOAT;
3827 rettv->vval.v_float = f1;
3828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003830 {
3831 if (op == '+')
3832 n1 = n1 + n2;
3833 else
3834 n1 = n1 - n2;
3835 rettv->v_type = VAR_NUMBER;
3836 rettv->vval.v_number = n1;
3837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 }
3842 return OK;
3843}
3844
3845/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003846 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 * * number multiplication
3848 * / number division
3849 * % number modulo
3850 *
3851 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003852 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 *
3854 * Return OK or FAIL.
3855 */
3856 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003857eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858 char_u **arg,
3859 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003860 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003863 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
3865 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003866 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003868 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FAIL;
3870
3871 /*
3872 * Repeat computing, until no '*', '/' or '%' is following.
3873 */
3874 for (;;)
3875 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003876 int evaluate;
3877 int getnext;
3878 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003879 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003880 int op;
3881 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003882 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003883 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003884
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003885 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003886 p = eval_next_non_blank(*arg, evalarg, &getnext);
3887 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003888 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003890
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003891 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003892 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003893 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003894 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003895 {
3896 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3897 {
mityu4ac198c2021-05-28 17:52:40 +02003898 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003899 clear_tv(rettv);
3900 return FAIL;
3901 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003902 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003905 f1 = 0;
3906 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003907 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003908 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003910 if (rettv->v_type == VAR_FLOAT)
3911 {
3912 f1 = rettv->vval.v_float;
3913 use_float = TRUE;
3914 n1 = 0;
3915 }
3916 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003917 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003919 if (error)
3920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else
3923 n1 = 0;
3924
3925 /*
3926 * Get the second variable.
3927 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003928 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3929 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003930 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003931 clear_tv(rettv);
3932 return FAIL;
3933 }
3934 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003935 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return FAIL;
3937
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003938 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003940 if (var2.v_type == VAR_FLOAT)
3941 {
3942 if (!use_float)
3943 {
3944 f1 = n1;
3945 use_float = TRUE;
3946 }
3947 f2 = var2.vval.v_float;
3948 n2 = 0;
3949 }
3950 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003952 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003953 clear_tv(&var2);
3954 if (error)
3955 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003956 if (use_float)
3957 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 /*
3961 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003962 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003964 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003966 if (op == '*')
3967 f1 = f1 * f2;
3968 else if (op == '/')
3969 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003970#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003971 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003972 if (f2 == 0.0)
3973 {
3974 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003975 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003976 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003977 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003978 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003979 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003980 }
3981 else
3982 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003983#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003984 // We rely on the floating point library to handle divide
3985 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003986 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003987#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003990 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003991 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003992 return FAIL;
3993 }
3994 rettv->v_type = VAR_FLOAT;
3995 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997 else
3998 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003999 int failed = FALSE;
4000
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004001 if (op == '*')
4002 n1 = n1 * n2;
4003 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004004 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004006 n1 = num_modulus(n1, n2, &failed);
4007 if (failed)
4008 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004009
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004010 rettv->v_type = VAR_NUMBER;
4011 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014 }
4015
4016 return OK;
4017}
4018
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004019/*
4020 * Handle a type cast before a base level expression.
4021 * "arg" must point to the first non-white of the expression.
4022 * "arg" is advanced to just after the recognized expression.
4023 * Return OK or FAIL.
4024 */
4025 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004026eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004027 char_u **arg,
4028 typval_T *rettv,
4029 evalarg_T *evalarg,
4030 int want_string) // after "." operator
4031{
4032 type_T *want_type = NULL;
4033 garray_T type_list; // list of pointers to allocated types
4034 int res;
4035 int evaluate = evalarg == NULL ? 0
4036 : (evalarg->eval_flags & EVAL_EVALUATE);
4037
4038 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004039 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4040 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004041 {
4042 ++*arg;
4043 ga_init2(&type_list, sizeof(type_T *), 10);
4044 want_type = parse_type(arg, &type_list, TRUE);
4045 if (want_type == NULL && (evaluate || **arg != '>'))
4046 {
4047 clear_type_list(&type_list);
4048 return FAIL;
4049 }
4050
4051 if (**arg != '>')
4052 {
4053 if (*skipwhite(*arg) == '>')
4054 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4055 else
4056 emsg(_(e_missing_gt));
4057 clear_type_list(&type_list);
4058 return FAIL;
4059 }
4060 ++*arg;
4061 *arg = skipwhite_and_linebreak(*arg, evalarg);
4062 }
4063
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004064 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004065
4066 if (want_type != NULL && evaluate)
4067 {
4068 if (res == OK)
4069 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004070 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4071 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004072
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004073 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004074 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004075 if (want_type->tt_type == VAR_BOOL
4076 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004077 && (actual->tt_flags & TTFLAG_BOOL_OK))
4078 {
4079 int n = tv2bool(rettv);
4080
4081 // can use "0" and "1" for boolean in some places
4082 clear_tv(rettv);
4083 rettv->v_type = VAR_BOOL;
4084 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4085 }
4086 else
4087 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004088 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004089
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004090 res = check_type(want_type, actual, TRUE, where);
4091 }
4092 }
4093 }
4094 clear_type_list(&type_list);
4095 }
4096
4097 return res;
4098}
4099
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004100 int
4101eval_leader(char_u **arg, int vim9)
4102{
4103 char_u *s = *arg;
4104 char_u *p = *arg;
4105
4106 while (*p == '!' || *p == '-' || *p == '+')
4107 {
4108 char_u *n = skipwhite(p + 1);
4109
4110 // ++, --, -+ and +- are not accepted in Vim9 script
4111 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4112 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004113 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004114 return FAIL;
4115 }
4116 p = n;
4117 }
4118 *arg = p;
4119 return OK;
4120}
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004123 * Check for a predefined value "true", "false" and "null.*".
4124 * Return OK when recognized.
4125 */
4126 int
4127handle_predefined(char_u *s, int len, typval_T *rettv)
4128{
4129 switch (len)
4130 {
4131 case 4: if (STRNCMP(s, "true", 4) == 0)
4132 {
4133 rettv->v_type = VAR_BOOL;
4134 rettv->vval.v_number = VVAL_TRUE;
4135 return OK;
4136 }
4137 if (STRNCMP(s, "null", 4) == 0)
4138 {
4139 rettv->v_type = VAR_SPECIAL;
4140 rettv->vval.v_number = VVAL_NULL;
4141 return OK;
4142 }
4143 break;
4144 case 5: if (STRNCMP(s, "false", 5) == 0)
4145 {
4146 rettv->v_type = VAR_BOOL;
4147 rettv->vval.v_number = VVAL_FALSE;
4148 return OK;
4149 }
4150 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004151 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4152 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004153#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004154 rettv->v_type = VAR_JOB;
4155 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004156#else
4157 rettv->v_type = VAR_SPECIAL;
4158 rettv->vval.v_number = VVAL_NULL;
4159#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004160 return OK;
4161 }
4162 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004163 case 9:
4164 if (STRNCMP(s, "null_", 5) != 0)
4165 break;
4166 if (STRNCMP(s + 5, "list", 4) == 0)
4167 {
4168 rettv->v_type = VAR_LIST;
4169 rettv->vval.v_list = NULL;
4170 return OK;
4171 }
4172 if (STRNCMP(s + 5, "dict", 4) == 0)
4173 {
4174 rettv->v_type = VAR_DICT;
4175 rettv->vval.v_dict = NULL;
4176 return OK;
4177 }
4178 if (STRNCMP(s + 5, "blob", 4) == 0)
4179 {
4180 rettv->v_type = VAR_BLOB;
4181 rettv->vval.v_blob = NULL;
4182 return OK;
4183 }
4184 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004185 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4186 {
4187 rettv->v_type = VAR_CLASS;
4188 rettv->vval.v_class = NULL;
4189 return OK;
4190 }
4191 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004192 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4193 {
4194 rettv->v_type = VAR_STRING;
4195 rettv->vval.v_string = NULL;
4196 return OK;
4197 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004198 if (STRNCMP(s, "null_object", 11) == 0)
4199 {
4200 rettv->v_type = VAR_OBJECT;
4201 rettv->vval.v_object = NULL;
4202 return OK;
4203 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004204 break;
4205 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004206 if (STRNCMP(s, "null_channel", 12) == 0)
4207 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004208#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004209 rettv->v_type = VAR_CHANNEL;
4210 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004211#else
4212 rettv->v_type = VAR_SPECIAL;
4213 rettv->vval.v_number = VVAL_NULL;
4214#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004215 return OK;
4216 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004217 if (STRNCMP(s, "null_partial", 12) == 0)
4218 {
4219 rettv->v_type = VAR_PARTIAL;
4220 rettv->vval.v_partial = NULL;
4221 return OK;
4222 }
4223 break;
4224 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4225 {
4226 rettv->v_type = VAR_FUNC;
4227 rettv->vval.v_string = NULL;
4228 return OK;
4229 }
4230 break;
4231 }
4232 return FAIL;
4233}
4234
4235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 * Handle sixth level expression:
4237 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004238 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004239 * "string" string constant
4240 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * &option-name option value
4242 * @r register contents
4243 * identifier variable value
4244 * function() function call
4245 * $VAR environment variable
4246 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004247 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004249 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004250 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 *
4252 * Also handle:
4253 * ! in front logical NOT
4254 * - in front unary minus
4255 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004256 * trailing [] subscript in String or List
4257 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004258 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 *
4260 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004261 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 *
4263 * Return OK or FAIL.
4264 */
4265 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004266eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004267 char_u **arg,
4268 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004269 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004272 int evaluate = evalarg != NULL
4273 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int len;
4275 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004276 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 char_u *start_leader, *end_leader;
4278 int ret = OK;
4279 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004280 static int recurse = 0;
4281 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004290 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 */
4292 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004293 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004294 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 end_leader = *arg;
4296
Keith Thompson184f71c2024-01-04 21:19:04 +01004297 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004298 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004299 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004300 ++*arg;
4301 return FAIL;
4302 }
4303
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004304 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004305 // and crash. With MSVC the stack is smaller.
4306 if (recurse ==
4307#ifdef _MSC_VER
4308 300
4309#else
4310 1000
4311#endif
4312 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004313 {
4314 semsg(_(e_expression_too_recursive_str), *arg);
4315 return FAIL;
4316 }
4317 ++recurse;
4318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 switch (**arg)
4320 {
4321 /*
4322 * Number constant.
4323 */
4324 case '0':
4325 case '1':
4326 case '2':
4327 case '3':
4328 case '4':
4329 case '5':
4330 case '6':
4331 case '7':
4332 case '8':
4333 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004334 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004335
4336 // Apply prefixed "-" and "+" now. Matters especially when
4337 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004338 if (ret == OK && evaluate && end_leader > start_leader
4339 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004340 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 break;
4342
4343 /*
4344 * String constant: "string".
4345 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004346 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 break;
4348
4349 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004352 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004353 break;
4354
4355 /*
4356 * List: [expr, expr]
4357 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004358 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 break;
4360
4361 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004362 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004363 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004364 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004365 {
4366 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4367 }
4368 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004369 {
4370 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004371 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004372 }
4373 else
4374 ret = NOTDONE;
4375 break;
4376
4377 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004378 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004379 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004381 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004382 ret = NOTDONE;
4383 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004384 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004385 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004386 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387 break;
4388
4389 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004390 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004392 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 break;
4394
4395 /*
4396 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004397 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 */
LemonBoy2eaef102022-05-06 13:14:50 +01004399 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4400 ret = eval_interp_string(arg, rettv, evaluate);
4401 else
4402 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 break;
4404
4405 /*
4406 * Register contents: @r.
4407 */
4408 case '@': ++*arg;
4409 if (evaluate)
4410 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004411 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004412 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004413 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004414 emsg_invreg(**arg);
4415 else
4416 {
4417 rettv->v_type = VAR_STRING;
4418 rettv->vval.v_string = get_reg_contents(**arg,
4419 GREG_EXPR_SRC);
4420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422 if (**arg != NUL)
4423 ++*arg;
4424 break;
4425
4426 /*
4427 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004428 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004430 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004431 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004432 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004433 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004434 if (ret == OK && evaluate)
4435 {
4436 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4437
Bram Moolenaara9931532021-06-12 15:58:16 +02004438 // Compile it here to get the return type. The return
4439 // type is optional, when it's missing use t_unknown.
4440 // This is recognized in compile_return().
4441 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4442 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004443 if (compile_def_function(ufunc, FALSE,
4444 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004445 {
4446 clear_tv(rettv);
4447 ret = FAIL;
4448 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004449 }
4450 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004451 if (ret == NOTDONE)
4452 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004453 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004454 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004455
Bram Moolenaar9215f012020-06-27 21:18:00 +02004456 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004457 if (**arg == ')')
4458 ++*arg;
4459 else if (ret == OK)
4460 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004461 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004462 clear_tv(rettv);
4463 ret = FAIL;
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466 break;
4467
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 break;
4470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004471
4472 if (ret == NOTDONE)
4473 {
4474 /*
4475 * Must be a variable or function name.
4476 * Can also be a curly-braces kind of name: {expr}.
4477 */
4478 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004479 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004480 if (alias != NULL)
4481 s = alias;
4482
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004483 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 ret = FAIL;
4485 else
4486 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004487 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4488
Bram Moolenaar4525a572022-02-13 11:57:33 +00004489 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004490 {
4491 emsg(_(e_cannot_use_underscore_here));
4492 ret = FAIL;
4493 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004494 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004495 && s[0] == 's' && s[1] == ':')
4496 {
4497 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4498 ret = FAIL;
4499 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004500 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004501 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004502 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004503 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004504 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004507 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004508 // get the value of "true", "false", etc. or a variable
4509 ret = FAIL;
4510 if (vim9script)
4511 ret = handle_predefined(s, len, rettv);
4512 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004513 {
4514 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004515 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004516 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004517 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004518 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004519 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004520 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004521 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004522 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004523 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004524 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004526 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004527 }
4528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4530 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004532 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 /*
4535 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4536 */
4537 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004538 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004539
4540 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004541 return ret;
4542}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004543
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004544/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004545 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004546 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004547 * Adjusts "end_leaderp" until it is at "start_leader".
4548 */
4549 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004550eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004551 typval_T *rettv,
4552 int numeric_only,
4553 char_u *start_leader,
4554 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004555{
4556 char_u *end_leader = *end_leaderp;
4557 int ret = OK;
4558 int error = FALSE;
4559 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004560 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004561 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004562 float_T f = 0.0;
4563
4564 if (rettv->v_type == VAR_FLOAT)
4565 f = rettv->vval.v_float;
4566 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004567 {
4568 while (VIM_ISWHITE(end_leader[-1]))
4569 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004570 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004571 val = tv2bool(rettv);
4572 else
4573 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004574 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004575 if (error)
4576 {
4577 clear_tv(rettv);
4578 ret = FAIL;
4579 }
4580 else
4581 {
4582 while (end_leader > start_leader)
4583 {
4584 --end_leader;
4585 if (*end_leader == '!')
4586 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004587 if (numeric_only)
4588 {
4589 ++end_leader;
4590 break;
4591 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004592 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004593 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004594 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004595 {
4596 rettv->v_type = VAR_BOOL;
4597 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4598 }
4599 else
4600 f = !f;
4601 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004602 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004603 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004604 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004605 type = VAR_BOOL;
4606 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004607 }
4608 else if (*end_leader == '-')
4609 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004610 if (rettv->v_type == VAR_FLOAT)
4611 f = -f;
4612 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004613 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004614 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004615 type = VAR_NUMBER;
4616 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004617 }
4618 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004619 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004622 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 else
4625 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004626 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004627 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004628 rettv->v_type = type;
4629 else
4630 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004631 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004634 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 return ret;
4636}
4637
4638/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004639 * Call the function referred to in "rettv".
4640 */
4641 static int
4642call_func_rettv(
4643 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004644 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004645 typval_T *rettv,
4646 int evaluate,
4647 dict_T *selfdict,
4648 typval_T *basetv)
4649{
4650 partial_T *pt = NULL;
4651 funcexe_T funcexe;
4652 typval_T functv;
4653 char_u *s;
4654 int ret;
4655
4656 // need to copy the funcref so that we can clear rettv
4657 if (evaluate)
4658 {
4659 functv = *rettv;
4660 rettv->v_type = VAR_UNKNOWN;
4661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004663 if (functv.v_type == VAR_PARTIAL)
4664 {
4665 pt = functv.vval.v_partial;
4666 s = partial_name(pt);
4667 }
4668 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004669 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004670 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004671 if (s == NULL || *s == NUL)
4672 {
4673 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004674 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004675 goto theend;
4676 }
4677 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004678 }
4679 else
4680 s = (char_u *)"";
4681
Bram Moolenaara80faa82020-04-12 19:37:17 +02004682 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004683 funcexe.fe_firstline = curwin->w_cursor.lnum;
4684 funcexe.fe_lastline = curwin->w_cursor.lnum;
4685 funcexe.fe_evaluate = evaluate;
4686 funcexe.fe_partial = pt;
4687 funcexe.fe_selfdict = selfdict;
4688 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004689 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004690
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004691theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // Clear the funcref afterwards, so that deleting it while
4693 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004694 if (evaluate)
4695 clear_tv(&functv);
4696
4697 return ret;
4698}
4699
4700/*
4701 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004702 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004703 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4704 */
4705 static int
4706eval_lambda(
4707 char_u **arg,
4708 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004709 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004711{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004712 int evaluate = evalarg != NULL
4713 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004714 typval_T base = *rettv;
4715 int ret;
4716
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004717 rettv->v_type = VAR_UNKNOWN;
4718
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004719 if (**arg == '{')
4720 {
4721 // ->{lambda}()
4722 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4723 }
4724 else
4725 {
4726 // ->(lambda)()
4727 ++*arg;
4728 ret = eval1(arg, rettv, evalarg);
4729 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004730 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004731 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004732 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004733 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004734 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004735 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4736 && rettv->v_type != VAR_PARTIAL)
4737 {
4738 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4739 return FAIL;
4740 }
4741 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004742 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004743 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004744 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004745
4746 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004747 {
4748 if (verbose)
4749 {
4750 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004751 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004752 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004753 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004754 }
4755 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004756 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004757 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004758 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004759 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004760
4761 // Clear the funcref afterwards, so that deleting it while
4762 // evaluating the arguments is possible (see test55).
4763 if (evaluate)
4764 clear_tv(&base);
4765
4766 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004767}
4768
4769/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004770 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004771 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004772 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4773 */
4774 static int
4775eval_method(
4776 char_u **arg,
4777 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004778 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004780{
4781 char_u *name;
4782 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004783 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004784 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004785 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004786 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004787 int evaluate = evalarg != NULL
4788 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004789
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004790 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004791
Bram Moolenaarac92e252019-08-03 21:58:38 +02004792 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004793 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004794 if (alias != NULL)
4795 name = alias;
4796
4797 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004798 {
4799 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004800 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004801 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004802 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004803 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004804 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004805 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004806
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004807 // If there is no "(" immediately following, but there is further on,
4808 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4809 // Does not handle anything where "(" is part of the expression.
4810 *arg = skipwhite(*arg);
4811
4812 if (**arg != '(' && alias == NULL
4813 && (paren = vim_strchr(*arg, '(')) != NULL)
4814 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004815 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004816
4817 // Truncate the name a the "(". Avoid trying to get another line
4818 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004819 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004820 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4821 if (evalarg != NULL)
4822 {
4823 getline = evalarg->eval_getline;
4824 evalarg->eval_getline = NULL;
4825 }
4826
4827 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004828 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004829 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004830 *arg = name + len;
4831 ret = FAIL;
4832 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004833 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004834 {
4835 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004836 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004837 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004838
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004839 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004840 if (getline != NULL)
4841 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004842 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004843
4844 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004845 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004846 *arg = skipwhite(*arg);
4847
4848 if (**arg != '(')
4849 {
4850 if (verbose)
4851 semsg(_(e_missing_parenthesis_str), name);
4852 ret = FAIL;
4853 }
4854 else if (VIM_ISWHITE((*arg)[-1]))
4855 {
4856 if (verbose)
4857 emsg(_(e_no_white_space_allowed_before_parenthesis));
4858 ret = FAIL;
4859 }
4860 else
4861 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004862 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004863 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004864 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004865
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004866 // Clear the funcref afterwards, so that deleting it while
4867 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004868 if (evaluate)
4869 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004870 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004871
Bram Moolenaarac92e252019-08-03 21:58:38 +02004872 return ret;
4873}
4874
4875/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004876 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4877 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4879 */
4880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004881eval_index(
4882 char_u **arg,
4883 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004884 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004887 int evaluate = evalarg != NULL
4888 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004891 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004893 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004894 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004895
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004896 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4897 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004899 init_tv(&var1);
4900 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 /*
4904 * dict.name
4905 */
4906 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004907 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004909 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004911 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 }
4913 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 /*
4916 * something[idx]
4917 *
4918 * Get the (first) variable from inside the [].
4919 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004920 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 if (**arg == ':')
4922 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004923 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004925 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004926 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004927 semsg(_(e_white_space_required_before_and_after_str_at_str),
4928 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004929 clear_tv(&var1);
4930 return FAIL;
4931 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004932 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004933 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004934 int error = FALSE;
4935
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004936 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004937 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004938 && var1.v_type == VAR_FLOAT)
4939 {
4940 var1.vval.v_string = typval_tostring(&var1, TRUE);
4941 var1.v_type = VAR_STRING;
4942 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004943
Bram Moolenaar4525a572022-02-13 11:57:33 +00004944 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004945 tv_get_number_chk(&var1, &error);
4946 else
4947 error = tv_get_string_chk(&var1) == NULL;
4948 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004949 {
4950 // not a number or string
4951 clear_tv(&var1);
4952 return FAIL;
4953 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955
4956 /*
4957 * Get the second variable from inside the [:].
4958 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004959 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 if (**arg == ':')
4961 {
4962 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004963 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004964 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004965 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004966 semsg(_(e_white_space_required_before_and_after_str_at_str),
4967 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004968 if (!empty1)
4969 clear_tv(&var1);
4970 return FAIL;
4971 }
4972 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 if (**arg == ']')
4974 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004975 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004977 if (!empty1)
4978 clear_tv(&var1);
4979 return FAIL;
4980 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004984 if (!empty1)
4985 clear_tv(&var1);
4986 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 return FAIL;
4988 }
4989 }
4990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004992 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 if (**arg != ']')
4994 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004996 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 clear_tv(&var1);
4998 if (range)
4999 clear_tv(&var2);
5000 return FAIL;
5001 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005002 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003 }
5004
5005 if (evaluate)
5006 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005007 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005008 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005009 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005010
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005011 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005014 clear_tv(&var2);
5015 return res;
5016 }
5017 return OK;
5018}
5019
5020/*
5021 * Check if "rettv" can have an [index] or [sli:ce]
5022 */
5023 int
5024check_can_index(typval_T *rettv, int evaluate, int verbose)
5025{
5026 switch (rettv->v_type)
5027 {
5028 case VAR_FUNC:
5029 case VAR_PARTIAL:
5030 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005031 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005032 return FAIL;
5033 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005034 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005035 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005036 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005037 case VAR_BOOL:
5038 case VAR_SPECIAL:
5039 case VAR_JOB:
5040 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005041 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005042 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005043 if (verbose)
5044 emsg(_(e_cannot_index_special_variable));
5045 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005046 case VAR_CLASS:
5047 case VAR_TYPEALIAS:
5048 if (verbose)
5049 check_typval_is_value(rettv);
5050 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005051 case VAR_UNKNOWN:
5052 case VAR_ANY:
5053 case VAR_VOID:
5054 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005056 emsg(_(e_cannot_index_special_variable));
5057 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005058 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005059 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005061 case VAR_STRING:
5062 case VAR_LIST:
5063 case VAR_DICT:
5064 case VAR_BLOB:
5065 break;
5066 case VAR_NUMBER:
5067 if (in_vim9script())
5068 emsg(_(e_cannot_index_number));
5069 break;
5070 }
5071 return OK;
5072}
5073
5074/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005075 * slice() function
5076 */
5077 void
5078f_slice(typval_T *argvars, typval_T *rettv)
5079{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005080 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005081 && ((argvars[0].v_type != VAR_STRING
5082 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005083 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005084 && check_for_list_arg(argvars, 0) == FAIL)
5085 || check_for_number_arg(argvars, 1) == FAIL
5086 || check_for_opt_number_arg(argvars, 2) == FAIL))
5087 return;
5088
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005089 if (check_can_index(argvars, TRUE, FALSE) != OK)
5090 return;
5091
5092 copy_tv(argvars, rettv);
5093 eval_index_inner(rettv, TRUE, argvars + 1,
5094 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5095 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005096}
5097
5098/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005099 * Apply index or range to "rettv".
5100 * "var1" is the first index, NULL for [:expr].
5101 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005102 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5103 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005104 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5105 */
5106 int
5107eval_index_inner(
5108 typval_T *rettv,
5109 int is_range,
5110 typval_T *var1,
5111 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005112 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005113 char_u *key,
5114 int keylen,
5115 int verbose)
5116{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005117 varnumber_T n1, n2 = 0;
5118 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005119
5120 n1 = 0;
5121 if (var1 != NULL && rettv->v_type != VAR_DICT)
5122 n1 = tv_get_number(var1);
5123
5124 if (is_range)
5125 {
5126 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005128 if (verbose)
5129 emsg(_(e_cannot_slice_dictionary));
5130 return FAIL;
5131 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005132 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005133 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005134 else
5135 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005136 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005137
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005138 switch (rettv->v_type)
5139 {
5140 case VAR_UNKNOWN:
5141 case VAR_ANY:
5142 case VAR_VOID:
5143 case VAR_FUNC:
5144 case VAR_PARTIAL:
5145 case VAR_FLOAT:
5146 case VAR_BOOL:
5147 case VAR_SPECIAL:
5148 case VAR_JOB:
5149 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005150 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005151 case VAR_CLASS:
5152 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005153 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005154 break; // not evaluating, skipping over subscript
5155
5156 case VAR_NUMBER:
5157 case VAR_STRING:
5158 {
5159 char_u *s = tv_get_string(rettv);
5160
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005162 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005163 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005164 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005165 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005166 else
5167 s = char_from_string(s, n1);
5168 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005169 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005171 // The resulting variable is a substring. If the indexes
5172 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 if (n1 < 0)
5174 {
5175 n1 = len + n1;
5176 if (n1 < 0)
5177 n1 = 0;
5178 }
5179 if (n2 < 0)
5180 n2 = len + n2;
5181 else if (n2 >= len)
5182 n2 = len;
5183 if (n1 >= len || n2 < 0 || n1 > n2)
5184 s = NULL;
5185 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005186 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 }
5188 else
5189 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005190 // The resulting variable is a string of a single
5191 // character. If the index is too big or negative the
5192 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 if (n1 >= len || n1 < 0)
5194 s = NULL;
5195 else
5196 s = vim_strnsave(s + n1, 1);
5197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 clear_tv(rettv);
5199 rettv->v_type = VAR_STRING;
5200 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005201 }
5202 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005204 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005205 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5206 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005207 break;
5208
5209 case VAR_LIST:
5210 if (var1 == NULL)
5211 n1 = 0;
5212 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005213 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005214 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005215 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005216 return FAIL;
5217 break;
5218
5219 case VAR_DICT:
5220 {
5221 dictitem_T *item;
5222 typval_T tmp;
5223
5224 if (key == NULL)
5225 {
5226 key = tv_get_string_chk(var1);
5227 if (key == NULL)
5228 return FAIL;
5229 }
5230
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005231 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005232
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005233 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005234 {
5235 if (verbose)
5236 {
5237 if (keylen > 0)
5238 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005239 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005240 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005241 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005242 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005243
5244 copy_tv(&item->di_tv, &tmp);
5245 clear_tv(rettv);
5246 *rettv = tmp;
5247 }
5248 break;
5249 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 return OK;
5251}
5252
5253/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005254 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005255 */
5256 char_u *
5257partial_name(partial_T *pt)
5258{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005259 if (pt != NULL)
5260 {
5261 if (pt->pt_name != NULL)
5262 return pt->pt_name;
5263 if (pt->pt_func != NULL)
5264 return pt->pt_func->uf_name;
5265 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005266 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005267}
5268
Bram Moolenaarddecc252016-04-06 22:59:37 +02005269 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005270partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005271{
5272 int i;
5273
5274 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005275 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005276 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005277 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005278 if (pt->pt_name != NULL)
5279 {
5280 func_unref(pt->pt_name);
5281 vim_free(pt->pt_name);
5282 }
5283 else
5284 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005285 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005286
Bram Moolenaar54656012021-06-09 20:50:46 +02005287 // "out_up" is no longer used, decrement refcount on partial that owns it.
5288 partial_unref(pt->pt_outer.out_up_partial);
5289
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005290 // Using pt_outer from another partial.
5291 partial_unref(pt->pt_outer_partial);
5292
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005293 // Decrease the reference count for the context of a closure. If down
5294 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005295 if (pt->pt_funcstack != NULL)
5296 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005297 --pt->pt_funcstack->fs_refcount;
5298 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005299 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005300 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005301 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5302 if (pt->pt_loopvars[i] != NULL)
5303 {
5304 --pt->pt_loopvars[i]->lvs_refcount;
5305 loopvars_check_refcount(pt->pt_loopvars[i]);
5306 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005307
Bram Moolenaarddecc252016-04-06 22:59:37 +02005308 vim_free(pt);
5309}
5310
5311/*
5312 * Unreference a closure: decrement the reference count and free it when it
5313 * becomes zero.
5314 */
5315 void
5316partial_unref(partial_T *pt)
5317{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005318 if (pt == NULL)
5319 return;
5320
5321 int done = FALSE;
5322
5323 if (--pt->pt_refcount <= 0)
5324 partial_free(pt);
5325
5326 // If the reference count goes down to one, the funcstack may be the
5327 // only reference and can be freed if no other partials reference it.
5328 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005329 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005330 // careful: if the funcstack is freed it may contain this partial
5331 // and it gets freed as well
5332 if (pt->pt_funcstack != NULL)
5333 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005334
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005335 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005336 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005337 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005338
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005339 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5340 if (pt->pt_loopvars[depth] != NULL
5341 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005342 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005343 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005344 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005345}
5346
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005348 * Return the next (unique) copy ID.
5349 * Used for serializing nested structures.
5350 */
5351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005353{
5354 current_copyID += COPYID_INC;
5355 return current_copyID;
5356}
5357
5358/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005359 * Garbage collection for lists and dictionaries.
5360 *
5361 * We use reference counts to be able to free most items right away when they
5362 * are no longer used. But for composite items it's possible that it becomes
5363 * unused while the reference count is > 0: When there is a recursive
5364 * reference. Example:
5365 * :let l = [1, 2, 3]
5366 * :let d = {9: l}
5367 * :let l[1] = d
5368 *
5369 * Since this is quite unusual we handle this with garbage collection: every
5370 * once in a while find out which lists and dicts are not referenced from any
5371 * variable.
5372 *
5373 * Here is a good reference text about garbage collection (refers to Python
5374 * but it applies to all reference-counting mechanisms):
5375 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005376 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005377
5378/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005380 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005382 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005384garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005385{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005386 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005387 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388 buf_T *buf;
5389 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005390 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005391 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005392
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005393 if (!testing)
5394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005396 want_garbage_collect = FALSE;
5397 may_garbage_collect = FALSE;
5398 garbage_collect_at_exit = FALSE;
5399 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005400
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005401 // The execution stack can grow big, limit the size.
5402 if (exestack.ga_maxlen - exestack.ga_len > 500)
5403 {
5404 size_t new_len;
5405 char_u *pp;
5406 int n;
5407
5408 // Keep 150% of the current size, with a minimum of the growth size.
5409 n = exestack.ga_len / 2;
5410 if (n < exestack.ga_growsize)
5411 n = exestack.ga_growsize;
5412
5413 // Don't make it bigger though.
5414 if (exestack.ga_len + n < exestack.ga_maxlen)
5415 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005416 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005417 pp = vim_realloc(exestack.ga_data, new_len);
5418 if (pp == NULL)
5419 return FAIL;
5420 exestack.ga_maxlen = exestack.ga_len + n;
5421 exestack.ga_data = pp;
5422 }
5423 }
5424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 // We advance by two because we add one for items referenced through
5426 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005427 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005428
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429 /*
5430 * 1. Go through all accessible variables and mark all lists and dicts
5431 * with copyID.
5432 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005434 // Don't free variables in the previous_funccal list unless they are only
5435 // referenced through previous_funccal. This must be first, because if
5436 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005437 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005439 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005440 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005442 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005443 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005444 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5445 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005448 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005449 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5450 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005451 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005452 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005453 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005454 abort = abort || set_ref_in_item(
5455 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005456#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005457 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005458 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5459 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005460 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005461 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005462 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5463 NULL, NULL);
5464#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005467 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005468 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5469 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005471 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005474 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005477 abort = abort || set_ref_in_functions(copyID);
5478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005479 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005480 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005481
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005482 // funcstacks keep variables for closures
5483 abort = abort || set_ref_in_funcstacks(copyID);
5484
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005485 // loopvars keep variables for loop blocks
5486 abort = abort || set_ref_in_loopvars(copyID);
5487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005489 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005490
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005491 // callbacks in buffers
5492 abort = abort || set_ref_in_buffers(copyID);
5493
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005494 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5495 abort = abort || set_ref_in_insexpand_funcs(copyID);
5496
5497 // 'operatorfunc' callback
5498 abort = abort || set_ref_in_opfunc(copyID);
5499
5500 // 'tagfunc' callback
5501 abort = abort || set_ref_in_tagfunc(copyID);
5502
5503 // 'imactivatefunc' and 'imstatusfunc' callbacks
5504 abort = abort || set_ref_in_im_funcs(copyID);
5505
Bram Moolenaar1dced572012-04-05 16:54:08 +02005506#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005507 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005508#endif
5509
Bram Moolenaardb913952012-06-29 12:54:53 +02005510#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005512#endif
5513
5514#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005516#endif
5517
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005518#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005519 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005520 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005521#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005522#ifdef FEAT_NETBEANS_INTG
5523 abort = abort || set_ref_in_nb_channel(copyID);
5524#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005525
Bram Moolenaare3188e22016-05-31 21:13:04 +02005526#ifdef FEAT_TIMERS
5527 abort = abort || set_ref_in_timer(copyID);
5528#endif
5529
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005530#ifdef FEAT_QUICKFIX
5531 abort = abort || set_ref_in_quickfix(copyID);
5532#endif
5533
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005534#ifdef FEAT_TERMINAL
5535 abort = abort || set_ref_in_term(copyID);
5536#endif
5537
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005538#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005539 abort = abort || set_ref_in_popups(copyID);
5540#endif
5541
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005542 abort = abort || set_ref_in_classes(copyID);
5543
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005544 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005545 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005546 /*
5547 * 2. Free lists and dictionaries that are not referenced.
5548 */
5549 did_free = free_unref_items(copyID);
5550
5551 /*
5552 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005553 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005554 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005555 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005556 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005557 else if (p_verbose > 0)
5558 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005559 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005560 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005561
5562 return did_free;
5563}
5564
5565/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005566 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005567 */
5568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005570{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005571 int did_free = FALSE;
5572
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 // Let all "free" functions know that we are here. This means no
5574 // dictionaries, lists, channels or jobs are to be freed, because we will
5575 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005576 in_free_unref_items = TRUE;
5577
5578 /*
5579 * PASS 1: free the contents of the items. We don't free the items
5580 * themselves yet, so that it is possible to decrement refcount counters
5581 */
5582
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005583 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005584 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005585
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005586 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005587 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005588
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005589 // Go through the list of objects and free items without this copyID.
5590 did_free |= object_free_nonref(copyID);
5591
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005592 // Go through the list of classes and free items without this copyID.
5593 did_free |= class_free_nonref(copyID);
5594
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005595#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005596 // Go through the list of jobs and free items without the copyID. This
5597 // must happen before doing channels, because jobs refer to channels, but
5598 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005599 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005601 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005602 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5603#endif
5604
5605 /*
5606 * PASS 2: free the items themselves.
5607 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005608 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005609 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005610 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005611
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005612#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005613 // Go through the list of jobs and free items without the copyID. This
5614 // must happen before doing channels, because jobs refer to channels, but
5615 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005616 free_unused_jobs(copyID, COPYID_MASK);
5617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005618 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005619 free_unused_channels(copyID, COPYID_MASK);
5620#endif
5621
5622 in_free_unref_items = FALSE;
5623
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005624 return did_free;
5625}
5626
5627/*
5628 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005629 * "list_stack" is used to add lists to be marked. Can be NULL.
5630 *
5631 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005632 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005634set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005635{
5636 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005637 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005638 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005639 hashtab_T *cur_ht;
5640 ht_stack_T *ht_stack = NULL;
5641 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005642
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005643 cur_ht = ht;
5644 for (;;)
5645 {
5646 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // Mark each item in the hashtab. If the item contains a hashtab
5649 // it is added to ht_stack, if it contains a list it is added to
5650 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005651 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005652 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005653 if (!HASHITEM_EMPTY(hi))
5654 {
5655 --todo;
5656 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5657 &ht_stack, list_stack);
5658 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005659 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005660
5661 if (ht_stack == NULL)
5662 break;
5663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005664 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005665 cur_ht = ht_stack->ht;
5666 tempitem = ht_stack;
5667 ht_stack = ht_stack->prev;
5668 free(tempitem);
5669 }
5670
5671 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005672}
5673
Dominique Pelle748b3082022-01-08 12:41:16 +00005674#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5675 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005676/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005677 * Mark a dict and its items with "copyID".
5678 * Returns TRUE if setting references failed somehow.
5679 */
5680 int
5681set_ref_in_dict(dict_T *d, int copyID)
5682{
5683 if (d != NULL && d->dv_copyID != copyID)
5684 {
5685 d->dv_copyID = copyID;
5686 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5687 }
5688 return FALSE;
5689}
Dominique Pelle748b3082022-01-08 12:41:16 +00005690#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005691
5692/*
5693 * Mark a list and its items with "copyID".
5694 * Returns TRUE if setting references failed somehow.
5695 */
5696 int
5697set_ref_in_list(list_T *ll, int copyID)
5698{
5699 if (ll != NULL && ll->lv_copyID != copyID)
5700 {
5701 ll->lv_copyID = copyID;
5702 return set_ref_in_list_items(ll, copyID, NULL);
5703 }
5704 return FALSE;
5705}
5706
5707/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005708 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005709 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5710 *
5711 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005712 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005713 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005714set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005715{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005716 listitem_T *li;
5717 int abort = FALSE;
5718 list_T *cur_l;
5719 list_stack_T *list_stack = NULL;
5720 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005721
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 cur_l = l;
5723 for (;;)
5724 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005725 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005726 // Mark each item in the list. If the item contains a hashtab
5727 // it is added to ht_stack, if it contains a list it is added to
5728 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5730 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5731 ht_stack, &list_stack);
5732 if (list_stack == NULL)
5733 break;
5734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005735 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 cur_l = list_stack->list;
5737 tempitem = list_stack;
5738 list_stack = list_stack->prev;
5739 free(tempitem);
5740 }
5741
5742 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005743}
5744
5745/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005746 * Mark the partial in callback 'cb' with "copyID".
5747 */
5748 int
5749set_ref_in_callback(callback_T *cb, int copyID)
5750{
5751 typval_T tv;
5752
5753 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5754 return FALSE;
5755
5756 tv.v_type = VAR_PARTIAL;
5757 tv.vval.v_partial = cb->cb_partial;
5758 return set_ref_in_item(&tv, copyID, NULL, NULL);
5759}
5760
5761/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005762 * Mark the dict "dd" with "copyID".
5763 * Also see set_ref_in_item().
5764 */
5765 static int
5766set_ref_in_item_dict(
5767 dict_T *dd,
5768 int copyID,
5769 ht_stack_T **ht_stack,
5770 list_stack_T **list_stack)
5771{
5772 if (dd == NULL || dd->dv_copyID == copyID)
5773 return FALSE;
5774
5775 // Didn't see this dict yet.
5776 dd->dv_copyID = copyID;
5777 if (ht_stack == NULL)
5778 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5779
5780 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5781 if (newitem == NULL)
5782 return TRUE;
5783
5784 newitem->ht = &dd->dv_hashtab;
5785 newitem->prev = *ht_stack;
5786 *ht_stack = newitem;
5787
5788 return FALSE;
5789}
5790
5791/*
5792 * Mark the list "ll" with "copyID".
5793 * Also see set_ref_in_item().
5794 */
5795 static int
5796set_ref_in_item_list(
5797 list_T *ll,
5798 int copyID,
5799 ht_stack_T **ht_stack,
5800 list_stack_T **list_stack)
5801{
5802 if (ll == NULL || ll->lv_copyID == copyID)
5803 return FALSE;
5804
5805 // Didn't see this list yet.
5806 ll->lv_copyID = copyID;
5807 if (list_stack == NULL)
5808 return set_ref_in_list_items(ll, copyID, ht_stack);
5809
5810 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5811 if (newitem == NULL)
5812 return TRUE;
5813
5814 newitem->list = ll;
5815 newitem->prev = *list_stack;
5816 *list_stack = newitem;
5817
5818 return FALSE;
5819}
5820
5821/*
5822 * Mark the partial "pt" with "copyID".
5823 * Also see set_ref_in_item().
5824 */
5825 static int
5826set_ref_in_item_partial(
5827 partial_T *pt,
5828 int copyID,
5829 ht_stack_T **ht_stack,
5830 list_stack_T **list_stack)
5831{
5832 if (pt == NULL || pt->pt_copyID == copyID)
5833 return FALSE;
5834
5835 // Didn't see this partial yet.
5836 pt->pt_copyID = copyID;
5837
5838 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5839
5840 if (pt->pt_dict != NULL)
5841 {
5842 typval_T dtv;
5843
5844 dtv.v_type = VAR_DICT;
5845 dtv.vval.v_dict = pt->pt_dict;
5846 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5847 }
5848
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005849 if (pt->pt_obj != NULL)
5850 {
5851 typval_T objtv;
5852
5853 objtv.v_type = VAR_OBJECT;
5854 objtv.vval.v_object = pt->pt_obj;
5855 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5856 }
5857
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005858 for (int i = 0; i < pt->pt_argc; ++i)
5859 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5860 ht_stack, list_stack);
5861 // pt_funcstack is handled in set_ref_in_funcstacks()
5862 // pt_loopvars is handled in set_ref_in_loopvars()
5863
5864 return abort;
5865}
5866
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005867#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005868/*
5869 * Mark the job "pt" with "copyID".
5870 * Also see set_ref_in_item().
5871 */
5872 static int
5873set_ref_in_item_job(
5874 job_T *job,
5875 int copyID,
5876 ht_stack_T **ht_stack,
5877 list_stack_T **list_stack)
5878{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005879 typval_T dtv;
5880
5881 if (job == NULL || job->jv_copyID == copyID)
5882 return FALSE;
5883
5884 job->jv_copyID = copyID;
5885 if (job->jv_channel != NULL)
5886 {
5887 dtv.v_type = VAR_CHANNEL;
5888 dtv.vval.v_channel = job->jv_channel;
5889 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5890 }
5891 if (job->jv_exit_cb.cb_partial != NULL)
5892 {
5893 dtv.v_type = VAR_PARTIAL;
5894 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5895 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5896 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005897
5898 return FALSE;
5899}
5900
5901/*
5902 * Mark the channel "ch" with "copyID".
5903 * Also see set_ref_in_item().
5904 */
5905 static int
5906set_ref_in_item_channel(
5907 channel_T *ch,
5908 int copyID,
5909 ht_stack_T **ht_stack,
5910 list_stack_T **list_stack)
5911{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005912 typval_T dtv;
5913
5914 if (ch == NULL || ch->ch_copyID == copyID)
5915 return FALSE;
5916
5917 ch->ch_copyID = copyID;
5918 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5919 {
5920 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5921 jq != NULL; jq = jq->jq_next)
5922 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5923 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5924 cq = cq->cq_next)
5925 if (cq->cq_callback.cb_partial != NULL)
5926 {
5927 dtv.v_type = VAR_PARTIAL;
5928 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5929 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5930 }
5931 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5932 {
5933 dtv.v_type = VAR_PARTIAL;
5934 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5935 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5936 }
5937 }
5938 if (ch->ch_callback.cb_partial != NULL)
5939 {
5940 dtv.v_type = VAR_PARTIAL;
5941 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5942 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5943 }
5944 if (ch->ch_close_cb.cb_partial != NULL)
5945 {
5946 dtv.v_type = VAR_PARTIAL;
5947 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5948 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5949 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005950
5951 return FALSE;
5952}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005953#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005954
5955/*
5956 * Mark the class "cl" with "copyID".
5957 * Also see set_ref_in_item().
5958 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005959 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005960set_ref_in_item_class(
5961 class_T *cl,
5962 int copyID,
5963 ht_stack_T **ht_stack,
5964 list_stack_T **list_stack)
5965{
5966 int abort = FALSE;
5967
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005968 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005969 return FALSE;
5970
5971 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005972 if (cl->class_members_tv != NULL)
5973 {
5974 // The "class_members_tv" table is allocated only for regular classes
5975 // and not for interfaces.
5976 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5977 abort = abort || set_ref_in_item(
5978 &cl->class_members_tv[i],
5979 copyID, ht_stack, list_stack);
5980 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005981
5982 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5983 abort = abort || set_ref_in_func(NULL,
5984 cl->class_class_functions[i], copyID);
5985
5986 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5987 abort = abort || set_ref_in_func(NULL,
5988 cl->class_obj_methods[i], copyID);
5989
5990 return abort;
5991}
5992
5993/*
5994 * Mark the object "cl" with "copyID".
5995 * Also see set_ref_in_item().
5996 */
5997 static int
5998set_ref_in_item_object(
5999 object_T *obj,
6000 int copyID,
6001 ht_stack_T **ht_stack,
6002 list_stack_T **list_stack)
6003{
6004 int abort = FALSE;
6005
6006 if (obj == NULL || obj->obj_copyID == copyID)
6007 return FALSE;
6008
6009 obj->obj_copyID = copyID;
6010
6011 // The typval_T array is right after the object_T.
6012 typval_T *mtv = (typval_T *)(obj + 1);
6013 for (int i = 0; !abort
6014 && i < obj->obj_class->class_obj_member_count; ++i)
6015 abort = abort || set_ref_in_item(mtv + i, copyID,
6016 ht_stack, list_stack);
6017
6018 return abort;
6019}
6020
6021/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006022 * Mark all lists, dicts and other container types referenced through typval
6023 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006024 * "list_stack" is used to add lists to be marked. Can be NULL.
6025 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6026 *
6027 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030set_ref_in_item(
6031 typval_T *tv,
6032 int copyID,
6033 ht_stack_T **ht_stack,
6034 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006035{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006036 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006037
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006038 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006039 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006040 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006041 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6042 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006043
6044 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006045 return set_ref_in_item_list(tv->vval.v_list, copyID,
6046 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006047
6048 case VAR_FUNC:
6049 {
6050 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6051 break;
6052 }
6053
6054 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006055 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6056 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006057
6058 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006059#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006060 return set_ref_in_item_job(tv->vval.v_job, copyID,
6061 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006062#else
6063 break;
6064#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006065
6066 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006067#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006068 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006069 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006070#else
6071 break;
6072#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006073
6074 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006075 return set_ref_in_item_class(tv->vval.v_class, copyID,
6076 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006077
6078 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006079 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006080 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006081
6082 case VAR_UNKNOWN:
6083 case VAR_ANY:
6084 case VAR_VOID:
6085 case VAR_BOOL:
6086 case VAR_SPECIAL:
6087 case VAR_NUMBER:
6088 case VAR_FLOAT:
6089 case VAR_STRING:
6090 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006091 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006092 case VAR_INSTR:
6093 // Types that do not contain any other item
6094 break;
6095 }
6096
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006097 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006098}
6099
Bram Moolenaar8c711452005-01-14 21:53:12 +00006100/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101 * Return a string with the string representation of a variable.
6102 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006104 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006105 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006106 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006107 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006108 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6109 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006110 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006112 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006113echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006114 typval_T *tv,
6115 char_u **tofree,
6116 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006117 int copyID,
6118 int echo_style,
6119 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006120 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 static int recurse = 0;
6123 char_u *r = NULL;
6124
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006126 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006127 if (!did_echo_string_emsg)
6128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006129 // Only give this message once for a recursive call to avoid
6130 // flooding the user with errors. And stop iterating over lists
6131 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006132 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006133 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006134 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006136 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137 }
6138 ++recurse;
6139
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 switch (tv->v_type)
6141 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006142 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006143 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006144 {
6145 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006146 r = tv->vval.v_string;
6147 if (r == NULL)
6148 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006149 }
6150 else
6151 {
6152 *tofree = string_quote(tv->vval.v_string, FALSE);
6153 r = *tofree;
6154 }
6155 break;
6156
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006158 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006159 char_u buf[MAX_FUNC_NAME_LEN];
6160
6161 if (echo_style)
6162 {
LemonBoya5d35902022-04-29 21:15:02 +01006163 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6164 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006165 buf, MAX_FUNC_NAME_LEN);
6166 if (r == buf)
6167 {
6168 r = vim_strsave(buf);
6169 *tofree = r;
6170 }
6171 else
6172 *tofree = NULL;
6173 }
6174 else
6175 {
6176 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6177 : make_ufunc_name_readable(
6178 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6179 TRUE);
6180 r = *tofree;
6181 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006182 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006184
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006185 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006186 {
6187 partial_T *pt = tv->vval.v_partial;
6188 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006189 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006190 garray_T ga;
6191 int i;
6192 char_u *tf;
6193
6194 ga_init2(&ga, 1, 100);
6195 ga_concat(&ga, (char_u *)"function(");
6196 if (fname != NULL)
6197 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006198 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006199 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006200 && vim_isupper(fname[1]))
6201 {
6202 ga_concat(&ga, (char_u *)"'g:");
6203 ga_concat(&ga, fname + 1);
6204 }
6205 else
6206 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006207 vim_free(fname);
6208 }
6209 if (pt != NULL && pt->pt_argc > 0)
6210 {
6211 ga_concat(&ga, (char_u *)", [");
6212 for (i = 0; i < pt->pt_argc; ++i)
6213 {
6214 if (i > 0)
6215 ga_concat(&ga, (char_u *)", ");
6216 ga_concat(&ga,
6217 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6218 vim_free(tf);
6219 }
6220 ga_concat(&ga, (char_u *)"]");
6221 }
6222 if (pt != NULL && pt->pt_dict != NULL)
6223 {
6224 typval_T dtv;
6225
6226 ga_concat(&ga, (char_u *)", ");
6227 dtv.v_type = VAR_DICT;
6228 dtv.vval.v_dict = pt->pt_dict;
6229 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6230 vim_free(tf);
6231 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006232 // terminate with ')' and a NUL
6233 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006234
6235 *tofree = ga.ga_data;
6236 r = *tofree;
6237 break;
6238 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006239
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006240 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006241 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006242 break;
6243
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006245 if (tv->vval.v_list == NULL)
6246 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006247 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006248 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006249 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006250 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006251 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6252 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006253 {
6254 *tofree = NULL;
6255 r = (char_u *)"[...]";
6256 }
6257 else
6258 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006259 int old_copyID = tv->vval.v_list->lv_copyID;
6260
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006261 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006262 *tofree = list2string(tv, copyID, restore_copyID);
6263 if (restore_copyID)
6264 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006265 r = *tofree;
6266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006267 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006268
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006270 if (tv->vval.v_dict == NULL)
6271 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006272 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006273 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006274 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006275 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006276 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6277 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006278 {
6279 *tofree = NULL;
6280 r = (char_u *)"{...}";
6281 }
6282 else
6283 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006284 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006285
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006286 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006287 *tofree = dict2string(tv, copyID, restore_copyID);
6288 if (restore_copyID)
6289 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006290 r = *tofree;
6291 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006295 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006296 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006298 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006299 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006300 break;
6301
Bram Moolenaar835dc632016-02-07 14:27:38 +01006302 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006303 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006304#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006306 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6307 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006308 if (composite_val)
6309 {
6310 *tofree = string_quote(r, FALSE);
6311 r = *tofree;
6312 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006313#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006315
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006316 case VAR_INSTR:
6317 *tofree = NULL;
6318 r = (char_u *)"instructions";
6319 break;
6320
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006321 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006322 {
6323 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006324 char *s = "class";
6325 if (IS_INTERFACE(cl))
6326 s = "interface";
6327 else if (IS_ENUM(cl))
6328 s = "enum";
6329 size_t len = STRLEN(s) + 1 +
6330 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006331 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006332 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006333 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6334 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006335 break;
6336
6337 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006338 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6339 echo_style, restore_copyID,
6340 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006341 break;
6342
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006343 case VAR_FLOAT:
6344 *tofree = NULL;
6345 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6346 r = numbuf;
6347 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006348
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006349 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006350 case VAR_SPECIAL:
6351 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006352 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006353 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006354
6355 case VAR_TYPEALIAS:
6356 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6357 r = *tofree;
6358 if (r == NULL)
6359 r = (char_u *)"";
6360 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006361 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006362
Bram Moolenaar8502c702014-06-17 12:51:16 +02006363 if (--recurse == 0)
6364 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006366}
6367
6368/*
6369 * Return a string with the string representation of a variable.
6370 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6371 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006372 * Does not put quotes around strings, as ":echo" displays values.
6373 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6374 * May return NULL.
6375 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006376 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006377echo_string(
6378 typval_T *tv,
6379 char_u **tofree,
6380 char_u *numbuf,
6381 int copyID)
6382{
6383 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6384}
6385
6386/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006387 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6388 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006389 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006390 */
6391 int
6392buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6393{
6394 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006395 char_u *t;
6396 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006397
6398 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6399 return -1;
6400
6401 if (lnum > buf->b_ml.ml_line_count)
6402 lnum = buf->b_ml.ml_line_count;
6403
6404 str = ml_get_buf(buf, lnum, FALSE);
6405 if (str == NULL)
6406 return -1;
6407
6408 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006409 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006410
Bram Moolenaar91458462021-01-13 20:08:38 +01006411 // count the number of characters
6412 t = str;
6413 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6414 t += mb_ptr2len(t);
6415
6416 // In insert mode, when the cursor is at the end of a non-empty line,
6417 // byteidx points to the NUL character immediately past the end of the
6418 // string. In this case, add one to the character count.
6419 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6420 count++;
6421
6422 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006423}
6424
6425/*
6426 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006427 * byte index. Works only for loaded buffers. Returns -1 on failure.
6428 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006429 */
6430 int
6431buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6432{
6433 char_u *str;
6434 char_u *t;
6435
6436 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6437 return -1;
6438
6439 if (lnum > buf->b_ml.ml_line_count)
6440 lnum = buf->b_ml.ml_line_count;
6441
6442 str = ml_get_buf(buf, lnum, FALSE);
6443 if (str == NULL)
6444 return -1;
6445
6446 // Convert the character offset to a byte offset
6447 t = str;
6448 while (*t != NUL && --charidx > 0)
6449 t += mb_ptr2len(t);
6450
Bram Moolenaar91458462021-01-13 20:08:38 +01006451 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006452}
6453
6454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006456 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006459var2fpos(
6460 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006461 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006462 int *fnum, // set to fnum for '0, 'A, etc.
6463 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006465 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006467 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006470 if (varp->v_type == VAR_LIST)
6471 {
6472 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006473 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006474 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006475 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006476
6477 l = varp->vval.v_list;
6478 if (l == NULL)
6479 return NULL;
6480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006482 pos.lnum = list_find_nr(l, 0L, &error);
6483 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006484 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006485 if (charcol)
6486 len = (long)mb_charlen(ml_get(pos.lnum));
6487 else
John Marriottbfcc8952024-03-11 22:04:45 +01006488 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006489
Bram Moolenaarec65d772020-08-20 22:29:12 +02006490 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006491 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006492 li = list_find(l, 1L);
6493 if (li != NULL && li->li_tv.v_type == VAR_STRING
6494 && li->li_tv.vval.v_string != NULL
6495 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006496 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006497 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006498 }
6499 else
6500 {
6501 pos.col = list_find_nr(l, 1L, &error);
6502 if (error)
6503 return NULL;
6504 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006507 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006508 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006509 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006511 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006512 pos.coladd = list_find_nr(l, 2L, &error);
6513 if (error)
6514 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006515
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006516 return &pos;
6517 }
6518
Bram Moolenaarc5809432021-03-27 21:23:30 +01006519 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6520 return NULL;
6521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006522 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006523 if (name == NULL)
6524 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006525
6526 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006527 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006528 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006529 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006530 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006531 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006532 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006533 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006534 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006535 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006536 pos = VIsual;
6537 else
6538 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006539 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006540 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006541 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006543 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006544 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6546 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006547 pos = *pp;
6548 }
6549 if (pos.lnum != 0)
6550 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006551 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006552 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6553 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006555
Bram Moolenaara5525202006-03-02 22:52:09 +00006556 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006557
Bram Moolenaar477933c2007-07-17 14:32:23 +00006558 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006559 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006560 // the "w_valid" flags are not reset when moving the cursor, but they
6561 // do matter for update_topline() and validate_botline().
6562 check_cursor_moved(curwin);
6563
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006564 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006565 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006566 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 // In silent Ex mode topline is zero, but that's not a valid line
6569 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006570 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006571 return &pos;
6572 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006573 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006574 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006575 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006576 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006577 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006578 return &pos;
6579 }
6580 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006581 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006583 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 {
6585 pos.lnum = curbuf->b_ml.ml_line_count;
6586 pos.col = 0;
6587 }
6588 else
6589 {
6590 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006591 if (charcol)
6592 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6593 else
John Marriottbfcc8952024-03-11 22:04:45 +01006594 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596 return &pos;
6597 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006598 if (in_vim9script())
6599 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 return NULL;
6601}
6602
6603/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006604 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006605 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006606 * Note that the column is passed on as-is, the caller may want to decrement
6607 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006608 * If "charcol" is TRUE use the column as the character index instead of the
6609 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006610 * Return FAIL when conversion is not possible, doesn't check the position for
6611 * validity.
6612 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614list2fpos(
6615 typval_T *arg,
6616 pos_T *posp,
6617 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006618 colnr_T *curswantp,
6619 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006620{
6621 list_T *l = arg->vval.v_list;
6622 long i = 0;
6623 long n;
6624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006625 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6626 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006627 if (arg->v_type != VAR_LIST
6628 || l == NULL
6629 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006630 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006631 return FAIL;
6632
6633 if (fnump != NULL)
6634 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006635 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006636 if (n < 0)
6637 return FAIL;
6638 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006639 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006640 *fnump = n;
6641 }
6642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006644 if (n < 0)
6645 return FAIL;
6646 posp->lnum = n;
6647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006649 if (n < 0)
6650 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006651 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006652 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006653 if (charcol)
6654 {
6655 buf_T *buf;
6656
6657 // Get the text for the specified line in a loaded buffer
6658 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6659 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6660 return FAIL;
6661
Bram Moolenaar79f23442022-10-10 12:42:57 +01006662 n = buf_charidx_to_byteidx(buf,
6663 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006664 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006665 posp->col = n;
6666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006667 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006668 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006669 posp->coladd = 0;
6670 else
6671 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006672
Bram Moolenaar493c1782014-05-28 14:34:46 +02006673 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006675
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006676 return OK;
6677}
6678
6679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 * Get the length of an environment variable name.
6681 * Advance "arg" to the first character after the name.
6682 * Return 0 for error.
6683 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 char_u *p;
6688 int len;
6689
6690 for (p = *arg; vim_isIDc(*p); ++p)
6691 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006692 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 return 0;
6694
6695 len = (int)(p - *arg);
6696 *arg = p;
6697 return len;
6698}
6699
6700/*
6701 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006702 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 * Return 0 if something is wrong.
6704 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 char_u *p;
6709 int len;
6710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006713 {
6714 if (*p == ':')
6715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006716 // "s:" is start of "s:var", but "n:" is not and can be used in
6717 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006718 len = (int)(p - *arg);
6719 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6720 || len > 1)
6721 break;
6722 }
6723 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006724 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 return 0;
6726
6727 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006728 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 return len;
6731}
6732
6733/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006734 * Get the length of the name of a variable or function.
6735 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006737 * Return -1 if curly braces expansion failed.
6738 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 * If the name contains 'magic' {}'s, expand them and return the
6740 * expanded name in an allocated string via 'alias' - caller must free.
6741 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743get_name_len(
6744 char_u **arg,
6745 char_u **alias,
6746 int evaluate,
6747 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 char_u *p;
6751 char_u *expr_start;
6752 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006754 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6757 && (*arg)[2] == (int)KE_SNR)
6758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006759 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 *arg += 3;
6761 return get_id_len(arg) + 3;
6762 }
6763 len = eval_fname_script(*arg);
6764 if (len > 0)
6765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006766 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 *arg += len;
6768 }
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006771 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006773 p = find_name_end(*arg, &expr_start, &expr_end,
6774 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (expr_start != NULL)
6776 {
6777 char_u *temp_string;
6778
6779 if (!evaluate)
6780 {
6781 len += (int)(p - *arg);
6782 *arg = skipwhite(p);
6783 return len;
6784 }
6785
6786 /*
6787 * Include any <SID> etc in the expanded string:
6788 * Thus the -len here.
6789 */
6790 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6791 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006792 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 *alias = temp_string;
6794 *arg = skipwhite(p);
6795 return (int)STRLEN(temp_string);
6796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006799 // Only give an error when there is something, otherwise it will be
6800 // reported at a higher level.
6801 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006802 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 return len;
6805}
6806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807/*
6808 * Find the end of a variable or function name, taking care of magic braces.
6809 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6810 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006811 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 * Return a pointer to just after the name. Equal to "arg" if there is no
6813 * valid name.
6814 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006815 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006816find_name_end(
6817 char_u *arg,
6818 char_u **expr_start,
6819 char_u **expr_end,
6820 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006822 int mb_nest = 0;
6823 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006825 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006826 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006828 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006830 *expr_start = NULL;
6831 *expr_end = NULL;
6832 }
6833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006834 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006835 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006836 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006837 return arg;
6838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006839 for (p = arg; *p != NUL
6840 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006841 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006842 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006843 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006845 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006846 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006847 if (*p == '\'')
6848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006849 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006850 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006851 ;
6852 if (*p == NUL)
6853 break;
6854 }
6855 else if (*p == '"')
6856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006857 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006858 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006859 if (*p == '\\' && p[1] != NUL)
6860 ++p;
6861 if (*p == NUL)
6862 break;
6863 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006864 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006866 // "s:" is start of "s:var", but "n:" is not and can be used in
6867 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006868 len = (int)(p - arg);
6869 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006870 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006871 break;
6872 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006874 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006876 if (*p == '[')
6877 ++br_nest;
6878 else if (*p == ']')
6879 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006881
zeertzjqa93d9cd2023-05-02 16:25:47 +01006882 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006884 if (*p == '{')
6885 {
6886 mb_nest++;
6887 if (expr_start != NULL && *expr_start == NULL)
6888 *expr_start = p;
6889 }
6890 else if (*p == '}')
6891 {
6892 mb_nest--;
6893 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6894 *expr_end = p;
6895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
6898
6899 return p;
6900}
6901
6902/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006903 * Expands out the 'magic' {}'s in a variable/function name.
6904 * Note that this can call itself recursively, to deal with
6905 * constructs like foo{bar}{baz}{bam}
6906 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6907 * "in_start" ^
6908 * "expr_start" ^
6909 * "expr_end" ^
6910 * "in_end" ^
6911 *
6912 * Returns a new allocated string, which the caller must free.
6913 * Returns NULL for failure.
6914 */
6915 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006916make_expanded_name(
6917 char_u *in_start,
6918 char_u *expr_start,
6919 char_u *expr_end,
6920 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006921{
6922 char_u c1;
6923 char_u *retval = NULL;
6924 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006925
6926 if (expr_end == NULL || in_end == NULL)
6927 return NULL;
6928 *expr_start = NUL;
6929 *expr_end = NUL;
6930 c1 = *in_end;
6931 *in_end = NUL;
6932
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006933 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006934 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006935 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006936 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6937 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006938 if (retval != NULL)
6939 {
6940 STRCPY(retval, in_start);
6941 STRCAT(retval, temp_result);
6942 STRCAT(retval, expr_end + 1);
6943 }
6944 }
6945 vim_free(temp_result);
6946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006947 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006948 *expr_start = '{';
6949 *expr_end = '}';
6950
6951 if (retval != NULL)
6952 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006953 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006954 if (expr_start != NULL)
6955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006956 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006957 temp_result = make_expanded_name(retval, expr_start,
6958 expr_end, temp_result);
6959 vim_free(retval);
6960 retval = temp_result;
6961 }
6962 }
6963
6964 return retval;
6965}
6966
6967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006974 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006975}
6976
6977/*
6978 * Return TRUE if character "c" can be used as the first character in a
6979 * variable or function name (excluding '{' and '}').
6980 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006983{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006984 return ASCII_ISALPHA(c) || c == '_';
6985}
6986
6987/*
6988 * Return TRUE if character "c" can be used as the first character of a
6989 * dictionary key.
6990 */
6991 int
6992eval_isdictc(int c)
6993{
6994 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995}
6996
6997/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006998 * Handle:
6999 * - expr[expr], expr[expr:expr] subscript
7000 * - ".name" lookup
7001 * - function call with Funcref variable: func(expr)
7002 * - method call: var->method()
7003 *
7004 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007005 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007008handle_subscript(
7009 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007010 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007011 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007012 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007013 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007014{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007015 int evaluate = evalarg != NULL
7016 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007017 int ret = OK;
7018 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007019 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007020 int getnext;
7021 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007022
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007023 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007024 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007025 // When at the end of the line and ".name" or "->{" or "->X" follows in
7026 // the next line then consume the line break.
7027 p = eval_next_non_blank(*arg, evalarg, &getnext);
7028 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007029 && ((*p == '.'
7030 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7031 || rettv->v_type == VAR_CLASS
7032 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007033 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7034 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7035 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007036 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007037 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007038 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007039 check_white = FALSE;
7040 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007041
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007042 if (rettv->v_type == VAR_ANY)
7043 {
7044 char_u *exp_name;
7045 int cc;
7046 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007047 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007048 type_T *type;
7049
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007050 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007051 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007052 if (**arg != '.')
7053 {
7054 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007055 semsg(_(e_expected_dot_after_name_str),
7056 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007057 ret = FAIL;
7058 break;
7059 }
7060 ++*arg;
7061 if (IS_WHITE_OR_NUL(**arg))
7062 {
7063 if (verbose)
7064 emsg(_(e_no_white_space_allowed_after_dot));
7065 ret = FAIL;
7066 break;
7067 }
7068
7069 // isolate the name
7070 exp_name = *arg;
7071 while (eval_isnamec(**arg))
7072 ++*arg;
7073 cc = **arg;
7074 **arg = NUL;
7075
7076 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007077 evalarg == NULL ? NULL : evalarg->eval_cctx,
7078 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007079 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007080
7081 if (idx < 0 && ufunc == NULL)
7082 {
7083 ret = FAIL;
7084 break;
7085 }
7086 if (idx >= 0)
7087 {
7088 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7089 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7090
7091 copy_tv(sv->sv_tv, rettv);
7092 }
7093 else
7094 {
7095 rettv->v_type = VAR_FUNC;
7096 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7097 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007098 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007099 }
7100
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007101 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7102 || rettv->v_type == VAR_PARTIAL))
7103 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007104 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007105 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7106 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007107
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007108 // Stop the expression evaluation when immediately aborting on
7109 // error, or when an interrupt occurred or an exception was thrown
7110 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007111 if (aborting())
7112 {
7113 if (ret == OK)
7114 clear_tv(rettv);
7115 ret = FAIL;
7116 }
7117 dict_unref(selfdict);
7118 selfdict = NULL;
7119 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007120 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007121 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007122 if (in_vim9script())
7123 *arg = skipwhite(p + 2);
7124 else
7125 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007126 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007127 {
zeertzjqc481ad32023-03-11 16:18:51 +00007128 emsg(_(e_no_white_space_allowed_before_parenthesis));
7129 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007130 }
zeertzjqc481ad32023-03-11 16:18:51 +00007131 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7132 // expr->{lambda}() or expr->(lambda)()
7133 ret = eval_lambda(arg, rettv, evalarg, verbose);
7134 else
7135 // expr->name()
7136 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007137 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007138 // "." is ".name" lookup when we found a dict or when evaluating and
7139 // scriptversion is at least 2, where string concatenation is "..".
7140 else if (**arg == '['
7141 || (**arg == '.' && (rettv->v_type == VAR_DICT
7142 || (!evaluate
7143 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007144 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007145 {
7146 dict_unref(selfdict);
7147 if (rettv->v_type == VAR_DICT)
7148 {
7149 selfdict = rettv->vval.v_dict;
7150 if (selfdict != NULL)
7151 ++selfdict->dv_refcount;
7152 }
7153 else
7154 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007155 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007156 {
7157 clear_tv(rettv);
7158 ret = FAIL;
7159 }
7160 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007161 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7162 || rettv->v_type == VAR_OBJECT))
7163 {
7164 // class member: SomeClass.varname
7165 // class method: SomeClass.SomeMethod()
7166 // class constructor: SomeClass.new()
7167 // object member: someObject.varname
7168 // object method: someObject.SomeMethod()
7169 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7170 {
7171 clear_tv(rettv);
7172 ret = FAIL;
7173 }
7174 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007175 else
7176 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007177 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007178
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007179 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7180 // Don't do this when "Func" is already a partial that was bound
7181 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007182 if (selfdict != NULL
7183 && (rettv->v_type == VAR_FUNC
7184 || (rettv->v_type == VAR_PARTIAL
7185 && (rettv->vval.v_partial->pt_auto
7186 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007187 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007188
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007189 dict_unref(selfdict);
7190 return ret;
7191}
7192
7193/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 * Make a copy of an item.
7195 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007196 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7198 * reference to an already copied list/dict can be used.
7199 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202item_copy(
7203 typval_T *from,
7204 typval_T *to,
7205 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007206 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208{
7209 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007210 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007214 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007216 }
7217 ++recurse;
7218
7219 switch (from->v_type)
7220 {
7221 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007222 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 case VAR_STRING:
7224 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007225 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007226 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007227 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007228 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007229 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007230 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007231 case VAR_CLASS:
7232 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007233 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 copy_tv(from, to);
7235 break;
7236 case VAR_LIST:
7237 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007238 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239 if (from->vval.v_list == NULL)
7240 to->vval.v_list = NULL;
7241 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007243 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007244 to->vval.v_list = from->vval.v_list->lv_copylist;
7245 ++to->vval.v_list->lv_refcount;
7246 }
7247 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007248 to->vval.v_list = list_copy(from->vval.v_list,
7249 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007250 if (to->vval.v_list == NULL)
7251 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007252 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007253 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007255 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256 case VAR_DICT:
7257 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007258 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007259 if (from->vval.v_dict == NULL)
7260 to->vval.v_dict = NULL;
7261 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007263 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007264 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7265 ++to->vval.v_dict->dv_refcount;
7266 }
7267 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007268 to->vval.v_dict = dict_copy(from->vval.v_dict,
7269 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007270 if (to->vval.v_dict == NULL)
7271 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007272 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007273 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007274 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007276 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 }
7279 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007280 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281}
7282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283 void
7284echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7285{
7286 char_u *tofree;
7287 char_u numbuf[NUMBUFLEN];
7288 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7289
7290 if (*atstart)
7291 {
7292 *atstart = FALSE;
7293 // Call msg_start() after eval1(), evaluating the expression
7294 // may cause a message to appear.
7295 if (with_space)
7296 {
7297 // Mark the saved text as finishing the line, so that what
7298 // follows is displayed on a new line when scrolling back
7299 // at the more prompt.
7300 msg_sb_eol();
7301 msg_start();
7302 }
7303 }
7304 else if (with_space)
7305 msg_puts_attr(" ", echo_attr);
7306
7307 if (p != NULL)
7308 for ( ; *p != NUL && !got_int; ++p)
7309 {
7310 if (*p == '\n' || *p == '\r' || *p == TAB)
7311 {
7312 if (*p != TAB && *needclr)
7313 {
7314 // remove any text still there from the command
7315 msg_clr_eos();
7316 *needclr = FALSE;
7317 }
7318 msg_putchar_attr(*p, echo_attr);
7319 }
7320 else
7321 {
7322 if (has_mbyte)
7323 {
7324 int i = (*mb_ptr2len)(p);
7325
7326 (void)msg_outtrans_len_attr(p, i, echo_attr);
7327 p += i - 1;
7328 }
7329 else
7330 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7331 }
7332 }
7333 vim_free(tofree);
7334}
7335
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 * ":echo expr1 ..." print each argument separated with a space, add a
7338 * newline at the end.
7339 * ":echon expr1 ..." print each argument plain.
7340 */
7341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007346 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 int needclr = TRUE;
7348 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007349 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007350 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007351 evalarg_T evalarg;
7352
Bram Moolenaare707c882020-07-01 13:07:37 +02007353 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
7355 if (eap->skip)
7356 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007357 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007359 // If eval1() causes an error message the text from the command may
7360 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007361 need_clr_eos = needclr;
7362
Bram Moolenaar68db9962021-05-09 23:19:22 +02007363 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007364 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
7366 /*
7367 * Report the invalid expression unless the expression evaluation
7368 * has been cancelled due to an aborting error, an interrupt, or an
7369 * exception.
7370 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007371 if (!aborting() && did_emsg == did_emsg_before
7372 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007373 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007374 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 break;
7376 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007377 need_clr_eos = FALSE;
7378
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007380 {
7381 if (rettv.v_type == VAR_VOID)
7382 {
7383 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7384 break;
7385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007387 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 arg = skipwhite(arg);
7391 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007392 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007393 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394
7395 if (eap->skip)
7396 --emsg_skip;
7397 else
7398 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007399 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (needclr)
7401 msg_clr_eos();
7402 if (eap->cmdidx == CMD_echo)
7403 msg_end();
7404 }
7405}
7406
7407/*
7408 * ":echohl {name}".
7409 */
7410 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007413 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415
7416/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007417 * Returns the :echo attribute
7418 */
7419 int
7420get_echo_attr(void)
7421{
7422 return echo_attr;
7423}
7424
7425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 * ":execute expr1 ..." execute the result of an expression.
7427 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007428 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007430 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 * Each gets spaces around each argument and a newline at the end for
7432 * echo commands
7433 */
7434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int ret = OK;
7440 char_u *p;
7441 garray_T ga;
7442 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007443 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
7445 ga_init2(&ga, 1, 80);
7446
7447 if (eap->skip)
7448 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007449 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007451 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007452 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454
7455 if (!eap->skip)
7456 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007457 char_u buf[NUMBUFLEN];
7458
7459 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007460 {
7461 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7462 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007463 semsg(_(e_using_invalid_value_as_string_str),
7464 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007465 p = NULL;
7466 }
7467 else
7468 p = tv_get_string_buf(&rettv, buf);
7469 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007470 else
7471 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007472 if (p == NULL)
7473 {
7474 clear_tv(&rettv);
7475 ret = FAIL;
7476 break;
7477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 len = (int)STRLEN(p);
7479 if (ga_grow(&ga, len + 2) == FAIL)
7480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 ret = FAIL;
7483 break;
7484 }
7485 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 ga.ga_len += len;
7489 }
7490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 arg = skipwhite(arg);
7493 }
7494
7495 if (ret != FAIL && ga.ga_data != NULL)
7496 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007497 // use the first line of continuation lines for messages
7498 SOURCING_LNUM = start_lnum;
7499
Bram Moolenaar37fef162022-08-29 18:16:32 +01007500 if (eap->cmdidx == CMD_echomsg
7501 || eap->cmdidx == CMD_echowindow
7502 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007504 // Mark the already saved text as finishing the line, so that what
7505 // follows is displayed on a new line when scrolling back at the
7506 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007507 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007508 }
7509
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007510 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007511 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007512 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007513 out_flush();
7514 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007515 else if (eap->cmdidx == CMD_echowindow)
7516 {
7517#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007518 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007519#endif
7520 msg_attr(ga.ga_data, echo_attr);
7521#ifdef HAS_MESSAGE_WINDOW
7522 end_echowindow();
7523#endif
7524 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007525 else if (eap->cmdidx == CMD_echoconsole)
7526 {
7527 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7528 ui_write((char_u *)"\r\n", 2, TRUE);
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 else if (eap->cmdidx == CMD_echoerr)
7531 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007532 int save_did_emsg = did_emsg;
7533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007534 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007535 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (!force_abort)
7537 did_emsg = save_did_emsg;
7538 }
7539 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007540 {
7541 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7542
7543 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7544 sticky_cmdmod_flags = cmdmod.cmod_flags
7545 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007547 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007548 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551
7552 ga_clear(&ga);
7553
7554 if (eap->skip)
7555 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007556 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557}
7558
7559/*
7560 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7561 * "arg" points to the "&" or '+' when called, to "option" when returning.
7562 * Returns NULL when no option name found. Otherwise pointer to the char
7563 * after the option name.
7564 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007565 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007566find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 char_u *p = *arg;
7569
7570 ++p;
7571 if (*p == 'g' && p[1] == ':')
7572 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007573 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 p += 2;
7575 }
7576 else if (*p == 'l' && p[1] == ':')
7577 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007578 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 p += 2;
7580 }
7581 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007582 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
7584 if (!ASCII_ISALPHA(*p))
7585 return NULL;
7586 *arg = p;
7587
7588 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007589 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 else
7591 while (ASCII_ISALPHA(*p))
7592 ++p;
7593 return p;
7594}
7595
7596/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007597 * Display script name where an item was last set.
7598 * Should only be invoked when 'verbose' is non-zero.
7599 */
7600 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007601last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007602{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007603 char_u *p;
7604
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007605 if (script_ctx.sc_sid == 0)
7606 return;
7607
7608 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7609 if (p == NULL)
7610 return;
7611
7612 verbose_enter();
7613 msg_puts(_("\n\tLast set from "));
7614 msg_puts((char *)p);
7615 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007616 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007617 msg_puts(_(line_msg));
7618 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007619 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007620 verbose_leave();
7621 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007622}
7623
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007624#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625
7626/*
7627 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007628 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 * "flags" can be "g" to do a global substitute.
7630 * Returns an allocated string, NULL for error.
7631 */
7632 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007633do_string_sub(
7634 char_u *str,
7635 char_u *pat,
7636 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007637 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007638 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
7640 int sublen;
7641 regmatch_T regmatch;
7642 int i;
7643 int do_all;
7644 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007645 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 garray_T ga;
7647 char_u *ret;
7648 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007649 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007651 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007653 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
7655 ga_init2(&ga, 1, 200);
7656
7657 do_all = (flags[0] == 'g');
7658
7659 regmatch.rm_ic = p_ic;
7660 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7661 if (regmatch.regprog != NULL)
7662 {
7663 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007664 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007667 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007668 if (regmatch.startp[0] == regmatch.endp[0])
7669 {
7670 if (zero_width == regmatch.startp[0])
7671 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007672 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007673 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007674 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7675 (size_t)i);
7676 ga.ga_len += i;
7677 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007678 continue;
7679 }
7680 zero_width = regmatch.startp[0];
7681 }
7682
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 /*
7684 * Get some space for a temporary buffer to do the substitution
7685 * into. It will contain:
7686 * - The text up to where the match is.
7687 * - The substituted text.
7688 * - The text after the match.
7689 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007690 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007691 if (sublen <= 0)
7692 {
7693 ga_clear(&ga);
7694 break;
7695 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007696 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7698 {
7699 ga_clear(&ga);
7700 break;
7701 }
7702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007703 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 i = (int)(regmatch.startp[0] - tail);
7705 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007706 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007707 (void)vim_regsub(&regmatch, sub, expr,
7708 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7709 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007711 tail = regmatch.endp[0];
7712 if (*tail == NUL)
7713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 if (!do_all)
7715 break;
7716 }
7717
7718 if (ga.ga_data != NULL)
7719 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7720
Bram Moolenaar473de612013-06-08 18:19:48 +02007721 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723
7724 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7725 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007726 if (p_cpo == empty_option)
7727 p_cpo = save_cpo;
7728 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007730 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007731 // If it's still empty it was changed and restored, need to restore in
7732 // the complicated way.
7733 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007734 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007735 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737
7738 return ret;
7739}