blob: d9fbec234cba753a4cc697175466094e269e4467 [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;
136 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000138 evalarg->eval_getline = eap->getline;
139 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 Rael64885642023-10-04 20:16:22 +02001053get_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001054{
1055#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001056 ch_log(NULL, "LKVAR: get_lval_root(): name %s", lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001057#endif
Ernie Rael64885642023-10-04 20:16:22 +02001058 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001059 {
Ernie Rael64885642023-10-04 20:16:22 +02001060 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001061 {
1062 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001063 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001064 int m_idx;
1065 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1066 lp->ll_name_end - lp->ll_name, &m_idx);
1067 if (m != NULL)
1068 {
1069 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001070 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001071 lp->ll_oi = m_idx;
1072 lp->ll_valtype = m->ocm_type;
1073 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1074#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001075 ch_log(NULL, "LKVAR: ... class member %s.%s",
1076 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001077#endif
1078 return;
1079 }
1080 }
1081 }
1082
1083#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001084 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001085#endif
Ernie Rael64885642023-10-04 20:16:22 +02001086 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001087 lp->ll_is_root = TRUE;
1088}
1089
1090/*
Ernie Rael64885642023-10-04 20:16:22 +02001091 * Check if the class has permission to access the member.
1092 * Returns OK or FAIL.
1093 */
1094 static int
1095get_lval_check_access(
1096 class_T *cl_exec, // executing class, NULL if :def or script level
1097 class_T *cl, // class which contains the member
1098 ocmember_T *om, // member being accessed
1099 char_u *p, // char after member name
1100 int flags) // GLV flags to check if writing to lval
1101{
1102#ifdef LOG_LOCKVAR
1103 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1104 (void*)cl_exec, (void*)cl, *p);
1105#endif
1106 if (cl_exec == NULL || cl_exec != cl)
1107 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001108 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001109 switch (om->ocm_access)
1110 {
1111 case VIM_ACCESS_PRIVATE:
Ernie Raele6c9aa52023-10-06 19:55:52 +02001112 msg = e_cannot_access_private_variable_str;
1113 break;
Ernie Rael64885642023-10-04 20:16:22 +02001114 case VIM_ACCESS_READ:
1115 // If [idx] or .key following, read only OK.
1116 if (*p == '[' || *p == '.')
1117 break;
1118 if ((flags & GLV_READ_ONLY) == 0)
Ernie Raele6c9aa52023-10-06 19:55:52 +02001119 msg = e_variable_is_not_writable_str;
Ernie Rael64885642023-10-04 20:16:22 +02001120 break;
1121 case VIM_ACCESS_ALL:
1122 break;
1123 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001124 if (msg != NULL)
1125 {
1126 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1127 return FAIL;
1128 }
1129
Ernie Rael64885642023-10-04 20:16:22 +02001130 }
1131 return OK;
1132}
1133
1134/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 * Get an lval: variable, Dict item or List item that can be assigned a value
1136 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1137 * "name.key", "name.key[expr]" etc.
1138 * Indexing only works if "name" is an existing List or Dictionary.
1139 * "name" points to the start of the name.
1140 * If "rettv" is not NULL it points to the value to be assigned.
1141 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1142 * wrong; must end in space or cmd separator.
1143 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001144 * flags:
1145 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001146 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001147 * GLV_NO_AUTOLOAD: do not use script autoloading
1148 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001150 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001153 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001154get_lval(
1155 char_u *name,
1156 typval_T *rettv,
1157 lval_T *lp,
1158 int unlet,
1159 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001160 int flags, // GLV_ values
1161 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001163 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 char_u *expr_start, *expr_end;
1165 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001166 dictitem_T *v;
1167 typval_T var1;
1168 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001172 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001173 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001174 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001175 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001176 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177
Ernie Raelee865f32023-09-29 19:53:55 +02001178#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001179 if (lval_root == NULL)
1180 ch_log(NULL,
1181 "LKVAR: get_lval(): name %s, lval_root (nil)", name);
1182 else
1183 ch_log(NULL,
1184 "LKVAR: get_lval(): name %s, lr_tv %p lr_is_arg %d",
1185 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
1186 char buf[80];
1187 ch_log(NULL, "LKVAR: ...: GLV flags %s",
1188 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001189#endif
1190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001191 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001192 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001194 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001196 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001198 lp->ll_name_end = find_name_end(name, NULL, NULL,
1199 FNE_INCL_BR | fne_flags);
1200 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 }
1202
Bram Moolenaara749a422022-02-12 19:52:25 +00001203 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001204 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001205 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1206 {
1207 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1208 return NULL;
1209 }
1210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001212 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 if (expr_start != NULL)
1215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001216 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001217 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 && *p != '[' && *p != '.')
1219 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001220 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 return NULL;
1222 }
1223
1224 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1225 if (lp->ll_exp_name == NULL)
1226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // Report an invalid expression in braces, unless the
1228 // expression evaluation has been cancelled due to an
1229 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (!aborting() && !quiet)
1231 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001232 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001233 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 return NULL;
1235 }
1236 }
1237 lp->ll_name = lp->ll_exp_name;
1238 }
1239 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 lp->ll_name = name;
1242
Bram Moolenaar4525a572022-02-13 11:57:33 +00001243 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001245 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001246 // However, "g:[key]" is indexing a dictionary.
1247 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001248 {
1249 --p;
1250 lp->ll_name_end = p;
1251 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001252 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001253 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001254 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001255
Bram Moolenaar9510d222022-09-11 15:14:05 +01001256 if (is_scoped_variable(name))
1257 {
1258 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1259 return NULL;
1260 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001261 if (VIM_ISWHITE(*p))
1262 {
1263 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1264 return NULL;
1265 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001266 if (tp == p + 1 && !quiet)
1267 {
1268 semsg(_(e_white_space_required_after_str_str), ":", p);
1269 return NULL;
1270 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001271 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001272 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001273 semsg(_(e_using_type_not_in_script_context_str), p);
1274 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001275 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001276 if (vim9script && (flags & GLV_NO_DECL) &&
1277 !(flags & GLV_FOR_LOOP))
1278 {
1279 // Using a type and not in a "var" declaration.
1280 semsg(_(e_trailing_characters_str), p);
1281 return NULL;
1282 }
1283
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001284
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001285 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001286 lp->ll_type = parse_type(&tp,
1287 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1288 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001289 if (lp->ll_type == NULL && !quiet)
1290 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001291 lp->ll_name_end = tp;
1292 }
Ernie Raelee865f32023-09-29 19:53:55 +02001293 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 }
1295 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001296 if (lp->ll_name == NULL)
1297 return p;
1298
Bram Moolenaar62aec932022-01-29 21:45:34 +00001299 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001300 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001301 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001302
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001303 if (import != NULL)
1304 {
1305 ufunc_T *ufunc;
1306 type_T *type;
1307
Bram Moolenaar753885b2022-08-24 16:30:36 +01001308 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001309 lp->ll_sid = import->imp_sid;
1310 lp->ll_name = skipwhite(p + 1);
1311 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1312 lp->ll_name_end = p;
1313
1314 // check the item is exported
1315 cc = *p;
1316 *p = NUL;
1317 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001318 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001319 {
1320 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001321 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001322 }
1323 *p = cc;
1324 }
1325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001327 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001328 if ((*p != '[' && *p != '.'))
Ernie Raelee865f32023-09-29 19:53:55 +02001329 {
1330 if (lval_root != NULL)
Ernie Rael64885642023-10-04 20:16:22 +02001331 get_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001333 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334
Bram Moolenaar4525a572022-02-13 11:57:33 +00001335 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001336 {
1337 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001338 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001339 v = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001340 cl_exec = lval_root->lr_cl_exec;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001341 }
1342 else
1343 {
1344 cc = *p;
1345 *p = NUL;
1346 // When we would write to the variable pass &ht and prevent autoload.
1347 writing = !(flags & GLV_READ_ONLY);
1348 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001349 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001350 if (v == NULL && !quiet)
1351 semsg(_(e_undefined_variable_str), lp->ll_name);
1352 *p = cc;
1353 if (v == NULL)
1354 return NULL;
1355 lp->ll_tv = &v->di_tv;
1356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357
Bram Moolenaar4525a572022-02-13 11:57:33 +00001358 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001359 {
1360 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001361 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001362 return NULL;
1363 }
1364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 /*
1366 * Loop until no more [idx] or .key is following.
1367 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001368 var1.v_type = VAR_UNKNOWN;
1369 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001370 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001372 vartype_T v_type = lp->ll_tv->v_type;
1373
1374 if (*p == '.' && v_type != VAR_DICT
1375 && v_type != VAR_OBJECT
1376 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001377 {
1378 if (!quiet)
1379 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1380 return NULL;
1381 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001382 if (v_type != VAR_LIST
1383 && v_type != VAR_DICT
1384 && v_type != VAR_BLOB
1385 && v_type != VAR_OBJECT
1386 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001389 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001390 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001392
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001393 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001394 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001395 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001396 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001397 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001398 r = rettv_blob_alloc(lp->ll_tv);
1399 if (r == FAIL)
1400 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001402 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001405 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001408
Bram Moolenaar4525a572022-02-13 11:57:33 +00001409 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001410 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001411 && lp->ll_tv == &v->di_tv
1412 && ht != NULL && ht == get_script_local_ht())
1413 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001414 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001415
1416 // Vim9 script local variable: get the type
1417 if (sv != NULL)
1418 lp->ll_valtype = sv->sv_type;
1419 }
1420
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 len = -1;
1422 if (*p == '.')
1423 {
1424 key = p + 1;
1425 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1426 ;
1427 if (len == 0)
1428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001430 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001432 }
1433 p = key + len;
1434 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001435 else
1436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001437 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001439 if (*p == ':')
1440 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001441 else
1442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001443 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001444 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001446 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001448 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 clear_tv(&var1);
1450 return NULL;
1451 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001452 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001453 }
1454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001455 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001456 if (*p == ':')
1457 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001458 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001461 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001462 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001464 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 if (rettv != NULL
1466 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001467 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001469 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001472 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001473 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001475 }
1476 p = skipwhite(p + 1);
1477 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001478 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001479 else
1480 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001482 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001483 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001485 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001487 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001488 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001490 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001491 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001492 clear_tv(&var2);
1493 return NULL;
1494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001499 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001500
Bram Moolenaar8c711452005-01-14 21:53:12 +00001501 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001504 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001505 clear_tv(&var1);
1506 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001508 }
1509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001510 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001511 ++p;
1512 }
1513
Bram Moolenaard505d172022-12-18 21:42:55 +00001514 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001515 {
1516 if (len == -1)
1517 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001518 // "[key]": get key from "var1"
1519 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001520 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001524 }
1525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001527 lp->ll_object = NULL;
1528 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001529
1530 // a NULL dict is equivalent with an empty dict
1531 if (lp->ll_tv->vval.v_dict == NULL)
1532 {
1533 lp->ll_tv->vval.v_dict = dict_alloc();
1534 if (lp->ll_tv->vval.v_dict == NULL)
1535 {
1536 clear_tv(&var1);
1537 return NULL;
1538 }
1539 ++lp->ll_tv->vval.v_dict->dv_refcount;
1540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001542
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001543 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001545 // When assigning to a scope dictionary check that a function and
1546 // variable name is valid (only variable name unless it is l: or
1547 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001548 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001549 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001550 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001551
1552 if (len != -1)
1553 {
1554 prevval = key[len];
1555 key[len] = NUL;
1556 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001557 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001558 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001559 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001560 && (rettv->v_type == VAR_FUNC
1561 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001562 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001563 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001564 if (len != -1)
1565 key[len] = prevval;
1566 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001567 {
1568 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001569 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001570 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001571 }
1572
Bram Moolenaar10c65862020-10-08 21:16:42 +02001573 if (lp->ll_valtype != NULL)
1574 // use the type of the member
1575 lp->ll_valtype = lp->ll_valtype->tt_member;
1576
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001577 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001578 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001579 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001580 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001581 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001582 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001583 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001584 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001585 return NULL;
1586 }
1587
Bram Moolenaar31b81602019-02-10 22:14:27 +01001588 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001589 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001591 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001592 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001593 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001594 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001595 }
1596 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001597 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001598 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001599 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001600 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001601 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001602 p = NULL;
1603 break;
1604 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001605 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001606 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001607 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1608 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001609 {
1610 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001611 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001612 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001613
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001615 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001616 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001617 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001618 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001619 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1620
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001621 /*
1622 * Get the number and item for the only or first index of the List.
1623 */
1624 if (empty1)
1625 lp->ll_n1 = 0;
1626 else
1627 // is number or string
1628 lp->ll_n1 = (long)tv_get_number(&var1);
1629 clear_tv(&var1);
1630
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001631 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001632 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001633 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001634 return NULL;
1635 }
1636 if (lp->ll_range && !lp->ll_empty2)
1637 {
1638 lp->ll_n2 = (long)tv_get_number(&var2);
1639 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001640 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1641 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001642 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001643 }
1644 lp->ll_blob = lp->ll_tv->vval.v_blob;
1645 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001646 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001647 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001648 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001649 {
1650 /*
1651 * Get the number and item for the only or first index of the List.
1652 */
1653 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001654 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001655 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001656 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001657 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001658 clear_tv(&var1);
1659
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001661 lp->ll_object = NULL;
1662 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001663 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001664 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1665 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001666 if (lp->ll_li == NULL)
1667 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001668 clear_tv(&var2);
1669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001670 }
1671
Bram Moolenaar10c65862020-10-08 21:16:42 +02001672 if (lp->ll_valtype != NULL)
1673 // use the type of the member
1674 lp->ll_valtype = lp->ll_valtype->tt_member;
1675
Bram Moolenaar8c711452005-01-14 21:53:12 +00001676 /*
1677 * May need to find the item or absolute index for the second
1678 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001679 * When no index given: "lp->ll_empty2" is TRUE.
1680 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001681 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001682 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001683 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001684 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001685 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001686 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001687 if (check_range_index_two(lp->ll_list,
1688 &lp->ll_n1, lp->ll_li,
1689 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001690 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001691 }
1692
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001693 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001694 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001695 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001696 {
Ernie Raelee865f32023-09-29 19:53:55 +02001697 lp->ll_dict = NULL;
1698 lp->ll_list = NULL;
1699
1700 class_T *cl;
1701 if (v_type == VAR_OBJECT && lp->ll_tv->vval.v_object != NULL)
1702 {
1703 cl = lp->ll_tv->vval.v_object->obj_class;
1704 lp->ll_object = lp->ll_tv->vval.v_object;
1705 }
1706 else
1707 {
1708 cl = lp->ll_tv->vval.v_class;
1709 lp->ll_object = NULL;
1710 }
1711 lp->ll_class = cl;
1712
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001713 // TODO: what if class is NULL?
1714 if (cl != NULL)
1715 {
1716 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001717
1718 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001719 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001720 // First look for a function with this name.
1721 // round 1: class functions (skipped for an object)
1722 // round 2: object methods
1723 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001724 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001725 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001726 int m_idx;
1727 ufunc_T *fp;
1728
1729 fp = method_lookup(cl,
1730 round == 1 ? VAR_CLASS : VAR_OBJECT,
1731 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001732 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001733 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001734 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001735 lp->ll_ufunc = fp;
1736 lp->ll_valtype = fp->uf_func_type;
1737 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001738 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001739 }
1740 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001741
Ernie Raelee865f32023-09-29 19:53:55 +02001742 // TODO: dont' check access if inside class
1743 // TODO: is GLV_READ_ONLY the right thing to use
1744 // for class/object member access?
1745 // Probably in some cases. Need inside class check
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001746 if (lp->ll_valtype == NULL)
1747 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001748 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001749 ocmember_T *om
1750 = member_lookup(cl, v_type, key, p - key, &m_idx);
1751 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001752 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001753 {
Ernie Rael64885642023-10-04 20:16:22 +02001754 if (get_lval_check_access(cl_exec, cl, om,
1755 p, flags) == FAIL)
1756 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001757
1758 lp->ll_valtype = om->ocm_type;
1759
1760 if (v_type == VAR_OBJECT)
1761 lp->ll_tv = ((typval_T *)(
1762 lp->ll_tv->vval.v_object + 1)) + m_idx;
1763 else
1764 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001765 }
1766 }
1767
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001768 if (lp->ll_valtype == NULL)
1769 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001770 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001771 return NULL;
1772 }
1773 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 }
1776
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001777 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001779 return p;
1780}
1781
1782/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001784 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001787{
1788 vim_free(lp->ll_exp_name);
1789 vim_free(lp->ll_newkey);
1790}
1791
1792/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001793 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001794 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001795 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1796 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799set_var_lval(
1800 lval_T *lp,
1801 char_u *endp,
1802 typval_T *rettv,
1803 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001804 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1805 char_u *op,
1806 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001807{
1808 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001809 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001810
1811 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001813 cc = *endp;
1814 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001815 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001816 return;
1817
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001818 if (lp->ll_blob != NULL)
1819 {
1820 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001821
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 if (op != NULL && *op != '=')
1823 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001824 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001825 return;
1826 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001827 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001828 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001829
1830 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1831 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001832 if (lp->ll_empty2)
1833 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1834
Bram Moolenaar68452172021-04-12 21:21:02 +02001835 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1836 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001837 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001838 }
1839 else
1840 {
1841 val = (int)tv_get_number_chk(rettv, &error);
1842 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001843 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001844 }
1845 }
1846 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001848 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001850 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1851 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001852 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001853 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001854 *endp = cc;
1855 return;
1856 }
1857
Bram Moolenaarff697e62019-02-12 22:28:33 +01001858 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001859 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001860 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001861 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001862 {
1863 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001864 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1865 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001866 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001867 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001868 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001869 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001872 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001873 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001874 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1875 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001876 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001877 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001878 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001879 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001880 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001882 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001883 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001884 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001885 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 else if (lp->ll_range)
1887 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001888 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1889 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001890 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001891 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001892 return;
1893 }
1894
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001895 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1896 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 }
1898 else
1899 {
1900 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001901 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001903 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1904 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001905 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001906 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001907 return;
1908 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001909
1910 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001911 && check_typval_arg_type(lp->ll_valtype, rettv,
1912 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001913 return;
1914
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 if (lp->ll_newkey != NULL)
1916 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 if (op != NULL && *op != '=')
1918 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001919 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 return;
1921 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001922 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1923 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001924 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001927 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 if (di == NULL)
1929 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001930 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1931 {
1932 vim_free(di);
1933 return;
1934 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001936 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 else if (op != NULL && *op != '=')
1938 {
1939 tv_op(lp->ll_tv, rettv, op);
1940 return;
1941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945 /*
1946 * Assign the value to the variable or list item.
1947 */
1948 if (copy)
1949 copy_tv(rettv, lp->ll_tv);
1950 else
1951 {
1952 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001953 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 }
1956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957}
1958
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001960 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1961 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 * Returns OK or FAIL.
1963 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001965tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001967 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 char_u numbuf[NUMBUFLEN];
1969 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001970 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001972 // Can't do anything with a Funcref or Dict on the right.
1973 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001974 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001975 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1976 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 {
1978 switch (tv1->v_type)
1979 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001980 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001981 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 case VAR_DICT:
1984 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001985 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001986 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001987 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001988 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001989 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001990 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001991 case VAR_CLASS:
1992 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 break;
1994
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001995 case VAR_BLOB:
1996 if (*op != '+' || tv2->v_type != VAR_BLOB)
1997 break;
1998 // BLOB += BLOB
1999 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2000 {
2001 blob_T *b1 = tv1->vval.v_blob;
2002 blob_T *b2 = tv2->vval.v_blob;
2003 int i, len = blob_len(b2);
2004 for (i = 0; i < len; i++)
2005 ga_append(&b1->bv_ga, blob_get(b2, i));
2006 }
2007 return OK;
2008
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 case VAR_LIST:
2010 if (*op != '+' || tv2->v_type != VAR_LIST)
2011 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002012 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002013 if (tv2->vval.v_list != NULL)
2014 {
2015 if (tv1->vval.v_list == NULL)
2016 {
2017 tv1->vval.v_list = tv2->vval.v_list;
2018 ++tv1->vval.v_list->lv_refcount;
2019 }
2020 else
2021 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 return OK;
2024
2025 case VAR_NUMBER:
2026 case VAR_STRING:
2027 if (tv2->v_type == VAR_LIST)
2028 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002029 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002031 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002032 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002033 if (tv2->v_type == VAR_FLOAT)
2034 {
2035 float_T f = n;
2036
Bram Moolenaarff697e62019-02-12 22:28:33 +01002037 if (*op == '%')
2038 break;
2039 switch (*op)
2040 {
2041 case '+': f += tv2->vval.v_float; break;
2042 case '-': f -= tv2->vval.v_float; break;
2043 case '*': f *= tv2->vval.v_float; break;
2044 case '/': f /= tv2->vval.v_float; break;
2045 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002046 clear_tv(tv1);
2047 tv1->v_type = VAR_FLOAT;
2048 tv1->vval.v_float = f;
2049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002051 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002052 switch (*op)
2053 {
2054 case '+': n += tv_get_number(tv2); break;
2055 case '-': n -= tv_get_number(tv2); break;
2056 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002057 case '/': n = num_divide(n, tv_get_number(tv2),
2058 &failed); break;
2059 case '%': n = num_modulus(n, tv_get_number(tv2),
2060 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002061 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002062 clear_tv(tv1);
2063 tv1->v_type = VAR_NUMBER;
2064 tv1->vval.v_number = n;
2065 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 }
2067 else
2068 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002069 if (tv2->v_type == VAR_FLOAT)
2070 break;
2071
Bram Moolenaarff697e62019-02-12 22:28:33 +01002072 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002073 s = tv_get_string(tv1);
2074 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 clear_tv(tv1);
2076 tv1->v_type = VAR_STRING;
2077 tv1->vval.v_string = s;
2078 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002079 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002080
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002081 case VAR_FLOAT:
2082 {
2083 float_T f;
2084
Bram Moolenaarff697e62019-02-12 22:28:33 +01002085 if (*op == '%' || *op == '.'
2086 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002087 && tv2->v_type != VAR_NUMBER
2088 && tv2->v_type != VAR_STRING))
2089 break;
2090 if (tv2->v_type == VAR_FLOAT)
2091 f = tv2->vval.v_float;
2092 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002093 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002094 switch (*op)
2095 {
2096 case '+': tv1->vval.v_float += f; break;
2097 case '-': tv1->vval.v_float -= f; break;
2098 case '*': tv1->vval.v_float *= f; break;
2099 case '/': tv1->vval.v_float /= f; break;
2100 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002101 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002102 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 }
2104 }
2105
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002106 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107 return FAIL;
2108}
2109
2110/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111 * Evaluate the expression used in a ":for var in expr" command.
2112 * "arg" points to "var".
2113 * Set "*errp" to TRUE for an error, FALSE otherwise;
2114 * Return a pointer that holds the info. Null when there is an error.
2115 */
2116 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002117eval_for_line(
2118 char_u *arg,
2119 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002120 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002121 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122{
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002124 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 typval_T tv;
2127 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002128 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002130 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002132 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002133 if (fi == NULL)
2134 return NULL;
2135
Bram Moolenaar404557e2021-07-05 21:41:48 +02002136 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2137 &fi->fi_semicolon, FALSE);
2138 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 return fi;
2140
Bram Moolenaar404557e2021-07-05 21:41:48 +02002141 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002142 if (expr[0] != 'i' || expr[1] != 'n'
2143 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002144 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002145 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2146 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2147 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002148 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002149 return fi;
2150 }
2151
2152 if (skip)
2153 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002154 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2155 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156 {
2157 *errp = FALSE;
2158 if (!skip)
2159 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002160 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002161 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002162 l = tv.vval.v_list;
2163 if (l == NULL)
2164 {
2165 // a null list is like an empty list: do nothing
2166 clear_tv(&tv);
2167 }
2168 else
2169 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002171 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002173 // No need to increment the refcount, it's already set for
2174 // the list being used in "tv".
2175 fi->fi_list = l;
2176 list_add_watch(l, &fi->fi_lw);
2177 fi->fi_lw.lw_item = l->lv_first;
2178 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002179 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002180 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002181 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002182 fi->fi_bi = 0;
2183 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002184 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002185 typval_T btv;
2186
2187 // Make a copy, so that the iteration still works when the
2188 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002190 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002191 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002192 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002193 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002194 else if (tv.v_type == VAR_STRING)
2195 {
2196 fi->fi_byte_idx = 0;
2197 fi->fi_string = tv.vval.v_string;
2198 tv.vval.v_string = NULL;
2199 if (fi->fi_string == NULL)
2200 fi->fi_string = vim_strsave((char_u *)"");
2201 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002202 else
2203 {
Sean Dewar80d73952021-08-04 19:25:54 +02002204 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002205 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002206 }
2207 }
2208 }
2209 if (skip)
2210 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002211 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002212
2213 return fi;
2214}
2215
2216/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002217 * Used when looping over a :for line, skip the "in expr" part.
2218 */
2219 void
2220skip_for_lines(void *fi_void, evalarg_T *evalarg)
2221{
2222 forinfo_T *fi = (forinfo_T *)fi_void;
2223 int i;
2224
2225 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002226 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002227}
2228
2229/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002230 * Use the first item in a ":for" list. Advance to the next.
2231 * Assign the values to the variable (list). "arg" points to the first one.
2232 * Return TRUE when a valid item was found, FALSE when at end of list or
2233 * something wrong.
2234 */
2235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002238 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002239 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002240 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002241 ? (ASSIGN_FINAL
2242 // first round: error if variable exists
2243 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002244 | ASSIGN_NO_MEMBER_TYPE
2245 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002246 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002247 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002248 int skip_assign = in_vim9script() && arg[0] == '_'
2249 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002250
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002251 if (fi->fi_blob != NULL)
2252 {
2253 typval_T tv;
2254
2255 if (fi->fi_bi >= blob_len(fi->fi_blob))
2256 return FALSE;
2257 tv.v_type = VAR_NUMBER;
2258 tv.v_lock = VAR_FIXED;
2259 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2260 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002261 if (skip_assign)
2262 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002263 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002264 fi->fi_varcount, flag, NULL) == OK;
2265 }
2266
2267 if (fi->fi_string != NULL)
2268 {
2269 typval_T tv;
2270 int len;
2271
2272 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2273 if (len == 0)
2274 return FALSE;
2275 tv.v_type = VAR_STRING;
2276 tv.v_lock = VAR_FIXED;
2277 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2278 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002279 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002280 if (skip_assign)
2281 result = TRUE;
2282 else
2283 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002284 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002285 vim_free(tv.vval.v_string);
2286 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002287 }
2288
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002289 item = fi->fi_lw.lw_item;
2290 if (item == NULL)
2291 result = FALSE;
2292 else
2293 {
2294 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002295 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002296 if (skip_assign)
2297 result = TRUE;
2298 else
2299 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002300 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 }
2302 return result;
2303}
2304
2305/*
2306 * Free the structure used to store info used by ":for".
2307 */
2308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002309free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002310{
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002312
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002313 if (fi == NULL)
2314 return;
2315 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002316 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002317 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002318 list_unref(fi->fi_list);
2319 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002320 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002321 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002322 else
2323 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002324 vim_free(fi);
2325}
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328set_context_for_expression(
2329 expand_T *xp,
2330 char_u *arg,
2331 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002333 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002335 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002337 if (cmdidx == CMD_let || cmdidx == CMD_var
2338 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002339 {
2340 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002341 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002343 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002344 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 {
2346 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002347 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002348 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 break;
2350 }
2351 return;
2352 }
2353 }
2354 else
2355 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2356 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 while ((xp->xp_pattern = vim_strpbrk(arg,
2358 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2359 {
2360 c = *xp->xp_pattern;
2361 if (c == '&')
2362 {
2363 c = xp->xp_pattern[1];
2364 if (c == '&')
2365 {
2366 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002367 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002372 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2373 xp->xp_pattern += 2;
2374
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 else if (c == '$')
2378 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002379 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 xp->xp_context = EXPAND_ENV_VARS;
2381 }
2382 else if (c == '=')
2383 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002384 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 xp->xp_context = EXPAND_EXPRESSION;
2386 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002387 else if (c == '#'
2388 && xp->xp_context == EXPAND_EXPRESSION)
2389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002390 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002391 break;
2392 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002393 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 && xp->xp_context == EXPAND_FUNCTIONS
2395 && vim_strchr(xp->xp_pattern, '(') == NULL)
2396 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002397 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 break;
2399 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002400 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002402 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2405 if (c == '\\' && xp->xp_pattern[1] != NUL)
2406 ++xp->xp_pattern;
2407 xp->xp_context = EXPAND_NOTHING;
2408 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002409 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002411 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2413 /* skip */ ;
2414 xp->xp_context = EXPAND_NOTHING;
2415 }
2416 else if (c == '|')
2417 {
2418 if (xp->xp_pattern[1] == '|')
2419 {
2420 ++xp->xp_pattern;
2421 xp->xp_context = EXPAND_EXPRESSION;
2422 }
2423 else
2424 xp->xp_context = EXPAND_COMMANDS;
2425 }
2426 else
2427 xp->xp_context = EXPAND_EXPRESSION;
2428 }
2429 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002430 // Doesn't look like something valid, expand as an expression
2431 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 arg = xp->xp_pattern;
2434 if (*arg != NUL)
2435 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2436 /* skip */ ;
2437 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002438
2439 // ":exe one two" completes "two"
2440 if ((cmdidx == CMD_execute
2441 || cmdidx == CMD_echo
2442 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002443 || cmdidx == CMD_echomsg
2444 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002445 && xp->xp_context == EXPAND_EXPRESSION)
2446 {
2447 for (;;)
2448 {
2449 char_u *n = skiptowhite(arg);
2450
2451 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2452 break;
2453 arg = skipwhite(n);
2454 }
2455 }
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 xp->xp_pattern = arg;
2458}
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002461 * Return TRUE if "pat" matches "text".
2462 * Does not use 'cpo' and always uses 'magic'.
2463 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002464 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002465pattern_match(char_u *pat, char_u *text, int ic)
2466{
2467 int matches = FALSE;
2468 char_u *save_cpo;
2469 regmatch_T regmatch;
2470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002471 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002472 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002473 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002474 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2475 if (regmatch.regprog != NULL)
2476 {
2477 regmatch.rm_ic = ic;
2478 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2479 vim_regfree(regmatch.regprog);
2480 }
2481 p_cpo = save_cpo;
2482 return matches;
2483}
2484
2485/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002486 * Handle a name followed by "(". Both for just "name(arg)" and for
2487 * "expr->name(arg)".
2488 * Returns OK or FAIL.
2489 */
2490 static int
2491eval_func(
2492 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002493 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002494 char_u *name,
2495 int name_len,
2496 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002497 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002498 typval_T *basetv) // "expr" for "expr->name(arg)"
2499{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002500 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002501 char_u *s = name;
2502 int len = name_len;
2503 partial_T *partial;
2504 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002505 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002506 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002507
2508 if (!evaluate)
2509 check_vars(s, len);
2510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002511 // If "s" is the name of a variable of type VAR_FUNC
2512 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002513 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002514 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002516 // Need to make a copy, in case evaluating the arguments makes
2517 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002518 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002519 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002520 ret = FAIL;
2521 else
2522 {
2523 funcexe_T funcexe;
2524
2525 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002526 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002527 funcexe.fe_firstline = curwin->w_cursor.lnum;
2528 funcexe.fe_lastline = curwin->w_cursor.lnum;
2529 funcexe.fe_evaluate = evaluate;
2530 funcexe.fe_partial = partial;
2531 funcexe.fe_basetv = basetv;
2532 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002533 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002534 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002535 }
2536 vim_free(s);
2537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002538 // If evaluate is FALSE rettv->v_type was not set in
2539 // get_func_tv, but it's needed in handle_subscript() to parse
2540 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002541 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2542 {
2543 rettv->vval.v_string = NULL;
2544 rettv->v_type = VAR_FUNC;
2545 }
2546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002547 // Stop the expression evaluation when immediately
2548 // aborting on error, or when an interrupt occurred or
2549 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002550 if (evaluate && aborting())
2551 {
2552 if (ret == OK)
2553 clear_tv(rettv);
2554 ret = FAIL;
2555 }
2556 return ret;
2557}
2558
2559/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002560 * After a NL, skip over empty lines and comment-only lines.
2561 */
2562 static char_u *
2563newline_skip_comments(char_u *arg)
2564{
2565 char_u *p = arg + 1;
2566
2567 for (;;)
2568 {
2569 p = skipwhite(p);
2570
2571 if (*p == NUL)
2572 break;
2573 if (vim9_comment_start(p))
2574 {
2575 char_u *nl = vim_strchr(p, NL);
2576
2577 if (nl == NULL)
2578 break;
2579 p = nl;
2580 }
2581 if (*p != NL)
2582 break;
2583 ++p; // skip another NL
2584 }
2585 return p;
2586}
2587
2588/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002589 * Get the next line source line without advancing. But do skip over comment
2590 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002591 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002592 */
2593 static char_u *
2594getline_peek_skip_comments(evalarg_T *evalarg)
2595{
2596 for (;;)
2597 {
2598 char_u *next = getline_peek(evalarg->eval_getline,
2599 evalarg->eval_cookie);
2600 char_u *p;
2601
2602 if (next == NULL)
2603 break;
2604 p = skipwhite(next);
2605 if (*p != NUL && !vim9_comment_start(p))
2606 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002607 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002608 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002609 }
2610 return NULL;
2611}
2612
2613/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002614 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2615 * comment) and there is a next line, return the next line (skipping blanks)
2616 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002617 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2618 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002619 * "arg" must point somewhere inside a line, not at the start.
2620 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002621 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002622eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2623{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002624 char_u *p = skipwhite(arg);
2625
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002627 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002628 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002629 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2630 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002631 && (*p == NUL || *p == NL
2632 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002633 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002634 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002635
Bram Moolenaare442d592022-05-05 12:20:28 +01002636 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002637 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002638 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002639 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002640 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002641 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002642
Bram Moolenaar9d489562020-07-30 20:08:50 +02002643 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002645 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002646 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 }
2648 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002649 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650}
2651
2652/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002653 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002654 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002655 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002656 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002657eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002658{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002659 garray_T *gap = &evalarg->eval_ga;
2660 char_u *line;
2661
Bram Moolenaar39be4982022-05-06 21:51:50 +01002662 if (arg != NULL)
2663 {
2664 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002665 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002666 // Truncate before a trailing comment, so that concatenating the lines
2667 // won't turn the rest into a comment.
2668 if (*skipwhite(arg) == '#')
2669 *arg = NUL;
2670 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002671
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002672 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002673 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2674 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002675 else
2676 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002677 if (line == NULL)
2678 return NULL;
2679
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002680 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002681 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2682 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002683 char_u *p = skipwhite(line);
2684
2685 // Going to concatenate the lines after parsing. For an empty or
2686 // comment line use an empty string.
2687 if (*p == NUL || vim9_comment_start(p))
2688 {
2689 vim_free(line);
2690 line = vim_strsave((char_u *)"");
2691 }
2692
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002693 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2694 ++gap->ga_len;
2695 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002696 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002697 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002698 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002699 evalarg->eval_tofree = line;
2700 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002701
2702 // Advanced to the next line, "arg" no longer points into the previous
2703 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002704 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002705 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002706}
2707
Bram Moolenaare6b53242020-07-01 17:28:33 +02002708/*
2709 * Call eval_next_non_blank() and get the next line if needed.
2710 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002711 char_u *
2712skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2713{
2714 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002715 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002716
Bram Moolenaar962d7212020-07-04 14:15:00 +02002717 if (evalarg == NULL)
2718 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002719 eval_next_non_blank(p, evalarg, &getnext);
2720 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002721 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002722 return p;
2723}
2724
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002725/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002726 * The "eval" functions have an "evalarg" argument: When NULL or
2727 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2728 * parsed but not executed. The functions may return OK, but the rettv will be
2729 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 */
2731
2732/*
2733 * Handle zero level expression.
2734 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002735 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002736 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 * Return OK or FAIL.
2739 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002741eval0(
2742 char_u *arg,
2743 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002744 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002747 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2748}
2749
2750/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002751 * If "arg" is a simple function call without arguments then call it and return
2752 * the result. Otherwise return NOTDONE.
2753 */
2754 int
2755may_call_simple_func(
2756 char_u *arg,
2757 typval_T *rettv)
2758{
2759 char_u *parens = (char_u *)strstr((char *)arg, "()");
2760 int r = NOTDONE;
2761
2762 // If the expression is "FuncName()" then we can skip a lot of overhead.
2763 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2764 {
2765 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2766
2767 if (to_name_end(p, TRUE) == parens)
2768 r = call_simple_func(arg, (int)(parens - arg), rettv);
2769 }
2770 return r;
2771}
2772
2773/*
2774 * Handle zero level expression with optimization for a simple function call.
2775 * Same arguments and return value as eval0().
2776 */
2777 int
2778eval0_simple_funccal(
2779 char_u *arg,
2780 typval_T *rettv,
2781 exarg_T *eap,
2782 evalarg_T *evalarg)
2783{
2784 int r = may_call_simple_func(arg, rettv);
2785
2786 if (r == NOTDONE)
2787 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2788 return r;
2789}
2790
2791/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002792 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2793 * expression and don't check what comes after the expression.
2794 */
2795 int
2796eval0_retarg(
2797 char_u *arg,
2798 typval_T *rettv,
2799 exarg_T *eap,
2800 evalarg_T *evalarg,
2801 char_u **retarg)
2802{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 int ret;
2804 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002805 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002806 int did_emsg_before = did_emsg;
2807 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002808 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002809 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002813
Bram Moolenaar79481362022-06-27 20:15:10 +01002814 if (ret != FAIL)
2815 {
2816 expr_end = p;
2817 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002818
Bram Moolenaar79481362022-06-27 20:15:10 +01002819 // In Vim9 script a command block is not split at NL characters for
2820 // commands using an expression argument. Skip over a '#' comment to
2821 // check for a following NL. Require white space before the '#'.
2822 if (in_vim9script() && p > expr_end && retarg == NULL)
2823 while (*p == '#')
2824 {
2825 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002826
Bram Moolenaar79481362022-06-27 20:15:10 +01002827 if (nl == NULL)
2828 break;
2829 p = skipwhite(nl + 1);
2830 if (eap != NULL && *p != NUL)
2831 eap->nextcmd = p;
2832 check_for_end = FALSE;
2833 }
2834
2835 if (check_for_end)
2836 end_error = !ends_excmd2(arg, p);
2837 }
2838
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002839 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
2841 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002842 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 /*
2844 * Report the invalid expression unless the expression evaluation has
2845 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002846 * exception, or we already gave a more specific error.
2847 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002849 if (!aborting()
2850 && did_emsg == did_emsg_before
2851 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002852 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002853 {
2854 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002855 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002856 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002857 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002858 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002859
zeertzjqf2588b62023-05-05 17:22:35 +01002860 if (eap != NULL && p != NULL)
2861 {
2862 // Some of the expression may not have been consumed.
2863 // Only execute a next command if it cannot be a "||" operator.
2864 // The next command may be "catch".
2865 char_u *nextcmd = check_nextcmd(p);
2866 if (nextcmd != NULL && *nextcmd != '|')
2867 eap->nextcmd = nextcmd;
2868 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002869 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002871
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002872 if (retarg != NULL)
2873 *retarg = p;
2874 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002875 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002876
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 return ret;
2878}
2879
2880/*
2881 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002882 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002883 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002886 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002888 * Note: "rettv.v_lock" is not set.
2889 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * Return OK or FAIL.
2891 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002892 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002895 char_u *p;
2896 int getnext;
2897
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002898 CLEAR_POINTER(rettv);
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 /*
2901 * Get the first variable.
2902 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002903 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 return FAIL;
2905
Bram Moolenaar793648f2020-06-26 21:28:25 +02002906 p = eval_next_non_blank(*arg, evalarg, &getnext);
2907 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002909 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002910 int result;
2911 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002912 evalarg_T *evalarg_used = evalarg;
2913 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002915 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002916 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917
2918 if (evalarg == NULL)
2919 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002920 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002921 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002923 orig_flags = evalarg_used->eval_flags;
2924 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002926 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002927 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002928 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002929 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002930 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002931 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002932 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002933 clear_tv(rettv);
2934 return FAIL;
2935 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002936 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002937 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 int error = FALSE;
2943
Bram Moolenaar13106602020-10-04 16:06:05 +02002944 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002945 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002946 else if (vim9script)
2947 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002948 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002950 if (error || !op_falsy || !result)
2951 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002952 if (error)
2953 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955
2956 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002957 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002959 if (op_falsy)
2960 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002961 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002962 {
mityu4ac198c2021-05-28 17:52:40 +02002963 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002964 clear_tv(rettv);
2965 return FAIL;
2966 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002967 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002968 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002969 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002970 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002971 {
2972 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002974 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002975 if (!op_falsy || !result)
2976 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002978 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002980 /*
2981 * Check for the ":".
2982 */
2983 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2984 if (*p != ':')
2985 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002986 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002987 if (evaluate && result)
2988 clear_tv(rettv);
2989 evalarg_used->eval_flags = orig_flags;
2990 return FAIL;
2991 }
2992 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002993 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002994 else
2995 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002996 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002997 {
2998 error_white_both(p, 1);
2999 clear_tv(rettv);
3000 evalarg_used->eval_flags = orig_flags;
3001 return FAIL;
3002 }
3003 *arg = p;
3004 }
3005
3006 /*
3007 * Get the third variable. Recursive!
3008 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003009 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003010 {
mityu4ac198c2021-05-28 17:52:40 +02003011 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003012 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003013 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003014 return FAIL;
3015 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003016 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3017 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003018 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003019 if (eval1(arg, &var2, evalarg_used) == FAIL)
3020 {
3021 if (evaluate && result)
3022 clear_tv(rettv);
3023 evalarg_used->eval_flags = orig_flags;
3024 return FAIL;
3025 }
3026 if (evaluate && !result)
3027 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003029
3030 if (evalarg == NULL)
3031 clear_evalarg(&local_evalarg, NULL);
3032 else
3033 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035
3036 return OK;
3037}
3038
3039/*
3040 * Handle first level expression:
3041 * expr2 || expr2 || expr2 logical OR
3042 *
3043 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003044 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 *
3046 * Return OK or FAIL.
3047 */
3048 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003051 char_u *p;
3052 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003055 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003057 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return FAIL;
3059
3060 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003061 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003063 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003064 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003066 evalarg_T *evalarg_used = evalarg;
3067 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003068 int evaluate;
3069 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003070 long result = FALSE;
3071 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003072 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003073 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003074
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075 if (evalarg == NULL)
3076 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003077 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003078 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003079 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003080 orig_flags = evalarg_used->eval_flags;
3081 evaluate = orig_flags & EVAL_EVALUATE;
3082 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003083 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003084 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003085 result = tv_get_bool_chk(rettv, &error);
3086 else if (tv_get_number_chk(rettv, &error) != 0)
3087 result = TRUE;
3088 clear_tv(rettv);
3089 if (error)
3090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092
3093 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003094 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003096 while (p[0] == '|' && p[1] == '|')
3097 {
3098 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003099 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003100 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003101 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003102 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003103 {
3104 error_white_both(p, 2);
3105 clear_tv(rettv);
3106 return FAIL;
3107 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003108 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003109 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003110
3111 /*
3112 * Get the second variable.
3113 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003114 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003115 {
mityu4ac198c2021-05-28 17:52:40 +02003116 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003117 clear_tv(rettv);
3118 return FAIL;
3119 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003120 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3121 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003122 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003123 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003124 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003125
3126 /*
3127 * Compute the result.
3128 */
3129 if (evaluate && !result)
3130 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003131 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003132 result = tv_get_bool_chk(&var2, &error);
3133 else if (tv_get_number_chk(&var2, &error) != 0)
3134 result = TRUE;
3135 clear_tv(&var2);
3136 if (error)
3137 return FAIL;
3138 }
3139 if (evaluate)
3140 {
3141 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003142 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003143 rettv->v_type = VAR_BOOL;
3144 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003145 }
3146 else
3147 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003148 rettv->v_type = VAR_NUMBER;
3149 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003150 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003151 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003152
3153 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003155
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003156 if (evalarg == NULL)
3157 clear_evalarg(&local_evalarg, NULL);
3158 else
3159 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161
3162 return OK;
3163}
3164
3165/*
3166 * Handle second level expression:
3167 * expr3 && expr3 && expr3 logical AND
3168 *
3169 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003170 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 *
3172 * Return OK or FAIL.
3173 */
3174 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003175eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003177 char_u *p;
3178 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003181 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003183 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 return FAIL;
3185
3186 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003187 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003189 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003190 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003192 evalarg_T *evalarg_used = evalarg;
3193 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003194 int orig_flags;
3195 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003196 long result = TRUE;
3197 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003198 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003199 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003200
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003201 if (evalarg == NULL)
3202 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003203 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003204 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003205 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003206 orig_flags = evalarg_used->eval_flags;
3207 evaluate = orig_flags & EVAL_EVALUATE;
3208 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003210 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003211 result = tv_get_bool_chk(rettv, &error);
3212 else if (tv_get_number_chk(rettv, &error) == 0)
3213 result = FALSE;
3214 clear_tv(rettv);
3215 if (error)
3216 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218
3219 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003220 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003222 while (p[0] == '&' && p[1] == '&')
3223 {
3224 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003225 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003226 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003227 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003228 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003229 {
3230 error_white_both(p, 2);
3231 clear_tv(rettv);
3232 return FAIL;
3233 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003234 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003235 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003236
3237 /*
3238 * Get the second variable.
3239 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003240 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003241 {
mityu4ac198c2021-05-28 17:52:40 +02003242 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003243 clear_tv(rettv);
3244 return FAIL;
3245 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003246 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3247 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003248 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003249 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003250 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003251 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003252
3253 /*
3254 * Compute the result.
3255 */
3256 if (evaluate && result)
3257 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003258 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003259 result = tv_get_bool_chk(&var2, &error);
3260 else if (tv_get_number_chk(&var2, &error) == 0)
3261 result = FALSE;
3262 clear_tv(&var2);
3263 if (error)
3264 return FAIL;
3265 }
3266 if (evaluate)
3267 {
3268 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003269 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003270 rettv->v_type = VAR_BOOL;
3271 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003272 }
3273 else
3274 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003275 rettv->v_type = VAR_NUMBER;
3276 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003277 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003278 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003279
3280 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003282
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003283 if (evalarg == NULL)
3284 clear_evalarg(&local_evalarg, NULL);
3285 else
3286 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288
3289 return OK;
3290}
3291
3292/*
3293 * Handle third level expression:
3294 * var1 == var2
3295 * var1 =~ var2
3296 * var1 != var2
3297 * var1 !~ var2
3298 * var1 > var2
3299 * var1 >= var2
3300 * var1 < var2
3301 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003302 * var1 is var2
3303 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 *
3305 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003306 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 *
3308 * Return OK or FAIL.
3309 */
3310 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003311eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003314 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003315 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003317 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
3319 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003320 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003322 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 return FAIL;
3324
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003325 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003326
Bram Moolenaar696ba232020-07-29 21:20:41 +02003327 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003330 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003332 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003334 typval_T var2;
3335 int ic;
3336 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003337 int evaluate = evalarg == NULL
3338 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003339 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003340
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003341 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003342 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003343 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003344 p = *arg;
3345 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003346 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3347 {
mityu4ac198c2021-05-28 17:52:40 +02003348 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003349 clear_tv(rettv);
3350 return FAIL;
3351 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003352
Bram Moolenaar696ba232020-07-29 21:20:41 +02003353 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3354 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003355 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003356 clear_tv(rettv);
3357 return FAIL;
3358 }
3359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (p[len] == '?')
3362 {
3363 ic = TRUE;
3364 ++len;
3365 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else if (p[len] == '#')
3368 {
3369 ic = FALSE;
3370 ++len;
3371 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003372 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003374 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 /*
3377 * Get the second variable.
3378 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003379 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3380 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003381 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003382 clear_tv(rettv);
3383 return FAIL;
3384 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003385 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003386 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003388 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return FAIL;
3390 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003391 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003392 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003393 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003394
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003395 // use the line of the comparison for messages
3396 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003397 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003398 {
3399 ret = FAIL;
3400 clear_tv(rettv);
3401 }
3402 else
3403 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003404 clear_tv(&var2);
3405 return ret;
3406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408
3409 return OK;
3410}
3411
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003412/*
3413 * Make a copy of blob "tv1" and append blob "tv2".
3414 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 void
3416eval_addblob(typval_T *tv1, typval_T *tv2)
3417{
3418 blob_T *b1 = tv1->vval.v_blob;
3419 blob_T *b2 = tv2->vval.v_blob;
3420 blob_T *b = blob_alloc();
3421 int i;
3422
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003423 if (b == NULL)
3424 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003426 for (i = 0; i < blob_len(b1); i++)
3427 ga_append(&b->bv_ga, blob_get(b1, i));
3428 for (i = 0; i < blob_len(b2); i++)
3429 ga_append(&b->bv_ga, blob_get(b2, i));
3430
3431 clear_tv(tv1);
3432 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433}
3434
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003435/*
3436 * Make a copy of list "tv1" and append list "tv2".
3437 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 int
3439eval_addlist(typval_T *tv1, typval_T *tv2)
3440{
3441 typval_T var3;
3442
3443 // concatenate Lists
3444 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3445 {
3446 clear_tv(tv1);
3447 clear_tv(tv2);
3448 return FAIL;
3449 }
3450 clear_tv(tv1);
3451 *tv1 = var3;
3452 return OK;
3453}
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003456 * Handle the bitwise left/right shift operator expression:
3457 * var1 << var2
3458 * var1 >> var2
3459 *
3460 * "arg" must point to the first non-white of the expression.
3461 * "arg" is advanced to just after the recognized expression.
3462 *
3463 * Return OK or FAIL.
3464 */
3465 static int
3466eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3467{
3468 /*
3469 * Get the first expression.
3470 */
3471 if (eval6(arg, rettv, evalarg) == FAIL)
3472 return FAIL;
3473
3474 /*
3475 * Repeat computing, until no '<<' or '>>' is following.
3476 */
3477 for (;;)
3478 {
3479 char_u *p;
3480 int getnext;
3481 exprtype_T type;
3482 int evaluate;
3483 typval_T var2;
3484 int vim9script;
3485
3486 p = eval_next_non_blank(*arg, evalarg, &getnext);
3487 if (p[0] == '<' && p[1] == '<')
3488 type = EXPR_LSHIFT;
3489 else if (p[0] == '>' && p[1] == '>')
3490 type = EXPR_RSHIFT;
3491 else
3492 return OK;
3493
3494 // Handle a bitwise left or right shift operator
3495 if (rettv->v_type != VAR_NUMBER)
3496 {
3497 // left operand should be a number
3498 emsg(_(e_bitshift_ops_must_be_number));
3499 clear_tv(rettv);
3500 return FAIL;
3501 }
3502
3503 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3504 vim9script = in_vim9script();
3505 if (getnext)
3506 {
3507 *arg = eval_next_line(*arg, evalarg);
3508 p = *arg;
3509 }
3510 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3511 {
3512 error_white_both(*arg, 2);
3513 clear_tv(rettv);
3514 return FAIL;
3515 }
3516
3517 /*
3518 * Get the second variable.
3519 */
3520 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3521 {
3522 error_white_both(p, 2);
3523 clear_tv(rettv);
3524 return FAIL;
3525 }
3526 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3527 if (eval6(arg, &var2, evalarg) == FAIL)
3528 {
3529 clear_tv(rettv);
3530 return FAIL;
3531 }
3532
3533 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3534 {
3535 // right operand should be a positive number
3536 if (var2.v_type != VAR_NUMBER)
3537 emsg(_(e_bitshift_ops_must_be_number));
3538 else
dundargocc57b5bc2022-11-02 13:30:51 +00003539 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003540 clear_tv(rettv);
3541 clear_tv(&var2);
3542 return FAIL;
3543 }
3544
3545 if (evaluate)
3546 {
3547 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3548 // shifting more bits than we have always results in zero
3549 rettv->vval.v_number = 0;
3550 else if (type == EXPR_LSHIFT)
3551 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003552 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003553 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003554 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003555 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003556 }
3557
3558 clear_tv(&var2);
3559 }
3560
3561 return OK;
3562}
3563
3564/*
3565 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003566 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003568 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003569 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 *
3571 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003572 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 *
3574 * Return OK or FAIL.
3575 */
3576 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003577eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003580 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003582 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 return FAIL;
3584
3585 /*
3586 * Repeat computing, until no '+', '-' or '.' is following.
3587 */
3588 for (;;)
3589 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003590 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003591 int getnext;
3592 char_u *p;
3593 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003594 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003595 int concat;
3596 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003597 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003598
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003599 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003600 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003601 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003602 p = eval_next_non_blank(*arg, evalarg, &getnext);
3603 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003604 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003605 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3606 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003608 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3609 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003610
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003611 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003612 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003613 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003614 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003615 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003616 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003617 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003618 {
mityu4ac198c2021-05-28 17:52:40 +02003619 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003620 clear_tv(rettv);
3621 return FAIL;
3622 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003623 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003624 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003625 if ((op != '+' || (rettv->v_type != VAR_LIST
3626 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003627 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003628 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003630 int error = FALSE;
3631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003632 // For "list + ...", an illegal use of the first operand as
3633 // a number cannot be determined before evaluating the 2nd
3634 // operand: if this is also a list, all is ok.
3635 // For "something . ...", "something - ..." or "non-list + ...",
3636 // we know that the first operand needs to be a string or number
3637 // without evaluating the 2nd operand. So check before to avoid
3638 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003639 if (op != '.')
3640 tv_get_number_chk(rettv, &error);
3641 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003642 {
3643 clear_tv(rettv);
3644 return FAIL;
3645 }
3646 }
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 /*
3649 * Get the second variable.
3650 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003651 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003652 {
mityu89dcb4d2021-05-28 13:50:17 +02003653 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003654 clear_tv(rettv);
3655 return FAIL;
3656 }
3657 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003658 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003660 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 return FAIL;
3662 }
3663
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003664 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
3666 /*
3667 * Compute the result.
3668 */
3669 if (op == '.')
3670 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003671 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3672 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003673 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003674
Bram Moolenaar2e086612020-08-25 22:37:48 +02003675 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003676 || var2.v_type == VAR_CHANNEL
3677 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003678 semsg(_(e_using_invalid_value_as_string_str),
3679 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003680 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003681 {
3682 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3683 var2.vval.v_float);
3684 s2 = buf2;
3685 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003686 else
3687 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003688 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 {
3690 clear_tv(rettv);
3691 clear_tv(&var2);
3692 return FAIL;
3693 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003694 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 clear_tv(rettv);
3696 rettv->v_type = VAR_STRING;
3697 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003699 else if (op == '+' && rettv->v_type == VAR_BLOB
3700 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003702 else if (op == '+' && rettv->v_type == VAR_LIST
3703 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003704 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003706 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 else
3709 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003710 int error = FALSE;
3711 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003712 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003713
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003716 f1 = rettv->vval.v_float;
3717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003719 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003721 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003722 if (error)
3723 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003724 // This can only happen for "list + non-list" or
3725 // "blob + non-blob". For "non-list + ..." or
3726 // "something - ...", we returned before evaluating the
3727 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003729 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730 return FAIL;
3731 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003732 if (var2.v_type == VAR_FLOAT)
3733 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003734 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735 if (var2.v_type == VAR_FLOAT)
3736 {
3737 f2 = var2.vval.v_float;
3738 n2 = 0;
3739 }
3740 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003741 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003742 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003743 if (error)
3744 {
3745 clear_tv(rettv);
3746 clear_tv(&var2);
3747 return FAIL;
3748 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003749 if (rettv->v_type == VAR_FLOAT)
3750 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003754 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003755 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3756 {
3757 if (op == '+')
3758 f1 = f1 + f2;
3759 else
3760 f1 = f1 - f2;
3761 rettv->v_type = VAR_FLOAT;
3762 rettv->vval.v_float = f1;
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003765 {
3766 if (op == '+')
3767 n1 = n1 + n2;
3768 else
3769 n1 = n1 - n2;
3770 rettv->v_type = VAR_NUMBER;
3771 rettv->vval.v_number = n1;
3772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 }
3777 return OK;
3778}
3779
3780/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003781 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * * number multiplication
3783 * / number division
3784 * % number modulo
3785 *
3786 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003787 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 *
3789 * Return OK or FAIL.
3790 */
3791 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003792eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003793 char_u **arg,
3794 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003795 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003796 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003798 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003801 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003803 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805
3806 /*
3807 * Repeat computing, until no '*', '/' or '%' is following.
3808 */
3809 for (;;)
3810 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003811 int evaluate;
3812 int getnext;
3813 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003814 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003815 int op;
3816 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003817 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003818 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003819
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003820 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003821 p = eval_next_non_blank(*arg, evalarg, &getnext);
3822 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003823 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003825
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003826 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003827 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003828 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003829 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003830 {
3831 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3832 {
mityu4ac198c2021-05-28 17:52:40 +02003833 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003834 clear_tv(rettv);
3835 return FAIL;
3836 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003837 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003840 f1 = 0;
3841 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003842 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003843 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003845 if (rettv->v_type == VAR_FLOAT)
3846 {
3847 f1 = rettv->vval.v_float;
3848 use_float = TRUE;
3849 n1 = 0;
3850 }
3851 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003852 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 if (error)
3855 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 else
3858 n1 = 0;
3859
3860 /*
3861 * Get the second variable.
3862 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003863 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3864 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003865 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003866 clear_tv(rettv);
3867 return FAIL;
3868 }
3869 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003870 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 return FAIL;
3872
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003873 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003875 if (var2.v_type == VAR_FLOAT)
3876 {
3877 if (!use_float)
3878 {
3879 f1 = n1;
3880 use_float = TRUE;
3881 }
3882 f2 = var2.vval.v_float;
3883 n2 = 0;
3884 }
3885 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003887 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888 clear_tv(&var2);
3889 if (error)
3890 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003891 if (use_float)
3892 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
3895 /*
3896 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003897 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003899 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003901 if (op == '*')
3902 f1 = f1 * f2;
3903 else if (op == '/')
3904 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003905#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003906 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003907 if (f2 == 0.0)
3908 {
3909 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003911 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003912 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003913 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003914 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003915 }
3916 else
3917 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003918#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003919 // We rely on the floating point library to handle divide
3920 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003921 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003922#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003925 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003926 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003927 return FAIL;
3928 }
3929 rettv->v_type = VAR_FLOAT;
3930 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 else
3933 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003934 int failed = FALSE;
3935
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003936 if (op == '*')
3937 n1 = n1 * n2;
3938 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003939 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003941 n1 = num_modulus(n1, n2, &failed);
3942 if (failed)
3943 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003944
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003945 rettv->v_type = VAR_NUMBER;
3946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 }
3950
3951 return OK;
3952}
3953
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003954/*
3955 * Handle a type cast before a base level expression.
3956 * "arg" must point to the first non-white of the expression.
3957 * "arg" is advanced to just after the recognized expression.
3958 * Return OK or FAIL.
3959 */
3960 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003961eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003962 char_u **arg,
3963 typval_T *rettv,
3964 evalarg_T *evalarg,
3965 int want_string) // after "." operator
3966{
3967 type_T *want_type = NULL;
3968 garray_T type_list; // list of pointers to allocated types
3969 int res;
3970 int evaluate = evalarg == NULL ? 0
3971 : (evalarg->eval_flags & EVAL_EVALUATE);
3972
3973 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003974 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3975 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003976 {
3977 ++*arg;
3978 ga_init2(&type_list, sizeof(type_T *), 10);
3979 want_type = parse_type(arg, &type_list, TRUE);
3980 if (want_type == NULL && (evaluate || **arg != '>'))
3981 {
3982 clear_type_list(&type_list);
3983 return FAIL;
3984 }
3985
3986 if (**arg != '>')
3987 {
3988 if (*skipwhite(*arg) == '>')
3989 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3990 else
3991 emsg(_(e_missing_gt));
3992 clear_type_list(&type_list);
3993 return FAIL;
3994 }
3995 ++*arg;
3996 *arg = skipwhite_and_linebreak(*arg, evalarg);
3997 }
3998
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003999 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004000
4001 if (want_type != NULL && evaluate)
4002 {
4003 if (res == OK)
4004 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004005 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4006 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004007
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004008 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004009 {
4010 if (want_type == &t_bool && actual != &t_bool
4011 && (actual->tt_flags & TTFLAG_BOOL_OK))
4012 {
4013 int n = tv2bool(rettv);
4014
4015 // can use "0" and "1" for boolean in some places
4016 clear_tv(rettv);
4017 rettv->v_type = VAR_BOOL;
4018 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4019 }
4020 else
4021 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004022 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004023
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004024 res = check_type(want_type, actual, TRUE, where);
4025 }
4026 }
4027 }
4028 clear_type_list(&type_list);
4029 }
4030
4031 return res;
4032}
4033
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004034 int
4035eval_leader(char_u **arg, int vim9)
4036{
4037 char_u *s = *arg;
4038 char_u *p = *arg;
4039
4040 while (*p == '!' || *p == '-' || *p == '+')
4041 {
4042 char_u *n = skipwhite(p + 1);
4043
4044 // ++, --, -+ and +- are not accepted in Vim9 script
4045 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4046 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004047 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004048 return FAIL;
4049 }
4050 p = n;
4051 }
4052 *arg = p;
4053 return OK;
4054}
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004057 * Check for a predefined value "true", "false" and "null.*".
4058 * Return OK when recognized.
4059 */
4060 int
4061handle_predefined(char_u *s, int len, typval_T *rettv)
4062{
4063 switch (len)
4064 {
4065 case 4: if (STRNCMP(s, "true", 4) == 0)
4066 {
4067 rettv->v_type = VAR_BOOL;
4068 rettv->vval.v_number = VVAL_TRUE;
4069 return OK;
4070 }
4071 if (STRNCMP(s, "null", 4) == 0)
4072 {
4073 rettv->v_type = VAR_SPECIAL;
4074 rettv->vval.v_number = VVAL_NULL;
4075 return OK;
4076 }
4077 break;
4078 case 5: if (STRNCMP(s, "false", 5) == 0)
4079 {
4080 rettv->v_type = VAR_BOOL;
4081 rettv->vval.v_number = VVAL_FALSE;
4082 return OK;
4083 }
4084 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004085 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4086 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004087#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004088 rettv->v_type = VAR_JOB;
4089 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004090#else
4091 rettv->v_type = VAR_SPECIAL;
4092 rettv->vval.v_number = VVAL_NULL;
4093#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004094 return OK;
4095 }
4096 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004097 case 9:
4098 if (STRNCMP(s, "null_", 5) != 0)
4099 break;
4100 if (STRNCMP(s + 5, "list", 4) == 0)
4101 {
4102 rettv->v_type = VAR_LIST;
4103 rettv->vval.v_list = NULL;
4104 return OK;
4105 }
4106 if (STRNCMP(s + 5, "dict", 4) == 0)
4107 {
4108 rettv->v_type = VAR_DICT;
4109 rettv->vval.v_dict = NULL;
4110 return OK;
4111 }
4112 if (STRNCMP(s + 5, "blob", 4) == 0)
4113 {
4114 rettv->v_type = VAR_BLOB;
4115 rettv->vval.v_blob = NULL;
4116 return OK;
4117 }
4118 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004119 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4120 {
4121 rettv->v_type = VAR_CLASS;
4122 rettv->vval.v_class = NULL;
4123 return OK;
4124 }
4125 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004126 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4127 {
4128 rettv->v_type = VAR_STRING;
4129 rettv->vval.v_string = NULL;
4130 return OK;
4131 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004132 if (STRNCMP(s, "null_object", 11) == 0)
4133 {
4134 rettv->v_type = VAR_OBJECT;
4135 rettv->vval.v_object = NULL;
4136 return OK;
4137 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004138 break;
4139 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004140 if (STRNCMP(s, "null_channel", 12) == 0)
4141 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004143 rettv->v_type = VAR_CHANNEL;
4144 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004145#else
4146 rettv->v_type = VAR_SPECIAL;
4147 rettv->vval.v_number = VVAL_NULL;
4148#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004149 return OK;
4150 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004151 if (STRNCMP(s, "null_partial", 12) == 0)
4152 {
4153 rettv->v_type = VAR_PARTIAL;
4154 rettv->vval.v_partial = NULL;
4155 return OK;
4156 }
4157 break;
4158 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4159 {
4160 rettv->v_type = VAR_FUNC;
4161 rettv->vval.v_string = NULL;
4162 return OK;
4163 }
4164 break;
4165 }
4166 return FAIL;
4167}
4168
4169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 * Handle sixth level expression:
4171 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004172 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004173 * "string" string constant
4174 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 * &option-name option value
4176 * @r register contents
4177 * identifier variable value
4178 * function() function call
4179 * $VAR environment variable
4180 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004181 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004183 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004184 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 *
4186 * Also handle:
4187 * ! in front logical NOT
4188 * - in front unary minus
4189 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004190 * trailing [] subscript in String or List
4191 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004192 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 *
4194 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004195 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 *
4197 * Return OK or FAIL.
4198 */
4199 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004200eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201 char_u **arg,
4202 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004203 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004206 int evaluate = evalarg != NULL
4207 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 int len;
4209 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004210 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 char_u *start_leader, *end_leader;
4212 int ret = OK;
4213 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004214 static int recurse = 0;
4215 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004219 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004224 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 */
4226 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004227 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004228 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 end_leader = *arg;
4230
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004231 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004232 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004233 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004234 ++*arg;
4235 return FAIL;
4236 }
4237
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004238 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004239 // and crash. With MSVC the stack is smaller.
4240 if (recurse ==
4241#ifdef _MSC_VER
4242 300
4243#else
4244 1000
4245#endif
4246 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004247 {
4248 semsg(_(e_expression_too_recursive_str), *arg);
4249 return FAIL;
4250 }
4251 ++recurse;
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 switch (**arg)
4254 {
4255 /*
4256 * Number constant.
4257 */
4258 case '0':
4259 case '1':
4260 case '2':
4261 case '3':
4262 case '4':
4263 case '5':
4264 case '6':
4265 case '7':
4266 case '8':
4267 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004268 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004269
4270 // Apply prefixed "-" and "+" now. Matters especially when
4271 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004272 if (ret == OK && evaluate && end_leader > start_leader
4273 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004274 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 break;
4276
4277 /*
4278 * String constant: "string".
4279 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004280 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 break;
4282
4283 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004284 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004286 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 break;
4288
4289 /*
4290 * List: [expr, expr]
4291 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004292 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 break;
4294
4295 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004296 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004297 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004298 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004299 {
4300 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4301 }
4302 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004303 {
4304 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004305 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004306 }
4307 else
4308 ret = NOTDONE;
4309 break;
4310
4311 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004312 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004313 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004315 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004316 ret = NOTDONE;
4317 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004318 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004319 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004320 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004321 break;
4322
4323 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004324 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004326 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 break;
4328
4329 /*
4330 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004331 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 */
LemonBoy2eaef102022-05-06 13:14:50 +01004333 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4334 ret = eval_interp_string(arg, rettv, evaluate);
4335 else
4336 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 break;
4338
4339 /*
4340 * Register contents: @r.
4341 */
4342 case '@': ++*arg;
4343 if (evaluate)
4344 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004345 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004346 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004347 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004348 emsg_invreg(**arg);
4349 else
4350 {
4351 rettv->v_type = VAR_STRING;
4352 rettv->vval.v_string = get_reg_contents(**arg,
4353 GREG_EXPR_SRC);
4354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 if (**arg != NUL)
4357 ++*arg;
4358 break;
4359
4360 /*
4361 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004362 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004364 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004365 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004366 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004367 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004368 if (ret == OK && evaluate)
4369 {
4370 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4371
Bram Moolenaara9931532021-06-12 15:58:16 +02004372 // Compile it here to get the return type. The return
4373 // type is optional, when it's missing use t_unknown.
4374 // This is recognized in compile_return().
4375 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4376 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004377 if (compile_def_function(ufunc, FALSE,
4378 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004379 {
4380 clear_tv(rettv);
4381 ret = FAIL;
4382 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004383 }
4384 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004385 if (ret == NOTDONE)
4386 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004387 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004388 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004389
Bram Moolenaar9215f012020-06-27 21:18:00 +02004390 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004391 if (**arg == ')')
4392 ++*arg;
4393 else if (ret == OK)
4394 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004395 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004396 clear_tv(rettv);
4397 ret = FAIL;
4398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 break;
4401
Bram Moolenaar8c711452005-01-14 21:53:12 +00004402 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 break;
4404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004405
4406 if (ret == NOTDONE)
4407 {
4408 /*
4409 * Must be a variable or function name.
4410 * Can also be a curly-braces kind of name: {expr}.
4411 */
4412 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004413 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414 if (alias != NULL)
4415 s = alias;
4416
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004417 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 ret = FAIL;
4419 else
4420 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004421 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4422
Bram Moolenaar4525a572022-02-13 11:57:33 +00004423 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004424 {
4425 emsg(_(e_cannot_use_underscore_here));
4426 ret = FAIL;
4427 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004428 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004429 && s[0] == 's' && s[1] == ':')
4430 {
4431 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4432 ret = FAIL;
4433 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004434 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004435 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004436 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004437 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004438 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004439 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004440 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004441 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004442 // get the value of "true", "false", etc. or a variable
4443 ret = FAIL;
4444 if (vim9script)
4445 ret = handle_predefined(s, len, rettv);
4446 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004447 {
4448 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004449 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004450 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004451 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004452 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004453 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004454 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004455 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004456 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004457 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004458 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004460 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 }
4462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004463 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4464 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004465 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004466 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 /*
4469 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4470 */
4471 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004472 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004473
4474 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004475 return ret;
4476}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004477
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004478/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004479 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004480 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004481 * Adjusts "end_leaderp" until it is at "start_leader".
4482 */
4483 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004484eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004485 typval_T *rettv,
4486 int numeric_only,
4487 char_u *start_leader,
4488 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004489{
4490 char_u *end_leader = *end_leaderp;
4491 int ret = OK;
4492 int error = FALSE;
4493 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004494 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004495 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004496 float_T f = 0.0;
4497
4498 if (rettv->v_type == VAR_FLOAT)
4499 f = rettv->vval.v_float;
4500 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004501 {
4502 while (VIM_ISWHITE(end_leader[-1]))
4503 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004504 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004505 val = tv2bool(rettv);
4506 else
4507 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004508 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004509 if (error)
4510 {
4511 clear_tv(rettv);
4512 ret = FAIL;
4513 }
4514 else
4515 {
4516 while (end_leader > start_leader)
4517 {
4518 --end_leader;
4519 if (*end_leader == '!')
4520 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004521 if (numeric_only)
4522 {
4523 ++end_leader;
4524 break;
4525 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004526 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004527 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004528 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004529 {
4530 rettv->v_type = VAR_BOOL;
4531 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4532 }
4533 else
4534 f = !f;
4535 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004536 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004537 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004538 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004539 type = VAR_BOOL;
4540 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004541 }
4542 else if (*end_leader == '-')
4543 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004544 if (rettv->v_type == VAR_FLOAT)
4545 f = -f;
4546 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004547 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004548 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004549 type = VAR_NUMBER;
4550 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004551 }
4552 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004553 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004555 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004556 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 else
4559 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004560 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004561 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004562 rettv->v_type = type;
4563 else
4564 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004565 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004568 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 return ret;
4570}
4571
4572/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004573 * Call the function referred to in "rettv".
4574 */
4575 static int
4576call_func_rettv(
4577 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004578 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004579 typval_T *rettv,
4580 int evaluate,
4581 dict_T *selfdict,
4582 typval_T *basetv)
4583{
4584 partial_T *pt = NULL;
4585 funcexe_T funcexe;
4586 typval_T functv;
4587 char_u *s;
4588 int ret;
4589
4590 // need to copy the funcref so that we can clear rettv
4591 if (evaluate)
4592 {
4593 functv = *rettv;
4594 rettv->v_type = VAR_UNKNOWN;
4595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004596 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004597 if (functv.v_type == VAR_PARTIAL)
4598 {
4599 pt = functv.vval.v_partial;
4600 s = partial_name(pt);
4601 }
4602 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004603 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004604 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004605 if (s == NULL || *s == NUL)
4606 {
4607 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004608 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004609 goto theend;
4610 }
4611 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004612 }
4613 else
4614 s = (char_u *)"";
4615
Bram Moolenaara80faa82020-04-12 19:37:17 +02004616 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004617 funcexe.fe_firstline = curwin->w_cursor.lnum;
4618 funcexe.fe_lastline = curwin->w_cursor.lnum;
4619 funcexe.fe_evaluate = evaluate;
4620 funcexe.fe_partial = pt;
4621 funcexe.fe_selfdict = selfdict;
4622 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004623 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004624
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004625theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004626 // Clear the funcref afterwards, so that deleting it while
4627 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004628 if (evaluate)
4629 clear_tv(&functv);
4630
4631 return ret;
4632}
4633
4634/*
4635 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004636 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004637 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4638 */
4639 static int
4640eval_lambda(
4641 char_u **arg,
4642 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004643 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004645{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004646 int evaluate = evalarg != NULL
4647 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004648 typval_T base = *rettv;
4649 int ret;
4650
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004651 rettv->v_type = VAR_UNKNOWN;
4652
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004653 if (**arg == '{')
4654 {
4655 // ->{lambda}()
4656 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4657 }
4658 else
4659 {
4660 // ->(lambda)()
4661 ++*arg;
4662 ret = eval1(arg, rettv, evalarg);
4663 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004664 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004665 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004666 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004667 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004668 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004669 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4670 && rettv->v_type != VAR_PARTIAL)
4671 {
4672 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4673 return FAIL;
4674 }
4675 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004676 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004677 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004678 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004679
4680 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004681 {
4682 if (verbose)
4683 {
4684 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004685 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004686 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004687 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004688 }
4689 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004690 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004691 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004692 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004693 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004694
4695 // Clear the funcref afterwards, so that deleting it while
4696 // evaluating the arguments is possible (see test55).
4697 if (evaluate)
4698 clear_tv(&base);
4699
4700 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004701}
4702
4703/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004704 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004705 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004706 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4707 */
4708 static int
4709eval_method(
4710 char_u **arg,
4711 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004712 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004713 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004714{
4715 char_u *name;
4716 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004717 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004718 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004719 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004720 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004721 int evaluate = evalarg != NULL
4722 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004723
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004724 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004725
Bram Moolenaarac92e252019-08-03 21:58:38 +02004726 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004727 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004728 if (alias != NULL)
4729 name = alias;
4730
4731 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004732 {
4733 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004734 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004735 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004736 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004737 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004738 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004739 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004740
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004741 // If there is no "(" immediately following, but there is further on,
4742 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4743 // Does not handle anything where "(" is part of the expression.
4744 *arg = skipwhite(*arg);
4745
4746 if (**arg != '(' && alias == NULL
4747 && (paren = vim_strchr(*arg, '(')) != NULL)
4748 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004749 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004750
4751 // Truncate the name a the "(". Avoid trying to get another line
4752 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004753 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004754 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4755 if (evalarg != NULL)
4756 {
4757 getline = evalarg->eval_getline;
4758 evalarg->eval_getline = NULL;
4759 }
4760
4761 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004762 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004763 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004764 *arg = name + len;
4765 ret = FAIL;
4766 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004767 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004768 {
4769 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004770 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004771 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004772
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004773 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004774 if (getline != NULL)
4775 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004776 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004777
4778 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004779 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004780 *arg = skipwhite(*arg);
4781
4782 if (**arg != '(')
4783 {
4784 if (verbose)
4785 semsg(_(e_missing_parenthesis_str), name);
4786 ret = FAIL;
4787 }
4788 else if (VIM_ISWHITE((*arg)[-1]))
4789 {
4790 if (verbose)
4791 emsg(_(e_no_white_space_allowed_before_parenthesis));
4792 ret = FAIL;
4793 }
4794 else
4795 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004796 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004797 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004798 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004799
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004800 // Clear the funcref afterwards, so that deleting it while
4801 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004802 if (evaluate)
4803 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004804 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004805
Bram Moolenaarac92e252019-08-03 21:58:38 +02004806 return ret;
4807}
4808
4809/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004810 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4811 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4813 */
4814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004815eval_index(
4816 char_u **arg,
4817 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004818 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004819 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004821 int evaluate = evalarg != NULL
4822 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004824 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004827 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004828 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004830 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4831 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004833 init_tv(&var1);
4834 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004837 /*
4838 * dict.name
4839 */
4840 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004841 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004843 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004845 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 }
4847 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 /*
4850 * something[idx]
4851 *
4852 * Get the (first) variable from inside the [].
4853 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004854 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 if (**arg == ':')
4856 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004857 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004859 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004860 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004861 semsg(_(e_white_space_required_before_and_after_str_at_str),
4862 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004863 clear_tv(&var1);
4864 return FAIL;
4865 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004866 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004868 int error = FALSE;
4869
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004870 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004871 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004872 && var1.v_type == VAR_FLOAT)
4873 {
4874 var1.vval.v_string = typval_tostring(&var1, TRUE);
4875 var1.v_type = VAR_STRING;
4876 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004877
Bram Moolenaar4525a572022-02-13 11:57:33 +00004878 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004879 tv_get_number_chk(&var1, &error);
4880 else
4881 error = tv_get_string_chk(&var1) == NULL;
4882 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004883 {
4884 // not a number or string
4885 clear_tv(&var1);
4886 return FAIL;
4887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889
4890 /*
4891 * Get the second variable from inside the [:].
4892 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004893 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004894 if (**arg == ':')
4895 {
4896 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004897 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004898 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004899 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004900 semsg(_(e_white_space_required_before_and_after_str_at_str),
4901 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004902 if (!empty1)
4903 clear_tv(&var1);
4904 return FAIL;
4905 }
4906 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004907 if (**arg == ']')
4908 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004909 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004911 if (!empty1)
4912 clear_tv(&var1);
4913 return FAIL;
4914 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004915 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004917 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004918 if (!empty1)
4919 clear_tv(&var1);
4920 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 return FAIL;
4922 }
4923 }
4924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004926 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 if (**arg != ']')
4928 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004929 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004930 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004931 clear_tv(&var1);
4932 if (range)
4933 clear_tv(&var2);
4934 return FAIL;
4935 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004936 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 }
4938
4939 if (evaluate)
4940 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004942 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004943 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004944
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004945 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004948 clear_tv(&var2);
4949 return res;
4950 }
4951 return OK;
4952}
4953
4954/*
4955 * Check if "rettv" can have an [index] or [sli:ce]
4956 */
4957 int
4958check_can_index(typval_T *rettv, int evaluate, int verbose)
4959{
4960 switch (rettv->v_type)
4961 {
4962 case VAR_FUNC:
4963 case VAR_PARTIAL:
4964 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004965 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004966 return FAIL;
4967 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004968 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004969 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004970 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004971 case VAR_BOOL:
4972 case VAR_SPECIAL:
4973 case VAR_JOB:
4974 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004975 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004976 case VAR_CLASS:
4977 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004978 if (verbose)
4979 emsg(_(e_cannot_index_special_variable));
4980 return FAIL;
4981 case VAR_UNKNOWN:
4982 case VAR_ANY:
4983 case VAR_VOID:
4984 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004986 emsg(_(e_cannot_index_special_variable));
4987 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004989 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004991 case VAR_STRING:
4992 case VAR_LIST:
4993 case VAR_DICT:
4994 case VAR_BLOB:
4995 break;
4996 case VAR_NUMBER:
4997 if (in_vim9script())
4998 emsg(_(e_cannot_index_number));
4999 break;
5000 }
5001 return OK;
5002}
5003
5004/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005005 * slice() function
5006 */
5007 void
5008f_slice(typval_T *argvars, typval_T *rettv)
5009{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005010 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005011 && ((argvars[0].v_type != VAR_STRING
5012 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005013 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005014 && check_for_list_arg(argvars, 0) == FAIL)
5015 || check_for_number_arg(argvars, 1) == FAIL
5016 || check_for_opt_number_arg(argvars, 2) == FAIL))
5017 return;
5018
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005019 if (check_can_index(argvars, TRUE, FALSE) != OK)
5020 return;
5021
5022 copy_tv(argvars, rettv);
5023 eval_index_inner(rettv, TRUE, argvars + 1,
5024 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5025 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005026}
5027
5028/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005029 * Apply index or range to "rettv".
5030 * "var1" is the first index, NULL for [:expr].
5031 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005032 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5033 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005034 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5035 */
5036 int
5037eval_index_inner(
5038 typval_T *rettv,
5039 int is_range,
5040 typval_T *var1,
5041 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005042 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005043 char_u *key,
5044 int keylen,
5045 int verbose)
5046{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005047 varnumber_T n1, n2 = 0;
5048 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005049
5050 n1 = 0;
5051 if (var1 != NULL && rettv->v_type != VAR_DICT)
5052 n1 = tv_get_number(var1);
5053
5054 if (is_range)
5055 {
5056 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005057 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005058 if (verbose)
5059 emsg(_(e_cannot_slice_dictionary));
5060 return FAIL;
5061 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005062 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005063 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005064 else
5065 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005066 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005067
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005068 switch (rettv->v_type)
5069 {
5070 case VAR_UNKNOWN:
5071 case VAR_ANY:
5072 case VAR_VOID:
5073 case VAR_FUNC:
5074 case VAR_PARTIAL:
5075 case VAR_FLOAT:
5076 case VAR_BOOL:
5077 case VAR_SPECIAL:
5078 case VAR_JOB:
5079 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005080 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005081 case VAR_CLASS:
5082 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005083 break; // not evaluating, skipping over subscript
5084
5085 case VAR_NUMBER:
5086 case VAR_STRING:
5087 {
5088 char_u *s = tv_get_string(rettv);
5089
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005090 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005091 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005092 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005093 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005094 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005095 else
5096 s = char_from_string(s, n1);
5097 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005098 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // The resulting variable is a substring. If the indexes
5101 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102 if (n1 < 0)
5103 {
5104 n1 = len + n1;
5105 if (n1 < 0)
5106 n1 = 0;
5107 }
5108 if (n2 < 0)
5109 n2 = len + n2;
5110 else if (n2 >= len)
5111 n2 = len;
5112 if (n1 >= len || n2 < 0 || n1 > n2)
5113 s = NULL;
5114 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005115 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 }
5117 else
5118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // The resulting variable is a string of a single
5120 // character. If the index is too big or negative the
5121 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 if (n1 >= len || n1 < 0)
5123 s = NULL;
5124 else
5125 s = vim_strnsave(s + n1, 1);
5126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 clear_tv(rettv);
5128 rettv->v_type = VAR_STRING;
5129 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005130 }
5131 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005132
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005133 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005134 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5135 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005136 break;
5137
5138 case VAR_LIST:
5139 if (var1 == NULL)
5140 n1 = 0;
5141 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005142 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005143 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005144 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005145 return FAIL;
5146 break;
5147
5148 case VAR_DICT:
5149 {
5150 dictitem_T *item;
5151 typval_T tmp;
5152
5153 if (key == NULL)
5154 {
5155 key = tv_get_string_chk(var1);
5156 if (key == NULL)
5157 return FAIL;
5158 }
5159
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005160 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005161
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005162 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005163 {
5164 if (verbose)
5165 {
5166 if (keylen > 0)
5167 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005168 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005169 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005170 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005171 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005172
5173 copy_tv(&item->di_tv, &tmp);
5174 clear_tv(rettv);
5175 *rettv = tmp;
5176 }
5177 break;
5178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 return OK;
5180}
5181
5182/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005183 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005184 */
5185 char_u *
5186partial_name(partial_T *pt)
5187{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005188 if (pt != NULL)
5189 {
5190 if (pt->pt_name != NULL)
5191 return pt->pt_name;
5192 if (pt->pt_func != NULL)
5193 return pt->pt_func->uf_name;
5194 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005195 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005196}
5197
Bram Moolenaarddecc252016-04-06 22:59:37 +02005198 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005199partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005200{
5201 int i;
5202
5203 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005204 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005205 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005206 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005207 if (pt->pt_name != NULL)
5208 {
5209 func_unref(pt->pt_name);
5210 vim_free(pt->pt_name);
5211 }
5212 else
5213 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005214
Bram Moolenaar54656012021-06-09 20:50:46 +02005215 // "out_up" is no longer used, decrement refcount on partial that owns it.
5216 partial_unref(pt->pt_outer.out_up_partial);
5217
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005218 // Using pt_outer from another partial.
5219 partial_unref(pt->pt_outer_partial);
5220
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005221 // Decrease the reference count for the context of a closure. If down
5222 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005223 if (pt->pt_funcstack != NULL)
5224 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005225 --pt->pt_funcstack->fs_refcount;
5226 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005227 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005228 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005229 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5230 if (pt->pt_loopvars[i] != NULL)
5231 {
5232 --pt->pt_loopvars[i]->lvs_refcount;
5233 loopvars_check_refcount(pt->pt_loopvars[i]);
5234 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005235
Bram Moolenaarddecc252016-04-06 22:59:37 +02005236 vim_free(pt);
5237}
5238
5239/*
5240 * Unreference a closure: decrement the reference count and free it when it
5241 * becomes zero.
5242 */
5243 void
5244partial_unref(partial_T *pt)
5245{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005246 if (pt == NULL)
5247 return;
5248
5249 int done = FALSE;
5250
5251 if (--pt->pt_refcount <= 0)
5252 partial_free(pt);
5253
5254 // If the reference count goes down to one, the funcstack may be the
5255 // only reference and can be freed if no other partials reference it.
5256 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005257 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005258 // careful: if the funcstack is freed it may contain this partial
5259 // and it gets freed as well
5260 if (pt->pt_funcstack != NULL)
5261 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005262
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005263 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005264 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005265 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005266
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005267 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5268 if (pt->pt_loopvars[depth] != NULL
5269 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005270 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005271 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005272 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005273}
5274
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005275/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005276 * Return the next (unique) copy ID.
5277 * Used for serializing nested structures.
5278 */
5279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005280get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005281{
5282 current_copyID += COPYID_INC;
5283 return current_copyID;
5284}
5285
5286/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005287 * Garbage collection for lists and dictionaries.
5288 *
5289 * We use reference counts to be able to free most items right away when they
5290 * are no longer used. But for composite items it's possible that it becomes
5291 * unused while the reference count is > 0: When there is a recursive
5292 * reference. Example:
5293 * :let l = [1, 2, 3]
5294 * :let d = {9: l}
5295 * :let l[1] = d
5296 *
5297 * Since this is quite unusual we handle this with garbage collection: every
5298 * once in a while find out which lists and dicts are not referenced from any
5299 * variable.
5300 *
5301 * Here is a good reference text about garbage collection (refers to Python
5302 * but it applies to all reference-counting mechanisms):
5303 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005304 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005305
5306/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005307 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005308 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005309 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005310 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005311 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005312garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005313{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005314 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005315 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005316 buf_T *buf;
5317 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005318 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005319 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005321 if (!testing)
5322 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005324 want_garbage_collect = FALSE;
5325 may_garbage_collect = FALSE;
5326 garbage_collect_at_exit = FALSE;
5327 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005328
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005329 // The execution stack can grow big, limit the size.
5330 if (exestack.ga_maxlen - exestack.ga_len > 500)
5331 {
5332 size_t new_len;
5333 char_u *pp;
5334 int n;
5335
5336 // Keep 150% of the current size, with a minimum of the growth size.
5337 n = exestack.ga_len / 2;
5338 if (n < exestack.ga_growsize)
5339 n = exestack.ga_growsize;
5340
5341 // Don't make it bigger though.
5342 if (exestack.ga_len + n < exestack.ga_maxlen)
5343 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005344 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005345 pp = vim_realloc(exestack.ga_data, new_len);
5346 if (pp == NULL)
5347 return FAIL;
5348 exestack.ga_maxlen = exestack.ga_len + n;
5349 exestack.ga_data = pp;
5350 }
5351 }
5352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005353 // We advance by two because we add one for items referenced through
5354 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005355 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005356
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005357 /*
5358 * 1. Go through all accessible variables and mark all lists and dicts
5359 * with copyID.
5360 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 // Don't free variables in the previous_funccal list unless they are only
5363 // referenced through previous_funccal. This must be first, because if
5364 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005365 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005368 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005371 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005372 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5373 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005376 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005377 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5378 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005379 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005380 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005381 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005382 abort = abort || set_ref_in_item(
5383 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005384#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005385 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005386 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5387 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005388 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005389 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005390 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5391 NULL, NULL);
5392#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005393
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005394 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005395 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5397 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005399 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005401 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005405 abort = abort || set_ref_in_functions(copyID);
5406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005409
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005410 // funcstacks keep variables for closures
5411 abort = abort || set_ref_in_funcstacks(copyID);
5412
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005413 // loopvars keep variables for loop blocks
5414 abort = abort || set_ref_in_loopvars(copyID);
5415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005416 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005417 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005418
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005419 // callbacks in buffers
5420 abort = abort || set_ref_in_buffers(copyID);
5421
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005422 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5423 abort = abort || set_ref_in_insexpand_funcs(copyID);
5424
5425 // 'operatorfunc' callback
5426 abort = abort || set_ref_in_opfunc(copyID);
5427
5428 // 'tagfunc' callback
5429 abort = abort || set_ref_in_tagfunc(copyID);
5430
5431 // 'imactivatefunc' and 'imstatusfunc' callbacks
5432 abort = abort || set_ref_in_im_funcs(copyID);
5433
Bram Moolenaar1dced572012-04-05 16:54:08 +02005434#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005435 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005436#endif
5437
Bram Moolenaardb913952012-06-29 12:54:53 +02005438#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005439 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005440#endif
5441
5442#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005443 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005444#endif
5445
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005446#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005447 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005448 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005449#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005450#ifdef FEAT_NETBEANS_INTG
5451 abort = abort || set_ref_in_nb_channel(copyID);
5452#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005453
Bram Moolenaare3188e22016-05-31 21:13:04 +02005454#ifdef FEAT_TIMERS
5455 abort = abort || set_ref_in_timer(copyID);
5456#endif
5457
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005458#ifdef FEAT_QUICKFIX
5459 abort = abort || set_ref_in_quickfix(copyID);
5460#endif
5461
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005462#ifdef FEAT_TERMINAL
5463 abort = abort || set_ref_in_term(copyID);
5464#endif
5465
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005466#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005467 abort = abort || set_ref_in_popups(copyID);
5468#endif
5469
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005470 abort = abort || set_ref_in_classes(copyID);
5471
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005473 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005474 /*
5475 * 2. Free lists and dictionaries that are not referenced.
5476 */
5477 did_free = free_unref_items(copyID);
5478
5479 /*
5480 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005481 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005482 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005483 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005484 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 else if (p_verbose > 0)
5486 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005487 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005489
5490 return did_free;
5491}
5492
5493/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005494 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005495 */
5496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005497free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005498{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005499 int did_free = FALSE;
5500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005501 // Let all "free" functions know that we are here. This means no
5502 // dictionaries, lists, channels or jobs are to be freed, because we will
5503 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005504 in_free_unref_items = TRUE;
5505
5506 /*
5507 * PASS 1: free the contents of the items. We don't free the items
5508 * themselves yet, so that it is possible to decrement refcount counters
5509 */
5510
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005511 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005512 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005513
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005514 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005515 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005516
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005517 // Go through the list of objects and free items without this copyID.
5518 did_free |= object_free_nonref(copyID);
5519
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005520 // Go through the list of classes and free items without this copyID.
5521 did_free |= class_free_nonref(copyID);
5522
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005524 // Go through the list of jobs and free items without the copyID. This
5525 // must happen before doing channels, because jobs refer to channels, but
5526 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005527 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005529 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005530 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5531#endif
5532
5533 /*
5534 * PASS 2: free the items themselves.
5535 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005536 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005537 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005538
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005539#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 // Go through the list of jobs and free items without the copyID. This
5541 // must happen before doing channels, because jobs refer to channels, but
5542 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005543 free_unused_jobs(copyID, COPYID_MASK);
5544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005546 free_unused_channels(copyID, COPYID_MASK);
5547#endif
5548
5549 in_free_unref_items = FALSE;
5550
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551 return did_free;
5552}
5553
5554/*
5555 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005556 * "list_stack" is used to add lists to be marked. Can be NULL.
5557 *
5558 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005559 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005561set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005562{
5563 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005564 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005565 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005566 hashtab_T *cur_ht;
5567 ht_stack_T *ht_stack = NULL;
5568 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005569
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005570 cur_ht = ht;
5571 for (;;)
5572 {
5573 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 // Mark each item in the hashtab. If the item contains a hashtab
5576 // it is added to ht_stack, if it contains a list it is added to
5577 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005578 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005579 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005580 if (!HASHITEM_EMPTY(hi))
5581 {
5582 --todo;
5583 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5584 &ht_stack, list_stack);
5585 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005586 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005587
5588 if (ht_stack == NULL)
5589 break;
5590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005591 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005592 cur_ht = ht_stack->ht;
5593 tempitem = ht_stack;
5594 ht_stack = ht_stack->prev;
5595 free(tempitem);
5596 }
5597
5598 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005599}
5600
Dominique Pelle748b3082022-01-08 12:41:16 +00005601#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5602 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005603/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005604 * Mark a dict and its items with "copyID".
5605 * Returns TRUE if setting references failed somehow.
5606 */
5607 int
5608set_ref_in_dict(dict_T *d, int copyID)
5609{
5610 if (d != NULL && d->dv_copyID != copyID)
5611 {
5612 d->dv_copyID = copyID;
5613 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5614 }
5615 return FALSE;
5616}
Dominique Pelle748b3082022-01-08 12:41:16 +00005617#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005618
5619/*
5620 * Mark a list and its items with "copyID".
5621 * Returns TRUE if setting references failed somehow.
5622 */
5623 int
5624set_ref_in_list(list_T *ll, int copyID)
5625{
5626 if (ll != NULL && ll->lv_copyID != copyID)
5627 {
5628 ll->lv_copyID = copyID;
5629 return set_ref_in_list_items(ll, copyID, NULL);
5630 }
5631 return FALSE;
5632}
5633
5634/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005635 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005636 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5637 *
5638 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005639 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005640 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005641set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005642{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005643 listitem_T *li;
5644 int abort = FALSE;
5645 list_T *cur_l;
5646 list_stack_T *list_stack = NULL;
5647 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005648
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 cur_l = l;
5650 for (;;)
5651 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005652 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005653 // Mark each item in the list. If the item contains a hashtab
5654 // it is added to ht_stack, if it contains a list it is added to
5655 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005656 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5657 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5658 ht_stack, &list_stack);
5659 if (list_stack == NULL)
5660 break;
5661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005663 cur_l = list_stack->list;
5664 tempitem = list_stack;
5665 list_stack = list_stack->prev;
5666 free(tempitem);
5667 }
5668
5669 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005670}
5671
5672/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005673 * Mark the partial in callback 'cb' with "copyID".
5674 */
5675 int
5676set_ref_in_callback(callback_T *cb, int copyID)
5677{
5678 typval_T tv;
5679
5680 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5681 return FALSE;
5682
5683 tv.v_type = VAR_PARTIAL;
5684 tv.vval.v_partial = cb->cb_partial;
5685 return set_ref_in_item(&tv, copyID, NULL, NULL);
5686}
5687
5688/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005689 * Mark the dict "dd" with "copyID".
5690 * Also see set_ref_in_item().
5691 */
5692 static int
5693set_ref_in_item_dict(
5694 dict_T *dd,
5695 int copyID,
5696 ht_stack_T **ht_stack,
5697 list_stack_T **list_stack)
5698{
5699 if (dd == NULL || dd->dv_copyID == copyID)
5700 return FALSE;
5701
5702 // Didn't see this dict yet.
5703 dd->dv_copyID = copyID;
5704 if (ht_stack == NULL)
5705 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5706
5707 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5708 if (newitem == NULL)
5709 return TRUE;
5710
5711 newitem->ht = &dd->dv_hashtab;
5712 newitem->prev = *ht_stack;
5713 *ht_stack = newitem;
5714
5715 return FALSE;
5716}
5717
5718/*
5719 * Mark the list "ll" with "copyID".
5720 * Also see set_ref_in_item().
5721 */
5722 static int
5723set_ref_in_item_list(
5724 list_T *ll,
5725 int copyID,
5726 ht_stack_T **ht_stack,
5727 list_stack_T **list_stack)
5728{
5729 if (ll == NULL || ll->lv_copyID == copyID)
5730 return FALSE;
5731
5732 // Didn't see this list yet.
5733 ll->lv_copyID = copyID;
5734 if (list_stack == NULL)
5735 return set_ref_in_list_items(ll, copyID, ht_stack);
5736
5737 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5738 if (newitem == NULL)
5739 return TRUE;
5740
5741 newitem->list = ll;
5742 newitem->prev = *list_stack;
5743 *list_stack = newitem;
5744
5745 return FALSE;
5746}
5747
5748/*
5749 * Mark the partial "pt" with "copyID".
5750 * Also see set_ref_in_item().
5751 */
5752 static int
5753set_ref_in_item_partial(
5754 partial_T *pt,
5755 int copyID,
5756 ht_stack_T **ht_stack,
5757 list_stack_T **list_stack)
5758{
5759 if (pt == NULL || pt->pt_copyID == copyID)
5760 return FALSE;
5761
5762 // Didn't see this partial yet.
5763 pt->pt_copyID = copyID;
5764
5765 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5766
5767 if (pt->pt_dict != NULL)
5768 {
5769 typval_T dtv;
5770
5771 dtv.v_type = VAR_DICT;
5772 dtv.vval.v_dict = pt->pt_dict;
5773 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5774 }
5775
5776 for (int i = 0; i < pt->pt_argc; ++i)
5777 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5778 ht_stack, list_stack);
5779 // pt_funcstack is handled in set_ref_in_funcstacks()
5780 // pt_loopvars is handled in set_ref_in_loopvars()
5781
5782 return abort;
5783}
5784
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005785#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005786/*
5787 * Mark the job "pt" with "copyID".
5788 * Also see set_ref_in_item().
5789 */
5790 static int
5791set_ref_in_item_job(
5792 job_T *job,
5793 int copyID,
5794 ht_stack_T **ht_stack,
5795 list_stack_T **list_stack)
5796{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005797 typval_T dtv;
5798
5799 if (job == NULL || job->jv_copyID == copyID)
5800 return FALSE;
5801
5802 job->jv_copyID = copyID;
5803 if (job->jv_channel != NULL)
5804 {
5805 dtv.v_type = VAR_CHANNEL;
5806 dtv.vval.v_channel = job->jv_channel;
5807 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5808 }
5809 if (job->jv_exit_cb.cb_partial != NULL)
5810 {
5811 dtv.v_type = VAR_PARTIAL;
5812 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5813 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5814 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005815
5816 return FALSE;
5817}
5818
5819/*
5820 * Mark the channel "ch" with "copyID".
5821 * Also see set_ref_in_item().
5822 */
5823 static int
5824set_ref_in_item_channel(
5825 channel_T *ch,
5826 int copyID,
5827 ht_stack_T **ht_stack,
5828 list_stack_T **list_stack)
5829{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005830 typval_T dtv;
5831
5832 if (ch == NULL || ch->ch_copyID == copyID)
5833 return FALSE;
5834
5835 ch->ch_copyID = copyID;
5836 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5837 {
5838 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5839 jq != NULL; jq = jq->jq_next)
5840 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5841 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5842 cq = cq->cq_next)
5843 if (cq->cq_callback.cb_partial != NULL)
5844 {
5845 dtv.v_type = VAR_PARTIAL;
5846 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5847 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5848 }
5849 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5850 {
5851 dtv.v_type = VAR_PARTIAL;
5852 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5853 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5854 }
5855 }
5856 if (ch->ch_callback.cb_partial != NULL)
5857 {
5858 dtv.v_type = VAR_PARTIAL;
5859 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5860 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5861 }
5862 if (ch->ch_close_cb.cb_partial != NULL)
5863 {
5864 dtv.v_type = VAR_PARTIAL;
5865 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5866 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5867 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005868
5869 return FALSE;
5870}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005871#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005872
5873/*
5874 * Mark the class "cl" with "copyID".
5875 * Also see set_ref_in_item().
5876 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005877 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005878set_ref_in_item_class(
5879 class_T *cl,
5880 int copyID,
5881 ht_stack_T **ht_stack,
5882 list_stack_T **list_stack)
5883{
5884 int abort = FALSE;
5885
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005886 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005887 return FALSE;
5888
5889 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005890 if (cl->class_members_tv != NULL)
5891 {
5892 // The "class_members_tv" table is allocated only for regular classes
5893 // and not for interfaces.
5894 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5895 abort = abort || set_ref_in_item(
5896 &cl->class_members_tv[i],
5897 copyID, ht_stack, list_stack);
5898 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005899
5900 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5901 abort = abort || set_ref_in_func(NULL,
5902 cl->class_class_functions[i], copyID);
5903
5904 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5905 abort = abort || set_ref_in_func(NULL,
5906 cl->class_obj_methods[i], copyID);
5907
5908 return abort;
5909}
5910
5911/*
5912 * Mark the object "cl" with "copyID".
5913 * Also see set_ref_in_item().
5914 */
5915 static int
5916set_ref_in_item_object(
5917 object_T *obj,
5918 int copyID,
5919 ht_stack_T **ht_stack,
5920 list_stack_T **list_stack)
5921{
5922 int abort = FALSE;
5923
5924 if (obj == NULL || obj->obj_copyID == copyID)
5925 return FALSE;
5926
5927 obj->obj_copyID = copyID;
5928
5929 // The typval_T array is right after the object_T.
5930 typval_T *mtv = (typval_T *)(obj + 1);
5931 for (int i = 0; !abort
5932 && i < obj->obj_class->class_obj_member_count; ++i)
5933 abort = abort || set_ref_in_item(mtv + i, copyID,
5934 ht_stack, list_stack);
5935
5936 return abort;
5937}
5938
5939/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005940 * Mark all lists, dicts and other container types referenced through typval
5941 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005942 * "list_stack" is used to add lists to be marked. Can be NULL.
5943 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5944 *
5945 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005946 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005947 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005948set_ref_in_item(
5949 typval_T *tv,
5950 int copyID,
5951 ht_stack_T **ht_stack,
5952 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005954 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005955
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005956 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005957 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005958 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005959 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5960 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005961
5962 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005963 return set_ref_in_item_list(tv->vval.v_list, copyID,
5964 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005965
5966 case VAR_FUNC:
5967 {
5968 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5969 break;
5970 }
5971
5972 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005973 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5974 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005975
5976 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005977#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005978 return set_ref_in_item_job(tv->vval.v_job, copyID,
5979 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005980#else
5981 break;
5982#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005983
5984 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005985#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005986 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005987 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005988#else
5989 break;
5990#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005991
5992 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005993 return set_ref_in_item_class(tv->vval.v_class, copyID,
5994 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005995
5996 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005997 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005998 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005999
6000 case VAR_UNKNOWN:
6001 case VAR_ANY:
6002 case VAR_VOID:
6003 case VAR_BOOL:
6004 case VAR_SPECIAL:
6005 case VAR_NUMBER:
6006 case VAR_FLOAT:
6007 case VAR_STRING:
6008 case VAR_BLOB:
6009 case VAR_INSTR:
6010 // Types that do not contain any other item
6011 break;
6012 }
6013
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006014 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015}
6016
Bram Moolenaar8c711452005-01-14 21:53:12 +00006017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Return a string with the string representation of a variable.
6019 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006020 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006021 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006022 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006023 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006024 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6026 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006027 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006029 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006030echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031 typval_T *tv,
6032 char_u **tofree,
6033 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006034 int copyID,
6035 int echo_style,
6036 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006037 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006039 static int recurse = 0;
6040 char_u *r = NULL;
6041
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006043 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006044 if (!did_echo_string_emsg)
6045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 // Only give this message once for a recursive call to avoid
6047 // flooding the user with errors. And stop iterating over lists
6048 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006049 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006050 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006051 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006052 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006053 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054 }
6055 ++recurse;
6056
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 switch (tv->v_type)
6058 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006059 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006060 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006061 {
6062 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006063 r = tv->vval.v_string;
6064 if (r == NULL)
6065 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006066 }
6067 else
6068 {
6069 *tofree = string_quote(tv->vval.v_string, FALSE);
6070 r = *tofree;
6071 }
6072 break;
6073
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006075 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006076 char_u buf[MAX_FUNC_NAME_LEN];
6077
6078 if (echo_style)
6079 {
LemonBoya5d35902022-04-29 21:15:02 +01006080 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6081 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006082 buf, MAX_FUNC_NAME_LEN);
6083 if (r == buf)
6084 {
6085 r = vim_strsave(buf);
6086 *tofree = r;
6087 }
6088 else
6089 *tofree = NULL;
6090 }
6091 else
6092 {
6093 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6094 : make_ufunc_name_readable(
6095 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6096 TRUE);
6097 r = *tofree;
6098 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006100 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006101
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006102 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006103 {
6104 partial_T *pt = tv->vval.v_partial;
6105 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006106 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006107 garray_T ga;
6108 int i;
6109 char_u *tf;
6110
6111 ga_init2(&ga, 1, 100);
6112 ga_concat(&ga, (char_u *)"function(");
6113 if (fname != NULL)
6114 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006115 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006116 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006117 && vim_isupper(fname[1]))
6118 {
6119 ga_concat(&ga, (char_u *)"'g:");
6120 ga_concat(&ga, fname + 1);
6121 }
6122 else
6123 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006124 vim_free(fname);
6125 }
6126 if (pt != NULL && pt->pt_argc > 0)
6127 {
6128 ga_concat(&ga, (char_u *)", [");
6129 for (i = 0; i < pt->pt_argc; ++i)
6130 {
6131 if (i > 0)
6132 ga_concat(&ga, (char_u *)", ");
6133 ga_concat(&ga,
6134 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6135 vim_free(tf);
6136 }
6137 ga_concat(&ga, (char_u *)"]");
6138 }
6139 if (pt != NULL && pt->pt_dict != NULL)
6140 {
6141 typval_T dtv;
6142
6143 ga_concat(&ga, (char_u *)", ");
6144 dtv.v_type = VAR_DICT;
6145 dtv.vval.v_dict = pt->pt_dict;
6146 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6147 vim_free(tf);
6148 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006149 // terminate with ')' and a NUL
6150 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006151
6152 *tofree = ga.ga_data;
6153 r = *tofree;
6154 break;
6155 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006156
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006157 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006158 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006159 break;
6160
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006162 if (tv->vval.v_list == NULL)
6163 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006164 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006165 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006166 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006167 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006168 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6169 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006170 {
6171 *tofree = NULL;
6172 r = (char_u *)"[...]";
6173 }
6174 else
6175 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006176 int old_copyID = tv->vval.v_list->lv_copyID;
6177
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006178 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006179 *tofree = list2string(tv, copyID, restore_copyID);
6180 if (restore_copyID)
6181 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006182 r = *tofree;
6183 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006185
Bram Moolenaar8c711452005-01-14 21:53:12 +00006186 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006187 if (tv->vval.v_dict == NULL)
6188 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006189 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006190 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006191 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006192 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006193 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6194 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006195 {
6196 *tofree = NULL;
6197 r = (char_u *)"{...}";
6198 }
6199 else
6200 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006201 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006202
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006203 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006204 *tofree = dict2string(tv, copyID, restore_copyID);
6205 if (restore_copyID)
6206 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006207 r = *tofree;
6208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006211 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006212 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006213 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006215 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006216 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006217 break;
6218
Bram Moolenaar835dc632016-02-07 14:27:38 +01006219 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006220 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006221#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006222 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006223 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6224 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006225 if (composite_val)
6226 {
6227 *tofree = string_quote(r, FALSE);
6228 r = *tofree;
6229 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006230#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006232
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006233 case VAR_INSTR:
6234 *tofree = NULL;
6235 r = (char_u *)"instructions";
6236 break;
6237
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006238 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006239 {
6240 class_T *cl = tv->vval.v_class;
6241 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6242 r = *tofree = alloc(len);
6243 vim_snprintf((char *)r, len, "class %s",
6244 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6245 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006246 break;
6247
6248 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006249 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006250 garray_T ga;
6251 ga_init2(&ga, 1, 50);
6252 ga_concat(&ga, (char_u *)"object of ");
6253 object_T *obj = tv->vval.v_object;
6254 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6255 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6256 : cl->class_name);
6257 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006258 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006259 ga_concat(&ga, (char_u *)" {");
6260 for (int i = 0; i < cl->class_obj_member_count; ++i)
6261 {
6262 if (i > 0)
6263 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006264 ocmember_T *m = &cl->class_obj_members[i];
6265 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006266 ga_concat(&ga, (char_u *)": ");
6267 char_u *tf = NULL;
6268 ga_concat(&ga, echo_string_core(
6269 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006270 &tf, numbuf, copyID, echo_style,
6271 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006272 vim_free(tf);
6273 }
6274 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006275 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006276
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006277 *tofree = r = ga.ga_data;
6278 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006279 break;
6280
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006281 case VAR_FLOAT:
6282 *tofree = NULL;
6283 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6284 r = numbuf;
6285 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006286
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006287 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006288 case VAR_SPECIAL:
6289 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006290 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006291 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006292 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006293
Bram Moolenaar8502c702014-06-17 12:51:16 +02006294 if (--recurse == 0)
6295 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006297}
6298
6299/*
6300 * Return a string with the string representation of a variable.
6301 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6302 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006303 * Does not put quotes around strings, as ":echo" displays values.
6304 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6305 * May return NULL.
6306 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006308echo_string(
6309 typval_T *tv,
6310 char_u **tofree,
6311 char_u *numbuf,
6312 int copyID)
6313{
6314 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6315}
6316
6317/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006318 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6319 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006320 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006321 */
6322 int
6323buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6324{
6325 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006326 char_u *t;
6327 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006328
6329 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6330 return -1;
6331
6332 if (lnum > buf->b_ml.ml_line_count)
6333 lnum = buf->b_ml.ml_line_count;
6334
6335 str = ml_get_buf(buf, lnum, FALSE);
6336 if (str == NULL)
6337 return -1;
6338
6339 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006340 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006341
Bram Moolenaar91458462021-01-13 20:08:38 +01006342 // count the number of characters
6343 t = str;
6344 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6345 t += mb_ptr2len(t);
6346
6347 // In insert mode, when the cursor is at the end of a non-empty line,
6348 // byteidx points to the NUL character immediately past the end of the
6349 // string. In this case, add one to the character count.
6350 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6351 count++;
6352
6353 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006354}
6355
6356/*
6357 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006358 * byte index. Works only for loaded buffers. Returns -1 on failure.
6359 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006360 */
6361 int
6362buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6363{
6364 char_u *str;
6365 char_u *t;
6366
6367 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6368 return -1;
6369
6370 if (lnum > buf->b_ml.ml_line_count)
6371 lnum = buf->b_ml.ml_line_count;
6372
6373 str = ml_get_buf(buf, lnum, FALSE);
6374 if (str == NULL)
6375 return -1;
6376
6377 // Convert the character offset to a byte offset
6378 t = str;
6379 while (*t != NUL && --charidx > 0)
6380 t += mb_ptr2len(t);
6381
Bram Moolenaar91458462021-01-13 20:08:38 +01006382 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006383}
6384
6385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006387 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390var2fpos(
6391 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006392 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006393 int *fnum, // set to fnum for '0, 'A, etc.
6394 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006396 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006398 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006401 if (varp->v_type == VAR_LIST)
6402 {
6403 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006404 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006405 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006406 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006407
6408 l = varp->vval.v_list;
6409 if (l == NULL)
6410 return NULL;
6411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006412 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006413 pos.lnum = list_find_nr(l, 0L, &error);
6414 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006415 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006416 if (charcol)
6417 len = (long)mb_charlen(ml_get(pos.lnum));
6418 else
6419 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006420
Bram Moolenaarec65d772020-08-20 22:29:12 +02006421 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006422 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006423 li = list_find(l, 1L);
6424 if (li != NULL && li->li_tv.v_type == VAR_STRING
6425 && li->li_tv.vval.v_string != NULL
6426 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006427 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006428 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006429 }
6430 else
6431 {
6432 pos.col = list_find_nr(l, 1L, &error);
6433 if (error)
6434 return NULL;
6435 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006437 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006438 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006439 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006440 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006442 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006443 pos.coladd = list_find_nr(l, 2L, &error);
6444 if (error)
6445 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006446
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006447 return &pos;
6448 }
6449
Bram Moolenaarc5809432021-03-27 21:23:30 +01006450 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6451 return NULL;
6452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006453 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006454 if (name == NULL)
6455 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006456
6457 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006458 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006459 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006460 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006461 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006462 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006463 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006464 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006465 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006466 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006467 pos = VIsual;
6468 else
6469 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006470 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006471 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006472 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006474 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006475 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6477 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006478 pos = *pp;
6479 }
6480 if (pos.lnum != 0)
6481 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006482 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006483 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6484 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006486
Bram Moolenaara5525202006-03-02 22:52:09 +00006487 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006488
Bram Moolenaar477933c2007-07-17 14:32:23 +00006489 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006490 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006491 // the "w_valid" flags are not reset when moving the cursor, but they
6492 // do matter for update_topline() and validate_botline().
6493 check_cursor_moved(curwin);
6494
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006495 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006496 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006497 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006498 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006499 // In silent Ex mode topline is zero, but that's not a valid line
6500 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006501 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006502 return &pos;
6503 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006504 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006505 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006506 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006507 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006508 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006509 return &pos;
6510 }
6511 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006512 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006514 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 pos.lnum = curbuf->b_ml.ml_line_count;
6517 pos.col = 0;
6518 }
6519 else
6520 {
6521 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006522 if (charcol)
6523 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6524 else
6525 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527 return &pos;
6528 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006529 if (in_vim9script())
6530 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 return NULL;
6532}
6533
6534/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006535 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006536 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006537 * Note that the column is passed on as-is, the caller may want to decrement
6538 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006539 * If "charcol" is TRUE use the column as the character index instead of the
6540 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006541 * Return FAIL when conversion is not possible, doesn't check the position for
6542 * validity.
6543 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006545list2fpos(
6546 typval_T *arg,
6547 pos_T *posp,
6548 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006549 colnr_T *curswantp,
6550 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006551{
6552 list_T *l = arg->vval.v_list;
6553 long i = 0;
6554 long n;
6555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006556 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6557 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006558 if (arg->v_type != VAR_LIST
6559 || l == NULL
6560 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006561 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006562 return FAIL;
6563
6564 if (fnump != NULL)
6565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006566 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006567 if (n < 0)
6568 return FAIL;
6569 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006570 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006571 *fnump = n;
6572 }
6573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006574 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006575 if (n < 0)
6576 return FAIL;
6577 posp->lnum = n;
6578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006580 if (n < 0)
6581 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006582 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006583 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006584 if (charcol)
6585 {
6586 buf_T *buf;
6587
6588 // Get the text for the specified line in a loaded buffer
6589 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6590 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6591 return FAIL;
6592
Bram Moolenaar79f23442022-10-10 12:42:57 +01006593 n = buf_charidx_to_byteidx(buf,
6594 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006595 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006596 posp->col = n;
6597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006599 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006600 posp->coladd = 0;
6601 else
6602 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006603
Bram Moolenaar493c1782014-05-28 14:34:46 +02006604 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006605 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006606
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006607 return OK;
6608}
6609
6610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 * Get the length of an environment variable name.
6612 * Advance "arg" to the first character after the name.
6613 * Return 0 for error.
6614 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
6618 char_u *p;
6619 int len;
6620
6621 for (p = *arg; vim_isIDc(*p); ++p)
6622 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006623 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 return 0;
6625
6626 len = (int)(p - *arg);
6627 *arg = p;
6628 return len;
6629}
6630
6631/*
6632 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006633 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 * Return 0 if something is wrong.
6635 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006637get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 char_u *p;
6640 int len;
6641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006644 {
6645 if (*p == ':')
6646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006647 // "s:" is start of "s:var", but "n:" is not and can be used in
6648 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006649 len = (int)(p - *arg);
6650 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6651 || len > 1)
6652 break;
6653 }
6654 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 return 0;
6657
6658 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006659 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 return len;
6662}
6663
6664/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006665 * Get the length of the name of a variable or function.
6666 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006668 * Return -1 if curly braces expansion failed.
6669 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 * If the name contains 'magic' {}'s, expand them and return the
6671 * expanded name in an allocated string via 'alias' - caller must free.
6672 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674get_name_len(
6675 char_u **arg,
6676 char_u **alias,
6677 int evaluate,
6678 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 char_u *p;
6682 char_u *expr_start;
6683 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006685 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6688 && (*arg)[2] == (int)KE_SNR)
6689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006690 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 *arg += 3;
6692 return get_id_len(arg) + 3;
6693 }
6694 len = eval_fname_script(*arg);
6695 if (len > 0)
6696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006697 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 *arg += len;
6699 }
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006702 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006704 p = find_name_end(*arg, &expr_start, &expr_end,
6705 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 if (expr_start != NULL)
6707 {
6708 char_u *temp_string;
6709
6710 if (!evaluate)
6711 {
6712 len += (int)(p - *arg);
6713 *arg = skipwhite(p);
6714 return len;
6715 }
6716
6717 /*
6718 * Include any <SID> etc in the expanded string:
6719 * Thus the -len here.
6720 */
6721 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6722 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006723 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 *alias = temp_string;
6725 *arg = skipwhite(p);
6726 return (int)STRLEN(temp_string);
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
6729 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006730 // Only give an error when there is something, otherwise it will be
6731 // reported at a higher level.
6732 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006733 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734
6735 return len;
6736}
6737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738/*
6739 * Find the end of a variable or function name, taking care of magic braces.
6740 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6741 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006742 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006743 * Return a pointer to just after the name. Equal to "arg" if there is no
6744 * valid name.
6745 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747find_name_end(
6748 char_u *arg,
6749 char_u **expr_start,
6750 char_u **expr_end,
6751 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006753 int mb_nest = 0;
6754 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006756 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006757 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006759 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006761 *expr_start = NULL;
6762 *expr_end = NULL;
6763 }
6764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006765 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006766 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006767 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006768 return arg;
6769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006770 for (p = arg; *p != NUL
6771 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006772 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006773 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006774 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006776 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006777 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006778 if (*p == '\'')
6779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006780 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006781 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006782 ;
6783 if (*p == NUL)
6784 break;
6785 }
6786 else if (*p == '"')
6787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006788 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006789 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006790 if (*p == '\\' && p[1] != NUL)
6791 ++p;
6792 if (*p == NUL)
6793 break;
6794 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006795 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6796 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006797 // "s:" is start of "s:var", but "n:" is not and can be used in
6798 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006799 len = (int)(p - arg);
6800 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006801 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006802 break;
6803 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 if (*p == '[')
6808 ++br_nest;
6809 else if (*p == ']')
6810 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006812
zeertzjqa93d9cd2023-05-02 16:25:47 +01006813 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 if (*p == '{')
6816 {
6817 mb_nest++;
6818 if (expr_start != NULL && *expr_start == NULL)
6819 *expr_start = p;
6820 }
6821 else if (*p == '}')
6822 {
6823 mb_nest--;
6824 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6825 *expr_end = p;
6826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829
6830 return p;
6831}
6832
6833/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006834 * Expands out the 'magic' {}'s in a variable/function name.
6835 * Note that this can call itself recursively, to deal with
6836 * constructs like foo{bar}{baz}{bam}
6837 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6838 * "in_start" ^
6839 * "expr_start" ^
6840 * "expr_end" ^
6841 * "in_end" ^
6842 *
6843 * Returns a new allocated string, which the caller must free.
6844 * Returns NULL for failure.
6845 */
6846 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006847make_expanded_name(
6848 char_u *in_start,
6849 char_u *expr_start,
6850 char_u *expr_end,
6851 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006852{
6853 char_u c1;
6854 char_u *retval = NULL;
6855 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006856
6857 if (expr_end == NULL || in_end == NULL)
6858 return NULL;
6859 *expr_start = NUL;
6860 *expr_end = NUL;
6861 c1 = *in_end;
6862 *in_end = NUL;
6863
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006864 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006865 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006866 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006867 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6868 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006869 if (retval != NULL)
6870 {
6871 STRCPY(retval, in_start);
6872 STRCAT(retval, temp_result);
6873 STRCAT(retval, expr_end + 1);
6874 }
6875 }
6876 vim_free(temp_result);
6877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006878 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006879 *expr_start = '{';
6880 *expr_end = '}';
6881
6882 if (retval != NULL)
6883 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006884 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006885 if (expr_start != NULL)
6886 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006887 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006888 temp_result = make_expanded_name(retval, expr_start,
6889 expr_end, temp_result);
6890 vim_free(retval);
6891 retval = temp_result;
6892 }
6893 }
6894
6895 return retval;
6896}
6897
6898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006902 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006903eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006905 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006906}
6907
6908/*
6909 * Return TRUE if character "c" can be used as the first character in a
6910 * variable or function name (excluding '{' and '}').
6911 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006912 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006913eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006914{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006915 return ASCII_ISALPHA(c) || c == '_';
6916}
6917
6918/*
6919 * Return TRUE if character "c" can be used as the first character of a
6920 * dictionary key.
6921 */
6922 int
6923eval_isdictc(int c)
6924{
6925 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
6927
6928/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006929 * Handle:
6930 * - expr[expr], expr[expr:expr] subscript
6931 * - ".name" lookup
6932 * - function call with Funcref variable: func(expr)
6933 * - method call: var->method()
6934 *
6935 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006936 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006937 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006939handle_subscript(
6940 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006941 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006942 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006943 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006944 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006945{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006946 int evaluate = evalarg != NULL
6947 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006948 int ret = OK;
6949 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006950 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006951 int getnext;
6952 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006953
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006954 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006955 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006956 // When at the end of the line and ".name" or "->{" or "->X" follows in
6957 // the next line then consume the line break.
6958 p = eval_next_non_blank(*arg, evalarg, &getnext);
6959 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006960 && ((*p == '.'
6961 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6962 || rettv->v_type == VAR_CLASS
6963 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006964 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6965 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6966 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006967 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006968 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006969 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006970 check_white = FALSE;
6971 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006972
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006973 if (rettv->v_type == VAR_ANY)
6974 {
6975 char_u *exp_name;
6976 int cc;
6977 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006978 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006979 type_T *type;
6980
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006981 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006982 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006983 if (**arg != '.')
6984 {
6985 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006986 semsg(_(e_expected_dot_after_name_str),
6987 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006988 ret = FAIL;
6989 break;
6990 }
6991 ++*arg;
6992 if (IS_WHITE_OR_NUL(**arg))
6993 {
6994 if (verbose)
6995 emsg(_(e_no_white_space_allowed_after_dot));
6996 ret = FAIL;
6997 break;
6998 }
6999
7000 // isolate the name
7001 exp_name = *arg;
7002 while (eval_isnamec(**arg))
7003 ++*arg;
7004 cc = **arg;
7005 **arg = NUL;
7006
7007 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007008 evalarg == NULL ? NULL : evalarg->eval_cctx,
7009 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007010 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007011
7012 if (idx < 0 && ufunc == NULL)
7013 {
7014 ret = FAIL;
7015 break;
7016 }
7017 if (idx >= 0)
7018 {
7019 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7020 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7021
7022 copy_tv(sv->sv_tv, rettv);
7023 }
7024 else
7025 {
7026 rettv->v_type = VAR_FUNC;
7027 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7028 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007029 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007030 }
7031
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007032 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7033 || rettv->v_type == VAR_PARTIAL))
7034 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007035 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007036 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7037 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007038
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007039 // Stop the expression evaluation when immediately aborting on
7040 // error, or when an interrupt occurred or an exception was thrown
7041 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007042 if (aborting())
7043 {
7044 if (ret == OK)
7045 clear_tv(rettv);
7046 ret = FAIL;
7047 }
7048 dict_unref(selfdict);
7049 selfdict = NULL;
7050 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007051 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007052 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007053 if (in_vim9script())
7054 *arg = skipwhite(p + 2);
7055 else
7056 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007057 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007058 {
zeertzjqc481ad32023-03-11 16:18:51 +00007059 emsg(_(e_no_white_space_allowed_before_parenthesis));
7060 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007061 }
zeertzjqc481ad32023-03-11 16:18:51 +00007062 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7063 // expr->{lambda}() or expr->(lambda)()
7064 ret = eval_lambda(arg, rettv, evalarg, verbose);
7065 else
7066 // expr->name()
7067 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007068 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007069 // "." is ".name" lookup when we found a dict or when evaluating and
7070 // scriptversion is at least 2, where string concatenation is "..".
7071 else if (**arg == '['
7072 || (**arg == '.' && (rettv->v_type == VAR_DICT
7073 || (!evaluate
7074 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007075 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007076 {
7077 dict_unref(selfdict);
7078 if (rettv->v_type == VAR_DICT)
7079 {
7080 selfdict = rettv->vval.v_dict;
7081 if (selfdict != NULL)
7082 ++selfdict->dv_refcount;
7083 }
7084 else
7085 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007086 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007087 {
7088 clear_tv(rettv);
7089 ret = FAIL;
7090 }
7091 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007092 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7093 || rettv->v_type == VAR_OBJECT))
7094 {
7095 // class member: SomeClass.varname
7096 // class method: SomeClass.SomeMethod()
7097 // class constructor: SomeClass.new()
7098 // object member: someObject.varname
7099 // object method: someObject.SomeMethod()
7100 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7101 {
7102 clear_tv(rettv);
7103 ret = FAIL;
7104 }
7105 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007106 else
7107 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007108 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007110 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7111 // Don't do this when "Func" is already a partial that was bound
7112 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007113 if (selfdict != NULL
7114 && (rettv->v_type == VAR_FUNC
7115 || (rettv->v_type == VAR_PARTIAL
7116 && (rettv->vval.v_partial->pt_auto
7117 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007118 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007120 dict_unref(selfdict);
7121 return ret;
7122}
7123
7124/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007125 * Make a copy of an item.
7126 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007127 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007128 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7129 * reference to an already copied list/dict can be used.
7130 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007133item_copy(
7134 typval_T *from,
7135 typval_T *to,
7136 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007137 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139{
7140 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007141 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007145 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007146 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147 }
7148 ++recurse;
7149
7150 switch (from->v_type)
7151 {
7152 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007153 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 case VAR_STRING:
7155 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007156 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007157 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007158 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007159 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007160 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007161 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007162 case VAR_CLASS:
7163 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 copy_tv(from, to);
7165 break;
7166 case VAR_LIST:
7167 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007168 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 if (from->vval.v_list == NULL)
7170 to->vval.v_list = NULL;
7171 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007173 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007174 to->vval.v_list = from->vval.v_list->lv_copylist;
7175 ++to->vval.v_list->lv_refcount;
7176 }
7177 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007178 to->vval.v_list = list_copy(from->vval.v_list,
7179 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 if (to->vval.v_list == NULL)
7181 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007183 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007185 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 case VAR_DICT:
7187 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007188 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189 if (from->vval.v_dict == NULL)
7190 to->vval.v_dict = NULL;
7191 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007193 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7195 ++to->vval.v_dict->dv_refcount;
7196 }
7197 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007198 to->vval.v_dict = dict_copy(from->vval.v_dict,
7199 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 if (to->vval.v_dict == NULL)
7201 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007203 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007204 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007206 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007207 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 }
7209 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007210 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211}
7212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007213 void
7214echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7215{
7216 char_u *tofree;
7217 char_u numbuf[NUMBUFLEN];
7218 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7219
7220 if (*atstart)
7221 {
7222 *atstart = FALSE;
7223 // Call msg_start() after eval1(), evaluating the expression
7224 // may cause a message to appear.
7225 if (with_space)
7226 {
7227 // Mark the saved text as finishing the line, so that what
7228 // follows is displayed on a new line when scrolling back
7229 // at the more prompt.
7230 msg_sb_eol();
7231 msg_start();
7232 }
7233 }
7234 else if (with_space)
7235 msg_puts_attr(" ", echo_attr);
7236
7237 if (p != NULL)
7238 for ( ; *p != NUL && !got_int; ++p)
7239 {
7240 if (*p == '\n' || *p == '\r' || *p == TAB)
7241 {
7242 if (*p != TAB && *needclr)
7243 {
7244 // remove any text still there from the command
7245 msg_clr_eos();
7246 *needclr = FALSE;
7247 }
7248 msg_putchar_attr(*p, echo_attr);
7249 }
7250 else
7251 {
7252 if (has_mbyte)
7253 {
7254 int i = (*mb_ptr2len)(p);
7255
7256 (void)msg_outtrans_len_attr(p, i, echo_attr);
7257 p += i - 1;
7258 }
7259 else
7260 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7261 }
7262 }
7263 vim_free(tofree);
7264}
7265
Bram Moolenaare9a41262005-01-15 22:18:47 +00007266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 * ":echo expr1 ..." print each argument separated with a space, add a
7268 * newline at the end.
7269 * ":echon expr1 ..." print each argument plain.
7270 */
7271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007272ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
7274 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007276 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 int needclr = TRUE;
7278 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007279 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007280 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007281 evalarg_T evalarg;
7282
Bram Moolenaare707c882020-07-01 13:07:37 +02007283 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284
7285 if (eap->skip)
7286 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007287 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007289 // If eval1() causes an error message the text from the command may
7290 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007291 need_clr_eos = needclr;
7292
Bram Moolenaar68db9962021-05-09 23:19:22 +02007293 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007294 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
7296 /*
7297 * Report the invalid expression unless the expression evaluation
7298 * has been cancelled due to an aborting error, an interrupt, or an
7299 * exception.
7300 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007301 if (!aborting() && did_emsg == did_emsg_before
7302 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007303 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007304 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 break;
7306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007307 need_clr_eos = FALSE;
7308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007310 {
7311 if (rettv.v_type == VAR_VOID)
7312 {
7313 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7314 break;
7315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007317 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 arg = skipwhite(arg);
7321 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007322 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007323 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
7325 if (eap->skip)
7326 --emsg_skip;
7327 else
7328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007329 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (needclr)
7331 msg_clr_eos();
7332 if (eap->cmdidx == CMD_echo)
7333 msg_end();
7334 }
7335}
7336
7337/*
7338 * ":echohl {name}".
7339 */
7340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007343 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007347 * Returns the :echo attribute
7348 */
7349 int
7350get_echo_attr(void)
7351{
7352 return echo_attr;
7353}
7354
7355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 * ":execute expr1 ..." execute the result of an expression.
7357 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007358 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007360 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 * Each gets spaces around each argument and a newline at the end for
7362 * echo commands
7363 */
7364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007365ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 int ret = OK;
7370 char_u *p;
7371 garray_T ga;
7372 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007373 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
7375 ga_init2(&ga, 1, 80);
7376
7377 if (eap->skip)
7378 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007379 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007381 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007382 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 if (!eap->skip)
7386 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007387 char_u buf[NUMBUFLEN];
7388
7389 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007390 {
7391 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7392 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007393 semsg(_(e_using_invalid_value_as_string_str),
7394 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007395 p = NULL;
7396 }
7397 else
7398 p = tv_get_string_buf(&rettv, buf);
7399 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007400 else
7401 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007402 if (p == NULL)
7403 {
7404 clear_tv(&rettv);
7405 ret = FAIL;
7406 break;
7407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 len = (int)STRLEN(p);
7409 if (ga_grow(&ga, len + 2) == FAIL)
7410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007411 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 ret = FAIL;
7413 break;
7414 }
7415 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 ga.ga_len += len;
7419 }
7420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007421 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 arg = skipwhite(arg);
7423 }
7424
7425 if (ret != FAIL && ga.ga_data != NULL)
7426 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007427 // use the first line of continuation lines for messages
7428 SOURCING_LNUM = start_lnum;
7429
Bram Moolenaar37fef162022-08-29 18:16:32 +01007430 if (eap->cmdidx == CMD_echomsg
7431 || eap->cmdidx == CMD_echowindow
7432 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007434 // Mark the already saved text as finishing the line, so that what
7435 // follows is displayed on a new line when scrolling back at the
7436 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007437 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007438 }
7439
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007440 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007441 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007442 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007443 out_flush();
7444 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007445 else if (eap->cmdidx == CMD_echowindow)
7446 {
7447#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007448 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007449#endif
7450 msg_attr(ga.ga_data, echo_attr);
7451#ifdef HAS_MESSAGE_WINDOW
7452 end_echowindow();
7453#endif
7454 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007455 else if (eap->cmdidx == CMD_echoconsole)
7456 {
7457 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7458 ui_write((char_u *)"\r\n", 2, TRUE);
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else if (eap->cmdidx == CMD_echoerr)
7461 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007462 int save_did_emsg = did_emsg;
7463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007464 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007465 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 if (!force_abort)
7467 did_emsg = save_did_emsg;
7468 }
7469 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007470 {
7471 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7472
7473 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7474 sticky_cmdmod_flags = cmdmod.cmod_flags
7475 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 do_cmdline((char_u *)ga.ga_data,
7477 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007478 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
7481
7482 ga_clear(&ga);
7483
7484 if (eap->skip)
7485 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007486 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487}
7488
7489/*
7490 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7491 * "arg" points to the "&" or '+' when called, to "option" when returning.
7492 * Returns NULL when no option name found. Otherwise pointer to the char
7493 * after the option name.
7494 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007495 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007496find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 char_u *p = *arg;
7499
7500 ++p;
7501 if (*p == 'g' && p[1] == ':')
7502 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007503 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 p += 2;
7505 }
7506 else if (*p == 'l' && p[1] == ':')
7507 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007508 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 p += 2;
7510 }
7511 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007512 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
7514 if (!ASCII_ISALPHA(*p))
7515 return NULL;
7516 *arg = p;
7517
7518 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007519 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 else
7521 while (ASCII_ISALPHA(*p))
7522 ++p;
7523 return p;
7524}
7525
7526/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007527 * Display script name where an item was last set.
7528 * Should only be invoked when 'verbose' is non-zero.
7529 */
7530 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007531last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007532{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007533 char_u *p;
7534
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007535 if (script_ctx.sc_sid == 0)
7536 return;
7537
7538 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7539 if (p == NULL)
7540 return;
7541
7542 verbose_enter();
7543 msg_puts(_("\n\tLast set from "));
7544 msg_puts((char *)p);
7545 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007546 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007547 msg_puts(_(line_msg));
7548 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007549 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007550 verbose_leave();
7551 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007552}
7553
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007554#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
7556/*
7557 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007558 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 * "flags" can be "g" to do a global substitute.
7560 * Returns an allocated string, NULL for error.
7561 */
7562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563do_string_sub(
7564 char_u *str,
7565 char_u *pat,
7566 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007567 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 int sublen;
7571 regmatch_T regmatch;
7572 int i;
7573 int do_all;
7574 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007575 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 garray_T ga;
7577 char_u *ret;
7578 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007579 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007581 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007583 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
7585 ga_init2(&ga, 1, 200);
7586
7587 do_all = (flags[0] == 'g');
7588
7589 regmatch.rm_ic = p_ic;
7590 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7591 if (regmatch.regprog != NULL)
7592 {
7593 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007594 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7596 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007597 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007598 if (regmatch.startp[0] == regmatch.endp[0])
7599 {
7600 if (zero_width == regmatch.startp[0])
7601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007602 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007603 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007604 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7605 (size_t)i);
7606 ga.ga_len += i;
7607 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007608 continue;
7609 }
7610 zero_width = regmatch.startp[0];
7611 }
7612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 /*
7614 * Get some space for a temporary buffer to do the substitution
7615 * into. It will contain:
7616 * - The text up to where the match is.
7617 * - The substituted text.
7618 * - The text after the match.
7619 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007620 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007621 if (sublen <= 0)
7622 {
7623 ga_clear(&ga);
7624 break;
7625 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007626 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7628 {
7629 ga_clear(&ga);
7630 break;
7631 }
7632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007633 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 i = (int)(regmatch.startp[0] - tail);
7635 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007636 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007637 (void)vim_regsub(&regmatch, sub, expr,
7638 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7639 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007641 tail = regmatch.endp[0];
7642 if (*tail == NUL)
7643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (!do_all)
7645 break;
7646 }
7647
7648 if (ga.ga_data != NULL)
7649 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7650
Bram Moolenaar473de612013-06-08 18:19:48 +02007651 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
7653
7654 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7655 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007656 if (p_cpo == empty_option)
7657 p_cpo = save_cpo;
7658 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007660 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007661 // If it's still empty it was changed and restored, need to restore in
7662 // the complicated way.
7663 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007664 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007665 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
7668 return ret;
7669}