blob: 187b4d4d87e50e7c146f2e51704b082d9b3868de [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100136 if (sourcing_a_script(eap) || eap->ea_getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100138 evalarg->eval_getline = eap->ea_getline;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +0200552 ga_clear(freegap);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200553 }
554 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200555 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200557
558 // free lines that were explicitly marked for freeing
559 ga_clear_strings(freegap);
560 }
561
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200562 gap->ga_itemsize = 0;
563 if (p == NULL)
564 return FAIL;
565 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200566 vim_free(evalarg->eval_tofree_lambda);
567 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200568 // Compute "end" relative to the end.
569 *end = *start + STRLEN(*start) - endoff;
570 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200571 }
572
573 return res;
574}
575
576/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100577 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100578 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100579 * Returns an allocated string (NULL when out of memory).
580 */
581 char_u *
582typval2string(typval_T *tv, int convert)
583{
584 garray_T ga;
585 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100586
587 if (convert && tv->v_type == VAR_LIST)
588 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000589 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100590 if (tv->vval.v_list != NULL)
591 {
592 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
593 if (tv->vval.v_list->lv_len > 0)
594 ga_append(&ga, NL);
595 }
596 ga_append(&ga, NUL);
597 retval = (char_u *)ga.ga_data;
598 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100599 else
600 retval = vim_strsave(tv_get_string(tv));
601 return retval;
602}
603
604/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200605 * Top level evaluation function, returning a string. Does not handle line
606 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100607 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * Return pointer to allocated memory, or NULL for failure.
609 */
610 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100611eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100612 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100613 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100614 exarg_T *eap,
615 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
Bram Moolenaar33570922005-01-25 22:26:29 +0000617 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100619 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100620 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100622 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100623 if (use_simple_function)
624 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
625 else
626 r = eval0(arg, &tv, NULL, &evalarg);
627 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 retval = NULL;
629 else
630 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100631 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000632 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100634 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 return retval;
637}
638
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100639 char_u *
640eval_to_string(
641 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100642 int convert,
643 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100644{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100645 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100646}
647
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000649 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100650 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200651 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 */
653 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100654eval_to_string_safe(
655 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000656 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100657 int keep_script_version,
658 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200661 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200662 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200663 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaar9530b582022-01-22 13:39:08 +0000665 if (!keep_script_version)
666 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200667 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000668 if (use_sandbox)
669 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100670 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200671 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100672 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000673 if (use_sandbox)
674 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100675 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200676 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200677 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200678 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 return retval;
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682/*
683 * Top level evaluation function, returning a number.
684 * Evaluates "expr" silently.
685 * Returns -1 for an error.
686 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200687 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100688eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689{
Bram Moolenaar33570922005-01-25 22:26:29 +0000690 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200691 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000692 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100693 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 ++emsg_off;
696
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100697 if (use_simple_function)
698 r = may_call_simple_func(expr, &rettv);
699 if (r == NOTDONE)
700 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
701 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 retval = -1;
703 else
704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100705 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000706 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 }
708 --emsg_off;
709
710 return retval;
711}
712
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000713/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000714 * Top level evaluation function.
715 * Returns an allocated typval_T with the result.
716 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000717 */
718 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200719eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000720{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100721 return eval_expr_ext(arg, eap, FALSE);
722}
723
724 typval_T *
725eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
726{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000727 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200728 evalarg_T evalarg;
729
730 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000731
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200732 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100733 if (tv != NULL)
734 {
735 int r = NOTDONE;
736
737 if (use_simple_function)
738 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
739 if (r == NOTDONE)
740 r = eval0(arg, tv, eap, &evalarg);
741
742 if (r == FAIL)
743 VIM_CLEAR(tv);
744 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000745
Bram Moolenaar37c83712020-06-30 21:18:36 +0200746 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000747 return tv;
748}
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000751 * "*arg" points to what can be a function name in the form of "import.Name" or
752 * "Funcref". Return the name of the function. Set "tofree" to something that
753 * was allocated.
754 * If "verbose" is FALSE no errors are given.
755 * Return NULL for any failure.
756 */
757 static char_u *
758deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000759 char_u **arg,
760 char_u **tofree,
761 evalarg_T *evalarg,
762 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000763{
764 typval_T ref;
765 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100766 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000767
768 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100769 if (evalarg != NULL)
770 {
771 // need to evaluate this to get an import, like in "a.Func"
772 save_flags = evalarg->eval_flags;
773 evalarg->eval_flags |= EVAL_EVALUATE;
774 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100775 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100776 {
777 dictitem_T *v;
778
779 // If <SID>VarName was used it would not be found, try another way.
780 v = find_var_also_in_script(name, NULL, FALSE);
781 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100782 {
783 name = NULL;
784 goto theend;
785 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100786 copy_tv(&v->di_tv, &ref);
787 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000788 if (*skipwhite(*arg) != NUL)
789 {
790 if (verbose)
791 semsg(_(e_trailing_characters_str), *arg);
792 name = NULL;
793 }
794 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
795 {
796 name = ref.vval.v_string;
797 ref.vval.v_string = NULL;
798 *tofree = name;
799 }
800 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
801 {
802 if (ref.vval.v_partial->pt_argc > 0
803 || ref.vval.v_partial->pt_dict != NULL)
804 {
805 if (verbose)
806 emsg(_(e_cannot_use_partial_here));
807 name = NULL;
808 }
809 else
810 {
811 name = vim_strsave(partial_name(ref.vval.v_partial));
812 *tofree = name;
813 }
814 }
815 else
816 {
817 if (verbose)
818 semsg(_(e_not_callable_type_str), name);
819 name = NULL;
820 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100821
822theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000823 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100824 if (evalarg != NULL)
825 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000826 return name;
827}
828
829/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100830 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200831 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
832 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000833 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836call_vim_function(
837 char_u *func,
838 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200839 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200840 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000842 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200843 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000844 char_u *arg;
845 char_u *name;
846 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000847 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200850 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000851 funcexe.fe_firstline = curwin->w_cursor.lnum;
852 funcexe.fe_lastline = curwin->w_cursor.lnum;
853 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000854
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000855 // The name might be "import.Func" or "Funcref". We don't know, we need to
856 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000857 // autoload script has errors. Guess that when there is a dot in the name
858 // showing errors is the right choice.
859 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000860 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000861 if (ignore_errors)
862 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000863 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000864 if (ignore_errors)
865 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000866 if (name == NULL)
867 name = func;
868
869 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
870
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000871 if (ret == FAIL)
872 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000873 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000874
875 return ret;
876}
877
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100878/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100879 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000880 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
881 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000882 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000883 */
884 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100885call_func_retstr(
886 char_u *func,
887 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200888 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000889{
890 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000891 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000892
Bram Moolenaarded27a12018-08-01 19:06:03 +0200893 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000894 return NULL;
895
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100896 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 return retval;
899}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000900
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000901/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100902 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000903 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000904 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100905 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000906 */
907 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908call_func_retlist(
909 char_u *func,
910 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200911 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000912{
913 typval_T rettv;
914
Bram Moolenaarded27a12018-08-01 19:06:03 +0200915 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000916 return NULL;
917
918 if (rettv.v_type != VAR_LIST)
919 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100920 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
921 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000922 clear_tv(&rettv);
923 return NULL;
924 }
925
926 return rettv.vval.v_list;
927}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Bram Moolenaare70dd112022-01-21 16:31:11 +0000929#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100931 * Evaluate "arg", which is 'foldexpr'.
932 * Note: caller must set "curwin" to match "arg".
933 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
934 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
936 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000937eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000939 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200941 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000943 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000944 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100945 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100947 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000948 current_sctx = wp->w_p_script_ctx[WV_FDE];
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000951 if (use_sandbox)
952 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100953 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100955
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100956 // Evaluate the expression. If the expression is "FuncName()" call the
957 // function directly.
958 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 retval = 0;
960 else
961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100962 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000963 if (tv.v_type == VAR_NUMBER)
964 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000965 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 retval = 0;
967 else
968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // If the result is a string, check if there is a non-digit before
970 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000971 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200972 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 *cp = *s++;
974 retval = atol((char *)s);
975 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000976 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
978 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000979 if (use_sandbox)
980 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100981 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200982 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000983 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200985 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987#endif
988
Ernie Rael64885642023-10-04 20:16:22 +0200989#ifdef LOG_LOCKVAR
990typedef struct flag_string_S
991{
992 int flag;
993 char *str;
994} flag_string_T;
995
996 static char *
997flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
998{
999 char *p = buf;
1000 *p = NUL;
1001 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1002 {
1003 if ((fstring->flag & flags) != 0)
1004 {
1005 size_t len = STRLEN(fstring->str);
1006 if (n > p - buf + len + 7)
1007 {
1008 STRCAT(p, fstring->str);
1009 p += len;
1010 STRCAT(p, " ");
1011 ++p;
1012 }
1013 else
1014 {
1015 STRCAT(buf, "...");
1016 break;
1017 }
1018 }
1019 }
1020 return buf;
1021}
1022
1023flag_string_T glv_flag_strings[] = {
1024 { GLV_QUIET, "QUIET" },
1025 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1026 { GLV_READ_ONLY, "READ_ONLY" },
1027 { GLV_NO_DECL, "NO_DECL" },
1028 { GLV_COMPILING, "COMPILING" },
1029 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1030 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1031 { 0, NULL }
1032};
1033#endif
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035/*
Ernie Raelee865f32023-09-29 19:53:55 +02001036 * Fill in "lp" using "root". This is used in a special case when
1037 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1038 *
1039 * This is typically called with "lval_root" as "root". For a class, find
1040 * the name from lp in the class from root, fill in lval_T if found. For a
1041 * complex type, list/dict use it as the result; just put the root into
1042 * ll_tv.
1043 *
1044 * "lval_root" is a hack used during run-time/instr-execution to provide the
1045 * starting point for "get_lval()" to traverse a chain of indexes. In some
1046 * cases get_lval sees a bare name and uses this function to populate the
1047 * lval_T.
1048 *
1049 * For setting up "lval_root" (currently only used with lockvar)
1050 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1051 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1052 */
1053 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001054fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001055{
1056#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001057 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1058 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001059#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001060 if (lr->lr_tv == NULL)
1061 return;
Ernie Rael64885642023-10-04 20:16:22 +02001062 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001063 {
Ernie Rael64885642023-10-04 20:16:22 +02001064 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001065 {
1066 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001067 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001068 int m_idx;
1069 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1070 lp->ll_name_end - lp->ll_name, &m_idx);
1071 if (m != NULL)
1072 {
1073 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001074 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001075 lp->ll_oi = m_idx;
1076 lp->ll_valtype = m->ocm_type;
1077 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1078#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001079 ch_log(NULL, "LKVAR: ... class member %s.%s",
1080 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001081#endif
1082 return;
1083 }
1084 }
1085 }
1086
1087#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001088 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001089#endif
Ernie Rael64885642023-10-04 20:16:22 +02001090 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001091 lp->ll_is_root = TRUE;
1092}
1093
1094/*
Ernie Rael64885642023-10-04 20:16:22 +02001095 * Check if the class has permission to access the member.
1096 * Returns OK or FAIL.
1097 */
1098 static int
1099get_lval_check_access(
1100 class_T *cl_exec, // executing class, NULL if :def or script level
1101 class_T *cl, // class which contains the member
1102 ocmember_T *om, // member being accessed
1103 char_u *p, // char after member name
1104 int flags) // GLV flags to check if writing to lval
1105{
1106#ifdef LOG_LOCKVAR
1107 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1108 (void*)cl_exec, (void*)cl, *p);
1109#endif
1110 if (cl_exec == NULL || cl_exec != cl)
1111 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001112 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001113 switch (om->ocm_access)
1114 {
1115 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001116 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001117 break;
Ernie Rael64885642023-10-04 20:16:22 +02001118 case VIM_ACCESS_READ:
1119 // If [idx] or .key following, read only OK.
1120 if (*p == '[' || *p == '.')
1121 break;
1122 if ((flags & GLV_READ_ONLY) == 0)
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001123 {
1124 if (IS_ENUM(cl))
1125 {
1126 if (om->ocm_type->tt_type == VAR_OBJECT)
1127 semsg(_(e_enumvalue_str_cannot_be_modified),
1128 cl->class_name, om->ocm_name);
1129 else
1130 msg = e_variable_is_not_writable_str;
1131 }
1132 else
1133 msg = e_variable_is_not_writable_str;
1134 }
Ernie Rael64885642023-10-04 20:16:22 +02001135 break;
1136 case VIM_ACCESS_ALL:
1137 break;
1138 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001139 if (msg != NULL)
1140 {
1141 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1142 return FAIL;
1143 }
1144
Ernie Rael64885642023-10-04 20:16:22 +02001145 }
1146 return OK;
1147}
1148
1149/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001150 * Get lval information for a variable imported from script "imp_sid". On
1151 * success, updates "lp" with the variable name, type, script ID and typval.
1152 * The variable name starts at or after "p".
1153 * If "rettv" is not NULL it points to the value to be assigned. This used to
1154 * match the rhs and lhs types.
1155 * Returns a pointer to the character after the variable name if the imported
1156 * variable is valid and writable.
1157 * Returns NULL if the variable is not exported or typval is not found or the
1158 * rhs type doesn't match the lhs type or the variable is not writable.
1159 */
1160 static char_u *
1161get_lval_imported(
1162 lval_T *lp,
1163 typval_T *rettv,
1164 scid_T imp_sid,
1165 char_u *p,
1166 dictitem_T **dip,
1167 int fne_flags,
1168 int vim9script)
1169{
1170 ufunc_T *ufunc;
1171 type_T *type = NULL;
1172 int cc;
1173 int rc = FAIL;
1174
1175 p = skipwhite(p);
1176
1177 import_check_sourced_sid(&imp_sid);
1178 lp->ll_sid = imp_sid;
1179 lp->ll_name = p;
1180 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1181 lp->ll_name_end = p;
1182
1183 // check the item is exported
1184 cc = *p;
1185 *p = NUL;
1186 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1187 TRUE) == -1)
1188 goto failed;
1189
1190 if (vim9script && type != NULL)
1191 {
1192 where_T where = WHERE_INIT;
1193
1194 // In a vim9 script, do type check and make sure the variable is
1195 // writable.
1196 if (check_typval_type(type, rettv, where) == FAIL)
1197 goto failed;
1198 }
1199
1200 // Get the typval for the exported item
1201 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1202 if (ht == NULL)
1203 goto failed;
1204
1205 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1206 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001207 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001208 goto success;
1209
1210 *dip = di;
1211
1212 // Check whether the variable is writable.
1213 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1214 if (sv != NULL && sv->sv_const != 0)
1215 {
1216 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1217 goto failed;
1218 }
1219
1220 // check whether variable is locked
1221 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1222 goto failed;
1223
1224 lp->ll_tv = &di->di_tv;
1225
1226success:
1227 rc = OK;
1228
1229failed:
1230 *p = cc;
1231 return rc == OK ? p : NULL;
1232}
1233
1234/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 * Get an lval: variable, Dict item or List item that can be assigned a value
1236 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1237 * "name.key", "name.key[expr]" etc.
1238 * Indexing only works if "name" is an existing List or Dictionary.
1239 * "name" points to the start of the name.
1240 * If "rettv" is not NULL it points to the value to be assigned.
1241 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1242 * wrong; must end in space or cmd separator.
1243 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001244 * flags:
1245 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001246 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001247 * GLV_NO_AUTOLOAD: do not use script autoloading
1248 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001250 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001254get_lval(
1255 char_u *name,
1256 typval_T *rettv,
1257 lval_T *lp,
1258 int unlet,
1259 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001260 int flags, // GLV_ values
1261 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 char_u *expr_start, *expr_end;
1265 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001266 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T var1;
1268 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001271 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001272 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001273 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001274 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001275 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001276 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277
Ernie Raelee865f32023-09-29 19:53:55 +02001278#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001279 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001280 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001281 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001282 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1283 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001284 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001285 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001286 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001287#endif
1288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001289 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001290 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001292 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001294 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001296 lp->ll_name_end = find_name_end(name, NULL, NULL,
1297 FNE_INCL_BR | fne_flags);
1298 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299 }
1300
Bram Moolenaara749a422022-02-12 19:52:25 +00001301 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001302 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001303 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1304 {
1305 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1306 return NULL;
1307 }
1308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001309 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001310 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 if (expr_start != NULL)
1313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001314 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001315 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001316 && *p != '[' && *p != '.')
1317 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001318 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 return NULL;
1320 }
1321
1322 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1323 if (lp->ll_exp_name == NULL)
1324 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001325 // Report an invalid expression in braces, unless the
1326 // expression evaluation has been cancelled due to an
1327 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 if (!aborting() && !quiet)
1329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001330 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001331 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 return NULL;
1333 }
1334 }
1335 lp->ll_name = lp->ll_exp_name;
1336 }
1337 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001339 lp->ll_name = name;
1340
Bram Moolenaar4525a572022-02-13 11:57:33 +00001341 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001343 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001344 // However, "g:[key]" is indexing a dictionary.
1345 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001346 {
1347 --p;
1348 lp->ll_name_end = p;
1349 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001350 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001351 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001352 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001353
Bram Moolenaar9510d222022-09-11 15:14:05 +01001354 if (is_scoped_variable(name))
1355 {
1356 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1357 return NULL;
1358 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001359 if (VIM_ISWHITE(*p))
1360 {
1361 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1362 return NULL;
1363 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001364 if (tp == p + 1 && !quiet)
1365 {
1366 semsg(_(e_white_space_required_after_str_str), ":", p);
1367 return NULL;
1368 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001369 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001370 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001371 semsg(_(e_using_type_not_in_script_context_str), p);
1372 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001373 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001374 if (vim9script && (flags & GLV_NO_DECL) &&
1375 !(flags & GLV_FOR_LOOP))
1376 {
1377 // Using a type and not in a "var" declaration.
1378 semsg(_(e_trailing_characters_str), p);
1379 return NULL;
1380 }
1381
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001382
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001383 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001384 lp->ll_type = parse_type(&tp,
1385 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1386 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001387 if (lp->ll_type == NULL && !quiet)
1388 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001389 lp->ll_name_end = tp;
1390 }
Ernie Raelee865f32023-09-29 19:53:55 +02001391 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 }
1393 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001394 if (lp->ll_name == NULL)
1395 return p;
1396
Bram Moolenaar62aec932022-01-29 21:45:34 +00001397 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001398 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001399 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001400 if (import != NULL)
1401 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001402 p++; // skip '.'
1403 p = get_lval_imported(lp, rettv, import->imp_sid, p, &v,
1404 fne_flags, vim9script);
1405 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001406 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001407 }
1408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
Ernie Rael0f058d12023-10-14 11:25:04 +02001410 // Without [idx] or .key we are done.
1411 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001412 {
1413 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001414 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417
Ernie Rael0f058d12023-10-14 11:25:04 +02001418 if (vim9script && lval_root != NULL)
1419 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001420 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001421 {
1422 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001423 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001424 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001425 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001426 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001427 {
1428 cc = *p;
1429 *p = NUL;
1430 // When we would write to the variable pass &ht and prevent autoload.
1431 writing = !(flags & GLV_READ_ONLY);
1432 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001433 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001434 if (v == NULL && !quiet)
1435 semsg(_(e_undefined_variable_str), lp->ll_name);
1436 *p = cc;
1437 if (v == NULL)
1438 return NULL;
1439 lp->ll_tv = &v->di_tv;
1440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441
Bram Moolenaar4525a572022-02-13 11:57:33 +00001442 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001443 {
1444 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001445 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001446 return NULL;
1447 }
1448
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449 /*
1450 * Loop until no more [idx] or .key is following.
1451 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001452 var1.v_type = VAR_UNKNOWN;
1453 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001454 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001456 vartype_T v_type = lp->ll_tv->v_type;
1457
1458 if (*p == '.' && v_type != VAR_DICT
1459 && v_type != VAR_OBJECT
1460 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001461 {
1462 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001463 semsg(_(e_dot_not_allowed_after_str_str),
1464 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001465 return NULL;
1466 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001467 if (v_type != VAR_LIST
1468 && v_type != VAR_DICT
1469 && v_type != VAR_BLOB
1470 && v_type != VAR_OBJECT
1471 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001474 semsg(_(e_index_not_allowed_after_str_str),
1475 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001477 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001478
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001479 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001480 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001481 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001482 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001483 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001484 r = rettv_blob_alloc(lp->ll_tv);
1485 if (r == FAIL)
1486 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001487
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001491 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001494#ifdef LOG_LOCKVAR
1495 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1496 vartype_name(v_type));
1497#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001498
Bram Moolenaar4525a572022-02-13 11:57:33 +00001499 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001500 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001501 && lp->ll_tv == &v->di_tv
1502 && ht != NULL && ht == get_script_local_ht())
1503 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001504 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001505
1506 // Vim9 script local variable: get the type
1507 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001508 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001509 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001510#ifdef LOG_LOCKVAR
1511 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1512 vartype_name(lp->ll_valtype->tt_type));
1513#endif
1514 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001515 }
1516
Bram Moolenaar8c711452005-01-14 21:53:12 +00001517 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001518 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001519 {
1520 key = p + 1;
1521 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1522 ;
1523 if (len == 0)
1524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001525 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001526 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001528 }
1529 p = key + len;
1530 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001531 else
1532 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001533 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001534 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001535 if (*p == ':')
1536 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001537 else
1538 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001539 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001540 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001541 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001542 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001544 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001545 clear_tv(&var1);
1546 return NULL;
1547 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001548 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001549 }
1550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001551 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001552 if (*p == ':')
1553 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001554 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001556 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001557 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001558 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001559 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001560 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001561 if (rettv != NULL
1562 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001563 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001565 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001566 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001567 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001568 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001569 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001570 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001571 }
1572 p = skipwhite(p + 1);
1573 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001574 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001575 else
1576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001577 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001578 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001579 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001580 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001581 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001582 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001583 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001584 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001586 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001587 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001588 clear_tv(&var2);
1589 return NULL;
1590 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001591 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001592 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001594 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001596
Bram Moolenaar8c711452005-01-14 21:53:12 +00001597 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001599 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001600 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001601 clear_tv(&var1);
1602 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001604 }
1605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001606 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001607 ++p;
1608 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001609#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001610 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001611 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1612 empty1 ? ":" : (char*)tv_get_string(&var1));
1613 else
1614 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001615#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001616
Bram Moolenaard505d172022-12-18 21:42:55 +00001617 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001618 {
1619 if (len == -1)
1620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001621 // "[key]": get key from "var1"
1622 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001623 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001625 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001627 }
1628 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001630 lp->ll_object = NULL;
1631 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001632
1633 // a NULL dict is equivalent with an empty dict
1634 if (lp->ll_tv->vval.v_dict == NULL)
1635 {
1636 lp->ll_tv->vval.v_dict = dict_alloc();
1637 if (lp->ll_tv->vval.v_dict == NULL)
1638 {
1639 clear_tv(&var1);
1640 return NULL;
1641 }
1642 ++lp->ll_tv->vval.v_dict->dv_refcount;
1643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001645
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001646 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001648 // When assigning to a scope dictionary check that a function and
1649 // variable name is valid (only variable name unless it is l: or
1650 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001651 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001652 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001653 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001654
1655 if (len != -1)
1656 {
1657 prevval = key[len];
1658 key[len] = NUL;
1659 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001660 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001661 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001662 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001663 && (rettv->v_type == VAR_FUNC
1664 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001665 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001666 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001667 if (len != -1)
1668 key[len] = prevval;
1669 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001670 {
1671 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001672 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001673 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001674 }
1675
Bram Moolenaar10c65862020-10-08 21:16:42 +02001676 if (lp->ll_valtype != NULL)
1677 // use the type of the member
1678 lp->ll_valtype = lp->ll_valtype->tt_member;
1679
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001680 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001681 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001682 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001683 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001684 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001685 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001686 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001687 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001688 return NULL;
1689 }
1690
Bram Moolenaar31b81602019-02-10 22:14:27 +01001691 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001692 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001694 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001695 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001697 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001698 }
1699 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001700 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001701 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001702 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001703 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001704 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001705 p = NULL;
1706 break;
1707 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001709 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001710 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1711 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001712 {
1713 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001714 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001715 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001716
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001717 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001718 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001719 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001720 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001722 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1723
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 /*
1725 * Get the number and item for the only or first index of the List.
1726 */
1727 if (empty1)
1728 lp->ll_n1 = 0;
1729 else
1730 // is number or string
1731 lp->ll_n1 = (long)tv_get_number(&var1);
1732 clear_tv(&var1);
1733
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001734 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001736 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 return NULL;
1738 }
1739 if (lp->ll_range && !lp->ll_empty2)
1740 {
1741 lp->ll_n2 = (long)tv_get_number(&var2);
1742 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001743 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1744 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001745 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 }
1747 lp->ll_blob = lp->ll_tv->vval.v_blob;
1748 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001749 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001750 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001751 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001752 {
1753 /*
1754 * Get the number and item for the only or first index of the List.
1755 */
1756 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001757 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001758 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001759 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001760 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001761 clear_tv(&var1);
1762
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001764 lp->ll_object = NULL;
1765 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001766 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001767 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1768 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001769 if (lp->ll_li == NULL)
1770 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001771 clear_tv(&var2);
1772 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001773 }
1774
Bram Moolenaar10c65862020-10-08 21:16:42 +02001775 if (lp->ll_valtype != NULL)
1776 // use the type of the member
1777 lp->ll_valtype = lp->ll_valtype->tt_member;
1778
Bram Moolenaar8c711452005-01-14 21:53:12 +00001779 /*
1780 * May need to find the item or absolute index for the second
1781 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001782 * When no index given: "lp->ll_empty2" is TRUE.
1783 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001784 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001787 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001788 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001789 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001790 if (check_range_index_two(lp->ll_list,
1791 &lp->ll_n1, lp->ll_li,
1792 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001794 }
1795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001797 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001798 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001799 {
Ernie Raelee865f32023-09-29 19:53:55 +02001800 lp->ll_dict = NULL;
1801 lp->ll_list = NULL;
1802
1803 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001804 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001805 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001806 if (lp->ll_tv->vval.v_object == NULL)
1807 {
1808 if (!quiet)
1809 emsg(_(e_using_null_object));
1810 return NULL;
1811 }
Ernie Raelee865f32023-09-29 19:53:55 +02001812 cl = lp->ll_tv->vval.v_object->obj_class;
1813 lp->ll_object = lp->ll_tv->vval.v_object;
1814 }
1815 else
1816 {
1817 cl = lp->ll_tv->vval.v_class;
1818 lp->ll_object = NULL;
1819 }
1820 lp->ll_class = cl;
1821
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001822 // TODO: what if class is NULL?
1823 if (cl != NULL)
1824 {
1825 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001826
1827 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001828 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001829 // First look for a function with this name.
1830 // round 1: class functions (skipped for an object)
1831 // round 2: object methods
1832 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001833 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001834 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001835 int m_idx;
1836 ufunc_T *fp;
1837
1838 fp = method_lookup(cl,
1839 round == 1 ? VAR_CLASS : VAR_OBJECT,
1840 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001841 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001842 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001843 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001844 lp->ll_ufunc = fp;
1845 lp->ll_valtype = fp->uf_func_type;
1846 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001847 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001848 }
1849 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001850
1851 if (lp->ll_valtype == NULL)
1852 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001853 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001854 ocmember_T *om
1855 = member_lookup(cl, v_type, key, p - key, &m_idx);
1856 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001857 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001858 {
Ernie Rael64885642023-10-04 20:16:22 +02001859 if (get_lval_check_access(cl_exec, cl, om,
1860 p, flags) == FAIL)
1861 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001862
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001863 // When lhs is used to modify the variable, check it is
1864 // not a read-only variable.
1865 if ((flags & GLV_READ_ONLY) == 0
1866 && (*p != '.' && *p != '[')
1867 && oc_var_check_ro(cl, om))
1868 return NULL;
1869
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001870 lp->ll_valtype = om->ocm_type;
1871
1872 if (v_type == VAR_OBJECT)
1873 lp->ll_tv = ((typval_T *)(
1874 lp->ll_tv->vval.v_object + 1)) + m_idx;
1875 else
1876 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001877 }
1878 }
1879
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001880 if (lp->ll_valtype == NULL)
1881 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001882 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001883 return NULL;
1884 }
1885 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 }
1888
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001889 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 return p;
1892}
1893
1894/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899{
1900 vim_free(lp->ll_exp_name);
1901 vim_free(lp->ll_newkey);
1902}
1903
1904/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001905 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001907 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1908 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001910 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001911set_var_lval(
1912 lval_T *lp,
1913 char_u *endp,
1914 typval_T *rettv,
1915 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001916 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1917 char_u *op,
1918 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919{
1920 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922
1923 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001925 cc = *endp;
1926 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001927 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001928 return;
1929
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001930 if (lp->ll_blob != NULL)
1931 {
1932 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001933
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001934 if (op != NULL && *op != '=')
1935 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001936 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001937 return;
1938 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001939 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001940 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001941
1942 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1943 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001944 if (lp->ll_empty2)
1945 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1946
Bram Moolenaar68452172021-04-12 21:21:02 +02001947 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1948 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001949 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001950 }
1951 else
1952 {
1953 val = (int)tv_get_number_chk(rettv, &error);
1954 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001955 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001956 }
1957 }
1958 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001960 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001962 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1963 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001964 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001965 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001966 *endp = cc;
1967 return;
1968 }
1969
Bram Moolenaarff697e62019-02-12 22:28:33 +01001970 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001971 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001972 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001973 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001974 {
Ernie Raele75fde62023-12-21 17:18:54 +01001975 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001976 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001977 clear_tv(&tv);
1978 return;
1979 }
1980
Bram Moolenaar79518e22017-02-17 16:31:35 +01001981 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001982 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1983 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001984 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001985 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01001986 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001987 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001990 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001991 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001992 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1993 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001994 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001995 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001996 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001997 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001998 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002000 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002001 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002002 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002003 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 else if (lp->ll_range)
2005 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002006 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2007 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002008 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002009 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002010 return;
2011 }
2012
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002013 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2014 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 }
2016 else
2017 {
2018 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002019 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002021 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2022 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002023 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002024 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002025 return;
2026 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002027
2028 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002029 && check_typval_arg_type(lp->ll_valtype, rettv,
2030 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002031 return;
2032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 if (lp->ll_newkey != NULL)
2034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 if (op != NULL && *op != '=')
2036 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002037 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 return;
2039 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002040 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2041 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002042 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002044 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002045 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 if (di == NULL)
2047 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002048 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2049 {
2050 vim_free(di);
2051 return;
2052 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 else if (op != NULL && *op != '=')
2056 {
2057 tv_op(lp->ll_tv, rettv, op);
2058 return;
2059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 /*
2064 * Assign the value to the variable or list item.
2065 */
2066 if (copy)
2067 copy_tv(rettv, lp->ll_tv);
2068 else
2069 {
2070 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002071 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
2074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075}
2076
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002078 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2079 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002080 * Returns OK or FAIL.
2081 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002083tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002085 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 char_u numbuf[NUMBUFLEN];
2087 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002088 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089
Ernie Raele75fde62023-12-21 17:18:54 +01002090 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002091 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002092 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002093 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002094 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2095 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 {
2097 switch (tv1->v_type)
2098 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002099 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002100 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 case VAR_DICT:
2103 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002104 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002105 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002106 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002107 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002108 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002109 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002110 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002112 case VAR_CLASS:
2113 case VAR_TYPEALIAS:
2114 check_typval_is_value(tv1);
2115 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002116
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002117 case VAR_BLOB:
2118 if (*op != '+' || tv2->v_type != VAR_BLOB)
2119 break;
2120 // BLOB += BLOB
2121 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2122 {
2123 blob_T *b1 = tv1->vval.v_blob;
2124 blob_T *b2 = tv2->vval.v_blob;
2125 int i, len = blob_len(b2);
2126 for (i = 0; i < len; i++)
2127 ga_append(&b1->bv_ga, blob_get(b2, i));
2128 }
2129 return OK;
2130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002131 case VAR_LIST:
2132 if (*op != '+' || tv2->v_type != VAR_LIST)
2133 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002134 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002135 if (tv2->vval.v_list != NULL)
2136 {
2137 if (tv1->vval.v_list == NULL)
2138 {
2139 tv1->vval.v_list = tv2->vval.v_list;
2140 ++tv1->vval.v_list->lv_refcount;
2141 }
2142 else
2143 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002145 return OK;
2146
2147 case VAR_NUMBER:
2148 case VAR_STRING:
2149 if (tv2->v_type == VAR_LIST)
2150 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002151 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002153 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002154 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002155 if (tv2->v_type == VAR_FLOAT)
2156 {
2157 float_T f = n;
2158
Bram Moolenaarff697e62019-02-12 22:28:33 +01002159 if (*op == '%')
2160 break;
2161 switch (*op)
2162 {
2163 case '+': f += tv2->vval.v_float; break;
2164 case '-': f -= tv2->vval.v_float; break;
2165 case '*': f *= tv2->vval.v_float; break;
2166 case '/': f /= tv2->vval.v_float; break;
2167 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002168 clear_tv(tv1);
2169 tv1->v_type = VAR_FLOAT;
2170 tv1->vval.v_float = f;
2171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002173 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002174 switch (*op)
2175 {
2176 case '+': n += tv_get_number(tv2); break;
2177 case '-': n -= tv_get_number(tv2); break;
2178 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002179 case '/': n = num_divide(n, tv_get_number(tv2),
2180 &failed); break;
2181 case '%': n = num_modulus(n, tv_get_number(tv2),
2182 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002183 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002184 clear_tv(tv1);
2185 tv1->v_type = VAR_NUMBER;
2186 tv1->vval.v_number = n;
2187 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 }
2189 else
2190 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002191 if (tv2->v_type == VAR_FLOAT)
2192 break;
2193
Bram Moolenaarff697e62019-02-12 22:28:33 +01002194 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002195 s = tv_get_string(tv1);
2196 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 clear_tv(tv1);
2198 tv1->v_type = VAR_STRING;
2199 tv1->vval.v_string = s;
2200 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002201 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002202
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002203 case VAR_FLOAT:
2204 {
2205 float_T f;
2206
Bram Moolenaarff697e62019-02-12 22:28:33 +01002207 if (*op == '%' || *op == '.'
2208 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002209 && tv2->v_type != VAR_NUMBER
2210 && tv2->v_type != VAR_STRING))
2211 break;
2212 if (tv2->v_type == VAR_FLOAT)
2213 f = tv2->vval.v_float;
2214 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002215 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002216 switch (*op)
2217 {
2218 case '+': tv1->vval.v_float += f; break;
2219 case '-': tv1->vval.v_float -= f; break;
2220 case '*': tv1->vval.v_float *= f; break;
2221 case '/': tv1->vval.v_float /= f; break;
2222 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002223 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002224 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225 }
2226 }
2227
Ernie Raele75fde62023-12-21 17:18:54 +01002228 if (check_typval_is_value(tv2) == OK)
2229 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002230 return FAIL;
2231}
2232
2233/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002234 * Evaluate the expression used in a ":for var in expr" command.
2235 * "arg" points to "var".
2236 * Set "*errp" to TRUE for an error, FALSE otherwise;
2237 * Return a pointer that holds the info. Null when there is an error.
2238 */
2239 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240eval_for_line(
2241 char_u *arg,
2242 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002243 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002244 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002245{
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002247 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002248 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 typval_T tv;
2250 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002251 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002253 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002254
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002255 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002256 if (fi == NULL)
2257 return NULL;
2258
Bram Moolenaar404557e2021-07-05 21:41:48 +02002259 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2260 &fi->fi_semicolon, FALSE);
2261 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262 return fi;
2263
Bram Moolenaar404557e2021-07-05 21:41:48 +02002264 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002265 if (expr[0] != 'i' || expr[1] != 'n'
2266 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002267 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002268 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2269 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2270 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002271 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002272 return fi;
2273 }
2274
2275 if (skip)
2276 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002277 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2278 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002279 {
2280 *errp = FALSE;
2281 if (!skip)
2282 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002283 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002284 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002285 l = tv.vval.v_list;
2286 if (l == NULL)
2287 {
2288 // a null list is like an empty list: do nothing
2289 clear_tv(&tv);
2290 }
2291 else
2292 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002294 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002296 // No need to increment the refcount, it's already set for
2297 // the list being used in "tv".
2298 fi->fi_list = l;
2299 list_add_watch(l, &fi->fi_lw);
2300 fi->fi_lw.lw_item = l->lv_first;
2301 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002302 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002303 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002304 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002305 fi->fi_bi = 0;
2306 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002307 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002308 typval_T btv;
2309
2310 // Make a copy, so that the iteration still works when the
2311 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002313 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002314 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002315 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002316 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002317 else if (tv.v_type == VAR_STRING)
2318 {
2319 fi->fi_byte_idx = 0;
2320 fi->fi_string = tv.vval.v_string;
2321 tv.vval.v_string = NULL;
2322 if (fi->fi_string == NULL)
2323 fi->fi_string = vim_strsave((char_u *)"");
2324 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002325 else
2326 {
Sean Dewar80d73952021-08-04 19:25:54 +02002327 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002328 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 }
2330 }
2331 }
2332 if (skip)
2333 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002334 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002335
2336 return fi;
2337}
2338
2339/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002340 * Used when looping over a :for line, skip the "in expr" part.
2341 */
2342 void
2343skip_for_lines(void *fi_void, evalarg_T *evalarg)
2344{
2345 forinfo_T *fi = (forinfo_T *)fi_void;
2346 int i;
2347
2348 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002349 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002350}
2351
2352/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002353 * Use the first item in a ":for" list. Advance to the next.
2354 * Assign the values to the variable (list). "arg" points to the first one.
2355 * Return TRUE when a valid item was found, FALSE when at end of list or
2356 * something wrong.
2357 */
2358 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002359next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002360{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002361 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002363 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002364 ? (ASSIGN_FINAL
2365 // first round: error if variable exists
2366 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002367 | ASSIGN_NO_MEMBER_TYPE
2368 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002369 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002370 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002371 int skip_assign = in_vim9script() && arg[0] == '_'
2372 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002374 if (fi->fi_blob != NULL)
2375 {
2376 typval_T tv;
2377
2378 if (fi->fi_bi >= blob_len(fi->fi_blob))
2379 return FALSE;
2380 tv.v_type = VAR_NUMBER;
2381 tv.v_lock = VAR_FIXED;
2382 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2383 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002384 if (skip_assign)
2385 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002386 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002387 fi->fi_varcount, flag, NULL) == OK;
2388 }
2389
2390 if (fi->fi_string != NULL)
2391 {
2392 typval_T tv;
2393 int len;
2394
2395 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2396 if (len == 0)
2397 return FALSE;
2398 tv.v_type = VAR_STRING;
2399 tv.v_lock = VAR_FIXED;
2400 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2401 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002402 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002403 if (skip_assign)
2404 result = TRUE;
2405 else
2406 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002407 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002408 vim_free(tv.vval.v_string);
2409 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002410 }
2411
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002412 item = fi->fi_lw.lw_item;
2413 if (item == NULL)
2414 result = FALSE;
2415 else
2416 {
2417 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002418 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002419 if (skip_assign)
2420 result = TRUE;
2421 else
2422 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002423 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 }
2425 return result;
2426}
2427
2428/*
2429 * Free the structure used to store info used by ":for".
2430 */
2431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002432free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433{
Bram Moolenaar33570922005-01-25 22:26:29 +00002434 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002436 if (fi == NULL)
2437 return;
2438 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002439 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002441 list_unref(fi->fi_list);
2442 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002443 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002444 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002445 else
2446 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 vim_free(fi);
2448}
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002451set_context_for_expression(
2452 expand_T *xp,
2453 char_u *arg,
2454 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002456 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002458 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002460 if (cmdidx == CMD_let || cmdidx == CMD_var
2461 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002462 {
2463 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002464 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002466 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002467 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 {
2469 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002470 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002471 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472 break;
2473 }
2474 return;
2475 }
2476 }
2477 else
2478 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2479 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 while ((xp->xp_pattern = vim_strpbrk(arg,
2481 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2482 {
2483 c = *xp->xp_pattern;
2484 if (c == '&')
2485 {
2486 c = xp->xp_pattern[1];
2487 if (c == '&')
2488 {
2489 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002490 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002495 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2496 xp->xp_pattern += 2;
2497
2498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 else if (c == '$')
2501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002502 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 xp->xp_context = EXPAND_ENV_VARS;
2504 }
2505 else if (c == '=')
2506 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002507 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 xp->xp_context = EXPAND_EXPRESSION;
2509 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002510 else if (c == '#'
2511 && xp->xp_context == EXPAND_EXPRESSION)
2512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002513 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002514 break;
2515 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002516 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 && xp->xp_context == EXPAND_FUNCTIONS
2518 && vim_strchr(xp->xp_pattern, '(') == NULL)
2519 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002520 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 break;
2522 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002523 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002525 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2528 if (c == '\\' && xp->xp_pattern[1] != NUL)
2529 ++xp->xp_pattern;
2530 xp->xp_context = EXPAND_NOTHING;
2531 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002532 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002534 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2536 /* skip */ ;
2537 xp->xp_context = EXPAND_NOTHING;
2538 }
2539 else if (c == '|')
2540 {
2541 if (xp->xp_pattern[1] == '|')
2542 {
2543 ++xp->xp_pattern;
2544 xp->xp_context = EXPAND_EXPRESSION;
2545 }
2546 else
2547 xp->xp_context = EXPAND_COMMANDS;
2548 }
2549 else
2550 xp->xp_context = EXPAND_EXPRESSION;
2551 }
2552 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002553 // Doesn't look like something valid, expand as an expression
2554 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 arg = xp->xp_pattern;
2557 if (*arg != NUL)
2558 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2559 /* skip */ ;
2560 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002561
2562 // ":exe one two" completes "two"
2563 if ((cmdidx == CMD_execute
2564 || cmdidx == CMD_echo
2565 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002566 || cmdidx == CMD_echomsg
2567 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002568 && xp->xp_context == EXPAND_EXPRESSION)
2569 {
2570 for (;;)
2571 {
2572 char_u *n = skiptowhite(arg);
2573
2574 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2575 break;
2576 arg = skipwhite(n);
2577 }
2578 }
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 xp->xp_pattern = arg;
2581}
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002584 * Return TRUE if "pat" matches "text".
2585 * Does not use 'cpo' and always uses 'magic'.
2586 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002587 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002588pattern_match(char_u *pat, char_u *text, int ic)
2589{
2590 int matches = FALSE;
2591 char_u *save_cpo;
2592 regmatch_T regmatch;
2593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002594 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002595 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002596 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002597 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2598 if (regmatch.regprog != NULL)
2599 {
2600 regmatch.rm_ic = ic;
2601 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2602 vim_regfree(regmatch.regprog);
2603 }
2604 p_cpo = save_cpo;
2605 return matches;
2606}
2607
2608/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002609 * Handle a name followed by "(". Both for just "name(arg)" and for
2610 * "expr->name(arg)".
2611 * Returns OK or FAIL.
2612 */
2613 static int
2614eval_func(
2615 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002616 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002617 char_u *name,
2618 int name_len,
2619 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002620 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002621 typval_T *basetv) // "expr" for "expr->name(arg)"
2622{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002623 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002624 char_u *s = name;
2625 int len = name_len;
2626 partial_T *partial;
2627 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002628 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002629 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002630
2631 if (!evaluate)
2632 check_vars(s, len);
2633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002634 // If "s" is the name of a variable of type VAR_FUNC
2635 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002636 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002637 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002639 // Need to make a copy, in case evaluating the arguments makes
2640 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002641 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002642 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002643 ret = FAIL;
2644 else
2645 {
2646 funcexe_T funcexe;
2647
2648 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002649 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002650 funcexe.fe_firstline = curwin->w_cursor.lnum;
2651 funcexe.fe_lastline = curwin->w_cursor.lnum;
2652 funcexe.fe_evaluate = evaluate;
2653 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002654 if (partial != NULL)
2655 {
2656 funcexe.fe_object = partial->pt_obj;
2657 if (funcexe.fe_object != NULL)
2658 ++funcexe.fe_object->obj_refcount;
2659 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002660 funcexe.fe_basetv = basetv;
2661 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002662 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002663 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002664 }
2665 vim_free(s);
2666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002667 // If evaluate is FALSE rettv->v_type was not set in
2668 // get_func_tv, but it's needed in handle_subscript() to parse
2669 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002670 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2671 {
2672 rettv->vval.v_string = NULL;
2673 rettv->v_type = VAR_FUNC;
2674 }
2675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002676 // Stop the expression evaluation when immediately
2677 // aborting on error, or when an interrupt occurred or
2678 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002679 if (evaluate && aborting())
2680 {
2681 if (ret == OK)
2682 clear_tv(rettv);
2683 ret = FAIL;
2684 }
2685 return ret;
2686}
2687
2688/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002689 * After a NL, skip over empty lines and comment-only lines.
2690 */
2691 static char_u *
2692newline_skip_comments(char_u *arg)
2693{
2694 char_u *p = arg + 1;
2695
2696 for (;;)
2697 {
2698 p = skipwhite(p);
2699
2700 if (*p == NUL)
2701 break;
2702 if (vim9_comment_start(p))
2703 {
2704 char_u *nl = vim_strchr(p, NL);
2705
2706 if (nl == NULL)
2707 break;
2708 p = nl;
2709 }
2710 if (*p != NL)
2711 break;
2712 ++p; // skip another NL
2713 }
2714 return p;
2715}
2716
2717/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002718 * Get the next line source line without advancing. But do skip over comment
2719 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002720 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002721 */
2722 static char_u *
2723getline_peek_skip_comments(evalarg_T *evalarg)
2724{
2725 for (;;)
2726 {
2727 char_u *next = getline_peek(evalarg->eval_getline,
2728 evalarg->eval_cookie);
2729 char_u *p;
2730
2731 if (next == NULL)
2732 break;
2733 p = skipwhite(next);
2734 if (*p != NUL && !vim9_comment_start(p))
2735 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002736 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002737 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002738 }
2739 return NULL;
2740}
2741
2742/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002743 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2744 * comment) and there is a next line, return the next line (skipping blanks)
2745 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002746 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2747 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002748 * "arg" must point somewhere inside a line, not at the start.
2749 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002750 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2752{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002753 char_u *p = skipwhite(arg);
2754
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002756 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002758 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2759 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002760 && (*p == NUL || *p == NL
2761 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002762 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002763 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002764
Bram Moolenaare442d592022-05-05 12:20:28 +01002765 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002766 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002767 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002768 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002769 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002770 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002771
Bram Moolenaar9d489562020-07-30 20:08:50 +02002772 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002773 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002774 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002775 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 }
2777 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002778 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779}
2780
2781/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002782 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002783 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002784 *
2785 * If "arg" is not NULL, then the caller should assign the return value to
2786 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002787 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002788 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002789eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002790{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002791 garray_T *gap = &evalarg->eval_ga;
2792 char_u *line;
2793
Bram Moolenaar39be4982022-05-06 21:51:50 +01002794 if (arg != NULL)
2795 {
2796 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002797 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002798 // Truncate before a trailing comment, so that concatenating the lines
2799 // won't turn the rest into a comment.
2800 if (*skipwhite(arg) == '#')
2801 *arg = NUL;
2802 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002803
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002804 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002805 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2806 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002807 else
2808 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002809 if (line == NULL)
2810 return NULL;
2811
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002812 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002813 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2814 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002815 char_u *p = skipwhite(line);
2816
2817 // Going to concatenate the lines after parsing. For an empty or
2818 // comment line use an empty string.
2819 if (*p == NUL || vim9_comment_start(p))
2820 {
2821 vim_free(line);
2822 line = vim_strsave((char_u *)"");
2823 }
2824
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002825 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2826 ++gap->ga_len;
2827 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002828 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002829 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002830 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002831 evalarg->eval_tofree = line;
2832 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002833
2834 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002835 // line. The caller assigns the return value to "arg".
2836 // If "arg" is NULL, then the return value is discarded. In that case,
2837 // "arg" still points to the previous line. So don't reset
2838 // "eval_using_cmdline".
2839 if (arg != NULL)
2840 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002841 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002842}
2843
Bram Moolenaare6b53242020-07-01 17:28:33 +02002844/*
2845 * Call eval_next_non_blank() and get the next line if needed.
2846 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002847 char_u *
2848skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2849{
2850 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002851 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002852
Bram Moolenaar962d7212020-07-04 14:15:00 +02002853 if (evalarg == NULL)
2854 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002855 eval_next_non_blank(p, evalarg, &getnext);
2856 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002857 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002858 return p;
2859}
2860
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002861/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002862 * The "eval" functions have an "evalarg" argument: When NULL or
2863 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2864 * parsed but not executed. The functions may return OK, but the rettv will be
2865 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 */
2867
2868/*
2869 * Handle zero level expression.
2870 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002872 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 * Return OK or FAIL.
2875 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002877eval0(
2878 char_u *arg,
2879 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002880 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002881 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002883 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2884}
2885
2886/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002887 * If "arg" is a simple function call without arguments then call it and return
2888 * the result. Otherwise return NOTDONE.
2889 */
2890 int
2891may_call_simple_func(
2892 char_u *arg,
2893 typval_T *rettv)
2894{
2895 char_u *parens = (char_u *)strstr((char *)arg, "()");
2896 int r = NOTDONE;
2897
2898 // If the expression is "FuncName()" then we can skip a lot of overhead.
2899 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2900 {
2901 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2902
2903 if (to_name_end(p, TRUE) == parens)
2904 r = call_simple_func(arg, (int)(parens - arg), rettv);
2905 }
2906 return r;
2907}
2908
2909/*
2910 * Handle zero level expression with optimization for a simple function call.
2911 * Same arguments and return value as eval0().
2912 */
2913 int
2914eval0_simple_funccal(
2915 char_u *arg,
2916 typval_T *rettv,
2917 exarg_T *eap,
2918 evalarg_T *evalarg)
2919{
2920 int r = may_call_simple_func(arg, rettv);
2921
2922 if (r == NOTDONE)
2923 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2924 return r;
2925}
2926
2927/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002928 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2929 * expression and don't check what comes after the expression.
2930 */
2931 int
2932eval0_retarg(
2933 char_u *arg,
2934 typval_T *rettv,
2935 exarg_T *eap,
2936 evalarg_T *evalarg,
2937 char_u **retarg)
2938{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 int ret;
2940 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002941 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002942 int did_emsg_before = did_emsg;
2943 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002944 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002945 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002948 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002949
Bram Moolenaar79481362022-06-27 20:15:10 +01002950 if (ret != FAIL)
2951 {
2952 expr_end = p;
2953 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002954
Bram Moolenaar79481362022-06-27 20:15:10 +01002955 // In Vim9 script a command block is not split at NL characters for
2956 // commands using an expression argument. Skip over a '#' comment to
2957 // check for a following NL. Require white space before the '#'.
2958 if (in_vim9script() && p > expr_end && retarg == NULL)
2959 while (*p == '#')
2960 {
2961 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002962
Bram Moolenaar79481362022-06-27 20:15:10 +01002963 if (nl == NULL)
2964 break;
2965 p = skipwhite(nl + 1);
2966 if (eap != NULL && *p != NUL)
2967 eap->nextcmd = p;
2968 check_for_end = FALSE;
2969 }
2970
2971 if (check_for_end)
2972 end_error = !ends_excmd2(arg, p);
2973 }
2974
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002975 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
2977 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /*
2980 * Report the invalid expression unless the expression evaluation has
2981 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002982 * exception, or we already gave a more specific error.
2983 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002985 if (!aborting()
2986 && did_emsg == did_emsg_before
2987 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002988 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002989 {
2990 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002991 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002992 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002993 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002994 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002995
zeertzjqf2588b62023-05-05 17:22:35 +01002996 if (eap != NULL && p != NULL)
2997 {
2998 // Some of the expression may not have been consumed.
2999 // Only execute a next command if it cannot be a "||" operator.
3000 // The next command may be "catch".
3001 char_u *nextcmd = check_nextcmd(p);
3002 if (nextcmd != NULL && *nextcmd != '|')
3003 eap->nextcmd = nextcmd;
3004 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003005 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003007
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003008 if (retarg != NULL)
3009 *retarg = p;
3010 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003011 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 return ret;
3014}
3015
3016/*
3017 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003018 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003019 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 *
3021 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003022 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003024 * Note: "rettv.v_lock" is not set.
3025 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 * Return OK or FAIL.
3027 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003028 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003029eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003031 char_u *p;
3032 int getnext;
3033
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003034 CLEAR_POINTER(rettv);
3035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 /*
3037 * Get the first variable.
3038 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return FAIL;
3041
Bram Moolenaar793648f2020-06-26 21:28:25 +02003042 p = eval_next_non_blank(*arg, evalarg, &getnext);
3043 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003045 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046 int result;
3047 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003048 evalarg_T *evalarg_used = evalarg;
3049 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003050 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003051 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003052 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003053
3054 if (evalarg == NULL)
3055 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003056 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003057 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003059 orig_flags = evalarg_used->eval_flags;
3060 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003061
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003062 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003063 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003064 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003065 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003066 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003067 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003068 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003069 clear_tv(rettv);
3070 return FAIL;
3071 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003072 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003073 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003074
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003076 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078 int error = FALSE;
3079
Bram Moolenaar13106602020-10-04 16:06:05 +02003080 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003081 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003082 else if (vim9script)
3083 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003084 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003086 if (error || !op_falsy || !result)
3087 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003088 if (error)
3089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091
3092 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003093 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003095 if (op_falsy)
3096 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003097 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003098 {
mityu4ac198c2021-05-28 17:52:40 +02003099 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003100 clear_tv(rettv);
3101 return FAIL;
3102 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003103 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003104 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003105 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003106 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003107 {
3108 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003110 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003111 if (!op_falsy || !result)
3112 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003114 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003116 /*
3117 * Check for the ":".
3118 */
3119 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3120 if (*p != ':')
3121 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003122 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003123 if (evaluate && result)
3124 clear_tv(rettv);
3125 evalarg_used->eval_flags = orig_flags;
3126 return FAIL;
3127 }
3128 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003129 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003130 else
3131 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003132 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003133 {
3134 error_white_both(p, 1);
3135 clear_tv(rettv);
3136 evalarg_used->eval_flags = orig_flags;
3137 return FAIL;
3138 }
3139 *arg = p;
3140 }
3141
3142 /*
3143 * Get the third variable. Recursive!
3144 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003145 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003146 {
mityu4ac198c2021-05-28 17:52:40 +02003147 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003148 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003149 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003150 return FAIL;
3151 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003152 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3153 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003154 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003155 if (eval1(arg, &var2, evalarg_used) == FAIL)
3156 {
3157 if (evaluate && result)
3158 clear_tv(rettv);
3159 evalarg_used->eval_flags = orig_flags;
3160 return FAIL;
3161 }
3162 if (evaluate && !result)
3163 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003165
3166 if (evalarg == NULL)
3167 clear_evalarg(&local_evalarg, NULL);
3168 else
3169 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171
3172 return OK;
3173}
3174
3175/*
3176 * Handle first level expression:
3177 * expr2 || expr2 || expr2 logical OR
3178 *
3179 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003180 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 *
3182 * Return OK or FAIL.
3183 */
3184 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003185eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003187 char_u *p;
3188 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003191 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003193 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 return FAIL;
3195
3196 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003197 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003199 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003200 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003202 evalarg_T *evalarg_used = evalarg;
3203 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003204 int evaluate;
3205 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003206 long result = FALSE;
3207 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003208 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003209 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003210
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003211 if (evalarg == NULL)
3212 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003213 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003214 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003215 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003216 orig_flags = evalarg_used->eval_flags;
3217 evaluate = orig_flags & EVAL_EVALUATE;
3218 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003219 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003220 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003221 result = tv_get_bool_chk(rettv, &error);
3222 else if (tv_get_number_chk(rettv, &error) != 0)
3223 result = TRUE;
3224 clear_tv(rettv);
3225 if (error)
3226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228
3229 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003230 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003232 while (p[0] == '|' && p[1] == '|')
3233 {
3234 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003235 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003236 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003237 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003238 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003239 {
3240 error_white_both(p, 2);
3241 clear_tv(rettv);
3242 return FAIL;
3243 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003244 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003245 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003246
3247 /*
3248 * Get the second variable.
3249 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003250 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003251 {
mityu4ac198c2021-05-28 17:52:40 +02003252 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003253 clear_tv(rettv);
3254 return FAIL;
3255 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003256 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3257 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003258 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003259 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003260 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003261
3262 /*
3263 * Compute the result.
3264 */
3265 if (evaluate && !result)
3266 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003267 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003268 result = tv_get_bool_chk(&var2, &error);
3269 else if (tv_get_number_chk(&var2, &error) != 0)
3270 result = TRUE;
3271 clear_tv(&var2);
3272 if (error)
3273 return FAIL;
3274 }
3275 if (evaluate)
3276 {
3277 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003278 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003279 rettv->v_type = VAR_BOOL;
3280 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003281 }
3282 else
3283 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003284 rettv->v_type = VAR_NUMBER;
3285 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003286 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003287 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003288
3289 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003291
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003292 if (evalarg == NULL)
3293 clear_evalarg(&local_evalarg, NULL);
3294 else
3295 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297
3298 return OK;
3299}
3300
3301/*
3302 * Handle second level expression:
3303 * expr3 && expr3 && expr3 logical AND
3304 *
3305 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +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 +02003311eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003313 char_u *p;
3314 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003317 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003319 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 return FAIL;
3321
3322 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003323 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003325 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003326 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003328 evalarg_T *evalarg_used = evalarg;
3329 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003330 int orig_flags;
3331 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003332 long result = TRUE;
3333 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003334 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003335 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003336
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003337 if (evalarg == NULL)
3338 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003339 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003340 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003341 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003342 orig_flags = evalarg_used->eval_flags;
3343 evaluate = orig_flags & EVAL_EVALUATE;
3344 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003345 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003346 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003347 result = tv_get_bool_chk(rettv, &error);
3348 else if (tv_get_number_chk(rettv, &error) == 0)
3349 result = FALSE;
3350 clear_tv(rettv);
3351 if (error)
3352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354
3355 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003356 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003358 while (p[0] == '&' && p[1] == '&')
3359 {
3360 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003361 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003362 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003363 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003364 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003365 {
3366 error_white_both(p, 2);
3367 clear_tv(rettv);
3368 return FAIL;
3369 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003370 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003371 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003372
3373 /*
3374 * Get the second variable.
3375 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003376 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003377 {
mityu4ac198c2021-05-28 17:52:40 +02003378 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003379 clear_tv(rettv);
3380 return FAIL;
3381 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003382 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3383 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003384 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003385 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003386 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003387 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003388
3389 /*
3390 * Compute the result.
3391 */
3392 if (evaluate && result)
3393 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003394 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003395 result = tv_get_bool_chk(&var2, &error);
3396 else if (tv_get_number_chk(&var2, &error) == 0)
3397 result = FALSE;
3398 clear_tv(&var2);
3399 if (error)
3400 return FAIL;
3401 }
3402 if (evaluate)
3403 {
3404 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003405 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003406 rettv->v_type = VAR_BOOL;
3407 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003408 }
3409 else
3410 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003411 rettv->v_type = VAR_NUMBER;
3412 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003413 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003414 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003415
3416 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003418
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003419 if (evalarg == NULL)
3420 clear_evalarg(&local_evalarg, NULL);
3421 else
3422 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424
3425 return OK;
3426}
3427
3428/*
3429 * Handle third level expression:
3430 * var1 == var2
3431 * var1 =~ var2
3432 * var1 != var2
3433 * var1 !~ var2
3434 * var1 > var2
3435 * var1 >= var2
3436 * var1 < var2
3437 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003438 * var1 is var2
3439 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 *
3441 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003442 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 *
3444 * Return OK or FAIL.
3445 */
3446 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003447eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003450 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003451 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003453 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003456 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003458 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return FAIL;
3460
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003461 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003462
Bram Moolenaar696ba232020-07-29 21:20:41 +02003463 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003466 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003468 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003470 typval_T var2;
3471 int ic;
3472 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003473 int evaluate = evalarg == NULL
3474 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003475 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003476
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003477 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003478 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003479 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003480 p = *arg;
3481 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003482 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3483 {
mityu4ac198c2021-05-28 17:52:40 +02003484 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003485 clear_tv(rettv);
3486 return FAIL;
3487 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003488
Bram Moolenaar696ba232020-07-29 21:20:41 +02003489 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3490 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003491 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003492 clear_tv(rettv);
3493 return FAIL;
3494 }
3495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003496 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (p[len] == '?')
3498 {
3499 ic = TRUE;
3500 ++len;
3501 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003502 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 else if (p[len] == '#')
3504 {
3505 ic = FALSE;
3506 ++len;
3507 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003508 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003510 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 /*
3513 * Get the second variable.
3514 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003515 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3516 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003517 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003518 clear_tv(rettv);
3519 return FAIL;
3520 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003521 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003522 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return FAIL;
3526 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003527 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003528 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003529 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003530
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003531 // use the line of the comparison for messages
3532 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003533 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003534 {
3535 ret = FAIL;
3536 clear_tv(rettv);
3537 }
3538 else
3539 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003540 clear_tv(&var2);
3541 return ret;
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544
3545 return OK;
3546}
3547
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003548/*
3549 * Make a copy of blob "tv1" and append blob "tv2".
3550 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 void
3552eval_addblob(typval_T *tv1, typval_T *tv2)
3553{
3554 blob_T *b1 = tv1->vval.v_blob;
3555 blob_T *b2 = tv2->vval.v_blob;
3556 blob_T *b = blob_alloc();
3557 int i;
3558
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003559 if (b == NULL)
3560 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003562 for (i = 0; i < blob_len(b1); i++)
3563 ga_append(&b->bv_ga, blob_get(b1, i));
3564 for (i = 0; i < blob_len(b2); i++)
3565 ga_append(&b->bv_ga, blob_get(b2, i));
3566
3567 clear_tv(tv1);
3568 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569}
3570
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003571/*
3572 * Make a copy of list "tv1" and append list "tv2".
3573 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 int
3575eval_addlist(typval_T *tv1, typval_T *tv2)
3576{
3577 typval_T var3;
3578
3579 // concatenate Lists
3580 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3581 {
3582 clear_tv(tv1);
3583 clear_tv(tv2);
3584 return FAIL;
3585 }
3586 clear_tv(tv1);
3587 *tv1 = var3;
3588 return OK;
3589}
3590
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003592 * Handle the bitwise left/right shift operator expression:
3593 * var1 << var2
3594 * var1 >> var2
3595 *
3596 * "arg" must point to the first non-white of the expression.
3597 * "arg" is advanced to just after the recognized expression.
3598 *
3599 * Return OK or FAIL.
3600 */
3601 static int
3602eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3603{
3604 /*
3605 * Get the first expression.
3606 */
3607 if (eval6(arg, rettv, evalarg) == FAIL)
3608 return FAIL;
3609
3610 /*
3611 * Repeat computing, until no '<<' or '>>' is following.
3612 */
3613 for (;;)
3614 {
3615 char_u *p;
3616 int getnext;
3617 exprtype_T type;
3618 int evaluate;
3619 typval_T var2;
3620 int vim9script;
3621
3622 p = eval_next_non_blank(*arg, evalarg, &getnext);
3623 if (p[0] == '<' && p[1] == '<')
3624 type = EXPR_LSHIFT;
3625 else if (p[0] == '>' && p[1] == '>')
3626 type = EXPR_RSHIFT;
3627 else
3628 return OK;
3629
3630 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003631 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3632 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003633 {
3634 // left operand should be a number
3635 emsg(_(e_bitshift_ops_must_be_number));
3636 clear_tv(rettv);
3637 return FAIL;
3638 }
3639
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003640 vim9script = in_vim9script();
3641 if (getnext)
3642 {
3643 *arg = eval_next_line(*arg, evalarg);
3644 p = *arg;
3645 }
3646 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3647 {
3648 error_white_both(*arg, 2);
3649 clear_tv(rettv);
3650 return FAIL;
3651 }
3652
3653 /*
3654 * Get the second variable.
3655 */
3656 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3657 {
3658 error_white_both(p, 2);
3659 clear_tv(rettv);
3660 return FAIL;
3661 }
3662 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3663 if (eval6(arg, &var2, evalarg) == FAIL)
3664 {
3665 clear_tv(rettv);
3666 return FAIL;
3667 }
3668
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003669 if (evaluate)
3670 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003671 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3672 {
3673 // right operand should be a positive number
3674 if (var2.v_type != VAR_NUMBER)
3675 emsg(_(e_bitshift_ops_must_be_number));
3676 else
3677 emsg(_(e_bitshift_ops_must_be_positive));
3678 clear_tv(rettv);
3679 clear_tv(&var2);
3680 return FAIL;
3681 }
3682
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003683 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3684 // shifting more bits than we have always results in zero
3685 rettv->vval.v_number = 0;
3686 else if (type == EXPR_LSHIFT)
3687 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003688 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003689 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003690 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003691 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003692 }
3693
3694 clear_tv(&var2);
3695 }
3696
3697 return OK;
3698}
3699
3700/*
3701 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003702 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003704 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003705 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 *
3707 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003708 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 *
3710 * Return OK or FAIL.
3711 */
3712 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003713eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003716 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003718 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720
3721 /*
3722 * Repeat computing, until no '+', '-' or '.' is following.
3723 */
3724 for (;;)
3725 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003726 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003727 int getnext;
3728 char_u *p;
3729 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003730 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003731 int concat;
3732 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003733 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003734
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003735 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003736 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003737 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003738 p = eval_next_non_blank(*arg, evalarg, &getnext);
3739 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003740 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003741 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3742 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003744 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3745 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003746
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003747 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003748 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003749 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003750 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003751 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003752 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003753 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003754 {
mityu4ac198c2021-05-28 17:52:40 +02003755 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003756 clear_tv(rettv);
3757 return FAIL;
3758 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003759 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003760 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003761 if ((op != '+' || (rettv->v_type != VAR_LIST
3762 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003763 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003764 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003766 int error = FALSE;
3767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 // For "list + ...", an illegal use of the first operand as
3769 // a number cannot be determined before evaluating the 2nd
3770 // operand: if this is also a list, all is ok.
3771 // For "something . ...", "something - ..." or "non-list + ...",
3772 // we know that the first operand needs to be a string or number
3773 // without evaluating the 2nd operand. So check before to avoid
3774 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003775 if (op != '.')
3776 tv_get_number_chk(rettv, &error);
3777 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 {
3779 clear_tv(rettv);
3780 return FAIL;
3781 }
3782 }
3783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 /*
3785 * Get the second variable.
3786 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003787 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003788 {
mityu89dcb4d2021-05-28 13:50:17 +02003789 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003790 clear_tv(rettv);
3791 return FAIL;
3792 }
3793 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003794 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
3798 }
3799
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003800 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
3802 /*
3803 * Compute the result.
3804 */
3805 if (op == '.')
3806 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003807 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3808 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003809 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003810
Bram Moolenaar2e086612020-08-25 22:37:48 +02003811 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003812 || var2.v_type == VAR_CHANNEL
3813 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003814 semsg(_(e_using_invalid_value_as_string_str),
3815 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003816 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003817 {
3818 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3819 var2.vval.v_float);
3820 s2 = buf2;
3821 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003822 else
3823 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003824 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003825 {
3826 clear_tv(rettv);
3827 clear_tv(&var2);
3828 return FAIL;
3829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003830 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
3832 rettv->v_type = VAR_STRING;
3833 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003835 else if (op == '+' && rettv->v_type == VAR_BLOB
3836 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003838 else if (op == '+' && rettv->v_type == VAR_LIST
3839 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003840 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003842 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
3845 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003846 int error = FALSE;
3847 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003848 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003849
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003850 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003851 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003852 f1 = rettv->vval.v_float;
3853 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003855 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003857 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003858 if (error)
3859 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003860 // This can only happen for "list + non-list" or
3861 // "blob + non-blob". For "non-list + ..." or
3862 // "something - ...", we returned before evaluating the
3863 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003865 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003866 return FAIL;
3867 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 if (var2.v_type == VAR_FLOAT)
3869 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 if (var2.v_type == VAR_FLOAT)
3872 {
3873 f2 = var2.vval.v_float;
3874 n2 = 0;
3875 }
3876 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003878 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003879 if (error)
3880 {
3881 clear_tv(rettv);
3882 clear_tv(&var2);
3883 return FAIL;
3884 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003885 if (rettv->v_type == VAR_FLOAT)
3886 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003888 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003891 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3892 {
3893 if (op == '+')
3894 f1 = f1 + f2;
3895 else
3896 f1 = f1 - f2;
3897 rettv->v_type = VAR_FLOAT;
3898 rettv->vval.v_float = f1;
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003901 {
3902 if (op == '+')
3903 n1 = n1 + n2;
3904 else
3905 n1 = n1 - n2;
3906 rettv->v_type = VAR_NUMBER;
3907 rettv->vval.v_number = n1;
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912 }
3913 return OK;
3914}
3915
3916/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003917 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 * * number multiplication
3919 * / number division
3920 * % number modulo
3921 *
3922 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003923 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 *
3925 * Return OK or FAIL.
3926 */
3927 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003928eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003929 char_u **arg,
3930 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003931 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003932 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003934 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003937 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003939 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 return FAIL;
3941
3942 /*
3943 * Repeat computing, until no '*', '/' or '%' is following.
3944 */
3945 for (;;)
3946 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003947 int evaluate;
3948 int getnext;
3949 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003950 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003951 int op;
3952 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003953 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003954 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003955
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003956 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003957 p = eval_next_non_blank(*arg, evalarg, &getnext);
3958 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003959 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003961
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003962 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003963 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003964 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003965 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003966 {
3967 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3968 {
mityu4ac198c2021-05-28 17:52:40 +02003969 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003970 clear_tv(rettv);
3971 return FAIL;
3972 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003973 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003976 f1 = 0;
3977 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003978 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003979 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003981 if (rettv->v_type == VAR_FLOAT)
3982 {
3983 f1 = rettv->vval.v_float;
3984 use_float = TRUE;
3985 n1 = 0;
3986 }
3987 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003988 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 if (error)
3991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
3994 n1 = 0;
3995
3996 /*
3997 * Get the second variable.
3998 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003999 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4000 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004001 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004002 clear_tv(rettv);
4003 return FAIL;
4004 }
4005 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004006 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FAIL;
4008
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004009 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004011 if (var2.v_type == VAR_FLOAT)
4012 {
4013 if (!use_float)
4014 {
4015 f1 = n1;
4016 use_float = TRUE;
4017 }
4018 f2 = var2.vval.v_float;
4019 n2 = 0;
4020 }
4021 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004023 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004024 clear_tv(&var2);
4025 if (error)
4026 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004027 if (use_float)
4028 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 /*
4032 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004033 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004035 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004037 if (op == '*')
4038 f1 = f1 * f2;
4039 else if (op == '/')
4040 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004041#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004042 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004043 if (f2 == 0.0)
4044 {
4045 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004046 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004047 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004048 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004049 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004050 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004051 }
4052 else
4053 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004054#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004055 // We rely on the floating point library to handle divide
4056 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004057 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004058#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004062 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004063 return FAIL;
4064 }
4065 rettv->v_type = VAR_FLOAT;
4066 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 else
4069 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004070 int failed = FALSE;
4071
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072 if (op == '*')
4073 n1 = n1 * n2;
4074 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004075 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004077 n1 = num_modulus(n1, n2, &failed);
4078 if (failed)
4079 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004080
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004081 rettv->v_type = VAR_NUMBER;
4082 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 }
4086
4087 return OK;
4088}
4089
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004090/*
4091 * Handle a type cast before a base level expression.
4092 * "arg" must point to the first non-white of the expression.
4093 * "arg" is advanced to just after the recognized expression.
4094 * Return OK or FAIL.
4095 */
4096 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004097eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004098 char_u **arg,
4099 typval_T *rettv,
4100 evalarg_T *evalarg,
4101 int want_string) // after "." operator
4102{
4103 type_T *want_type = NULL;
4104 garray_T type_list; // list of pointers to allocated types
4105 int res;
4106 int evaluate = evalarg == NULL ? 0
4107 : (evalarg->eval_flags & EVAL_EVALUATE);
4108
4109 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004110 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4111 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004112 {
4113 ++*arg;
4114 ga_init2(&type_list, sizeof(type_T *), 10);
4115 want_type = parse_type(arg, &type_list, TRUE);
4116 if (want_type == NULL && (evaluate || **arg != '>'))
4117 {
4118 clear_type_list(&type_list);
4119 return FAIL;
4120 }
4121
4122 if (**arg != '>')
4123 {
4124 if (*skipwhite(*arg) == '>')
4125 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4126 else
4127 emsg(_(e_missing_gt));
4128 clear_type_list(&type_list);
4129 return FAIL;
4130 }
4131 ++*arg;
4132 *arg = skipwhite_and_linebreak(*arg, evalarg);
4133 }
4134
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004135 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004136
4137 if (want_type != NULL && evaluate)
4138 {
4139 if (res == OK)
4140 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004141 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4142 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004143
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004144 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004145 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004146 if (want_type->tt_type == VAR_BOOL
4147 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004148 && (actual->tt_flags & TTFLAG_BOOL_OK))
4149 {
4150 int n = tv2bool(rettv);
4151
4152 // can use "0" and "1" for boolean in some places
4153 clear_tv(rettv);
4154 rettv->v_type = VAR_BOOL;
4155 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4156 }
4157 else
4158 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004159 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004160
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004161 res = check_type(want_type, actual, TRUE, where);
4162 }
4163 }
4164 }
4165 clear_type_list(&type_list);
4166 }
4167
4168 return res;
4169}
4170
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004171 int
4172eval_leader(char_u **arg, int vim9)
4173{
4174 char_u *s = *arg;
4175 char_u *p = *arg;
4176
4177 while (*p == '!' || *p == '-' || *p == '+')
4178 {
4179 char_u *n = skipwhite(p + 1);
4180
4181 // ++, --, -+ and +- are not accepted in Vim9 script
4182 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4183 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004184 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004185 return FAIL;
4186 }
4187 p = n;
4188 }
4189 *arg = p;
4190 return OK;
4191}
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004194 * Check for a predefined value "true", "false" and "null.*".
4195 * Return OK when recognized.
4196 */
4197 int
4198handle_predefined(char_u *s, int len, typval_T *rettv)
4199{
4200 switch (len)
4201 {
4202 case 4: if (STRNCMP(s, "true", 4) == 0)
4203 {
4204 rettv->v_type = VAR_BOOL;
4205 rettv->vval.v_number = VVAL_TRUE;
4206 return OK;
4207 }
4208 if (STRNCMP(s, "null", 4) == 0)
4209 {
4210 rettv->v_type = VAR_SPECIAL;
4211 rettv->vval.v_number = VVAL_NULL;
4212 return OK;
4213 }
4214 break;
4215 case 5: if (STRNCMP(s, "false", 5) == 0)
4216 {
4217 rettv->v_type = VAR_BOOL;
4218 rettv->vval.v_number = VVAL_FALSE;
4219 return OK;
4220 }
4221 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004222 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4223 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004224#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004225 rettv->v_type = VAR_JOB;
4226 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004227#else
4228 rettv->v_type = VAR_SPECIAL;
4229 rettv->vval.v_number = VVAL_NULL;
4230#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004231 return OK;
4232 }
4233 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004234 case 9:
4235 if (STRNCMP(s, "null_", 5) != 0)
4236 break;
4237 if (STRNCMP(s + 5, "list", 4) == 0)
4238 {
4239 rettv->v_type = VAR_LIST;
4240 rettv->vval.v_list = NULL;
4241 return OK;
4242 }
4243 if (STRNCMP(s + 5, "dict", 4) == 0)
4244 {
4245 rettv->v_type = VAR_DICT;
4246 rettv->vval.v_dict = NULL;
4247 return OK;
4248 }
4249 if (STRNCMP(s + 5, "blob", 4) == 0)
4250 {
4251 rettv->v_type = VAR_BLOB;
4252 rettv->vval.v_blob = NULL;
4253 return OK;
4254 }
4255 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004256 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4257 {
4258 rettv->v_type = VAR_CLASS;
4259 rettv->vval.v_class = NULL;
4260 return OK;
4261 }
4262 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004263 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4264 {
4265 rettv->v_type = VAR_STRING;
4266 rettv->vval.v_string = NULL;
4267 return OK;
4268 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004269 if (STRNCMP(s, "null_object", 11) == 0)
4270 {
4271 rettv->v_type = VAR_OBJECT;
4272 rettv->vval.v_object = NULL;
4273 return OK;
4274 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004275 break;
4276 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004277 if (STRNCMP(s, "null_channel", 12) == 0)
4278 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004280 rettv->v_type = VAR_CHANNEL;
4281 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004282#else
4283 rettv->v_type = VAR_SPECIAL;
4284 rettv->vval.v_number = VVAL_NULL;
4285#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004286 return OK;
4287 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004288 if (STRNCMP(s, "null_partial", 12) == 0)
4289 {
4290 rettv->v_type = VAR_PARTIAL;
4291 rettv->vval.v_partial = NULL;
4292 return OK;
4293 }
4294 break;
4295 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4296 {
4297 rettv->v_type = VAR_FUNC;
4298 rettv->vval.v_string = NULL;
4299 return OK;
4300 }
4301 break;
4302 }
4303 return FAIL;
4304}
4305
4306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * Handle sixth level expression:
4308 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004309 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004310 * "string" string constant
4311 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * &option-name option value
4313 * @r register contents
4314 * identifier variable value
4315 * function() function call
4316 * $VAR environment variable
4317 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004318 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004320 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004321 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 *
4323 * Also handle:
4324 * ! in front logical NOT
4325 * - in front unary minus
4326 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004327 * trailing [] subscript in String or List
4328 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004329 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 *
4331 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004332 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 *
4334 * Return OK or FAIL.
4335 */
4336 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004337eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004338 char_u **arg,
4339 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004340 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004343 int evaluate = evalarg != NULL
4344 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int len;
4346 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004347 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_u *start_leader, *end_leader;
4349 int ret = OK;
4350 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004351 static int recurse = 0;
4352 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004356 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004361 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
4363 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004364 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004365 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 end_leader = *arg;
4367
Keith Thompson184f71c2024-01-04 21:19:04 +01004368 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004369 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004370 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004371 ++*arg;
4372 return FAIL;
4373 }
4374
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004375 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004376 // and crash. With MSVC the stack is smaller.
4377 if (recurse ==
4378#ifdef _MSC_VER
4379 300
4380#else
4381 1000
4382#endif
4383 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004384 {
4385 semsg(_(e_expression_too_recursive_str), *arg);
4386 return FAIL;
4387 }
4388 ++recurse;
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 switch (**arg)
4391 {
4392 /*
4393 * Number constant.
4394 */
4395 case '0':
4396 case '1':
4397 case '2':
4398 case '3':
4399 case '4':
4400 case '5':
4401 case '6':
4402 case '7':
4403 case '8':
4404 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004405 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004406
4407 // Apply prefixed "-" and "+" now. Matters especially when
4408 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004409 if (ret == OK && evaluate && end_leader > start_leader
4410 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004411 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 break;
4413
4414 /*
4415 * String constant: "string".
4416 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004417 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 break;
4419
4420 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004421 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004423 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 break;
4425
4426 /*
4427 * List: [expr, expr]
4428 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004429 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 break;
4431
4432 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004433 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004434 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004435 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004436 {
4437 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4438 }
4439 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004440 {
4441 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004442 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004443 }
4444 else
4445 ret = NOTDONE;
4446 break;
4447
4448 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004449 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004450 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004452 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004453 ret = NOTDONE;
4454 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004455 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004456 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004457 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 break;
4459
4460 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004461 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004463 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 break;
4465
4466 /*
4467 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004468 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 */
LemonBoy2eaef102022-05-06 13:14:50 +01004470 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4471 ret = eval_interp_string(arg, rettv, evaluate);
4472 else
4473 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
4475
4476 /*
4477 * Register contents: @r.
4478 */
4479 case '@': ++*arg;
4480 if (evaluate)
4481 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004482 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004483 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004484 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004485 emsg_invreg(**arg);
4486 else
4487 {
4488 rettv->v_type = VAR_STRING;
4489 rettv->vval.v_string = get_reg_contents(**arg,
4490 GREG_EXPR_SRC);
4491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 if (**arg != NUL)
4494 ++*arg;
4495 break;
4496
4497 /*
4498 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004499 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004501 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004502 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004503 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004504 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004505 if (ret == OK && evaluate)
4506 {
4507 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4508
Bram Moolenaara9931532021-06-12 15:58:16 +02004509 // Compile it here to get the return type. The return
4510 // type is optional, when it's missing use t_unknown.
4511 // This is recognized in compile_return().
4512 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4513 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004514 if (compile_def_function(ufunc, FALSE,
4515 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004516 {
4517 clear_tv(rettv);
4518 ret = FAIL;
4519 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004520 }
4521 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004522 if (ret == NOTDONE)
4523 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004524 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004525 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004526
Bram Moolenaar9215f012020-06-27 21:18:00 +02004527 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004528 if (**arg == ')')
4529 ++*arg;
4530 else if (ret == OK)
4531 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004532 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004533 clear_tv(rettv);
4534 ret = FAIL;
4535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537 break;
4538
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 break;
4541 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542
4543 if (ret == NOTDONE)
4544 {
4545 /*
4546 * Must be a variable or function name.
4547 * Can also be a curly-braces kind of name: {expr}.
4548 */
4549 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004550 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 if (alias != NULL)
4552 s = alias;
4553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004554 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 ret = FAIL;
4556 else
4557 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004558 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4559
Bram Moolenaar4525a572022-02-13 11:57:33 +00004560 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004561 {
4562 emsg(_(e_cannot_use_underscore_here));
4563 ret = FAIL;
4564 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004565 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004566 && s[0] == 's' && s[1] == ':')
4567 {
4568 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4569 ret = FAIL;
4570 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004571 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004572 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004573 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004574 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004575 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004576 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004578 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004579 // get the value of "true", "false", etc. or a variable
4580 ret = FAIL;
4581 if (vim9script)
4582 ret = handle_predefined(s, len, rettv);
4583 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004584 {
4585 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004586 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004587 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004588 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004590 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004591 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004592 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004593 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004594 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004597 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 }
4599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004600 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4601 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004603 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 /*
4606 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4607 */
4608 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004609 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004610
4611 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004612 return ret;
4613}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004615/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004616 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004617 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004618 * Adjusts "end_leaderp" until it is at "start_leader".
4619 */
4620 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004621eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004622 typval_T *rettv,
4623 int numeric_only,
4624 char_u *start_leader,
4625 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004626{
4627 char_u *end_leader = *end_leaderp;
4628 int ret = OK;
4629 int error = FALSE;
4630 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004631 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004632 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004633 float_T f = 0.0;
4634
4635 if (rettv->v_type == VAR_FLOAT)
4636 f = rettv->vval.v_float;
4637 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004638 {
4639 while (VIM_ISWHITE(end_leader[-1]))
4640 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004641 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004642 val = tv2bool(rettv);
4643 else
4644 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004645 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004646 if (error)
4647 {
4648 clear_tv(rettv);
4649 ret = FAIL;
4650 }
4651 else
4652 {
4653 while (end_leader > start_leader)
4654 {
4655 --end_leader;
4656 if (*end_leader == '!')
4657 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004658 if (numeric_only)
4659 {
4660 ++end_leader;
4661 break;
4662 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004663 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004664 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004665 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004666 {
4667 rettv->v_type = VAR_BOOL;
4668 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4669 }
4670 else
4671 f = !f;
4672 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004673 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004674 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004675 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004676 type = VAR_BOOL;
4677 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004678 }
4679 else if (*end_leader == '-')
4680 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004681 if (rettv->v_type == VAR_FLOAT)
4682 f = -f;
4683 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004684 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004685 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004686 type = VAR_NUMBER;
4687 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004688 }
4689 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004690 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004693 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 else
4696 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004697 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004698 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004699 rettv->v_type = type;
4700 else
4701 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004702 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004705 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return ret;
4707}
4708
4709/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004710 * Call the function referred to in "rettv".
4711 */
4712 static int
4713call_func_rettv(
4714 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004715 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004716 typval_T *rettv,
4717 int evaluate,
4718 dict_T *selfdict,
4719 typval_T *basetv)
4720{
4721 partial_T *pt = NULL;
4722 funcexe_T funcexe;
4723 typval_T functv;
4724 char_u *s;
4725 int ret;
4726
4727 // need to copy the funcref so that we can clear rettv
4728 if (evaluate)
4729 {
4730 functv = *rettv;
4731 rettv->v_type = VAR_UNKNOWN;
4732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004733 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004734 if (functv.v_type == VAR_PARTIAL)
4735 {
4736 pt = functv.vval.v_partial;
4737 s = partial_name(pt);
4738 }
4739 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004740 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004741 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004742 if (s == NULL || *s == NUL)
4743 {
4744 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004745 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004746 goto theend;
4747 }
4748 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004749 }
4750 else
4751 s = (char_u *)"";
4752
Bram Moolenaara80faa82020-04-12 19:37:17 +02004753 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004754 funcexe.fe_firstline = curwin->w_cursor.lnum;
4755 funcexe.fe_lastline = curwin->w_cursor.lnum;
4756 funcexe.fe_evaluate = evaluate;
4757 funcexe.fe_partial = pt;
4758 funcexe.fe_selfdict = selfdict;
4759 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004760 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004761
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004762theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004763 // Clear the funcref afterwards, so that deleting it while
4764 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004765 if (evaluate)
4766 clear_tv(&functv);
4767
4768 return ret;
4769}
4770
4771/*
4772 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004773 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004774 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4775 */
4776 static int
4777eval_lambda(
4778 char_u **arg,
4779 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004780 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004781 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004782{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004783 int evaluate = evalarg != NULL
4784 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004785 typval_T base = *rettv;
4786 int ret;
4787
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004788 rettv->v_type = VAR_UNKNOWN;
4789
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004790 if (**arg == '{')
4791 {
4792 // ->{lambda}()
4793 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4794 }
4795 else
4796 {
4797 // ->(lambda)()
4798 ++*arg;
4799 ret = eval1(arg, rettv, evalarg);
4800 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004801 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004802 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004803 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004804 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004805 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004806 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4807 && rettv->v_type != VAR_PARTIAL)
4808 {
4809 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4810 return FAIL;
4811 }
4812 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004813 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004814 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004815 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004816
4817 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004818 {
4819 if (verbose)
4820 {
4821 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004822 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004823 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004824 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004825 }
4826 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004827 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004828 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004829 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004830 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004831
4832 // Clear the funcref afterwards, so that deleting it while
4833 // evaluating the arguments is possible (see test55).
4834 if (evaluate)
4835 clear_tv(&base);
4836
4837 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004838}
4839
4840/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004841 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004842 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004843 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4844 */
4845 static int
4846eval_method(
4847 char_u **arg,
4848 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004849 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004851{
4852 char_u *name;
4853 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004854 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004855 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004856 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004857 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004858 int evaluate = evalarg != NULL
4859 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004860
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004861 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004862
Bram Moolenaarac92e252019-08-03 21:58:38 +02004863 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004864 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004865 if (alias != NULL)
4866 name = alias;
4867
4868 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004869 {
4870 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004871 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004872 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004873 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004874 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004875 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004876 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004877
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004878 // If there is no "(" immediately following, but there is further on,
4879 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4880 // Does not handle anything where "(" is part of the expression.
4881 *arg = skipwhite(*arg);
4882
4883 if (**arg != '(' && alias == NULL
4884 && (paren = vim_strchr(*arg, '(')) != NULL)
4885 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004886 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004887
4888 // Truncate the name a the "(". Avoid trying to get another line
4889 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004890 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004891 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4892 if (evalarg != NULL)
4893 {
4894 getline = evalarg->eval_getline;
4895 evalarg->eval_getline = NULL;
4896 }
4897
4898 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004899 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004900 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004901 *arg = name + len;
4902 ret = FAIL;
4903 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004904 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004905 {
4906 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004907 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004908 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004909
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004910 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004911 if (getline != NULL)
4912 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004913 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004914
4915 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004916 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004917 *arg = skipwhite(*arg);
4918
4919 if (**arg != '(')
4920 {
4921 if (verbose)
4922 semsg(_(e_missing_parenthesis_str), name);
4923 ret = FAIL;
4924 }
4925 else if (VIM_ISWHITE((*arg)[-1]))
4926 {
4927 if (verbose)
4928 emsg(_(e_no_white_space_allowed_before_parenthesis));
4929 ret = FAIL;
4930 }
4931 else
4932 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004933 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004934 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004935 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004936
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004937 // Clear the funcref afterwards, so that deleting it while
4938 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004939 if (evaluate)
4940 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004941 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004942
Bram Moolenaarac92e252019-08-03 21:58:38 +02004943 return ret;
4944}
4945
4946/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004947 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4948 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4950 */
4951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004952eval_index(
4953 char_u **arg,
4954 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004955 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004958 int evaluate = evalarg != NULL
4959 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004961 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004964 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004965 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004967 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4968 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004970 init_tv(&var1);
4971 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 /*
4975 * dict.name
4976 */
4977 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004978 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004980 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004982 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 }
4984 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 /*
4987 * something[idx]
4988 *
4989 * Get the (first) variable from inside the [].
4990 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004991 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 if (**arg == ':')
4993 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004994 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004996 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004997 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004998 semsg(_(e_white_space_required_before_and_after_str_at_str),
4999 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005000 clear_tv(&var1);
5001 return FAIL;
5002 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005003 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005004 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005005 int error = FALSE;
5006
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005007 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005008 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005009 && var1.v_type == VAR_FLOAT)
5010 {
5011 var1.vval.v_string = typval_tostring(&var1, TRUE);
5012 var1.v_type = VAR_STRING;
5013 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005014
Bram Moolenaar4525a572022-02-13 11:57:33 +00005015 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005016 tv_get_number_chk(&var1, &error);
5017 else
5018 error = tv_get_string_chk(&var1) == NULL;
5019 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005020 {
5021 // not a number or string
5022 clear_tv(&var1);
5023 return FAIL;
5024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005025 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026
5027 /*
5028 * Get the second variable from inside the [:].
5029 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005030 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 if (**arg == ':')
5032 {
5033 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005034 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005035 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005036 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005037 semsg(_(e_white_space_required_before_and_after_str_at_str),
5038 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005039 if (!empty1)
5040 clear_tv(&var1);
5041 return FAIL;
5042 }
5043 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 if (**arg == ']')
5045 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005046 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005048 if (!empty1)
5049 clear_tv(&var1);
5050 return FAIL;
5051 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005052 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005055 if (!empty1)
5056 clear_tv(&var1);
5057 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 return FAIL;
5059 }
5060 }
5061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005062 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005063 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 if (**arg != ']')
5065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005066 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005067 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 clear_tv(&var1);
5069 if (range)
5070 clear_tv(&var2);
5071 return FAIL;
5072 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005073 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 }
5075
5076 if (evaluate)
5077 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005078 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005079 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005080 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005081
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005082 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005085 clear_tv(&var2);
5086 return res;
5087 }
5088 return OK;
5089}
5090
5091/*
5092 * Check if "rettv" can have an [index] or [sli:ce]
5093 */
5094 int
5095check_can_index(typval_T *rettv, int evaluate, int verbose)
5096{
5097 switch (rettv->v_type)
5098 {
5099 case VAR_FUNC:
5100 case VAR_PARTIAL:
5101 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005102 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005103 return FAIL;
5104 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005105 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005106 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005107 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005108 case VAR_BOOL:
5109 case VAR_SPECIAL:
5110 case VAR_JOB:
5111 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005112 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005113 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005114 if (verbose)
5115 emsg(_(e_cannot_index_special_variable));
5116 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005117 case VAR_CLASS:
5118 case VAR_TYPEALIAS:
5119 if (verbose)
5120 check_typval_is_value(rettv);
5121 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005122 case VAR_UNKNOWN:
5123 case VAR_ANY:
5124 case VAR_VOID:
5125 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005127 emsg(_(e_cannot_index_special_variable));
5128 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005130 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005132 case VAR_STRING:
5133 case VAR_LIST:
5134 case VAR_DICT:
5135 case VAR_BLOB:
5136 break;
5137 case VAR_NUMBER:
5138 if (in_vim9script())
5139 emsg(_(e_cannot_index_number));
5140 break;
5141 }
5142 return OK;
5143}
5144
5145/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005146 * slice() function
5147 */
5148 void
5149f_slice(typval_T *argvars, typval_T *rettv)
5150{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005151 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005152 && ((argvars[0].v_type != VAR_STRING
5153 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005154 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005155 && check_for_list_arg(argvars, 0) == FAIL)
5156 || check_for_number_arg(argvars, 1) == FAIL
5157 || check_for_opt_number_arg(argvars, 2) == FAIL))
5158 return;
5159
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005160 if (check_can_index(argvars, TRUE, FALSE) != OK)
5161 return;
5162
5163 copy_tv(argvars, rettv);
5164 eval_index_inner(rettv, TRUE, argvars + 1,
5165 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5166 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005167}
5168
5169/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005170 * Apply index or range to "rettv".
5171 * "var1" is the first index, NULL for [:expr].
5172 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005173 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5174 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005175 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5176 */
5177 int
5178eval_index_inner(
5179 typval_T *rettv,
5180 int is_range,
5181 typval_T *var1,
5182 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005183 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005184 char_u *key,
5185 int keylen,
5186 int verbose)
5187{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005188 varnumber_T n1, n2 = 0;
5189 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005190
5191 n1 = 0;
5192 if (var1 != NULL && rettv->v_type != VAR_DICT)
5193 n1 = tv_get_number(var1);
5194
5195 if (is_range)
5196 {
5197 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005199 if (verbose)
5200 emsg(_(e_cannot_slice_dictionary));
5201 return FAIL;
5202 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005203 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005204 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005205 else
5206 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005207 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005208
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005209 switch (rettv->v_type)
5210 {
5211 case VAR_UNKNOWN:
5212 case VAR_ANY:
5213 case VAR_VOID:
5214 case VAR_FUNC:
5215 case VAR_PARTIAL:
5216 case VAR_FLOAT:
5217 case VAR_BOOL:
5218 case VAR_SPECIAL:
5219 case VAR_JOB:
5220 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005221 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005222 case VAR_CLASS:
5223 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005224 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005225 break; // not evaluating, skipping over subscript
5226
5227 case VAR_NUMBER:
5228 case VAR_STRING:
5229 {
5230 char_u *s = tv_get_string(rettv);
5231
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005233 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005234 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005235 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005236 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005237 else
5238 s = char_from_string(s, n1);
5239 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005240 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005242 // The resulting variable is a substring. If the indexes
5243 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 if (n1 < 0)
5245 {
5246 n1 = len + n1;
5247 if (n1 < 0)
5248 n1 = 0;
5249 }
5250 if (n2 < 0)
5251 n2 = len + n2;
5252 else if (n2 >= len)
5253 n2 = len;
5254 if (n1 >= len || n2 < 0 || n1 > n2)
5255 s = NULL;
5256 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005257 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 }
5259 else
5260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005261 // The resulting variable is a string of a single
5262 // character. If the index is too big or negative the
5263 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 if (n1 >= len || n1 < 0)
5265 s = NULL;
5266 else
5267 s = vim_strnsave(s + n1, 1);
5268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 clear_tv(rettv);
5270 rettv->v_type = VAR_STRING;
5271 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005272 }
5273 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005275 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005276 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5277 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005278 break;
5279
5280 case VAR_LIST:
5281 if (var1 == NULL)
5282 n1 = 0;
5283 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005284 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005285 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005286 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005287 return FAIL;
5288 break;
5289
5290 case VAR_DICT:
5291 {
5292 dictitem_T *item;
5293 typval_T tmp;
5294
5295 if (key == NULL)
5296 {
5297 key = tv_get_string_chk(var1);
5298 if (key == NULL)
5299 return FAIL;
5300 }
5301
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005302 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005303
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005304 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005305 {
5306 if (verbose)
5307 {
5308 if (keylen > 0)
5309 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005310 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005311 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005312 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005313 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005314
5315 copy_tv(&item->di_tv, &tmp);
5316 clear_tv(rettv);
5317 *rettv = tmp;
5318 }
5319 break;
5320 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 return OK;
5322}
5323
5324/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005325 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005326 */
5327 char_u *
5328partial_name(partial_T *pt)
5329{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005330 if (pt != NULL)
5331 {
5332 if (pt->pt_name != NULL)
5333 return pt->pt_name;
5334 if (pt->pt_func != NULL)
5335 return pt->pt_func->uf_name;
5336 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005337 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005338}
5339
Bram Moolenaarddecc252016-04-06 22:59:37 +02005340 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005341partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005342{
5343 int i;
5344
5345 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005346 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005347 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005348 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005349 if (pt->pt_name != NULL)
5350 {
5351 func_unref(pt->pt_name);
5352 vim_free(pt->pt_name);
5353 }
5354 else
5355 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005356 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005357
Bram Moolenaar54656012021-06-09 20:50:46 +02005358 // "out_up" is no longer used, decrement refcount on partial that owns it.
5359 partial_unref(pt->pt_outer.out_up_partial);
5360
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005361 // Using pt_outer from another partial.
5362 partial_unref(pt->pt_outer_partial);
5363
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005364 // Decrease the reference count for the context of a closure. If down
5365 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005366 if (pt->pt_funcstack != NULL)
5367 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005368 --pt->pt_funcstack->fs_refcount;
5369 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005370 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005371 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005372 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5373 if (pt->pt_loopvars[i] != NULL)
5374 {
5375 --pt->pt_loopvars[i]->lvs_refcount;
5376 loopvars_check_refcount(pt->pt_loopvars[i]);
5377 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005378
Bram Moolenaarddecc252016-04-06 22:59:37 +02005379 vim_free(pt);
5380}
5381
5382/*
5383 * Unreference a closure: decrement the reference count and free it when it
5384 * becomes zero.
5385 */
5386 void
5387partial_unref(partial_T *pt)
5388{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005389 if (pt == NULL)
5390 return;
5391
5392 int done = FALSE;
5393
5394 if (--pt->pt_refcount <= 0)
5395 partial_free(pt);
5396
5397 // If the reference count goes down to one, the funcstack may be the
5398 // only reference and can be freed if no other partials reference it.
5399 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005400 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005401 // careful: if the funcstack is freed it may contain this partial
5402 // and it gets freed as well
5403 if (pt->pt_funcstack != NULL)
5404 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005405
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005406 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005407 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005408 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005409
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005410 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5411 if (pt->pt_loopvars[depth] != NULL
5412 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005413 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005414 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005415 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005416}
5417
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005418/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005419 * Return the next (unique) copy ID.
5420 * Used for serializing nested structures.
5421 */
5422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005424{
5425 current_copyID += COPYID_INC;
5426 return current_copyID;
5427}
5428
5429/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430 * Garbage collection for lists and dictionaries.
5431 *
5432 * We use reference counts to be able to free most items right away when they
5433 * are no longer used. But for composite items it's possible that it becomes
5434 * unused while the reference count is > 0: When there is a recursive
5435 * reference. Example:
5436 * :let l = [1, 2, 3]
5437 * :let d = {9: l}
5438 * :let l[1] = d
5439 *
5440 * Since this is quite unusual we handle this with garbage collection: every
5441 * once in a while find out which lists and dicts are not referenced from any
5442 * variable.
5443 *
5444 * Here is a good reference text about garbage collection (refers to Python
5445 * but it applies to all reference-counting mechanisms):
5446 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005447 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005448
5449/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005450 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005451 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005452 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005453 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005454 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005455garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005456{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005457 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005459 buf_T *buf;
5460 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005461 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005462 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005463
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005464 if (!testing)
5465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005467 want_garbage_collect = FALSE;
5468 may_garbage_collect = FALSE;
5469 garbage_collect_at_exit = FALSE;
5470 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005471
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005472 // The execution stack can grow big, limit the size.
5473 if (exestack.ga_maxlen - exestack.ga_len > 500)
5474 {
5475 size_t new_len;
5476 char_u *pp;
5477 int n;
5478
5479 // Keep 150% of the current size, with a minimum of the growth size.
5480 n = exestack.ga_len / 2;
5481 if (n < exestack.ga_growsize)
5482 n = exestack.ga_growsize;
5483
5484 // Don't make it bigger though.
5485 if (exestack.ga_len + n < exestack.ga_maxlen)
5486 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005487 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005488 pp = vim_realloc(exestack.ga_data, new_len);
5489 if (pp == NULL)
5490 return FAIL;
5491 exestack.ga_maxlen = exestack.ga_len + n;
5492 exestack.ga_data = pp;
5493 }
5494 }
5495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005496 // We advance by two because we add one for items referenced through
5497 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005498 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005499
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005500 /*
5501 * 1. Go through all accessible variables and mark all lists and dicts
5502 * with copyID.
5503 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 // Don't free variables in the previous_funccal list unless they are only
5506 // referenced through previous_funccal. This must be first, because if
5507 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005511 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005513 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005514 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5516 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005518 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005519 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005520 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5521 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005522 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005523 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005524 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005525 abort = abort || set_ref_in_item(
5526 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005527#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005528 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005529 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5530 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005531 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005532 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005533 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5534 NULL, NULL);
5535#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005537 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005538 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005539 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5540 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005542 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005544 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005545 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005548 abort = abort || set_ref_in_functions(copyID);
5549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005551 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005552
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005553 // funcstacks keep variables for closures
5554 abort = abort || set_ref_in_funcstacks(copyID);
5555
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005556 // loopvars keep variables for loop blocks
5557 abort = abort || set_ref_in_loopvars(copyID);
5558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005560 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005561
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005562 // callbacks in buffers
5563 abort = abort || set_ref_in_buffers(copyID);
5564
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005565 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5566 abort = abort || set_ref_in_insexpand_funcs(copyID);
5567
5568 // 'operatorfunc' callback
5569 abort = abort || set_ref_in_opfunc(copyID);
5570
5571 // 'tagfunc' callback
5572 abort = abort || set_ref_in_tagfunc(copyID);
5573
5574 // 'imactivatefunc' and 'imstatusfunc' callbacks
5575 abort = abort || set_ref_in_im_funcs(copyID);
5576
Bram Moolenaar1dced572012-04-05 16:54:08 +02005577#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005578 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005579#endif
5580
Bram Moolenaardb913952012-06-29 12:54:53 +02005581#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005583#endif
5584
5585#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005587#endif
5588
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005589#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005590 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005591 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005592#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005593#ifdef FEAT_NETBEANS_INTG
5594 abort = abort || set_ref_in_nb_channel(copyID);
5595#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005596
Bram Moolenaare3188e22016-05-31 21:13:04 +02005597#ifdef FEAT_TIMERS
5598 abort = abort || set_ref_in_timer(copyID);
5599#endif
5600
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005601#ifdef FEAT_QUICKFIX
5602 abort = abort || set_ref_in_quickfix(copyID);
5603#endif
5604
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005605#ifdef FEAT_TERMINAL
5606 abort = abort || set_ref_in_term(copyID);
5607#endif
5608
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005609#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005610 abort = abort || set_ref_in_popups(copyID);
5611#endif
5612
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005613 abort = abort || set_ref_in_classes(copyID);
5614
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005616 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 /*
5618 * 2. Free lists and dictionaries that are not referenced.
5619 */
5620 did_free = free_unref_items(copyID);
5621
5622 /*
5623 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005625 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005626 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005627 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005628 else if (p_verbose > 0)
5629 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005630 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005631 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005632
5633 return did_free;
5634}
5635
5636/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005637 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005638 */
5639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005640free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005641{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005642 int did_free = FALSE;
5643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005644 // Let all "free" functions know that we are here. This means no
5645 // dictionaries, lists, channels or jobs are to be freed, because we will
5646 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005647 in_free_unref_items = TRUE;
5648
5649 /*
5650 * PASS 1: free the contents of the items. We don't free the items
5651 * themselves yet, so that it is possible to decrement refcount counters
5652 */
5653
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005654 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005655 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005656
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005657 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005658 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005659
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005660 // Go through the list of objects and free items without this copyID.
5661 did_free |= object_free_nonref(copyID);
5662
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005663 // Go through the list of classes and free items without this copyID.
5664 did_free |= class_free_nonref(copyID);
5665
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005666#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // Go through the list of jobs and free items without the copyID. This
5668 // must happen before doing channels, because jobs refer to channels, but
5669 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005670 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005672 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005673 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5674#endif
5675
5676 /*
5677 * PASS 2: free the items themselves.
5678 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005679 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005680 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005681 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005682
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005683#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005684 // Go through the list of jobs and free items without the copyID. This
5685 // must happen before doing channels, because jobs refer to channels, but
5686 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005687 free_unused_jobs(copyID, COPYID_MASK);
5688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005689 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005690 free_unused_channels(copyID, COPYID_MASK);
5691#endif
5692
5693 in_free_unref_items = FALSE;
5694
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005695 return did_free;
5696}
5697
5698/*
5699 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005700 * "list_stack" is used to add lists to be marked. Can be NULL.
5701 *
5702 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005703 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005706{
5707 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005708 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005709 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005710 hashtab_T *cur_ht;
5711 ht_stack_T *ht_stack = NULL;
5712 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005713
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005714 cur_ht = ht;
5715 for (;;)
5716 {
5717 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005719 // Mark each item in the hashtab. If the item contains a hashtab
5720 // it is added to ht_stack, if it contains a list it is added to
5721 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005723 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005724 if (!HASHITEM_EMPTY(hi))
5725 {
5726 --todo;
5727 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5728 &ht_stack, list_stack);
5729 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005730 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005731
5732 if (ht_stack == NULL)
5733 break;
5734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005735 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 cur_ht = ht_stack->ht;
5737 tempitem = ht_stack;
5738 ht_stack = ht_stack->prev;
5739 free(tempitem);
5740 }
5741
5742 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005743}
5744
Dominique Pelle748b3082022-01-08 12:41:16 +00005745#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5746 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005747/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005748 * Mark a dict and its items with "copyID".
5749 * Returns TRUE if setting references failed somehow.
5750 */
5751 int
5752set_ref_in_dict(dict_T *d, int copyID)
5753{
5754 if (d != NULL && d->dv_copyID != copyID)
5755 {
5756 d->dv_copyID = copyID;
5757 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5758 }
5759 return FALSE;
5760}
Dominique Pelle748b3082022-01-08 12:41:16 +00005761#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005762
5763/*
5764 * Mark a list and its items with "copyID".
5765 * Returns TRUE if setting references failed somehow.
5766 */
5767 int
5768set_ref_in_list(list_T *ll, int copyID)
5769{
5770 if (ll != NULL && ll->lv_copyID != copyID)
5771 {
5772 ll->lv_copyID = copyID;
5773 return set_ref_in_list_items(ll, copyID, NULL);
5774 }
5775 return FALSE;
5776}
5777
5778/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005780 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5781 *
5782 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005783 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005784 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005785set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005786{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005787 listitem_T *li;
5788 int abort = FALSE;
5789 list_T *cur_l;
5790 list_stack_T *list_stack = NULL;
5791 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005792
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005793 cur_l = l;
5794 for (;;)
5795 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005796 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005797 // Mark each item in the list. If the item contains a hashtab
5798 // it is added to ht_stack, if it contains a list it is added to
5799 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005800 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5801 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5802 ht_stack, &list_stack);
5803 if (list_stack == NULL)
5804 break;
5805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005807 cur_l = list_stack->list;
5808 tempitem = list_stack;
5809 list_stack = list_stack->prev;
5810 free(tempitem);
5811 }
5812
5813 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005814}
5815
5816/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005817 * Mark the partial in callback 'cb' with "copyID".
5818 */
5819 int
5820set_ref_in_callback(callback_T *cb, int copyID)
5821{
5822 typval_T tv;
5823
5824 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5825 return FALSE;
5826
5827 tv.v_type = VAR_PARTIAL;
5828 tv.vval.v_partial = cb->cb_partial;
5829 return set_ref_in_item(&tv, copyID, NULL, NULL);
5830}
5831
5832/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005833 * Mark the dict "dd" with "copyID".
5834 * Also see set_ref_in_item().
5835 */
5836 static int
5837set_ref_in_item_dict(
5838 dict_T *dd,
5839 int copyID,
5840 ht_stack_T **ht_stack,
5841 list_stack_T **list_stack)
5842{
5843 if (dd == NULL || dd->dv_copyID == copyID)
5844 return FALSE;
5845
5846 // Didn't see this dict yet.
5847 dd->dv_copyID = copyID;
5848 if (ht_stack == NULL)
5849 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5850
5851 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5852 if (newitem == NULL)
5853 return TRUE;
5854
5855 newitem->ht = &dd->dv_hashtab;
5856 newitem->prev = *ht_stack;
5857 *ht_stack = newitem;
5858
5859 return FALSE;
5860}
5861
5862/*
5863 * Mark the list "ll" with "copyID".
5864 * Also see set_ref_in_item().
5865 */
5866 static int
5867set_ref_in_item_list(
5868 list_T *ll,
5869 int copyID,
5870 ht_stack_T **ht_stack,
5871 list_stack_T **list_stack)
5872{
5873 if (ll == NULL || ll->lv_copyID == copyID)
5874 return FALSE;
5875
5876 // Didn't see this list yet.
5877 ll->lv_copyID = copyID;
5878 if (list_stack == NULL)
5879 return set_ref_in_list_items(ll, copyID, ht_stack);
5880
5881 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5882 if (newitem == NULL)
5883 return TRUE;
5884
5885 newitem->list = ll;
5886 newitem->prev = *list_stack;
5887 *list_stack = newitem;
5888
5889 return FALSE;
5890}
5891
5892/*
5893 * Mark the partial "pt" with "copyID".
5894 * Also see set_ref_in_item().
5895 */
5896 static int
5897set_ref_in_item_partial(
5898 partial_T *pt,
5899 int copyID,
5900 ht_stack_T **ht_stack,
5901 list_stack_T **list_stack)
5902{
5903 if (pt == NULL || pt->pt_copyID == copyID)
5904 return FALSE;
5905
5906 // Didn't see this partial yet.
5907 pt->pt_copyID = copyID;
5908
5909 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5910
5911 if (pt->pt_dict != NULL)
5912 {
5913 typval_T dtv;
5914
5915 dtv.v_type = VAR_DICT;
5916 dtv.vval.v_dict = pt->pt_dict;
5917 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5918 }
5919
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005920 if (pt->pt_obj != NULL)
5921 {
5922 typval_T objtv;
5923
5924 objtv.v_type = VAR_OBJECT;
5925 objtv.vval.v_object = pt->pt_obj;
5926 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5927 }
5928
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005929 for (int i = 0; i < pt->pt_argc; ++i)
5930 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5931 ht_stack, list_stack);
5932 // pt_funcstack is handled in set_ref_in_funcstacks()
5933 // pt_loopvars is handled in set_ref_in_loopvars()
5934
5935 return abort;
5936}
5937
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005938#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005939/*
5940 * Mark the job "pt" with "copyID".
5941 * Also see set_ref_in_item().
5942 */
5943 static int
5944set_ref_in_item_job(
5945 job_T *job,
5946 int copyID,
5947 ht_stack_T **ht_stack,
5948 list_stack_T **list_stack)
5949{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005950 typval_T dtv;
5951
5952 if (job == NULL || job->jv_copyID == copyID)
5953 return FALSE;
5954
5955 job->jv_copyID = copyID;
5956 if (job->jv_channel != NULL)
5957 {
5958 dtv.v_type = VAR_CHANNEL;
5959 dtv.vval.v_channel = job->jv_channel;
5960 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5961 }
5962 if (job->jv_exit_cb.cb_partial != NULL)
5963 {
5964 dtv.v_type = VAR_PARTIAL;
5965 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5966 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5967 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005968
5969 return FALSE;
5970}
5971
5972/*
5973 * Mark the channel "ch" with "copyID".
5974 * Also see set_ref_in_item().
5975 */
5976 static int
5977set_ref_in_item_channel(
5978 channel_T *ch,
5979 int copyID,
5980 ht_stack_T **ht_stack,
5981 list_stack_T **list_stack)
5982{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005983 typval_T dtv;
5984
5985 if (ch == NULL || ch->ch_copyID == copyID)
5986 return FALSE;
5987
5988 ch->ch_copyID = copyID;
5989 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5990 {
5991 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5992 jq != NULL; jq = jq->jq_next)
5993 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5994 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5995 cq = cq->cq_next)
5996 if (cq->cq_callback.cb_partial != NULL)
5997 {
5998 dtv.v_type = VAR_PARTIAL;
5999 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6000 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6001 }
6002 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6003 {
6004 dtv.v_type = VAR_PARTIAL;
6005 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6006 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6007 }
6008 }
6009 if (ch->ch_callback.cb_partial != NULL)
6010 {
6011 dtv.v_type = VAR_PARTIAL;
6012 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6013 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6014 }
6015 if (ch->ch_close_cb.cb_partial != NULL)
6016 {
6017 dtv.v_type = VAR_PARTIAL;
6018 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6019 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6020 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006021
6022 return FALSE;
6023}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006024#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006025
6026/*
6027 * Mark the class "cl" with "copyID".
6028 * Also see set_ref_in_item().
6029 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006030 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006031set_ref_in_item_class(
6032 class_T *cl,
6033 int copyID,
6034 ht_stack_T **ht_stack,
6035 list_stack_T **list_stack)
6036{
6037 int abort = FALSE;
6038
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006039 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006040 return FALSE;
6041
6042 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006043 if (cl->class_members_tv != NULL)
6044 {
6045 // The "class_members_tv" table is allocated only for regular classes
6046 // and not for interfaces.
6047 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6048 abort = abort || set_ref_in_item(
6049 &cl->class_members_tv[i],
6050 copyID, ht_stack, list_stack);
6051 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006052
6053 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6054 abort = abort || set_ref_in_func(NULL,
6055 cl->class_class_functions[i], copyID);
6056
6057 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6058 abort = abort || set_ref_in_func(NULL,
6059 cl->class_obj_methods[i], copyID);
6060
6061 return abort;
6062}
6063
6064/*
6065 * Mark the object "cl" with "copyID".
6066 * Also see set_ref_in_item().
6067 */
6068 static int
6069set_ref_in_item_object(
6070 object_T *obj,
6071 int copyID,
6072 ht_stack_T **ht_stack,
6073 list_stack_T **list_stack)
6074{
6075 int abort = FALSE;
6076
6077 if (obj == NULL || obj->obj_copyID == copyID)
6078 return FALSE;
6079
6080 obj->obj_copyID = copyID;
6081
6082 // The typval_T array is right after the object_T.
6083 typval_T *mtv = (typval_T *)(obj + 1);
6084 for (int i = 0; !abort
6085 && i < obj->obj_class->class_obj_member_count; ++i)
6086 abort = abort || set_ref_in_item(mtv + i, copyID,
6087 ht_stack, list_stack);
6088
6089 return abort;
6090}
6091
6092/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006093 * Mark all lists, dicts and other container types referenced through typval
6094 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006095 * "list_stack" is used to add lists to be marked. Can be NULL.
6096 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6097 *
6098 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006099 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006101set_ref_in_item(
6102 typval_T *tv,
6103 int copyID,
6104 ht_stack_T **ht_stack,
6105 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006107 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006109 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006111 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006112 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6113 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006114
6115 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006116 return set_ref_in_item_list(tv->vval.v_list, copyID,
6117 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006118
6119 case VAR_FUNC:
6120 {
6121 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6122 break;
6123 }
6124
6125 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006126 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6127 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006128
6129 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006130#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006131 return set_ref_in_item_job(tv->vval.v_job, copyID,
6132 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006133#else
6134 break;
6135#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006136
6137 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006138#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006139 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006140 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006141#else
6142 break;
6143#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006144
6145 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006146 return set_ref_in_item_class(tv->vval.v_class, copyID,
6147 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006148
6149 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006150 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006151 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006152
6153 case VAR_UNKNOWN:
6154 case VAR_ANY:
6155 case VAR_VOID:
6156 case VAR_BOOL:
6157 case VAR_SPECIAL:
6158 case VAR_NUMBER:
6159 case VAR_FLOAT:
6160 case VAR_STRING:
6161 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006162 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006163 case VAR_INSTR:
6164 // Types that do not contain any other item
6165 break;
6166 }
6167
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006168 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006169}
6170
Bram Moolenaar8c711452005-01-14 21:53:12 +00006171/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 * Return a string with the string representation of a variable.
6173 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006175 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006176 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006177 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006178 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006179 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6180 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006181 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006183 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006184echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006185 typval_T *tv,
6186 char_u **tofree,
6187 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006188 int copyID,
6189 int echo_style,
6190 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006191 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193 static int recurse = 0;
6194 char_u *r = NULL;
6195
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006198 if (!did_echo_string_emsg)
6199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006200 // Only give this message once for a recursive call to avoid
6201 // flooding the user with errors. And stop iterating over lists
6202 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006203 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006204 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006207 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 }
6209 ++recurse;
6210
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006211 switch (tv->v_type)
6212 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006213 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006214 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006215 {
6216 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006217 r = tv->vval.v_string;
6218 if (r == NULL)
6219 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006220 }
6221 else
6222 {
6223 *tofree = string_quote(tv->vval.v_string, FALSE);
6224 r = *tofree;
6225 }
6226 break;
6227
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006229 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006230 char_u buf[MAX_FUNC_NAME_LEN];
6231
6232 if (echo_style)
6233 {
LemonBoya5d35902022-04-29 21:15:02 +01006234 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6235 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006236 buf, MAX_FUNC_NAME_LEN);
6237 if (r == buf)
6238 {
6239 r = vim_strsave(buf);
6240 *tofree = r;
6241 }
6242 else
6243 *tofree = NULL;
6244 }
6245 else
6246 {
6247 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6248 : make_ufunc_name_readable(
6249 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6250 TRUE);
6251 r = *tofree;
6252 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006254 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006255
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006256 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006257 {
6258 partial_T *pt = tv->vval.v_partial;
6259 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006260 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006261 garray_T ga;
6262 int i;
6263 char_u *tf;
6264
6265 ga_init2(&ga, 1, 100);
6266 ga_concat(&ga, (char_u *)"function(");
6267 if (fname != NULL)
6268 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006269 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006270 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006271 && vim_isupper(fname[1]))
6272 {
6273 ga_concat(&ga, (char_u *)"'g:");
6274 ga_concat(&ga, fname + 1);
6275 }
6276 else
6277 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006278 vim_free(fname);
6279 }
6280 if (pt != NULL && pt->pt_argc > 0)
6281 {
6282 ga_concat(&ga, (char_u *)", [");
6283 for (i = 0; i < pt->pt_argc; ++i)
6284 {
6285 if (i > 0)
6286 ga_concat(&ga, (char_u *)", ");
6287 ga_concat(&ga,
6288 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6289 vim_free(tf);
6290 }
6291 ga_concat(&ga, (char_u *)"]");
6292 }
6293 if (pt != NULL && pt->pt_dict != NULL)
6294 {
6295 typval_T dtv;
6296
6297 ga_concat(&ga, (char_u *)", ");
6298 dtv.v_type = VAR_DICT;
6299 dtv.vval.v_dict = pt->pt_dict;
6300 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6301 vim_free(tf);
6302 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006303 // terminate with ')' and a NUL
6304 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006305
6306 *tofree = ga.ga_data;
6307 r = *tofree;
6308 break;
6309 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006310
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006311 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006312 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006313 break;
6314
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006316 if (tv->vval.v_list == NULL)
6317 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006318 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006319 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006320 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006321 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006322 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6323 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006324 {
6325 *tofree = NULL;
6326 r = (char_u *)"[...]";
6327 }
6328 else
6329 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006330 int old_copyID = tv->vval.v_list->lv_copyID;
6331
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006332 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006333 *tofree = list2string(tv, copyID, restore_copyID);
6334 if (restore_copyID)
6335 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006336 r = *tofree;
6337 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006338 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006339
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006341 if (tv->vval.v_dict == NULL)
6342 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006343 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006344 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006345 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006346 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006347 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6348 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006349 {
6350 *tofree = NULL;
6351 r = (char_u *)"{...}";
6352 }
6353 else
6354 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006355 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006356
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006357 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006358 *tofree = dict2string(tv, copyID, restore_copyID);
6359 if (restore_copyID)
6360 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006361 r = *tofree;
6362 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006365 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006366 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006367 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006369 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006370 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006371 break;
6372
Bram Moolenaar835dc632016-02-07 14:27:38 +01006373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006374 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006375#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006376 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006377 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6378 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006379 if (composite_val)
6380 {
6381 *tofree = string_quote(r, FALSE);
6382 r = *tofree;
6383 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006384#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006386
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006387 case VAR_INSTR:
6388 *tofree = NULL;
6389 r = (char_u *)"instructions";
6390 break;
6391
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006392 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006393 {
6394 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006395 char *s = "class";
6396 if (IS_INTERFACE(cl))
6397 s = "interface";
6398 else if (IS_ENUM(cl))
6399 s = "enum";
6400 size_t len = STRLEN(s) + 1 +
6401 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006402 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006403 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006404 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6405 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006406 break;
6407
6408 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006409 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6410 echo_style, restore_copyID,
6411 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006412 break;
6413
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006414 case VAR_FLOAT:
6415 *tofree = NULL;
6416 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6417 r = numbuf;
6418 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006419
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006420 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006421 case VAR_SPECIAL:
6422 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006423 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006424 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006425
6426 case VAR_TYPEALIAS:
6427 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6428 r = *tofree;
6429 if (r == NULL)
6430 r = (char_u *)"";
6431 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433
Bram Moolenaar8502c702014-06-17 12:51:16 +02006434 if (--recurse == 0)
6435 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006437}
6438
6439/*
6440 * Return a string with the string representation of a variable.
6441 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6442 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006443 * Does not put quotes around strings, as ":echo" displays values.
6444 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6445 * May return NULL.
6446 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006447 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006448echo_string(
6449 typval_T *tv,
6450 char_u **tofree,
6451 char_u *numbuf,
6452 int copyID)
6453{
6454 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6455}
6456
6457/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006458 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6459 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006460 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006461 */
6462 int
6463buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6464{
6465 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006466 char_u *t;
6467 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006468
6469 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6470 return -1;
6471
6472 if (lnum > buf->b_ml.ml_line_count)
6473 lnum = buf->b_ml.ml_line_count;
6474
6475 str = ml_get_buf(buf, lnum, FALSE);
6476 if (str == NULL)
6477 return -1;
6478
6479 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006480 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006481
Bram Moolenaar91458462021-01-13 20:08:38 +01006482 // count the number of characters
6483 t = str;
6484 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6485 t += mb_ptr2len(t);
6486
6487 // In insert mode, when the cursor is at the end of a non-empty line,
6488 // byteidx points to the NUL character immediately past the end of the
6489 // string. In this case, add one to the character count.
6490 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6491 count++;
6492
6493 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006494}
6495
6496/*
6497 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006498 * byte index. Works only for loaded buffers. Returns -1 on failure.
6499 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006500 */
6501 int
6502buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6503{
6504 char_u *str;
6505 char_u *t;
6506
6507 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6508 return -1;
6509
6510 if (lnum > buf->b_ml.ml_line_count)
6511 lnum = buf->b_ml.ml_line_count;
6512
6513 str = ml_get_buf(buf, lnum, FALSE);
6514 if (str == NULL)
6515 return -1;
6516
6517 // Convert the character offset to a byte offset
6518 t = str;
6519 while (*t != NUL && --charidx > 0)
6520 t += mb_ptr2len(t);
6521
Bram Moolenaar91458462021-01-13 20:08:38 +01006522 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006523}
6524
6525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006527 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006530var2fpos(
6531 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006533 int *fnum, // set to fnum for '0, 'A, etc.
6534 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006536 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006538 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006540 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006541 if (varp->v_type == VAR_LIST)
6542 {
6543 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006544 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006545 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006546 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006547
6548 l = varp->vval.v_list;
6549 if (l == NULL)
6550 return NULL;
6551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006552 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006553 pos.lnum = list_find_nr(l, 0L, &error);
6554 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006555 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006556 if (charcol)
6557 len = (long)mb_charlen(ml_get(pos.lnum));
6558 else
John Marriottbfcc8952024-03-11 22:04:45 +01006559 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006560
Bram Moolenaarec65d772020-08-20 22:29:12 +02006561 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006562 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006563 li = list_find(l, 1L);
6564 if (li != NULL && li->li_tv.v_type == VAR_STRING
6565 && li->li_tv.vval.v_string != NULL
6566 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006567 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006568 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006569 }
6570 else
6571 {
6572 pos.col = list_find_nr(l, 1L, &error);
6573 if (error)
6574 return NULL;
6575 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006578 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006580 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006581
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006582 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006583 pos.coladd = list_find_nr(l, 2L, &error);
6584 if (error)
6585 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006586
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006587 return &pos;
6588 }
6589
Bram Moolenaarc5809432021-03-27 21:23:30 +01006590 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6591 return NULL;
6592
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006593 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006594 if (name == NULL)
6595 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006596
6597 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006598 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006599 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006600 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006601 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006602 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006603 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006604 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006605 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006606 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006607 pos = VIsual;
6608 else
6609 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006610 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006611 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006612 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006614 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006615 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6617 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006618 pos = *pp;
6619 }
6620 if (pos.lnum != 0)
6621 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006622 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006623 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6624 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006626
Bram Moolenaara5525202006-03-02 22:52:09 +00006627 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006628
Bram Moolenaar477933c2007-07-17 14:32:23 +00006629 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006630 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006631 // the "w_valid" flags are not reset when moving the cursor, but they
6632 // do matter for update_topline() and validate_botline().
6633 check_cursor_moved(curwin);
6634
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006635 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006636 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006637 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006638 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006639 // In silent Ex mode topline is zero, but that's not a valid line
6640 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006641 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006642 return &pos;
6643 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006644 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006645 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006646 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006647 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006648 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006649 return &pos;
6650 }
6651 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006652 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006654 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 pos.lnum = curbuf->b_ml.ml_line_count;
6657 pos.col = 0;
6658 }
6659 else
6660 {
6661 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006662 if (charcol)
6663 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6664 else
John Marriottbfcc8952024-03-11 22:04:45 +01006665 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667 return &pos;
6668 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006669 if (in_vim9script())
6670 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 return NULL;
6672}
6673
6674/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006675 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006676 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006677 * Note that the column is passed on as-is, the caller may want to decrement
6678 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006679 * If "charcol" is TRUE use the column as the character index instead of the
6680 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006681 * Return FAIL when conversion is not possible, doesn't check the position for
6682 * validity.
6683 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list2fpos(
6686 typval_T *arg,
6687 pos_T *posp,
6688 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006689 colnr_T *curswantp,
6690 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006691{
6692 list_T *l = arg->vval.v_list;
6693 long i = 0;
6694 long n;
6695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006696 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6697 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006698 if (arg->v_type != VAR_LIST
6699 || l == NULL
6700 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006701 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006702 return FAIL;
6703
6704 if (fnump != NULL)
6705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006706 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006707 if (n < 0)
6708 return FAIL;
6709 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006710 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006711 *fnump = n;
6712 }
6713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006714 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006715 if (n < 0)
6716 return FAIL;
6717 posp->lnum = n;
6718
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006719 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006720 if (n < 0)
6721 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006722 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006723 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006724 if (charcol)
6725 {
6726 buf_T *buf;
6727
6728 // Get the text for the specified line in a loaded buffer
6729 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6730 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6731 return FAIL;
6732
Bram Moolenaar79f23442022-10-10 12:42:57 +01006733 n = buf_charidx_to_byteidx(buf,
6734 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006735 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006736 posp->col = n;
6737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006738 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006739 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006740 posp->coladd = 0;
6741 else
6742 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006743
Bram Moolenaar493c1782014-05-28 14:34:46 +02006744 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006745 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006746
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006747 return OK;
6748}
6749
6750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 * Get the length of an environment variable name.
6752 * Advance "arg" to the first character after the name.
6753 * Return 0 for error.
6754 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006756get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 char_u *p;
6759 int len;
6760
6761 for (p = *arg; vim_isIDc(*p); ++p)
6762 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006763 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 return 0;
6765
6766 len = (int)(p - *arg);
6767 *arg = p;
6768 return len;
6769}
6770
6771/*
6772 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006773 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 * Return 0 if something is wrong.
6775 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006777get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 char_u *p;
6780 int len;
6781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006782 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006784 {
6785 if (*p == ':')
6786 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006787 // "s:" is start of "s:var", but "n:" is not and can be used in
6788 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006789 len = (int)(p - *arg);
6790 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6791 || len > 1)
6792 break;
6793 }
6794 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006795 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 return 0;
6797
6798 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006799 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
6801 return len;
6802}
6803
6804/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006805 * Get the length of the name of a variable or function.
6806 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006808 * Return -1 if curly braces expansion failed.
6809 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 * If the name contains 'magic' {}'s, expand them and return the
6811 * expanded name in an allocated string via 'alias' - caller must free.
6812 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814get_name_len(
6815 char_u **arg,
6816 char_u **alias,
6817 int evaluate,
6818 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 char_u *p;
6822 char_u *expr_start;
6823 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006825 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
6827 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6828 && (*arg)[2] == (int)KE_SNR)
6829 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006830 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 *arg += 3;
6832 return get_id_len(arg) + 3;
6833 }
6834 len = eval_fname_script(*arg);
6835 if (len > 0)
6836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006837 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 *arg += len;
6839 }
6840
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006844 p = find_name_end(*arg, &expr_start, &expr_end,
6845 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (expr_start != NULL)
6847 {
6848 char_u *temp_string;
6849
6850 if (!evaluate)
6851 {
6852 len += (int)(p - *arg);
6853 *arg = skipwhite(p);
6854 return len;
6855 }
6856
6857 /*
6858 * Include any <SID> etc in the expanded string:
6859 * Thus the -len here.
6860 */
6861 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6862 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006863 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 *alias = temp_string;
6865 *arg = skipwhite(p);
6866 return (int)STRLEN(temp_string);
6867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868
6869 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006870 // Only give an error when there is something, otherwise it will be
6871 // reported at a higher level.
6872 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006873 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
6875 return len;
6876}
6877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878/*
6879 * Find the end of a variable or function name, taking care of magic braces.
6880 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6881 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006882 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883 * Return a pointer to just after the name. Equal to "arg" if there is no
6884 * valid name.
6885 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006886 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006887find_name_end(
6888 char_u *arg,
6889 char_u **expr_start,
6890 char_u **expr_end,
6891 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 int mb_nest = 0;
6894 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006896 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006897 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006901 *expr_start = NULL;
6902 *expr_end = NULL;
6903 }
6904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006905 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006906 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006907 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006908 return arg;
6909
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 for (p = arg; *p != NUL
6911 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006912 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006913 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006914 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006915 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006916 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006917 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006918 if (*p == '\'')
6919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006920 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006921 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006922 ;
6923 if (*p == NUL)
6924 break;
6925 }
6926 else if (*p == '"')
6927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006928 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006929 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006930 if (*p == '\\' && p[1] != NUL)
6931 ++p;
6932 if (*p == NUL)
6933 break;
6934 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006935 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006937 // "s:" is start of "s:var", but "n:" is not and can be used in
6938 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006939 len = (int)(p - arg);
6940 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006941 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006942 break;
6943 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006945 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006947 if (*p == '[')
6948 ++br_nest;
6949 else if (*p == ']')
6950 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006952
zeertzjqa93d9cd2023-05-02 16:25:47 +01006953 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006955 if (*p == '{')
6956 {
6957 mb_nest++;
6958 if (expr_start != NULL && *expr_start == NULL)
6959 *expr_start = p;
6960 }
6961 else if (*p == '}')
6962 {
6963 mb_nest--;
6964 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6965 *expr_end = p;
6966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969
6970 return p;
6971}
6972
6973/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006974 * Expands out the 'magic' {}'s in a variable/function name.
6975 * Note that this can call itself recursively, to deal with
6976 * constructs like foo{bar}{baz}{bam}
6977 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6978 * "in_start" ^
6979 * "expr_start" ^
6980 * "expr_end" ^
6981 * "in_end" ^
6982 *
6983 * Returns a new allocated string, which the caller must free.
6984 * Returns NULL for failure.
6985 */
6986 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006987make_expanded_name(
6988 char_u *in_start,
6989 char_u *expr_start,
6990 char_u *expr_end,
6991 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006992{
6993 char_u c1;
6994 char_u *retval = NULL;
6995 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006996
6997 if (expr_end == NULL || in_end == NULL)
6998 return NULL;
6999 *expr_start = NUL;
7000 *expr_end = NUL;
7001 c1 = *in_end;
7002 *in_end = NUL;
7003
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007004 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007005 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007006 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007007 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7008 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007009 if (retval != NULL)
7010 {
7011 STRCPY(retval, in_start);
7012 STRCAT(retval, temp_result);
7013 STRCAT(retval, expr_end + 1);
7014 }
7015 }
7016 vim_free(temp_result);
7017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007018 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007019 *expr_start = '{';
7020 *expr_end = '}';
7021
7022 if (retval != NULL)
7023 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007024 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007025 if (expr_start != NULL)
7026 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007027 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007028 temp_result = make_expanded_name(retval, expr_start,
7029 expr_end, temp_result);
7030 vim_free(retval);
7031 retval = temp_result;
7032 }
7033 }
7034
7035 return retval;
7036}
7037
7038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007043eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007045 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007046}
7047
7048/*
7049 * Return TRUE if character "c" can be used as the first character in a
7050 * variable or function name (excluding '{' and '}').
7051 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007053eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007054{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007055 return ASCII_ISALPHA(c) || c == '_';
7056}
7057
7058/*
7059 * Return TRUE if character "c" can be used as the first character of a
7060 * dictionary key.
7061 */
7062 int
7063eval_isdictc(int c)
7064{
7065 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
7068/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007069 * Handle:
7070 * - expr[expr], expr[expr:expr] subscript
7071 * - ".name" lookup
7072 * - function call with Funcref variable: func(expr)
7073 * - method call: var->method()
7074 *
7075 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007076 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007077 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079handle_subscript(
7080 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007081 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007082 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007083 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007084 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007085{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007086 int evaluate = evalarg != NULL
7087 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007088 int ret = OK;
7089 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007090 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007091 int getnext;
7092 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007093
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007094 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007095 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007096 // When at the end of the line and ".name" or "->{" or "->X" follows in
7097 // the next line then consume the line break.
7098 p = eval_next_non_blank(*arg, evalarg, &getnext);
7099 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007100 && ((*p == '.'
7101 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7102 || rettv->v_type == VAR_CLASS
7103 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007104 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7105 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7106 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007107 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007108 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007109 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007110 check_white = FALSE;
7111 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007112
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007113 if (rettv->v_type == VAR_ANY)
7114 {
7115 char_u *exp_name;
7116 int cc;
7117 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007118 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007119 type_T *type;
7120
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007121 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007122 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007123 if (**arg != '.')
7124 {
7125 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007126 semsg(_(e_expected_dot_after_name_str),
7127 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007128 ret = FAIL;
7129 break;
7130 }
7131 ++*arg;
7132 if (IS_WHITE_OR_NUL(**arg))
7133 {
7134 if (verbose)
7135 emsg(_(e_no_white_space_allowed_after_dot));
7136 ret = FAIL;
7137 break;
7138 }
7139
7140 // isolate the name
7141 exp_name = *arg;
7142 while (eval_isnamec(**arg))
7143 ++*arg;
7144 cc = **arg;
7145 **arg = NUL;
7146
7147 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007148 evalarg == NULL ? NULL : evalarg->eval_cctx,
7149 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007150 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007151
7152 if (idx < 0 && ufunc == NULL)
7153 {
7154 ret = FAIL;
7155 break;
7156 }
7157 if (idx >= 0)
7158 {
7159 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7160 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7161
7162 copy_tv(sv->sv_tv, rettv);
7163 }
7164 else
7165 {
7166 rettv->v_type = VAR_FUNC;
7167 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7168 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007169 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007170 }
7171
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007172 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7173 || rettv->v_type == VAR_PARTIAL))
7174 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007175 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007176 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7177 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007178
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007179 // Stop the expression evaluation when immediately aborting on
7180 // error, or when an interrupt occurred or an exception was thrown
7181 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007182 if (aborting())
7183 {
7184 if (ret == OK)
7185 clear_tv(rettv);
7186 ret = FAIL;
7187 }
7188 dict_unref(selfdict);
7189 selfdict = NULL;
7190 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007191 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007192 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007193 if (in_vim9script())
7194 *arg = skipwhite(p + 2);
7195 else
7196 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007197 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007198 {
zeertzjqc481ad32023-03-11 16:18:51 +00007199 emsg(_(e_no_white_space_allowed_before_parenthesis));
7200 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007201 }
zeertzjqc481ad32023-03-11 16:18:51 +00007202 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7203 // expr->{lambda}() or expr->(lambda)()
7204 ret = eval_lambda(arg, rettv, evalarg, verbose);
7205 else
7206 // expr->name()
7207 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007208 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007209 // "." is ".name" lookup when we found a dict or when evaluating and
7210 // scriptversion is at least 2, where string concatenation is "..".
7211 else if (**arg == '['
7212 || (**arg == '.' && (rettv->v_type == VAR_DICT
7213 || (!evaluate
7214 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007215 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007216 {
7217 dict_unref(selfdict);
7218 if (rettv->v_type == VAR_DICT)
7219 {
7220 selfdict = rettv->vval.v_dict;
7221 if (selfdict != NULL)
7222 ++selfdict->dv_refcount;
7223 }
7224 else
7225 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007226 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007227 {
7228 clear_tv(rettv);
7229 ret = FAIL;
7230 }
7231 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007232 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7233 || rettv->v_type == VAR_OBJECT))
7234 {
7235 // class member: SomeClass.varname
7236 // class method: SomeClass.SomeMethod()
7237 // class constructor: SomeClass.new()
7238 // object member: someObject.varname
7239 // object method: someObject.SomeMethod()
7240 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7241 {
7242 clear_tv(rettv);
7243 ret = FAIL;
7244 }
7245 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007246 else
7247 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007248 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007250 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7251 // Don't do this when "Func" is already a partial that was bound
7252 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007253 if (selfdict != NULL
7254 && (rettv->v_type == VAR_FUNC
7255 || (rettv->v_type == VAR_PARTIAL
7256 && (rettv->vval.v_partial->pt_auto
7257 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007258 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007259
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007260 dict_unref(selfdict);
7261 return ret;
7262}
7263
7264/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007265 * Make a copy of an item.
7266 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007267 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007268 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7269 * reference to an already copied list/dict can be used.
7270 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007271 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273item_copy(
7274 typval_T *from,
7275 typval_T *to,
7276 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007277 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007278 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279{
7280 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007281 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007285 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007286 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 }
7288 ++recurse;
7289
7290 switch (from->v_type)
7291 {
7292 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007293 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 case VAR_STRING:
7295 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007296 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007297 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007298 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007299 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007300 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007301 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007302 case VAR_CLASS:
7303 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007304 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 copy_tv(from, to);
7306 break;
7307 case VAR_LIST:
7308 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007309 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 if (from->vval.v_list == NULL)
7311 to->vval.v_list = NULL;
7312 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007314 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007315 to->vval.v_list = from->vval.v_list->lv_copylist;
7316 ++to->vval.v_list->lv_refcount;
7317 }
7318 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007319 to->vval.v_list = list_copy(from->vval.v_list,
7320 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007321 if (to->vval.v_list == NULL)
7322 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007324 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007326 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 case VAR_DICT:
7328 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007329 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007330 if (from->vval.v_dict == NULL)
7331 to->vval.v_dict = NULL;
7332 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7333 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007334 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007335 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7336 ++to->vval.v_dict->dv_refcount;
7337 }
7338 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007339 to->vval.v_dict = dict_copy(from->vval.v_dict,
7340 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007341 if (to->vval.v_dict == NULL)
7342 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007344 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007345 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007347 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007348 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 }
7350 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007351 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352}
7353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 void
7355echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7356{
7357 char_u *tofree;
7358 char_u numbuf[NUMBUFLEN];
7359 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7360
7361 if (*atstart)
7362 {
7363 *atstart = FALSE;
7364 // Call msg_start() after eval1(), evaluating the expression
7365 // may cause a message to appear.
7366 if (with_space)
7367 {
7368 // Mark the saved text as finishing the line, so that what
7369 // follows is displayed on a new line when scrolling back
7370 // at the more prompt.
7371 msg_sb_eol();
7372 msg_start();
7373 }
7374 }
7375 else if (with_space)
7376 msg_puts_attr(" ", echo_attr);
7377
7378 if (p != NULL)
7379 for ( ; *p != NUL && !got_int; ++p)
7380 {
7381 if (*p == '\n' || *p == '\r' || *p == TAB)
7382 {
7383 if (*p != TAB && *needclr)
7384 {
7385 // remove any text still there from the command
7386 msg_clr_eos();
7387 *needclr = FALSE;
7388 }
7389 msg_putchar_attr(*p, echo_attr);
7390 }
7391 else
7392 {
7393 if (has_mbyte)
7394 {
7395 int i = (*mb_ptr2len)(p);
7396
7397 (void)msg_outtrans_len_attr(p, i, echo_attr);
7398 p += i - 1;
7399 }
7400 else
7401 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7402 }
7403 }
7404 vim_free(tofree);
7405}
7406
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 * ":echo expr1 ..." print each argument separated with a space, add a
7409 * newline at the end.
7410 * ":echon expr1 ..." print each argument plain.
7411 */
7412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007413ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
7415 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007417 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 int needclr = TRUE;
7419 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007420 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007421 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007422 evalarg_T evalarg;
7423
Bram Moolenaare707c882020-07-01 13:07:37 +02007424 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 if (eap->skip)
7427 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007428 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007430 // If eval1() causes an error message the text from the command may
7431 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007432 need_clr_eos = needclr;
7433
Bram Moolenaar68db9962021-05-09 23:19:22 +02007434 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007435 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
7437 /*
7438 * Report the invalid expression unless the expression evaluation
7439 * has been cancelled due to an aborting error, an interrupt, or an
7440 * exception.
7441 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007442 if (!aborting() && did_emsg == did_emsg_before
7443 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007444 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007445 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 break;
7447 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007448 need_clr_eos = FALSE;
7449
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007451 {
7452 if (rettv.v_type == VAR_VOID)
7453 {
7454 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7455 break;
7456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007458 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 arg = skipwhite(arg);
7462 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007463 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007464 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
7466 if (eap->skip)
7467 --emsg_skip;
7468 else
7469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (needclr)
7472 msg_clr_eos();
7473 if (eap->cmdidx == CMD_echo)
7474 msg_end();
7475 }
7476}
7477
7478/*
7479 * ":echohl {name}".
7480 */
7481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007484 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485}
7486
7487/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007488 * Returns the :echo attribute
7489 */
7490 int
7491get_echo_attr(void)
7492{
7493 return echo_attr;
7494}
7495
7496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 * ":execute expr1 ..." execute the result of an expression.
7498 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007499 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007501 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 * Each gets spaces around each argument and a newline at the end for
7503 * echo commands
7504 */
7505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007506ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 int ret = OK;
7511 char_u *p;
7512 garray_T ga;
7513 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007514 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 ga_init2(&ga, 1, 80);
7517
7518 if (eap->skip)
7519 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007520 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007522 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007523 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 if (!eap->skip)
7527 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007528 char_u buf[NUMBUFLEN];
7529
7530 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007531 {
7532 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7533 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007534 semsg(_(e_using_invalid_value_as_string_str),
7535 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007536 p = NULL;
7537 }
7538 else
7539 p = tv_get_string_buf(&rettv, buf);
7540 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007541 else
7542 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007543 if (p == NULL)
7544 {
7545 clear_tv(&rettv);
7546 ret = FAIL;
7547 break;
7548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 len = (int)STRLEN(p);
7550 if (ga_grow(&ga, len + 2) == FAIL)
7551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 ret = FAIL;
7554 break;
7555 }
7556 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 ga.ga_len += len;
7560 }
7561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 arg = skipwhite(arg);
7564 }
7565
7566 if (ret != FAIL && ga.ga_data != NULL)
7567 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007568 // use the first line of continuation lines for messages
7569 SOURCING_LNUM = start_lnum;
7570
Bram Moolenaar37fef162022-08-29 18:16:32 +01007571 if (eap->cmdidx == CMD_echomsg
7572 || eap->cmdidx == CMD_echowindow
7573 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007575 // Mark the already saved text as finishing the line, so that what
7576 // follows is displayed on a new line when scrolling back at the
7577 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007578 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007579 }
7580
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007581 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007582 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007583 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007584 out_flush();
7585 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007586 else if (eap->cmdidx == CMD_echowindow)
7587 {
7588#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007589 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007590#endif
7591 msg_attr(ga.ga_data, echo_attr);
7592#ifdef HAS_MESSAGE_WINDOW
7593 end_echowindow();
7594#endif
7595 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007596 else if (eap->cmdidx == CMD_echoconsole)
7597 {
7598 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7599 ui_write((char_u *)"\r\n", 2, TRUE);
7600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 else if (eap->cmdidx == CMD_echoerr)
7602 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007603 int save_did_emsg = did_emsg;
7604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007605 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (!force_abort)
7608 did_emsg = save_did_emsg;
7609 }
7610 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007611 {
7612 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7613
7614 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7615 sticky_cmdmod_flags = cmdmod.cmod_flags
7616 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007618 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007619 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622
7623 ga_clear(&ga);
7624
7625 if (eap->skip)
7626 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007627 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628}
7629
7630/*
7631 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7632 * "arg" points to the "&" or '+' when called, to "option" when returning.
7633 * Returns NULL when no option name found. Otherwise pointer to the char
7634 * after the option name.
7635 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007636 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007637find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
7639 char_u *p = *arg;
7640
7641 ++p;
7642 if (*p == 'g' && p[1] == ':')
7643 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007644 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 p += 2;
7646 }
7647 else if (*p == 'l' && p[1] == ':')
7648 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007649 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 p += 2;
7651 }
7652 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007653 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
7655 if (!ASCII_ISALPHA(*p))
7656 return NULL;
7657 *arg = p;
7658
7659 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007660 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 else
7662 while (ASCII_ISALPHA(*p))
7663 ++p;
7664 return p;
7665}
7666
7667/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007668 * Display script name where an item was last set.
7669 * Should only be invoked when 'verbose' is non-zero.
7670 */
7671 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007672last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007673{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007674 char_u *p;
7675
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007676 if (script_ctx.sc_sid == 0)
7677 return;
7678
7679 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7680 if (p == NULL)
7681 return;
7682
7683 verbose_enter();
7684 msg_puts(_("\n\tLast set from "));
7685 msg_puts((char *)p);
7686 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007687 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007688 msg_puts(_(line_msg));
7689 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007690 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007691 verbose_leave();
7692 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007693}
7694
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007695#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
7697/*
7698 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007699 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 * "flags" can be "g" to do a global substitute.
7701 * Returns an allocated string, NULL for error.
7702 */
7703 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007704do_string_sub(
7705 char_u *str,
7706 char_u *pat,
7707 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007708 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007709 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 int sublen;
7712 regmatch_T regmatch;
7713 int i;
7714 int do_all;
7715 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007716 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 garray_T ga;
7718 char_u *ret;
7719 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007720 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007722 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007724 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726 ga_init2(&ga, 1, 200);
7727
7728 do_all = (flags[0] == 'g');
7729
7730 regmatch.rm_ic = p_ic;
7731 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7732 if (regmatch.regprog != NULL)
7733 {
7734 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007735 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7737 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007738 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007739 if (regmatch.startp[0] == regmatch.endp[0])
7740 {
7741 if (zero_width == regmatch.startp[0])
7742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007743 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007744 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007745 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7746 (size_t)i);
7747 ga.ga_len += i;
7748 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007749 continue;
7750 }
7751 zero_width = regmatch.startp[0];
7752 }
7753
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 /*
7755 * Get some space for a temporary buffer to do the substitution
7756 * into. It will contain:
7757 * - The text up to where the match is.
7758 * - The substituted text.
7759 * - The text after the match.
7760 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007761 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007762 if (sublen <= 0)
7763 {
7764 ga_clear(&ga);
7765 break;
7766 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007767 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7769 {
7770 ga_clear(&ga);
7771 break;
7772 }
7773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007774 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 i = (int)(regmatch.startp[0] - tail);
7776 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007777 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007778 (void)vim_regsub(&regmatch, sub, expr,
7779 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7780 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007782 tail = regmatch.endp[0];
7783 if (*tail == NUL)
7784 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (!do_all)
7786 break;
7787 }
7788
7789 if (ga.ga_data != NULL)
7790 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7791
Bram Moolenaar473de612013-06-08 18:19:48 +02007792 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 }
7794
7795 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7796 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007797 if (p_cpo == empty_option)
7798 p_cpo = save_cpo;
7799 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007801 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007802 // If it's still empty it was changed and restored, need to restore in
7803 // the complicated way.
7804 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007805 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007806 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
7809 return ret;
7810}