blob: b547143fe3b18fe389a736793ff1d076c6f9638c [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.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200578 * When "join_list" 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 *
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200582typval2string(typval_T *tv, int join_list)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100583{
584 garray_T ga;
585 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100586
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200587 if (join_list && tv->v_type == VAR_LIST)
Bram Moolenaar883cf972021-01-15 18:04:43 +0100588 {
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 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200599 else if (tv->v_type == VAR_LIST || tv->v_type == VAR_DICT)
600 {
601 char_u *tofree;
602 char_u numbuf[NUMBUFLEN];
603
604 retval = tv2string(tv, &tofree, numbuf, 0);
605 // Make a copy if we have a value but it's not in allocated memory.
606 if (retval != NULL && tofree == NULL)
607 retval = vim_strsave(retval);
608 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100609 else
610 retval = vim_strsave(tv_get_string(tv));
611 return retval;
612}
613
614/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200615 * Top level evaluation function, returning a string. Does not handle line
616 * breaks.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200617 * When "join_list" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 * Return pointer to allocated memory, or NULL for failure.
619 */
620 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100622 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200623 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100624 exarg_T *eap,
625 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
Bram Moolenaar33570922005-01-25 22:26:29 +0000627 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100629 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100630 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100632 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100633 if (use_simple_function)
634 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
635 else
636 r = eval0(arg, &tv, NULL, &evalarg);
637 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 retval = NULL;
639 else
640 {
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200641 retval = typval2string(&tv, join_list);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000642 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100644 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 return retval;
647}
648
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100649 char_u *
650eval_to_string(
651 char_u *arg,
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200652 int join_list,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100653 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100654{
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200655 return eval_to_string_eap(arg, join_list, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100656}
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000659 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100660 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 */
663 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100664eval_to_string_safe(
665 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000666 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100667 int keep_script_version,
668 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200671 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200672 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200673 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaar9530b582022-01-22 13:39:08 +0000675 if (!keep_script_version)
676 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200677 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000678 if (use_sandbox)
679 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100680 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200681 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100682 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000683 if (use_sandbox)
684 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100685 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200686 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200687 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200688 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 return retval;
690}
691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692/*
693 * Top level evaluation function, returning a number.
694 * Evaluates "expr" silently.
695 * Returns -1 for an error.
696 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200697 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100698eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
Bram Moolenaar33570922005-01-25 22:26:29 +0000700 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200701 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000702 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100703 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
705 ++emsg_off;
706
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100707 if (use_simple_function)
708 r = may_call_simple_func(expr, &rettv);
709 if (r == NOTDONE)
710 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
711 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 retval = -1;
713 else
714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100715 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718 --emsg_off;
719
720 return retval;
721}
722
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000723/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000724 * Top level evaluation function.
725 * Returns an allocated typval_T with the result.
726 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000727 */
728 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200729eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100731 return eval_expr_ext(arg, eap, FALSE);
732}
733
734 typval_T *
735eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
736{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000737 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200738 evalarg_T evalarg;
739
740 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000741
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200742 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100743 if (tv != NULL)
744 {
745 int r = NOTDONE;
746
747 if (use_simple_function)
748 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
749 if (r == NOTDONE)
750 r = eval0(arg, tv, eap, &evalarg);
751
752 if (r == FAIL)
753 VIM_CLEAR(tv);
754 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000755
Bram Moolenaar37c83712020-06-30 21:18:36 +0200756 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000757 return tv;
758}
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000761 * "*arg" points to what can be a function name in the form of "import.Name" or
762 * "Funcref". Return the name of the function. Set "tofree" to something that
763 * was allocated.
764 * If "verbose" is FALSE no errors are given.
765 * Return NULL for any failure.
766 */
767 static char_u *
768deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000769 char_u **arg,
770 char_u **tofree,
771 evalarg_T *evalarg,
772 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000773{
774 typval_T ref;
775 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100776 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000777
778 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100779 if (evalarg != NULL)
780 {
781 // need to evaluate this to get an import, like in "a.Func"
782 save_flags = evalarg->eval_flags;
783 evalarg->eval_flags |= EVAL_EVALUATE;
784 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100785 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100786 {
787 dictitem_T *v;
788
789 // If <SID>VarName was used it would not be found, try another way.
790 v = find_var_also_in_script(name, NULL, FALSE);
791 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100792 {
793 name = NULL;
794 goto theend;
795 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100796 copy_tv(&v->di_tv, &ref);
797 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000798 if (*skipwhite(*arg) != NUL)
799 {
800 if (verbose)
801 semsg(_(e_trailing_characters_str), *arg);
802 name = NULL;
803 }
804 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
805 {
806 name = ref.vval.v_string;
807 ref.vval.v_string = NULL;
808 *tofree = name;
809 }
810 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
811 {
812 if (ref.vval.v_partial->pt_argc > 0
813 || ref.vval.v_partial->pt_dict != NULL)
814 {
815 if (verbose)
816 emsg(_(e_cannot_use_partial_here));
817 name = NULL;
818 }
819 else
820 {
821 name = vim_strsave(partial_name(ref.vval.v_partial));
822 *tofree = name;
823 }
824 }
825 else
826 {
827 if (verbose)
828 semsg(_(e_not_callable_type_str), name);
829 name = NULL;
830 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100831
832theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000833 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100834 if (evalarg != NULL)
835 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000836 return name;
837}
838
839/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100840 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200841 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
842 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000843 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100846call_vim_function(
847 char_u *func,
848 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200849 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200850 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000852 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200853 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000854 char_u *arg;
855 char_u *name;
856 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000857 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200860 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000861 funcexe.fe_firstline = curwin->w_cursor.lnum;
862 funcexe.fe_lastline = curwin->w_cursor.lnum;
863 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000864
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000865 // The name might be "import.Func" or "Funcref". We don't know, we need to
866 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000867 // autoload script has errors. Guess that when there is a dot in the name
868 // showing errors is the right choice.
869 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000870 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000871 if (ignore_errors)
872 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000873 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000874 if (ignore_errors)
875 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000876 if (name == NULL)
877 name = func;
878
879 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
880
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000881 if (ret == FAIL)
882 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000883 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000884
885 return ret;
886}
887
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100888/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100889 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000890 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
891 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000892 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 */
894 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100895call_func_retstr(
896 char_u *func,
897 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200898 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899{
900 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000901 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000902
Bram Moolenaarded27a12018-08-01 19:06:03 +0200903 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000904 return NULL;
905
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100906 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return retval;
909}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000910
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000911/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100912 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000913 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000914 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100915 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000916 */
917 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918call_func_retlist(
919 char_u *func,
920 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200921 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000922{
923 typval_T rettv;
924
Bram Moolenaarded27a12018-08-01 19:06:03 +0200925 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000926 return NULL;
927
928 if (rettv.v_type != VAR_LIST)
929 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100930 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
931 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000932 clear_tv(&rettv);
933 return NULL;
934 }
935
936 return rettv.vval.v_list;
937}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
Bram Moolenaare70dd112022-01-21 16:31:11 +0000939#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100941 * Evaluate "arg", which is 'foldexpr'.
942 * Note: caller must set "curwin" to match "arg".
943 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
944 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 */
946 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000949 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200951 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000953 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000954 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100957 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000958 current_sctx = wp->w_p_script_ctx[WV_FDE];
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000961 if (use_sandbox)
962 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100963 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100965
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100966 // Evaluate the expression. If the expression is "FuncName()" call the
967 // function directly.
968 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 retval = 0;
970 else
971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 if (tv.v_type == VAR_NUMBER)
974 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000975 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 retval = 0;
977 else
978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // If the result is a string, check if there is a non-digit before
980 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000981 s = tv.vval.v_string;
zeertzjqa991ce92023-10-06 19:16:36 +0200982 if (*s != NUL && !VIM_ISDIGIT(*s) && *s != '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 *cp = *s++;
984 retval = atol((char *)s);
985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000986 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000989 if (use_sandbox)
990 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100991 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200992 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000993 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200995 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997#endif
998
Ernie Rael64885642023-10-04 20:16:22 +0200999#ifdef LOG_LOCKVAR
1000typedef struct flag_string_S
1001{
1002 int flag;
1003 char *str;
1004} flag_string_T;
1005
1006 static char *
1007flags_tostring(int flags, flag_string_T *_fstring, char *buf, size_t n)
1008{
1009 char *p = buf;
1010 *p = NUL;
1011 for (flag_string_T *fstring = _fstring; fstring->flag; ++fstring)
1012 {
1013 if ((fstring->flag & flags) != 0)
1014 {
1015 size_t len = STRLEN(fstring->str);
1016 if (n > p - buf + len + 7)
1017 {
1018 STRCAT(p, fstring->str);
1019 p += len;
1020 STRCAT(p, " ");
1021 ++p;
1022 }
1023 else
1024 {
1025 STRCAT(buf, "...");
1026 break;
1027 }
1028 }
1029 }
1030 return buf;
1031}
1032
1033flag_string_T glv_flag_strings[] = {
1034 { GLV_QUIET, "QUIET" },
1035 { GLV_NO_AUTOLOAD, "NO_AUTOLOAD" },
1036 { GLV_READ_ONLY, "READ_ONLY" },
1037 { GLV_NO_DECL, "NO_DECL" },
1038 { GLV_COMPILING, "COMPILING" },
1039 { GLV_ASSIGN_WITH_OP, "ASSIGN_WITH_OP" },
1040 { GLV_PREFER_FUNC, "PREFER_FUNC" },
1041 { 0, NULL }
1042};
1043#endif
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045/*
Ernie Raelee865f32023-09-29 19:53:55 +02001046 * Fill in "lp" using "root". This is used in a special case when
1047 * "get_lval()" parses a bare word when "lval_root" is not NULL.
1048 *
1049 * This is typically called with "lval_root" as "root". For a class, find
1050 * the name from lp in the class from root, fill in lval_T if found. For a
1051 * complex type, list/dict use it as the result; just put the root into
1052 * ll_tv.
1053 *
1054 * "lval_root" is a hack used during run-time/instr-execution to provide the
1055 * starting point for "get_lval()" to traverse a chain of indexes. In some
1056 * cases get_lval sees a bare name and uses this function to populate the
1057 * lval_T.
1058 *
1059 * For setting up "lval_root" (currently only used with lockvar)
1060 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1061 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1062 */
1063 static void
Ernie Rael4c8da022023-10-11 21:35:11 +02001064fill_lval_from_lval_root(lval_T *lp, lval_root_T *lr)
Ernie Raelee865f32023-09-29 19:53:55 +02001065{
1066#ifdef LOG_LOCKVAR
Ernie Rael4c8da022023-10-11 21:35:11 +02001067 ch_log(NULL, "LKVAR: fill_lval_from_lval_root(): name %s, tv %p",
1068 lp->ll_name, (void*)lr->lr_tv);
Ernie Raelee865f32023-09-29 19:53:55 +02001069#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02001070 if (lr->lr_tv == NULL)
1071 return;
Ernie Rael64885642023-10-04 20:16:22 +02001072 if (!lr->lr_is_arg && lr->lr_tv->v_type == VAR_CLASS)
Ernie Raelee865f32023-09-29 19:53:55 +02001073 {
Ernie Rael64885642023-10-04 20:16:22 +02001074 if (lr->lr_tv->vval.v_class != NULL)
Ernie Raelee865f32023-09-29 19:53:55 +02001075 {
1076 // Special special case. Look for a bare class variable reference.
Ernie Rael64885642023-10-04 20:16:22 +02001077 class_T *cl = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001078 int m_idx;
1079 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1080 lp->ll_name_end - lp->ll_name, &m_idx);
1081 if (m != NULL)
1082 {
1083 // Assuming "inside class" since bare reference.
Ernie Rael64885642023-10-04 20:16:22 +02001084 lp->ll_class = lr->lr_tv->vval.v_class;
Ernie Raelee865f32023-09-29 19:53:55 +02001085 lp->ll_oi = m_idx;
1086 lp->ll_valtype = m->ocm_type;
1087 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1088#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001089 ch_log(NULL, "LKVAR: ... class member %s.%s",
1090 lp->ll_class->class_name, lp->ll_name);
Ernie Raelee865f32023-09-29 19:53:55 +02001091#endif
1092 return;
1093 }
1094 }
1095 }
1096
1097#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001098 ch_log(NULL, "LKVAR: ... type: %s", vartype_name(lr->lr_tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02001099#endif
Ernie Rael64885642023-10-04 20:16:22 +02001100 lp->ll_tv = lr->lr_tv;
Ernie Raelee865f32023-09-29 19:53:55 +02001101 lp->ll_is_root = TRUE;
1102}
1103
1104/*
Ernie Rael64885642023-10-04 20:16:22 +02001105 * Check if the class has permission to access the member.
1106 * Returns OK or FAIL.
1107 */
1108 static int
1109get_lval_check_access(
1110 class_T *cl_exec, // executing class, NULL if :def or script level
1111 class_T *cl, // class which contains the member
1112 ocmember_T *om, // member being accessed
1113 char_u *p, // char after member name
1114 int flags) // GLV flags to check if writing to lval
1115{
1116#ifdef LOG_LOCKVAR
1117 ch_log(NULL, "LKVAR: get_lval_check_access(), cl_exec %p, cl %p, %c",
1118 (void*)cl_exec, (void*)cl, *p);
1119#endif
1120 if (cl_exec == NULL || cl_exec != cl)
1121 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02001122 char *msg = NULL;
Ernie Rael64885642023-10-04 20:16:22 +02001123 switch (om->ocm_access)
1124 {
1125 case VIM_ACCESS_PRIVATE:
Ernie Rael03042a22023-11-11 08:53:32 +01001126 msg = e_cannot_access_protected_variable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001127 break;
Ernie Rael64885642023-10-04 20:16:22 +02001128 case VIM_ACCESS_READ:
1129 // If [idx] or .key following, read only OK.
1130 if (*p == '[' || *p == '.')
1131 break;
1132 if ((flags & GLV_READ_ONLY) == 0)
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001133 {
1134 if (IS_ENUM(cl))
1135 {
1136 if (om->ocm_type->tt_type == VAR_OBJECT)
1137 semsg(_(e_enumvalue_str_cannot_be_modified),
1138 cl->class_name, om->ocm_name);
1139 else
1140 msg = e_variable_is_not_writable_str;
1141 }
1142 else
1143 msg = e_variable_is_not_writable_str;
1144 }
Ernie Rael64885642023-10-04 20:16:22 +02001145 break;
1146 case VIM_ACCESS_ALL:
1147 break;
1148 }
Ernie Raele6c9aa52023-10-06 19:55:52 +02001149 if (msg != NULL)
1150 {
1151 emsg_var_cl_define(msg, om->ocm_name, 0, cl);
1152 return FAIL;
1153 }
1154
Ernie Rael64885642023-10-04 20:16:22 +02001155 }
1156 return OK;
1157}
1158
1159/*
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001160 * Get lval information for a variable imported from script "imp_sid". On
1161 * success, updates "lp" with the variable name, type, script ID and typval.
1162 * The variable name starts at or after "p".
1163 * If "rettv" is not NULL it points to the value to be assigned. This used to
1164 * match the rhs and lhs types.
1165 * Returns a pointer to the character after the variable name if the imported
1166 * variable is valid and writable.
1167 * Returns NULL if the variable is not exported or typval is not found or the
1168 * rhs type doesn't match the lhs type or the variable is not writable.
1169 */
1170 static char_u *
1171get_lval_imported(
1172 lval_T *lp,
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001173 scid_T imp_sid,
1174 char_u *p,
1175 dictitem_T **dip,
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001176 int fne_flags)
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001177{
1178 ufunc_T *ufunc;
1179 type_T *type = NULL;
1180 int cc;
1181 int rc = FAIL;
1182
1183 p = skipwhite(p);
1184
1185 import_check_sourced_sid(&imp_sid);
1186 lp->ll_sid = imp_sid;
1187 lp->ll_name = p;
1188 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1189 lp->ll_name_end = p;
1190
1191 // check the item is exported
1192 cc = *p;
1193 *p = NUL;
1194 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1195 TRUE) == -1)
1196 goto failed;
1197
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001198 // Get the typval for the exported item
1199 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1200 if (ht == NULL)
1201 goto failed;
1202
1203 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1204 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001205 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001206 goto success;
1207
1208 *dip = di;
1209
1210 // Check whether the variable is writable.
1211 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1212 if (sv != NULL && sv->sv_const != 0)
1213 {
1214 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1215 goto failed;
1216 }
1217
1218 // check whether variable is locked
1219 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1220 goto failed;
1221
1222 lp->ll_tv = &di->di_tv;
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001223 lp->ll_valtype = type;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001224
1225success:
1226 rc = OK;
1227
1228failed:
1229 *p = cc;
1230 return rc == OK ? p : NULL;
1231}
1232
1233/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 * Get an lval: variable, Dict item or List item that can be assigned a value
1235 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1236 * "name.key", "name.key[expr]" etc.
1237 * Indexing only works if "name" is an existing List or Dictionary.
1238 * "name" points to the start of the name.
1239 * If "rettv" is not NULL it points to the value to be assigned.
1240 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1241 * wrong; must end in space or cmd separator.
1242 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001243 * flags:
1244 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001245 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001246 * GLV_NO_AUTOLOAD: do not use script autoloading
1247 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001249 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001251 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001252 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253get_lval(
1254 char_u *name,
1255 typval_T *rettv,
1256 lval_T *lp,
1257 int unlet,
1258 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001259 int flags, // GLV_ values
1260 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 char_u *expr_start, *expr_end;
1264 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001265 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T var1;
1267 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001271 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001272 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001273 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001274 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001275 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276
Ernie Raelee865f32023-09-29 19:53:55 +02001277#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001278 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001279 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001280 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001281 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1282 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001283 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001284 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001285 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001286#endif
1287
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001288 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001289 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001291 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001293 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001295 lp->ll_name_end = find_name_end(name, NULL, NULL,
1296 FNE_INCL_BR | fne_flags);
1297 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 }
1299
Bram Moolenaara749a422022-02-12 19:52:25 +00001300 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001301 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001302 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1303 {
1304 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1305 return NULL;
1306 }
1307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001309 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 if (expr_start != NULL)
1312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001313 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001314 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 && *p != '[' && *p != '.')
1316 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001317 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 return NULL;
1319 }
1320
1321 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1322 if (lp->ll_exp_name == NULL)
1323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // Report an invalid expression in braces, unless the
1325 // expression evaluation has been cancelled due to an
1326 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 if (!aborting() && !quiet)
1328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001329 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001330 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 return NULL;
1332 }
1333 }
1334 lp->ll_name = lp->ll_exp_name;
1335 }
1336 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 lp->ll_name = name;
1339
Bram Moolenaar4525a572022-02-13 11:57:33 +00001340 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001342 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001343 // However, "g:[key]" is indexing a dictionary.
1344 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001345 {
1346 --p;
1347 lp->ll_name_end = p;
1348 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001349 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001350 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001351 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001352
Bram Moolenaar9510d222022-09-11 15:14:05 +01001353 if (is_scoped_variable(name))
1354 {
1355 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1356 return NULL;
1357 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001358 if (VIM_ISWHITE(*p))
1359 {
1360 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1361 return NULL;
1362 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001363 if (tp == p + 1 && !quiet)
1364 {
1365 semsg(_(e_white_space_required_after_str_str), ":", p);
1366 return NULL;
1367 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001368 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001369 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001370 semsg(_(e_using_type_not_in_script_context_str), p);
1371 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001372 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001373 if (vim9script && (flags & GLV_NO_DECL) &&
1374 !(flags & GLV_FOR_LOOP))
1375 {
1376 // Using a type and not in a "var" declaration.
1377 semsg(_(e_trailing_characters_str), p);
1378 return NULL;
1379 }
1380
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001381
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001382 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001383 lp->ll_type = parse_type(&tp,
1384 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1385 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001386 if (lp->ll_type == NULL && !quiet)
1387 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001388 lp->ll_name_end = tp;
1389 }
Ernie Raelee865f32023-09-29 19:53:55 +02001390 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 }
1392 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001393 if (lp->ll_name == NULL)
1394 return p;
1395
Bram Moolenaar62aec932022-01-29 21:45:34 +00001396 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001397 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001398 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001399 if (import != NULL)
1400 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001401 p++; // skip '.'
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001402 p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001403 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001404 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001405 }
1406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
Ernie Rael0f058d12023-10-14 11:25:04 +02001408 // Without [idx] or .key we are done.
1409 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001410 {
1411 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001412 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001414 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415
Ernie Rael0f058d12023-10-14 11:25:04 +02001416 if (vim9script && lval_root != NULL)
1417 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001418 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001419 {
1420 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001421 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001422 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001423 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001424 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001425 {
1426 cc = *p;
1427 *p = NUL;
1428 // When we would write to the variable pass &ht and prevent autoload.
1429 writing = !(flags & GLV_READ_ONLY);
1430 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001431 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001432 if (v == NULL && !quiet)
1433 semsg(_(e_undefined_variable_str), lp->ll_name);
1434 *p = cc;
1435 if (v == NULL)
1436 return NULL;
1437 lp->ll_tv = &v->di_tv;
1438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439
Bram Moolenaar4525a572022-02-13 11:57:33 +00001440 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001441 {
1442 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001443 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001444 return NULL;
1445 }
1446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447 /*
1448 * Loop until no more [idx] or .key is following.
1449 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001450 var1.v_type = VAR_UNKNOWN;
1451 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001452 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001453 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001454 vartype_T v_type = lp->ll_tv->v_type;
1455
1456 if (*p == '.' && v_type != VAR_DICT
1457 && v_type != VAR_OBJECT
1458 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001459 {
1460 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001461 semsg(_(e_dot_not_allowed_after_str_str),
1462 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001463 return NULL;
1464 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001465 if (v_type != VAR_LIST
1466 && v_type != VAR_DICT
1467 && v_type != VAR_BLOB
1468 && v_type != VAR_OBJECT
1469 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001472 semsg(_(e_index_not_allowed_after_str_str),
1473 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001475 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001476
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001477 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001478 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001479 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001480 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001481 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001482 r = rettv_blob_alloc(lp->ll_tv);
1483 if (r == FAIL)
1484 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001485
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001489 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001492#ifdef LOG_LOCKVAR
1493 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1494 vartype_name(v_type));
1495#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001496
Bram Moolenaar4525a572022-02-13 11:57:33 +00001497 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001498 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001499 && lp->ll_tv == &v->di_tv
1500 && ht != NULL && ht == get_script_local_ht())
1501 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001502 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001503
1504 // Vim9 script local variable: get the type
1505 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001506 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001507 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001508#ifdef LOG_LOCKVAR
1509 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1510 vartype_name(lp->ll_valtype->tt_type));
1511#endif
1512 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001513 }
1514
Bram Moolenaar8c711452005-01-14 21:53:12 +00001515 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001516 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001517 {
1518 key = p + 1;
1519 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1520 ;
1521 if (len == 0)
1522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001524 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001526 }
1527 p = key + len;
1528 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001529 else
1530 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001531 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001532 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001533 if (*p == ':')
1534 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001535 else
1536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001537 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001538 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001539 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001540 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001542 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 clear_tv(&var1);
1544 return NULL;
1545 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001546 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001547 }
1548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001549 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001550 if (*p == ':')
1551 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001552 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001554 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001555 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001556 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001557 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001558 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001559 if (rettv != NULL
1560 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001561 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001562 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001563 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001565 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001566 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001567 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001568 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001569 }
1570 p = skipwhite(p + 1);
1571 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001572 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001573 else
1574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001575 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001576 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001577 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001578 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001579 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001580 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001581 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001582 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001584 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001585 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001586 clear_tv(&var2);
1587 return NULL;
1588 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001590 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001592 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001593 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001594
Bram Moolenaar8c711452005-01-14 21:53:12 +00001595 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001597 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001598 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001599 clear_tv(&var1);
1600 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001601 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001602 }
1603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001604 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001605 ++p;
1606 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001607#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001608 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001609 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1610 empty1 ? ":" : (char*)tv_get_string(&var1));
1611 else
1612 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001613#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001614
Bram Moolenaard505d172022-12-18 21:42:55 +00001615 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001616 {
1617 if (len == -1)
1618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001619 // "[key]": get key from "var1"
1620 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001621 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001623 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001624 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001625 }
1626 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001628 lp->ll_object = NULL;
1629 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001630
1631 // a NULL dict is equivalent with an empty dict
1632 if (lp->ll_tv->vval.v_dict == NULL)
1633 {
1634 lp->ll_tv->vval.v_dict = dict_alloc();
1635 if (lp->ll_tv->vval.v_dict == NULL)
1636 {
1637 clear_tv(&var1);
1638 return NULL;
1639 }
1640 ++lp->ll_tv->vval.v_dict->dv_refcount;
1641 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001643
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001644 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001646 // When assigning to a scope dictionary check that a function and
1647 // variable name is valid (only variable name unless it is l: or
1648 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001649 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001650 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001651 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001652
1653 if (len != -1)
1654 {
1655 prevval = key[len];
1656 key[len] = NUL;
1657 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001658 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001659 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001660 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001661 && (rettv->v_type == VAR_FUNC
1662 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001663 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001664 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001665 if (len != -1)
1666 key[len] = prevval;
1667 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001668 {
1669 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001670 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001671 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001672 }
1673
Bram Moolenaar10c65862020-10-08 21:16:42 +02001674 if (lp->ll_valtype != NULL)
1675 // use the type of the member
1676 lp->ll_valtype = lp->ll_valtype->tt_member;
1677
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001678 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001679 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001680 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001681 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001682 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001683 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001684 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001685 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001686 return NULL;
1687 }
1688
Bram Moolenaar31b81602019-02-10 22:14:27 +01001689 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001690 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001692 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001693 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001696 }
1697 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001698 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001699 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001700 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001702 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001703 p = NULL;
1704 break;
1705 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001706 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001707 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001708 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1709 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001710 {
1711 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001712 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001713 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001714
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001716 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001717 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001718 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001719 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001720 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1721
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 /*
1723 * Get the number and item for the only or first index of the List.
1724 */
1725 if (empty1)
1726 lp->ll_n1 = 0;
1727 else
1728 // is number or string
1729 lp->ll_n1 = (long)tv_get_number(&var1);
1730 clear_tv(&var1);
1731
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001732 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001734 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 return NULL;
1736 }
1737 if (lp->ll_range && !lp->ll_empty2)
1738 {
1739 lp->ll_n2 = (long)tv_get_number(&var2);
1740 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001741 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1742 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001743 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 }
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001745
1746 if (!lp->ll_range)
1747 // Indexing a single byte in a blob. So the rhs type is a
1748 // number.
1749 lp->ll_valtype = &t_number;
1750
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001751 lp->ll_blob = lp->ll_tv->vval.v_blob;
1752 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001753 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001755 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001756 {
1757 /*
1758 * Get the number and item for the only or first index of the List.
1759 */
1760 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001761 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001762 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001763 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001764 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001765 clear_tv(&var1);
1766
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001768 lp->ll_object = NULL;
1769 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001770 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001771 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1772 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001773 if (lp->ll_li == NULL)
1774 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001775 clear_tv(&var2);
1776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001777 }
1778
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001779 if (lp->ll_valtype != NULL && !lp->ll_range)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001780 // use the type of the member
1781 lp->ll_valtype = lp->ll_valtype->tt_member;
1782
Bram Moolenaar8c711452005-01-14 21:53:12 +00001783 /*
1784 * May need to find the item or absolute index for the second
1785 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001786 * When no index given: "lp->ll_empty2" is TRUE.
1787 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001788 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001789 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001791 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001792 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001793 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001794 if (check_range_index_two(lp->ll_list,
1795 &lp->ll_n1, lp->ll_li,
1796 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001798 }
1799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001800 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001801 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001802 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001803 {
Ernie Raelee865f32023-09-29 19:53:55 +02001804 lp->ll_dict = NULL;
1805 lp->ll_list = NULL;
1806
1807 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001808 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001809 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001810 if (lp->ll_tv->vval.v_object == NULL)
1811 {
1812 if (!quiet)
1813 emsg(_(e_using_null_object));
1814 return NULL;
1815 }
Ernie Raelee865f32023-09-29 19:53:55 +02001816 cl = lp->ll_tv->vval.v_object->obj_class;
1817 lp->ll_object = lp->ll_tv->vval.v_object;
1818 }
1819 else
1820 {
1821 cl = lp->ll_tv->vval.v_class;
1822 lp->ll_object = NULL;
1823 }
1824 lp->ll_class = cl;
1825
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001826 // TODO: what if class is NULL?
1827 if (cl != NULL)
1828 {
1829 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001830
1831 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001832 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001833 // First look for a function with this name.
1834 // round 1: class functions (skipped for an object)
1835 // round 2: object methods
1836 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001837 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001838 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001839 int m_idx;
1840 ufunc_T *fp;
1841
1842 fp = method_lookup(cl,
1843 round == 1 ? VAR_CLASS : VAR_OBJECT,
1844 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001845 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001846 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001847 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001848 lp->ll_ufunc = fp;
1849 lp->ll_valtype = fp->uf_func_type;
1850 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001851 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001852 }
1853 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001854
1855 if (lp->ll_valtype == NULL)
1856 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001857 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001858 ocmember_T *om
1859 = member_lookup(cl, v_type, key, p - key, &m_idx);
1860 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001861 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001862 {
Ernie Rael64885642023-10-04 20:16:22 +02001863 if (get_lval_check_access(cl_exec, cl, om,
1864 p, flags) == FAIL)
1865 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001866
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001867 // When lhs is used to modify the variable, check it is
1868 // not a read-only variable.
1869 if ((flags & GLV_READ_ONLY) == 0
1870 && (*p != '.' && *p != '[')
1871 && oc_var_check_ro(cl, om))
1872 return NULL;
1873
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001874 lp->ll_valtype = om->ocm_type;
1875
1876 if (v_type == VAR_OBJECT)
1877 lp->ll_tv = ((typval_T *)(
1878 lp->ll_tv->vval.v_object + 1)) + m_idx;
1879 else
1880 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001881 }
1882 }
1883
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001884 if (lp->ll_valtype == NULL)
1885 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001886 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001887 return NULL;
1888 }
1889 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 }
1892
Yegappan Lakshmanan9937d8b2024-05-08 20:24:33 +02001893 if (vim9script && lp->ll_valtype != NULL && rettv != NULL)
1894 {
1895 where_T where = WHERE_INIT;
1896
1897 // In a vim9 script, do type check and make sure the variable is
1898 // writable.
1899 if (check_typval_type(lp->ll_valtype, rettv, where) == FAIL)
1900 return NULL;
1901 }
1902
1903
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001904 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 return p;
1907}
1908
1909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001913clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914{
1915 vim_free(lp->ll_exp_name);
1916 vim_free(lp->ll_newkey);
1917}
1918
1919/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001920 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001922 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1923 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001926set_var_lval(
1927 lval_T *lp,
1928 char_u *endp,
1929 typval_T *rettv,
1930 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001931 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1932 char_u *op,
1933 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934{
1935 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937
1938 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001940 cc = *endp;
1941 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001942 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001943 return;
1944
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001945 if (lp->ll_blob != NULL)
1946 {
1947 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001948
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001949 if (op != NULL && *op != '=')
1950 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001951 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001952 return;
1953 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001954 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001955 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001956
1957 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1958 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001959 if (lp->ll_empty2)
1960 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1961
Bram Moolenaar68452172021-04-12 21:21:02 +02001962 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1963 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001964 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001965 }
1966 else
1967 {
1968 val = (int)tv_get_number_chk(rettv, &error);
1969 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001970 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001971 }
1972 }
1973 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001975 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001977 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1978 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001979 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001980 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001981 *endp = cc;
1982 return;
1983 }
1984
Bram Moolenaarff697e62019-02-12 22:28:33 +01001985 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001986 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001987 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001988 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001989 {
Ernie Raele75fde62023-12-21 17:18:54 +01001990 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001991 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001992 clear_tv(&tv);
1993 return;
1994 }
1995
Bram Moolenaar79518e22017-02-17 16:31:35 +01001996 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001997 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1998 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001999 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002000 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01002001 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002002 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002005 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002006 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002007 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2008 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002009 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002010 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002011 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002012 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002013 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002015 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002016 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002017 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002018 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002019 else if (lp->ll_range)
2020 {
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_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002025 return;
2026 }
2027
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002028 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2029 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 }
2031 else
2032 {
2033 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002034 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002036 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2037 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002038 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002039 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002040 return;
2041 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002042
2043 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002044 && check_typval_arg_type(lp->ll_valtype, rettv,
2045 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002046 return;
2047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 if (lp->ll_newkey != NULL)
2049 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 if (op != NULL && *op != '=')
2051 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002052 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053 return;
2054 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002055 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2056 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002057 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002059 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002060 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 if (di == NULL)
2062 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002063 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2064 {
2065 vim_free(di);
2066 return;
2067 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002070 else if (op != NULL && *op != '=')
2071 {
2072 tv_op(lp->ll_tv, rettv, op);
2073 return;
2074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002077
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 /*
2079 * Assign the value to the variable or list item.
2080 */
2081 if (copy)
2082 copy_tv(rettv, lp->ll_tv);
2083 else
2084 {
2085 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002086 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 }
2089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090}
2091
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002093 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2094 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002095 * Returns OK or FAIL.
2096 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002100 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 char_u numbuf[NUMBUFLEN];
2102 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002103 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002104
Ernie Raele75fde62023-12-21 17:18:54 +01002105 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002106 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002107 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002108 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002109 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2110 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 {
2112 switch (tv1->v_type)
2113 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002114 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002115 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117 case VAR_DICT:
2118 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002119 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002120 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002121 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002122 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002123 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002124 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002125 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002127 case VAR_CLASS:
2128 case VAR_TYPEALIAS:
2129 check_typval_is_value(tv1);
2130 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002131
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002132 case VAR_BLOB:
2133 if (*op != '+' || tv2->v_type != VAR_BLOB)
2134 break;
2135 // BLOB += BLOB
2136 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2137 {
2138 blob_T *b1 = tv1->vval.v_blob;
2139 blob_T *b2 = tv2->vval.v_blob;
2140 int i, len = blob_len(b2);
2141 for (i = 0; i < len; i++)
2142 ga_append(&b1->bv_ga, blob_get(b2, i));
2143 }
2144 return OK;
2145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002146 case VAR_LIST:
2147 if (*op != '+' || tv2->v_type != VAR_LIST)
2148 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002149 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002150 if (tv2->vval.v_list != NULL)
2151 {
2152 if (tv1->vval.v_list == NULL)
2153 {
2154 tv1->vval.v_list = tv2->vval.v_list;
2155 ++tv1->vval.v_list->lv_refcount;
2156 }
2157 else
2158 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160 return OK;
2161
2162 case VAR_NUMBER:
2163 case VAR_STRING:
2164 if (tv2->v_type == VAR_LIST)
2165 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002166 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002168 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002169 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002170 if (tv2->v_type == VAR_FLOAT)
2171 {
2172 float_T f = n;
2173
Bram Moolenaarff697e62019-02-12 22:28:33 +01002174 if (*op == '%')
2175 break;
2176 switch (*op)
2177 {
2178 case '+': f += tv2->vval.v_float; break;
2179 case '-': f -= tv2->vval.v_float; break;
2180 case '*': f *= tv2->vval.v_float; break;
2181 case '/': f /= tv2->vval.v_float; break;
2182 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002183 clear_tv(tv1);
2184 tv1->v_type = VAR_FLOAT;
2185 tv1->vval.v_float = f;
2186 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002188 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002189 switch (*op)
2190 {
2191 case '+': n += tv_get_number(tv2); break;
2192 case '-': n -= tv_get_number(tv2); break;
2193 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002194 case '/': n = num_divide(n, tv_get_number(tv2),
2195 &failed); break;
2196 case '%': n = num_modulus(n, tv_get_number(tv2),
2197 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002198 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002199 clear_tv(tv1);
2200 tv1->v_type = VAR_NUMBER;
2201 tv1->vval.v_number = n;
2202 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002203 }
2204 else
2205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002206 if (tv2->v_type == VAR_FLOAT)
2207 break;
2208
Bram Moolenaarff697e62019-02-12 22:28:33 +01002209 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002210 s = tv_get_string(tv1);
2211 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 clear_tv(tv1);
2213 tv1->v_type = VAR_STRING;
2214 tv1->vval.v_string = s;
2215 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002216 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002217
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002218 case VAR_FLOAT:
2219 {
2220 float_T f;
2221
Bram Moolenaarff697e62019-02-12 22:28:33 +01002222 if (*op == '%' || *op == '.'
2223 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002224 && tv2->v_type != VAR_NUMBER
2225 && tv2->v_type != VAR_STRING))
2226 break;
2227 if (tv2->v_type == VAR_FLOAT)
2228 f = tv2->vval.v_float;
2229 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002230 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002231 switch (*op)
2232 {
2233 case '+': tv1->vval.v_float += f; break;
2234 case '-': tv1->vval.v_float -= f; break;
2235 case '*': tv1->vval.v_float *= f; break;
2236 case '/': tv1->vval.v_float /= f; break;
2237 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002238 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002239 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 }
2241 }
2242
Ernie Raele75fde62023-12-21 17:18:54 +01002243 if (check_typval_is_value(tv2) == OK)
2244 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002245 return FAIL;
2246}
2247
2248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002249 * Evaluate the expression used in a ":for var in expr" command.
2250 * "arg" points to "var".
2251 * Set "*errp" to TRUE for an error, FALSE otherwise;
2252 * Return a pointer that holds the info. Null when there is an error.
2253 */
2254 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002255eval_for_line(
2256 char_u *arg,
2257 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002258 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002259 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002260{
Bram Moolenaar33570922005-01-25 22:26:29 +00002261 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002262 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002263 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002264 typval_T tv;
2265 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002266 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002268 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002269
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002270 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 if (fi == NULL)
2272 return NULL;
2273
Bram Moolenaar404557e2021-07-05 21:41:48 +02002274 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2275 &fi->fi_semicolon, FALSE);
2276 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002277 return fi;
2278
Bram Moolenaar404557e2021-07-05 21:41:48 +02002279 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002280 if (expr[0] != 'i' || expr[1] != 'n'
2281 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002283 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2284 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2285 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002286 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002287 return fi;
2288 }
2289
2290 if (skip)
2291 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002292 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2293 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002294 {
2295 *errp = FALSE;
2296 if (!skip)
2297 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002298 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002299 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002300 l = tv.vval.v_list;
2301 if (l == NULL)
2302 {
2303 // a null list is like an empty list: do nothing
2304 clear_tv(&tv);
2305 }
2306 else
2307 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002309 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002311 // No need to increment the refcount, it's already set for
2312 // the list being used in "tv".
2313 fi->fi_list = l;
2314 list_add_watch(l, &fi->fi_lw);
2315 fi->fi_lw.lw_item = l->lv_first;
2316 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002317 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002318 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002319 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002320 fi->fi_bi = 0;
2321 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002322 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002323 typval_T btv;
2324
2325 // Make a copy, so that the iteration still works when the
2326 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002328 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002329 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002330 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002331 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002332 else if (tv.v_type == VAR_STRING)
2333 {
2334 fi->fi_byte_idx = 0;
2335 fi->fi_string = tv.vval.v_string;
2336 tv.vval.v_string = NULL;
2337 if (fi->fi_string == NULL)
2338 fi->fi_string = vim_strsave((char_u *)"");
2339 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 else
2341 {
Sean Dewar80d73952021-08-04 19:25:54 +02002342 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002343 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344 }
2345 }
2346 }
2347 if (skip)
2348 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002349 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002350
2351 return fi;
2352}
2353
2354/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002355 * Used when looping over a :for line, skip the "in expr" part.
2356 */
2357 void
2358skip_for_lines(void *fi_void, evalarg_T *evalarg)
2359{
2360 forinfo_T *fi = (forinfo_T *)fi_void;
2361 int i;
2362
2363 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002364 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002365}
2366
2367/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 * Use the first item in a ":for" list. Advance to the next.
2369 * Assign the values to the variable (list). "arg" points to the first one.
2370 * Return TRUE when a valid item was found, FALSE when at end of list or
2371 * something wrong.
2372 */
2373 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002374next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002375{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002376 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002377 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002378 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002379 ? (ASSIGN_FINAL
2380 // first round: error if variable exists
2381 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002382 | ASSIGN_NO_MEMBER_TYPE
2383 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002384 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002385 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002386 int skip_assign = in_vim9script() && arg[0] == '_'
2387 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002388
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002389 if (fi->fi_blob != NULL)
2390 {
2391 typval_T tv;
2392
2393 if (fi->fi_bi >= blob_len(fi->fi_blob))
2394 return FALSE;
2395 tv.v_type = VAR_NUMBER;
2396 tv.v_lock = VAR_FIXED;
2397 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2398 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002399 if (skip_assign)
2400 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002401 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002402 fi->fi_varcount, flag, NULL) == OK;
2403 }
2404
2405 if (fi->fi_string != NULL)
2406 {
2407 typval_T tv;
2408 int len;
2409
2410 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2411 if (len == 0)
2412 return FALSE;
2413 tv.v_type = VAR_STRING;
2414 tv.v_lock = VAR_FIXED;
2415 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2416 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002417 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002418 if (skip_assign)
2419 result = TRUE;
2420 else
2421 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002422 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002423 vim_free(tv.vval.v_string);
2424 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002425 }
2426
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 item = fi->fi_lw.lw_item;
2428 if (item == NULL)
2429 result = FALSE;
2430 else
2431 {
2432 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002433 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002434 if (skip_assign)
2435 result = TRUE;
2436 else
2437 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002438 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439 }
2440 return result;
2441}
2442
2443/*
2444 * Free the structure used to store info used by ":for".
2445 */
2446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002447free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002448{
Bram Moolenaar33570922005-01-25 22:26:29 +00002449 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002451 if (fi == NULL)
2452 return;
2453 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002454 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002456 list_unref(fi->fi_list);
2457 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002458 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002459 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002460 else
2461 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002462 vim_free(fi);
2463}
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002466set_context_for_expression(
2467 expand_T *xp,
2468 char_u *arg,
2469 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002471 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002473 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002475 if (cmdidx == CMD_let || cmdidx == CMD_var
2476 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 {
2478 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002479 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002481 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002482 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 {
2484 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002485 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002486 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487 break;
2488 }
2489 return;
2490 }
2491 }
2492 else
2493 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2494 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 while ((xp->xp_pattern = vim_strpbrk(arg,
2496 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2497 {
2498 c = *xp->xp_pattern;
2499 if (c == '&')
2500 {
2501 c = xp->xp_pattern[1];
2502 if (c == '&')
2503 {
2504 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002505 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002510 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2511 xp->xp_pattern += 2;
2512
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515 else if (c == '$')
2516 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002517 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 xp->xp_context = EXPAND_ENV_VARS;
2519 }
2520 else if (c == '=')
2521 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 xp->xp_context = EXPAND_EXPRESSION;
2524 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002525 else if (c == '#'
2526 && xp->xp_context == EXPAND_EXPRESSION)
2527 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002528 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002529 break;
2530 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002531 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 && xp->xp_context == EXPAND_FUNCTIONS
2533 && vim_strchr(xp->xp_pattern, '(') == NULL)
2534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002535 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 break;
2537 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002538 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002540 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2543 if (c == '\\' && xp->xp_pattern[1] != NUL)
2544 ++xp->xp_pattern;
2545 xp->xp_context = EXPAND_NOTHING;
2546 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002547 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002549 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2551 /* skip */ ;
2552 xp->xp_context = EXPAND_NOTHING;
2553 }
2554 else if (c == '|')
2555 {
2556 if (xp->xp_pattern[1] == '|')
2557 {
2558 ++xp->xp_pattern;
2559 xp->xp_context = EXPAND_EXPRESSION;
2560 }
2561 else
2562 xp->xp_context = EXPAND_COMMANDS;
2563 }
2564 else
2565 xp->xp_context = EXPAND_EXPRESSION;
2566 }
2567 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // Doesn't look like something valid, expand as an expression
2569 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002570 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 arg = xp->xp_pattern;
2572 if (*arg != NUL)
2573 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2574 /* skip */ ;
2575 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002576
2577 // ":exe one two" completes "two"
2578 if ((cmdidx == CMD_execute
2579 || cmdidx == CMD_echo
2580 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002581 || cmdidx == CMD_echomsg
2582 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002583 && xp->xp_context == EXPAND_EXPRESSION)
2584 {
2585 for (;;)
2586 {
2587 char_u *n = skiptowhite(arg);
2588
2589 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2590 break;
2591 arg = skipwhite(n);
2592 }
2593 }
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 xp->xp_pattern = arg;
2596}
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002599 * Return TRUE if "pat" matches "text".
2600 * Does not use 'cpo' and always uses 'magic'.
2601 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002602 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002603pattern_match(char_u *pat, char_u *text, int ic)
2604{
2605 int matches = FALSE;
2606 char_u *save_cpo;
2607 regmatch_T regmatch;
2608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002609 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002610 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002611 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002612 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2613 if (regmatch.regprog != NULL)
2614 {
2615 regmatch.rm_ic = ic;
2616 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2617 vim_regfree(regmatch.regprog);
2618 }
2619 p_cpo = save_cpo;
2620 return matches;
2621}
2622
2623/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002624 * Handle a name followed by "(". Both for just "name(arg)" and for
2625 * "expr->name(arg)".
2626 * Returns OK or FAIL.
2627 */
2628 static int
2629eval_func(
2630 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002631 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002632 char_u *name,
2633 int name_len,
2634 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002635 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002636 typval_T *basetv) // "expr" for "expr->name(arg)"
2637{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002638 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002639 char_u *s = name;
2640 int len = name_len;
2641 partial_T *partial;
2642 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002643 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002644 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002645
2646 if (!evaluate)
2647 check_vars(s, len);
2648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002649 // If "s" is the name of a variable of type VAR_FUNC
2650 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002651 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002652 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002654 // Need to make a copy, in case evaluating the arguments makes
2655 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002656 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002657 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002658 ret = FAIL;
2659 else
2660 {
2661 funcexe_T funcexe;
2662
2663 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002664 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002665 funcexe.fe_firstline = curwin->w_cursor.lnum;
2666 funcexe.fe_lastline = curwin->w_cursor.lnum;
2667 funcexe.fe_evaluate = evaluate;
2668 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002669 if (partial != NULL)
2670 {
2671 funcexe.fe_object = partial->pt_obj;
2672 if (funcexe.fe_object != NULL)
2673 ++funcexe.fe_object->obj_refcount;
2674 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002675 funcexe.fe_basetv = basetv;
2676 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002677 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002678 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002679 }
2680 vim_free(s);
2681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002682 // If evaluate is FALSE rettv->v_type was not set in
2683 // get_func_tv, but it's needed in handle_subscript() to parse
2684 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002685 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2686 {
2687 rettv->vval.v_string = NULL;
2688 rettv->v_type = VAR_FUNC;
2689 }
2690
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002691 // Stop the expression evaluation when immediately
2692 // aborting on error, or when an interrupt occurred or
2693 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002694 if (evaluate && aborting())
2695 {
2696 if (ret == OK)
2697 clear_tv(rettv);
2698 ret = FAIL;
2699 }
2700 return ret;
2701}
2702
2703/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002704 * After a NL, skip over empty lines and comment-only lines.
2705 */
2706 static char_u *
2707newline_skip_comments(char_u *arg)
2708{
2709 char_u *p = arg + 1;
2710
2711 for (;;)
2712 {
2713 p = skipwhite(p);
2714
2715 if (*p == NUL)
2716 break;
2717 if (vim9_comment_start(p))
2718 {
2719 char_u *nl = vim_strchr(p, NL);
2720
2721 if (nl == NULL)
2722 break;
2723 p = nl;
2724 }
2725 if (*p != NL)
2726 break;
2727 ++p; // skip another NL
2728 }
2729 return p;
2730}
2731
2732/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002733 * Get the next line source line without advancing. But do skip over comment
2734 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002735 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002736 */
2737 static char_u *
2738getline_peek_skip_comments(evalarg_T *evalarg)
2739{
2740 for (;;)
2741 {
2742 char_u *next = getline_peek(evalarg->eval_getline,
2743 evalarg->eval_cookie);
2744 char_u *p;
2745
2746 if (next == NULL)
2747 break;
2748 p = skipwhite(next);
2749 if (*p != NUL && !vim9_comment_start(p))
2750 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002751 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002752 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002753 }
2754 return NULL;
2755}
2756
2757/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002758 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2759 * comment) and there is a next line, return the next line (skipping blanks)
2760 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002761 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2762 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763 * "arg" must point somewhere inside a line, not at the start.
2764 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002765 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002766eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2767{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002768 char_u *p = skipwhite(arg);
2769
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002770 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002771 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002772 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002773 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2774 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002775 && (*p == NUL || *p == NL
2776 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002777 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002778 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002779
Bram Moolenaare442d592022-05-05 12:20:28 +01002780 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002781 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002782 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002783 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002784 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002785 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002786
Bram Moolenaar9d489562020-07-30 20:08:50 +02002787 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002788 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002789 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002790 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002791 }
2792 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002793 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002794}
2795
2796/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002797 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002798 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002799 *
2800 * If "arg" is not NULL, then the caller should assign the return value to
2801 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002802 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002803 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002804eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002805{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002806 garray_T *gap = &evalarg->eval_ga;
2807 char_u *line;
2808
Bram Moolenaar39be4982022-05-06 21:51:50 +01002809 if (arg != NULL)
2810 {
2811 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002812 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002813 // Truncate before a trailing comment, so that concatenating the lines
2814 // won't turn the rest into a comment.
2815 if (*skipwhite(arg) == '#')
2816 *arg = NUL;
2817 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002818
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002819 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002820 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2821 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002822 else
2823 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002824 if (line == NULL)
2825 return NULL;
2826
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002827 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002828 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2829 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002830 char_u *p = skipwhite(line);
2831
2832 // Going to concatenate the lines after parsing. For an empty or
2833 // comment line use an empty string.
2834 if (*p == NUL || vim9_comment_start(p))
2835 {
2836 vim_free(line);
2837 line = vim_strsave((char_u *)"");
2838 }
2839
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002840 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2841 ++gap->ga_len;
2842 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002843 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002844 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002845 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002846 evalarg->eval_tofree = line;
2847 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002848
2849 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002850 // line. The caller assigns the return value to "arg".
2851 // If "arg" is NULL, then the return value is discarded. In that case,
2852 // "arg" still points to the previous line. So don't reset
2853 // "eval_using_cmdline".
2854 if (arg != NULL)
2855 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002856 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002857}
2858
Bram Moolenaare6b53242020-07-01 17:28:33 +02002859/*
2860 * Call eval_next_non_blank() and get the next line if needed.
2861 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002862 char_u *
2863skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2864{
2865 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002866 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002867
Bram Moolenaar962d7212020-07-04 14:15:00 +02002868 if (evalarg == NULL)
2869 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002870 eval_next_non_blank(p, evalarg, &getnext);
2871 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002872 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002873 return p;
2874}
2875
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002876/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002877 * The "eval" functions have an "evalarg" argument: When NULL or
2878 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2879 * parsed but not executed. The functions may return OK, but the rettv will be
2880 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 */
2882
2883/*
2884 * Handle zero level expression.
2885 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002887 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * Return OK or FAIL.
2890 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002892eval0(
2893 char_u *arg,
2894 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002895 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002898 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2899}
2900
2901/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002902 * If "arg" is a simple function call without arguments then call it and return
2903 * the result. Otherwise return NOTDONE.
2904 */
2905 int
2906may_call_simple_func(
2907 char_u *arg,
2908 typval_T *rettv)
2909{
2910 char_u *parens = (char_u *)strstr((char *)arg, "()");
2911 int r = NOTDONE;
2912
2913 // If the expression is "FuncName()" then we can skip a lot of overhead.
2914 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2915 {
2916 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2917
2918 if (to_name_end(p, TRUE) == parens)
2919 r = call_simple_func(arg, (int)(parens - arg), rettv);
2920 }
2921 return r;
2922}
2923
2924/*
2925 * Handle zero level expression with optimization for a simple function call.
2926 * Same arguments and return value as eval0().
2927 */
2928 int
2929eval0_simple_funccal(
2930 char_u *arg,
2931 typval_T *rettv,
2932 exarg_T *eap,
2933 evalarg_T *evalarg)
2934{
2935 int r = may_call_simple_func(arg, rettv);
2936
2937 if (r == NOTDONE)
2938 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2939 return r;
2940}
2941
2942/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002943 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2944 * expression and don't check what comes after the expression.
2945 */
2946 int
2947eval0_retarg(
2948 char_u *arg,
2949 typval_T *rettv,
2950 exarg_T *eap,
2951 evalarg_T *evalarg,
2952 char_u **retarg)
2953{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 int ret;
2955 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002956 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002957 int did_emsg_before = did_emsg;
2958 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002959 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002960 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002963 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002964
Bram Moolenaar79481362022-06-27 20:15:10 +01002965 if (ret != FAIL)
2966 {
2967 expr_end = p;
2968 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002969
Bram Moolenaar79481362022-06-27 20:15:10 +01002970 // In Vim9 script a command block is not split at NL characters for
2971 // commands using an expression argument. Skip over a '#' comment to
2972 // check for a following NL. Require white space before the '#'.
2973 if (in_vim9script() && p > expr_end && retarg == NULL)
2974 while (*p == '#')
2975 {
2976 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002977
Bram Moolenaar79481362022-06-27 20:15:10 +01002978 if (nl == NULL)
2979 break;
2980 p = skipwhite(nl + 1);
2981 if (eap != NULL && *p != NUL)
2982 eap->nextcmd = p;
2983 check_for_end = FALSE;
2984 }
2985
2986 if (check_for_end)
2987 end_error = !ends_excmd2(arg, p);
2988 }
2989
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002990 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
2992 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 /*
2995 * Report the invalid expression unless the expression evaluation has
2996 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002997 * exception, or we already gave a more specific error.
2998 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02003000 if (!aborting()
3001 && did_emsg == did_emsg_before
3002 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003003 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003004 {
3005 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003006 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003007 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003008 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003009 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003010
zeertzjqf2588b62023-05-05 17:22:35 +01003011 if (eap != NULL && p != NULL)
3012 {
3013 // Some of the expression may not have been consumed.
3014 // Only execute a next command if it cannot be a "||" operator.
3015 // The next command may be "catch".
3016 char_u *nextcmd = check_nextcmd(p);
3017 if (nextcmd != NULL && *nextcmd != '|')
3018 eap->nextcmd = nextcmd;
3019 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003022
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003023 if (retarg != NULL)
3024 *retarg = p;
3025 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003026 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 return ret;
3029}
3030
3031/*
3032 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003033 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003034 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 *
3036 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003037 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003039 * Note: "rettv.v_lock" is not set.
3040 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 * Return OK or FAIL.
3042 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003043 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003044eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003046 char_u *p;
3047 int getnext;
3048
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003049 CLEAR_POINTER(rettv);
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 /*
3052 * Get the first variable.
3053 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003054 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 return FAIL;
3056
Bram Moolenaar793648f2020-06-26 21:28:25 +02003057 p = eval_next_non_blank(*arg, evalarg, &getnext);
3058 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003060 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003061 int result;
3062 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003063 evalarg_T *evalarg_used = evalarg;
3064 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003066 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003067 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003068
3069 if (evalarg == NULL)
3070 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003071 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003072 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003073 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003074 orig_flags = evalarg_used->eval_flags;
3075 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003076
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003077 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003078 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003079 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003080 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003081 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003082 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003083 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003084 clear_tv(rettv);
3085 return FAIL;
3086 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003087 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003088 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003091 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093 int error = FALSE;
3094
Bram Moolenaar13106602020-10-04 16:06:05 +02003095 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003096 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003097 else if (vim9script)
3098 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003099 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003101 if (error || !op_falsy || !result)
3102 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003103 if (error)
3104 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106
3107 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003108 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003110 if (op_falsy)
3111 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003112 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003113 {
mityu4ac198c2021-05-28 17:52:40 +02003114 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003115 clear_tv(rettv);
3116 return FAIL;
3117 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003118 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003119 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003120 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003121 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003122 {
3123 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003125 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003126 if (!op_falsy || !result)
3127 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003129 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003131 /*
3132 * Check for the ":".
3133 */
3134 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3135 if (*p != ':')
3136 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003137 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003138 if (evaluate && result)
3139 clear_tv(rettv);
3140 evalarg_used->eval_flags = orig_flags;
3141 return FAIL;
3142 }
3143 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003144 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003145 else
3146 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003147 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003148 {
3149 error_white_both(p, 1);
3150 clear_tv(rettv);
3151 evalarg_used->eval_flags = orig_flags;
3152 return FAIL;
3153 }
3154 *arg = p;
3155 }
3156
3157 /*
3158 * Get the third variable. Recursive!
3159 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003160 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003161 {
mityu4ac198c2021-05-28 17:52:40 +02003162 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003163 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003164 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003165 return FAIL;
3166 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003167 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3168 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003169 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003170 if (eval1(arg, &var2, evalarg_used) == FAIL)
3171 {
3172 if (evaluate && result)
3173 clear_tv(rettv);
3174 evalarg_used->eval_flags = orig_flags;
3175 return FAIL;
3176 }
3177 if (evaluate && !result)
3178 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003180
3181 if (evalarg == NULL)
3182 clear_evalarg(&local_evalarg, NULL);
3183 else
3184 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186
3187 return OK;
3188}
3189
3190/*
3191 * Handle first level expression:
3192 * expr2 || expr2 || expr2 logical OR
3193 *
3194 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003195 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 *
3197 * Return OK or FAIL.
3198 */
3199 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003200eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003202 char_u *p;
3203 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003206 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 return FAIL;
3210
3211 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003212 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003214 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003215 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003217 evalarg_T *evalarg_used = evalarg;
3218 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003219 int evaluate;
3220 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003221 long result = FALSE;
3222 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003223 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003224 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003225
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003226 if (evalarg == NULL)
3227 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003228 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003229 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003230 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003231 orig_flags = evalarg_used->eval_flags;
3232 evaluate = orig_flags & EVAL_EVALUATE;
3233 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003234 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003235 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003236 result = tv_get_bool_chk(rettv, &error);
3237 else if (tv_get_number_chk(rettv, &error) != 0)
3238 result = TRUE;
3239 clear_tv(rettv);
3240 if (error)
3241 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243
3244 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003245 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003247 while (p[0] == '|' && p[1] == '|')
3248 {
3249 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003250 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003251 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003252 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003253 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003254 {
3255 error_white_both(p, 2);
3256 clear_tv(rettv);
3257 return FAIL;
3258 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003259 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003260 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003261
3262 /*
3263 * Get the second variable.
3264 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003265 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003266 {
mityu4ac198c2021-05-28 17:52:40 +02003267 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003268 clear_tv(rettv);
3269 return FAIL;
3270 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003271 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3272 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003273 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003274 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003275 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003276
3277 /*
3278 * Compute the result.
3279 */
3280 if (evaluate && !result)
3281 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003282 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003283 result = tv_get_bool_chk(&var2, &error);
3284 else if (tv_get_number_chk(&var2, &error) != 0)
3285 result = TRUE;
3286 clear_tv(&var2);
3287 if (error)
3288 return FAIL;
3289 }
3290 if (evaluate)
3291 {
3292 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003293 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003294 rettv->v_type = VAR_BOOL;
3295 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003296 }
3297 else
3298 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003299 rettv->v_type = VAR_NUMBER;
3300 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003301 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003302 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003303
3304 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003306
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003307 if (evalarg == NULL)
3308 clear_evalarg(&local_evalarg, NULL);
3309 else
3310 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312
3313 return OK;
3314}
3315
3316/*
3317 * Handle second level expression:
3318 * expr3 && expr3 && expr3 logical AND
3319 *
3320 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003321 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 *
3323 * Return OK or FAIL.
3324 */
3325 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003326eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003328 char_u *p;
3329 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003332 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003334 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return FAIL;
3336
3337 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003338 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003340 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003341 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003343 evalarg_T *evalarg_used = evalarg;
3344 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003345 int orig_flags;
3346 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003347 long result = TRUE;
3348 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003349 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003350 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003351
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003352 if (evalarg == NULL)
3353 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003354 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003355 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003356 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003357 orig_flags = evalarg_used->eval_flags;
3358 evaluate = orig_flags & EVAL_EVALUATE;
3359 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003360 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003361 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003362 result = tv_get_bool_chk(rettv, &error);
3363 else if (tv_get_number_chk(rettv, &error) == 0)
3364 result = FALSE;
3365 clear_tv(rettv);
3366 if (error)
3367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369
3370 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003371 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003373 while (p[0] == '&' && p[1] == '&')
3374 {
3375 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003376 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003377 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003378 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003379 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003380 {
3381 error_white_both(p, 2);
3382 clear_tv(rettv);
3383 return FAIL;
3384 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003385 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003386 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003387
3388 /*
3389 * Get the second variable.
3390 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003391 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003392 {
mityu4ac198c2021-05-28 17:52:40 +02003393 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003394 clear_tv(rettv);
3395 return FAIL;
3396 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003397 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3398 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003399 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003400 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003401 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003402 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003403
3404 /*
3405 * Compute the result.
3406 */
3407 if (evaluate && result)
3408 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003409 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003410 result = tv_get_bool_chk(&var2, &error);
3411 else if (tv_get_number_chk(&var2, &error) == 0)
3412 result = FALSE;
3413 clear_tv(&var2);
3414 if (error)
3415 return FAIL;
3416 }
3417 if (evaluate)
3418 {
3419 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003420 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003421 rettv->v_type = VAR_BOOL;
3422 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003423 }
3424 else
3425 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003426 rettv->v_type = VAR_NUMBER;
3427 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003428 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003429 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003430
3431 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003433
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003434 if (evalarg == NULL)
3435 clear_evalarg(&local_evalarg, NULL);
3436 else
3437 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439
3440 return OK;
3441}
3442
3443/*
3444 * Handle third level expression:
3445 * var1 == var2
3446 * var1 =~ var2
3447 * var1 != var2
3448 * var1 !~ var2
3449 * var1 > var2
3450 * var1 >= var2
3451 * var1 < var2
3452 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003453 * var1 is var2
3454 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 *
3456 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003457 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 *
3459 * Return OK or FAIL.
3460 */
3461 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003462eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003465 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003466 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003468 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003471 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003473 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 return FAIL;
3475
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003476 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003477
Bram Moolenaar696ba232020-07-29 21:20:41 +02003478 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003481 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003483 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003485 typval_T var2;
3486 int ic;
3487 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003488 int evaluate = evalarg == NULL
3489 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003490 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003491
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003492 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003493 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003494 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003495 p = *arg;
3496 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003497 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3498 {
mityu4ac198c2021-05-28 17:52:40 +02003499 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003500 clear_tv(rettv);
3501 return FAIL;
3502 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003503
Bram Moolenaar696ba232020-07-29 21:20:41 +02003504 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3505 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003506 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003507 clear_tv(rettv);
3508 return FAIL;
3509 }
3510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003511 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (p[len] == '?')
3513 {
3514 ic = TRUE;
3515 ++len;
3516 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003517 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 else if (p[len] == '#')
3519 {
3520 ic = FALSE;
3521 ++len;
3522 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003523 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003525 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
3527 /*
3528 * Get the second variable.
3529 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003530 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3531 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003532 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003533 clear_tv(rettv);
3534 return FAIL;
3535 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003536 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003537 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003542 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003543 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003544 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003545
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003546 // use the line of the comparison for messages
3547 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003548 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003549 {
3550 ret = FAIL;
3551 clear_tv(rettv);
3552 }
3553 else
3554 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003555 clear_tv(&var2);
3556 return ret;
3557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559
3560 return OK;
3561}
3562
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003563/*
3564 * Make a copy of blob "tv1" and append blob "tv2".
3565 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 void
3567eval_addblob(typval_T *tv1, typval_T *tv2)
3568{
3569 blob_T *b1 = tv1->vval.v_blob;
3570 blob_T *b2 = tv2->vval.v_blob;
3571 blob_T *b = blob_alloc();
3572 int i;
3573
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003574 if (b == NULL)
3575 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003577 for (i = 0; i < blob_len(b1); i++)
3578 ga_append(&b->bv_ga, blob_get(b1, i));
3579 for (i = 0; i < blob_len(b2); i++)
3580 ga_append(&b->bv_ga, blob_get(b2, i));
3581
3582 clear_tv(tv1);
3583 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584}
3585
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003586/*
3587 * Make a copy of list "tv1" and append list "tv2".
3588 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 int
3590eval_addlist(typval_T *tv1, typval_T *tv2)
3591{
3592 typval_T var3;
3593
3594 // concatenate Lists
3595 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3596 {
3597 clear_tv(tv1);
3598 clear_tv(tv2);
3599 return FAIL;
3600 }
3601 clear_tv(tv1);
3602 *tv1 = var3;
3603 return OK;
3604}
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003607 * Handle the bitwise left/right shift operator expression:
3608 * var1 << var2
3609 * var1 >> var2
3610 *
3611 * "arg" must point to the first non-white of the expression.
3612 * "arg" is advanced to just after the recognized expression.
3613 *
3614 * Return OK or FAIL.
3615 */
3616 static int
3617eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3618{
3619 /*
3620 * Get the first expression.
3621 */
3622 if (eval6(arg, rettv, evalarg) == FAIL)
3623 return FAIL;
3624
3625 /*
3626 * Repeat computing, until no '<<' or '>>' is following.
3627 */
3628 for (;;)
3629 {
3630 char_u *p;
3631 int getnext;
3632 exprtype_T type;
3633 int evaluate;
3634 typval_T var2;
3635 int vim9script;
3636
3637 p = eval_next_non_blank(*arg, evalarg, &getnext);
3638 if (p[0] == '<' && p[1] == '<')
3639 type = EXPR_LSHIFT;
3640 else if (p[0] == '>' && p[1] == '>')
3641 type = EXPR_RSHIFT;
3642 else
3643 return OK;
3644
3645 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003646 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3647 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003648 {
3649 // left operand should be a number
3650 emsg(_(e_bitshift_ops_must_be_number));
3651 clear_tv(rettv);
3652 return FAIL;
3653 }
3654
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003655 vim9script = in_vim9script();
3656 if (getnext)
3657 {
3658 *arg = eval_next_line(*arg, evalarg);
3659 p = *arg;
3660 }
3661 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3662 {
3663 error_white_both(*arg, 2);
3664 clear_tv(rettv);
3665 return FAIL;
3666 }
3667
3668 /*
3669 * Get the second variable.
3670 */
3671 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3672 {
3673 error_white_both(p, 2);
3674 clear_tv(rettv);
3675 return FAIL;
3676 }
3677 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3678 if (eval6(arg, &var2, evalarg) == FAIL)
3679 {
3680 clear_tv(rettv);
3681 return FAIL;
3682 }
3683
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003684 if (evaluate)
3685 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003686 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3687 {
3688 // right operand should be a positive number
3689 if (var2.v_type != VAR_NUMBER)
3690 emsg(_(e_bitshift_ops_must_be_number));
3691 else
3692 emsg(_(e_bitshift_ops_must_be_positive));
3693 clear_tv(rettv);
3694 clear_tv(&var2);
3695 return FAIL;
3696 }
3697
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003698 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3699 // shifting more bits than we have always results in zero
3700 rettv->vval.v_number = 0;
3701 else if (type == EXPR_LSHIFT)
3702 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003703 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003704 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003705 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003706 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003707 }
3708
3709 clear_tv(&var2);
3710 }
3711
3712 return OK;
3713}
3714
3715/*
3716 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003717 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003719 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003720 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 *
3722 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003723 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 *
3725 * Return OK or FAIL.
3726 */
3727 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003728eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003731 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003733 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 return FAIL;
3735
3736 /*
3737 * Repeat computing, until no '+', '-' or '.' is following.
3738 */
3739 for (;;)
3740 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003741 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003742 int getnext;
3743 char_u *p;
3744 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003745 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003746 int concat;
3747 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003748 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003749
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003750 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003751 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003752 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003753 p = eval_next_non_blank(*arg, evalarg, &getnext);
3754 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003755 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003756 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3757 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003759 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3760 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003761
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003762 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003763 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003764 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003765 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003766 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003767 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003768 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003769 {
mityu4ac198c2021-05-28 17:52:40 +02003770 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003771 clear_tv(rettv);
3772 return FAIL;
3773 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003774 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003775 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003776 if ((op != '+' || (rettv->v_type != VAR_LIST
3777 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003778 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003779 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003781 int error = FALSE;
3782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // For "list + ...", an illegal use of the first operand as
3784 // a number cannot be determined before evaluating the 2nd
3785 // operand: if this is also a list, all is ok.
3786 // For "something . ...", "something - ..." or "non-list + ...",
3787 // we know that the first operand needs to be a string or number
3788 // without evaluating the 2nd operand. So check before to avoid
3789 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003790 if (op != '.')
3791 tv_get_number_chk(rettv, &error);
3792 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 {
3794 clear_tv(rettv);
3795 return FAIL;
3796 }
3797 }
3798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 /*
3800 * Get the second variable.
3801 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003802 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003803 {
mityu89dcb4d2021-05-28 13:50:17 +02003804 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003805 clear_tv(rettv);
3806 return FAIL;
3807 }
3808 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003809 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003811 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 return FAIL;
3813 }
3814
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003815 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
3817 /*
3818 * Compute the result.
3819 */
3820 if (op == '.')
3821 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003822 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3823 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003824 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003825
Bram Moolenaar2e086612020-08-25 22:37:48 +02003826 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003827 || var2.v_type == VAR_CHANNEL
3828 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003829 semsg(_(e_using_invalid_value_as_string_str),
3830 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003831 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003832 {
3833 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3834 var2.vval.v_float);
3835 s2 = buf2;
3836 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003837 else
3838 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003839 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003840 {
3841 clear_tv(rettv);
3842 clear_tv(&var2);
3843 return FAIL;
3844 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003845 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 clear_tv(rettv);
3847 rettv->v_type = VAR_STRING;
3848 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003850 else if (op == '+' && rettv->v_type == VAR_BLOB
3851 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003853 else if (op == '+' && rettv->v_type == VAR_LIST
3854 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003855 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003857 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else
3860 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003861 int error = FALSE;
3862 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003863 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 f1 = rettv->vval.v_float;
3868 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003870 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003872 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873 if (error)
3874 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003875 // This can only happen for "list + non-list" or
3876 // "blob + non-blob". For "non-list + ..." or
3877 // "something - ...", we returned before evaluating the
3878 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003879 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003880 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003881 return FAIL;
3882 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003883 if (var2.v_type == VAR_FLOAT)
3884 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003885 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003886 if (var2.v_type == VAR_FLOAT)
3887 {
3888 f2 = var2.vval.v_float;
3889 n2 = 0;
3890 }
3891 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003894 if (error)
3895 {
3896 clear_tv(rettv);
3897 clear_tv(&var2);
3898 return FAIL;
3899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003900 if (rettv->v_type == VAR_FLOAT)
3901 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003906 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3907 {
3908 if (op == '+')
3909 f1 = f1 + f2;
3910 else
3911 f1 = f1 - f2;
3912 rettv->v_type = VAR_FLOAT;
3913 rettv->vval.v_float = f1;
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003916 {
3917 if (op == '+')
3918 n1 = n1 + n2;
3919 else
3920 n1 = n1 - n2;
3921 rettv->v_type = VAR_NUMBER;
3922 rettv->vval.v_number = n1;
3923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 }
3928 return OK;
3929}
3930
3931/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003932 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * * number multiplication
3934 * / number division
3935 * % number modulo
3936 *
3937 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003938 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 *
3940 * Return OK or FAIL.
3941 */
3942 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003943eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003944 char_u **arg,
3945 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003946 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003947 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003949 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003952 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003954 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return FAIL;
3956
3957 /*
3958 * Repeat computing, until no '*', '/' or '%' is following.
3959 */
3960 for (;;)
3961 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003962 int evaluate;
3963 int getnext;
3964 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003965 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003966 int op;
3967 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003968 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003969 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003970
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003971 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003972 p = eval_next_non_blank(*arg, evalarg, &getnext);
3973 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003974 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003976
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003977 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003978 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003979 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003980 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003981 {
3982 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3983 {
mityu4ac198c2021-05-28 17:52:40 +02003984 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003985 clear_tv(rettv);
3986 return FAIL;
3987 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003988 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003991 f1 = 0;
3992 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003993 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003994 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003996 if (rettv->v_type == VAR_FLOAT)
3997 {
3998 f1 = rettv->vval.v_float;
3999 use_float = TRUE;
4000 n1 = 0;
4001 }
4002 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004003 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 if (error)
4006 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008 else
4009 n1 = 0;
4010
4011 /*
4012 * Get the second variable.
4013 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004014 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4015 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004016 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004017 clear_tv(rettv);
4018 return FAIL;
4019 }
4020 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004021 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return FAIL;
4023
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004024 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004026 if (var2.v_type == VAR_FLOAT)
4027 {
4028 if (!use_float)
4029 {
4030 f1 = n1;
4031 use_float = TRUE;
4032 }
4033 f2 = var2.vval.v_float;
4034 n2 = 0;
4035 }
4036 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004037 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004038 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004039 clear_tv(&var2);
4040 if (error)
4041 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004042 if (use_float)
4043 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046 /*
4047 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004048 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004050 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004052 if (op == '*')
4053 f1 = f1 * f2;
4054 else if (op == '/')
4055 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004056#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004057 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004058 if (f2 == 0.0)
4059 {
4060 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004061 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004062 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004063 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004064 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004065 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004066 }
4067 else
4068 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004069#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004070 // We rely on the floating point library to handle divide
4071 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004073#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004077 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004078 return FAIL;
4079 }
4080 rettv->v_type = VAR_FLOAT;
4081 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 else
4084 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004085 int failed = FALSE;
4086
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 if (op == '*')
4088 n1 = n1 * n2;
4089 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004090 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004092 n1 = num_modulus(n1, n2, &failed);
4093 if (failed)
4094 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004095
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004096 rettv->v_type = VAR_NUMBER;
4097 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 }
4101
4102 return OK;
4103}
4104
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004105/*
4106 * Handle a type cast before a base level expression.
4107 * "arg" must point to the first non-white of the expression.
4108 * "arg" is advanced to just after the recognized expression.
4109 * Return OK or FAIL.
4110 */
4111 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004112eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004113 char_u **arg,
4114 typval_T *rettv,
4115 evalarg_T *evalarg,
4116 int want_string) // after "." operator
4117{
4118 type_T *want_type = NULL;
4119 garray_T type_list; // list of pointers to allocated types
4120 int res;
4121 int evaluate = evalarg == NULL ? 0
4122 : (evalarg->eval_flags & EVAL_EVALUATE);
4123
4124 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004125 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4126 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004127 {
4128 ++*arg;
4129 ga_init2(&type_list, sizeof(type_T *), 10);
4130 want_type = parse_type(arg, &type_list, TRUE);
4131 if (want_type == NULL && (evaluate || **arg != '>'))
4132 {
4133 clear_type_list(&type_list);
4134 return FAIL;
4135 }
4136
4137 if (**arg != '>')
4138 {
4139 if (*skipwhite(*arg) == '>')
4140 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4141 else
4142 emsg(_(e_missing_gt));
4143 clear_type_list(&type_list);
4144 return FAIL;
4145 }
4146 ++*arg;
4147 *arg = skipwhite_and_linebreak(*arg, evalarg);
4148 }
4149
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004150 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004151
4152 if (want_type != NULL && evaluate)
4153 {
4154 if (res == OK)
4155 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004156 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4157 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004158
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004159 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004160 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004161 if (want_type->tt_type == VAR_BOOL
4162 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004163 && (actual->tt_flags & TTFLAG_BOOL_OK))
4164 {
4165 int n = tv2bool(rettv);
4166
4167 // can use "0" and "1" for boolean in some places
4168 clear_tv(rettv);
4169 rettv->v_type = VAR_BOOL;
4170 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4171 }
4172 else
4173 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004174 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004175
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004176 res = check_type(want_type, actual, TRUE, where);
4177 }
4178 }
4179 }
4180 clear_type_list(&type_list);
4181 }
4182
4183 return res;
4184}
4185
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004186 int
4187eval_leader(char_u **arg, int vim9)
4188{
4189 char_u *s = *arg;
4190 char_u *p = *arg;
4191
4192 while (*p == '!' || *p == '-' || *p == '+')
4193 {
4194 char_u *n = skipwhite(p + 1);
4195
4196 // ++, --, -+ and +- are not accepted in Vim9 script
4197 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4198 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004199 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004200 return FAIL;
4201 }
4202 p = n;
4203 }
4204 *arg = p;
4205 return OK;
4206}
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004209 * Check for a predefined value "true", "false" and "null.*".
4210 * Return OK when recognized.
4211 */
4212 int
4213handle_predefined(char_u *s, int len, typval_T *rettv)
4214{
4215 switch (len)
4216 {
4217 case 4: if (STRNCMP(s, "true", 4) == 0)
4218 {
4219 rettv->v_type = VAR_BOOL;
4220 rettv->vval.v_number = VVAL_TRUE;
4221 return OK;
4222 }
4223 if (STRNCMP(s, "null", 4) == 0)
4224 {
4225 rettv->v_type = VAR_SPECIAL;
4226 rettv->vval.v_number = VVAL_NULL;
4227 return OK;
4228 }
4229 break;
4230 case 5: if (STRNCMP(s, "false", 5) == 0)
4231 {
4232 rettv->v_type = VAR_BOOL;
4233 rettv->vval.v_number = VVAL_FALSE;
4234 return OK;
4235 }
4236 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004237 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4238 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004239#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004240 rettv->v_type = VAR_JOB;
4241 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004242#else
4243 rettv->v_type = VAR_SPECIAL;
4244 rettv->vval.v_number = VVAL_NULL;
4245#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004246 return OK;
4247 }
4248 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004249 case 9:
4250 if (STRNCMP(s, "null_", 5) != 0)
4251 break;
4252 if (STRNCMP(s + 5, "list", 4) == 0)
4253 {
4254 rettv->v_type = VAR_LIST;
4255 rettv->vval.v_list = NULL;
4256 return OK;
4257 }
4258 if (STRNCMP(s + 5, "dict", 4) == 0)
4259 {
4260 rettv->v_type = VAR_DICT;
4261 rettv->vval.v_dict = NULL;
4262 return OK;
4263 }
4264 if (STRNCMP(s + 5, "blob", 4) == 0)
4265 {
4266 rettv->v_type = VAR_BLOB;
4267 rettv->vval.v_blob = NULL;
4268 return OK;
4269 }
4270 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004271 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4272 {
4273 rettv->v_type = VAR_CLASS;
4274 rettv->vval.v_class = NULL;
4275 return OK;
4276 }
4277 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004278 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4279 {
4280 rettv->v_type = VAR_STRING;
4281 rettv->vval.v_string = NULL;
4282 return OK;
4283 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004284 if (STRNCMP(s, "null_object", 11) == 0)
4285 {
4286 rettv->v_type = VAR_OBJECT;
4287 rettv->vval.v_object = NULL;
4288 return OK;
4289 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004290 break;
4291 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004292 if (STRNCMP(s, "null_channel", 12) == 0)
4293 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004294#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004295 rettv->v_type = VAR_CHANNEL;
4296 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004297#else
4298 rettv->v_type = VAR_SPECIAL;
4299 rettv->vval.v_number = VVAL_NULL;
4300#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004301 return OK;
4302 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004303 if (STRNCMP(s, "null_partial", 12) == 0)
4304 {
4305 rettv->v_type = VAR_PARTIAL;
4306 rettv->vval.v_partial = NULL;
4307 return OK;
4308 }
4309 break;
4310 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4311 {
4312 rettv->v_type = VAR_FUNC;
4313 rettv->vval.v_string = NULL;
4314 return OK;
4315 }
4316 break;
4317 }
4318 return FAIL;
4319}
4320
4321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 * Handle sixth level expression:
4323 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004324 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004325 * "string" string constant
4326 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * &option-name option value
4328 * @r register contents
4329 * identifier variable value
4330 * function() function call
4331 * $VAR environment variable
4332 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004333 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004335 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004336 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 *
4338 * Also handle:
4339 * ! in front logical NOT
4340 * - in front unary minus
4341 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004342 * trailing [] subscript in String or List
4343 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004344 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 *
4346 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004347 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 *
4349 * Return OK or FAIL.
4350 */
4351 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004352eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004353 char_u **arg,
4354 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004355 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004358 int evaluate = evalarg != NULL
4359 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 int len;
4361 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004362 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 char_u *start_leader, *end_leader;
4364 int ret = OK;
4365 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004366 static int recurse = 0;
4367 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004371 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
4375 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004376 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
4378 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004379 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004380 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 end_leader = *arg;
4382
Keith Thompson184f71c2024-01-04 21:19:04 +01004383 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004384 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004385 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004386 ++*arg;
4387 return FAIL;
4388 }
4389
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004390 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004391 // and crash. With MSVC the stack is smaller.
4392 if (recurse ==
4393#ifdef _MSC_VER
4394 300
4395#else
4396 1000
4397#endif
4398 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004399 {
4400 semsg(_(e_expression_too_recursive_str), *arg);
4401 return FAIL;
4402 }
4403 ++recurse;
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 switch (**arg)
4406 {
4407 /*
4408 * Number constant.
4409 */
4410 case '0':
4411 case '1':
4412 case '2':
4413 case '3':
4414 case '4':
4415 case '5':
4416 case '6':
4417 case '7':
4418 case '8':
4419 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004420 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004421
4422 // Apply prefixed "-" and "+" now. Matters especially when
4423 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004424 if (ret == OK && evaluate && end_leader > start_leader
4425 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004426 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 break;
4428
4429 /*
4430 * String constant: "string".
4431 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004432 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004438 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439 break;
4440
4441 /*
4442 * List: [expr, expr]
4443 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004444 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 break;
4446
4447 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004448 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004449 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004450 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004451 {
4452 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4453 }
4454 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004455 {
4456 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004457 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004458 }
4459 else
4460 ret = NOTDONE;
4461 break;
4462
4463 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004464 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004465 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004466 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004467 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004468 ret = NOTDONE;
4469 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004470 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004471 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004472 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 break;
4474
4475 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004476 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004478 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 break;
4480
4481 /*
4482 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004483 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
LemonBoy2eaef102022-05-06 13:14:50 +01004485 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4486 ret = eval_interp_string(arg, rettv, evaluate);
4487 else
4488 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 break;
4490
4491 /*
4492 * Register contents: @r.
4493 */
4494 case '@': ++*arg;
4495 if (evaluate)
4496 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004497 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004498 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004499 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004500 emsg_invreg(**arg);
4501 else
4502 {
4503 rettv->v_type = VAR_STRING;
4504 rettv->vval.v_string = get_reg_contents(**arg,
4505 GREG_EXPR_SRC);
4506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 if (**arg != NUL)
4509 ++*arg;
4510 break;
4511
4512 /*
4513 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004514 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004516 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004517 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004518 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004519 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004520 if (ret == OK && evaluate)
4521 {
4522 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4523
Bram Moolenaara9931532021-06-12 15:58:16 +02004524 // Compile it here to get the return type. The return
4525 // type is optional, when it's missing use t_unknown.
4526 // This is recognized in compile_return().
4527 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4528 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004529 if (compile_def_function(ufunc, FALSE,
4530 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004531 {
4532 clear_tv(rettv);
4533 ret = FAIL;
4534 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004535 }
4536 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004537 if (ret == NOTDONE)
4538 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004539 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004540 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004541
Bram Moolenaar9215f012020-06-27 21:18:00 +02004542 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004543 if (**arg == ')')
4544 ++*arg;
4545 else if (ret == OK)
4546 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004547 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004548 clear_tv(rettv);
4549 ret = FAIL;
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 }
4552 break;
4553
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 break;
4556 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557
4558 if (ret == NOTDONE)
4559 {
4560 /*
4561 * Must be a variable or function name.
4562 * Can also be a curly-braces kind of name: {expr}.
4563 */
4564 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004565 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004566 if (alias != NULL)
4567 s = alias;
4568
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004569 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 ret = FAIL;
4571 else
4572 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004573 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4574
Bram Moolenaar4525a572022-02-13 11:57:33 +00004575 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004576 {
4577 emsg(_(e_cannot_use_underscore_here));
4578 ret = FAIL;
4579 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004580 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004581 && s[0] == 's' && s[1] == ':')
4582 {
4583 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4584 ret = FAIL;
4585 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004586 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004587 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004588 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004589 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004590 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004593 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004594 // get the value of "true", "false", etc. or a variable
4595 ret = FAIL;
4596 if (vim9script)
4597 ret = handle_predefined(s, len, rettv);
4598 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004599 {
4600 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004601 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004602 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004603 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004604 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004605 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004606 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004607 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004608 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004609 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004612 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 }
4614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004615 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4616 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004617 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004618 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 /*
4621 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4622 */
4623 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004624 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004625
4626 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004627 return ret;
4628}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004630/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004631 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004632 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004633 * Adjusts "end_leaderp" until it is at "start_leader".
4634 */
4635 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004636eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004637 typval_T *rettv,
4638 int numeric_only,
4639 char_u *start_leader,
4640 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004641{
4642 char_u *end_leader = *end_leaderp;
4643 int ret = OK;
4644 int error = FALSE;
4645 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004646 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004647 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004648 float_T f = 0.0;
4649
4650 if (rettv->v_type == VAR_FLOAT)
4651 f = rettv->vval.v_float;
4652 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004653 {
4654 while (VIM_ISWHITE(end_leader[-1]))
4655 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004656 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004657 val = tv2bool(rettv);
4658 else
4659 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004660 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004661 if (error)
4662 {
4663 clear_tv(rettv);
4664 ret = FAIL;
4665 }
4666 else
4667 {
4668 while (end_leader > start_leader)
4669 {
4670 --end_leader;
4671 if (*end_leader == '!')
4672 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004673 if (numeric_only)
4674 {
4675 ++end_leader;
4676 break;
4677 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004678 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004679 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004680 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004681 {
4682 rettv->v_type = VAR_BOOL;
4683 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4684 }
4685 else
4686 f = !f;
4687 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004688 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004689 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004690 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004691 type = VAR_BOOL;
4692 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004693 }
4694 else if (*end_leader == '-')
4695 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004696 if (rettv->v_type == VAR_FLOAT)
4697 f = -f;
4698 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004699 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004700 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004701 type = VAR_NUMBER;
4702 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004703 }
4704 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004705 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004708 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 else
4711 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004712 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004713 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004714 rettv->v_type = type;
4715 else
4716 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004717 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004720 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return ret;
4722}
4723
4724/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004725 * Call the function referred to in "rettv".
4726 */
4727 static int
4728call_func_rettv(
4729 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004730 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004731 typval_T *rettv,
4732 int evaluate,
4733 dict_T *selfdict,
4734 typval_T *basetv)
4735{
4736 partial_T *pt = NULL;
4737 funcexe_T funcexe;
4738 typval_T functv;
4739 char_u *s;
4740 int ret;
4741
4742 // need to copy the funcref so that we can clear rettv
4743 if (evaluate)
4744 {
4745 functv = *rettv;
4746 rettv->v_type = VAR_UNKNOWN;
4747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004749 if (functv.v_type == VAR_PARTIAL)
4750 {
4751 pt = functv.vval.v_partial;
4752 s = partial_name(pt);
4753 }
4754 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004755 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004756 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004757 if (s == NULL || *s == NUL)
4758 {
4759 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004760 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004761 goto theend;
4762 }
4763 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004764 }
4765 else
4766 s = (char_u *)"";
4767
Bram Moolenaara80faa82020-04-12 19:37:17 +02004768 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004769 funcexe.fe_firstline = curwin->w_cursor.lnum;
4770 funcexe.fe_lastline = curwin->w_cursor.lnum;
4771 funcexe.fe_evaluate = evaluate;
4772 funcexe.fe_partial = pt;
4773 funcexe.fe_selfdict = selfdict;
4774 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004775 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004776
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004777theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 // Clear the funcref afterwards, so that deleting it while
4779 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004780 if (evaluate)
4781 clear_tv(&functv);
4782
4783 return ret;
4784}
4785
4786/*
4787 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004788 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004789 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4790 */
4791 static int
4792eval_lambda(
4793 char_u **arg,
4794 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004795 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004797{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004798 int evaluate = evalarg != NULL
4799 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004800 typval_T base = *rettv;
4801 int ret;
4802
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004803 rettv->v_type = VAR_UNKNOWN;
4804
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004805 if (**arg == '{')
4806 {
4807 // ->{lambda}()
4808 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4809 }
4810 else
4811 {
4812 // ->(lambda)()
4813 ++*arg;
4814 ret = eval1(arg, rettv, evalarg);
4815 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004816 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004817 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004818 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004819 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004820 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004821 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4822 && rettv->v_type != VAR_PARTIAL)
4823 {
4824 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4825 return FAIL;
4826 }
4827 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004828 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004829 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004830 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004831
4832 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004833 {
4834 if (verbose)
4835 {
4836 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004837 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004838 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004839 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004840 }
4841 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004842 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004843 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004844 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004845 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004846
4847 // Clear the funcref afterwards, so that deleting it while
4848 // evaluating the arguments is possible (see test55).
4849 if (evaluate)
4850 clear_tv(&base);
4851
4852 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004853}
4854
4855/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004856 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004857 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004858 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4859 */
4860 static int
4861eval_method(
4862 char_u **arg,
4863 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004864 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004866{
4867 char_u *name;
4868 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004869 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004870 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004871 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004872 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004873 int evaluate = evalarg != NULL
4874 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004875
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004876 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004877
Bram Moolenaarac92e252019-08-03 21:58:38 +02004878 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004879 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004880 if (alias != NULL)
4881 name = alias;
4882
4883 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004884 {
4885 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004886 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004887 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004888 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004889 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004890 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004891 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004892
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004893 // If there is no "(" immediately following, but there is further on,
4894 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4895 // Does not handle anything where "(" is part of the expression.
4896 *arg = skipwhite(*arg);
4897
4898 if (**arg != '(' && alias == NULL
4899 && (paren = vim_strchr(*arg, '(')) != NULL)
4900 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004901 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004902
4903 // Truncate the name a the "(". Avoid trying to get another line
4904 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004905 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004906 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4907 if (evalarg != NULL)
4908 {
4909 getline = evalarg->eval_getline;
4910 evalarg->eval_getline = NULL;
4911 }
4912
4913 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004914 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004915 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004916 *arg = name + len;
4917 ret = FAIL;
4918 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004919 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004920 {
4921 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004922 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004923 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004924
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004925 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004926 if (getline != NULL)
4927 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004928 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004929
4930 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004931 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004932 *arg = skipwhite(*arg);
4933
4934 if (**arg != '(')
4935 {
4936 if (verbose)
4937 semsg(_(e_missing_parenthesis_str), name);
4938 ret = FAIL;
4939 }
4940 else if (VIM_ISWHITE((*arg)[-1]))
4941 {
4942 if (verbose)
4943 emsg(_(e_no_white_space_allowed_before_parenthesis));
4944 ret = FAIL;
4945 }
4946 else
4947 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004948 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004949 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004950 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004951
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004952 // Clear the funcref afterwards, so that deleting it while
4953 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004954 if (evaluate)
4955 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004956 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004957
Bram Moolenaarac92e252019-08-03 21:58:38 +02004958 return ret;
4959}
4960
4961/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004962 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4963 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4965 */
4966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004967eval_index(
4968 char_u **arg,
4969 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004970 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004973 int evaluate = evalarg != NULL
4974 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004979 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004980 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004982 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4983 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004985 init_tv(&var1);
4986 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 /*
4990 * dict.name
4991 */
4992 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004993 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004995 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004997 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004998 }
4999 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005000 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 /*
5002 * something[idx]
5003 *
5004 * Get the (first) variable from inside the [].
5005 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005006 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 if (**arg == ':')
5008 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005009 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005011 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005012 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005013 semsg(_(e_white_space_required_before_and_after_str_at_str),
5014 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005015 clear_tv(&var1);
5016 return FAIL;
5017 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005018 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005019 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005020 int error = FALSE;
5021
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005022 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005023 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005024 && var1.v_type == VAR_FLOAT)
5025 {
5026 var1.vval.v_string = typval_tostring(&var1, TRUE);
5027 var1.v_type = VAR_STRING;
5028 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005029
Bram Moolenaar4525a572022-02-13 11:57:33 +00005030 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005031 tv_get_number_chk(&var1, &error);
5032 else
5033 error = tv_get_string_chk(&var1) == NULL;
5034 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005035 {
5036 // not a number or string
5037 clear_tv(&var1);
5038 return FAIL;
5039 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041
5042 /*
5043 * Get the second variable from inside the [:].
5044 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005045 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 if (**arg == ':')
5047 {
5048 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005049 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005050 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005051 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005052 semsg(_(e_white_space_required_before_and_after_str_at_str),
5053 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005054 if (!empty1)
5055 clear_tv(&var1);
5056 return FAIL;
5057 }
5058 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 if (**arg == ']')
5060 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005061 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005063 if (!empty1)
5064 clear_tv(&var1);
5065 return FAIL;
5066 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005067 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005070 if (!empty1)
5071 clear_tv(&var1);
5072 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 return FAIL;
5074 }
5075 }
5076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005077 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005078 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 if (**arg != ']')
5080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005081 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005082 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 clear_tv(&var1);
5084 if (range)
5085 clear_tv(&var2);
5086 return FAIL;
5087 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005088 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005089 }
5090
5091 if (evaluate)
5092 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005093 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005094 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005095 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005096
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005097 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005100 clear_tv(&var2);
5101 return res;
5102 }
5103 return OK;
5104}
5105
5106/*
5107 * Check if "rettv" can have an [index] or [sli:ce]
5108 */
5109 int
5110check_can_index(typval_T *rettv, int evaluate, int verbose)
5111{
5112 switch (rettv->v_type)
5113 {
5114 case VAR_FUNC:
5115 case VAR_PARTIAL:
5116 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005117 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005118 return FAIL;
5119 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005120 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005121 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005122 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005123 case VAR_BOOL:
5124 case VAR_SPECIAL:
5125 case VAR_JOB:
5126 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005127 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005128 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005129 if (verbose)
5130 emsg(_(e_cannot_index_special_variable));
5131 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005132 case VAR_CLASS:
5133 case VAR_TYPEALIAS:
5134 if (verbose)
5135 check_typval_is_value(rettv);
5136 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005137 case VAR_UNKNOWN:
5138 case VAR_ANY:
5139 case VAR_VOID:
5140 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005142 emsg(_(e_cannot_index_special_variable));
5143 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005145 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005147 case VAR_STRING:
5148 case VAR_LIST:
5149 case VAR_DICT:
5150 case VAR_BLOB:
5151 break;
5152 case VAR_NUMBER:
5153 if (in_vim9script())
5154 emsg(_(e_cannot_index_number));
5155 break;
5156 }
5157 return OK;
5158}
5159
5160/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005161 * slice() function
5162 */
5163 void
5164f_slice(typval_T *argvars, typval_T *rettv)
5165{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005166 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005167 && ((argvars[0].v_type != VAR_STRING
5168 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005169 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005170 && check_for_list_arg(argvars, 0) == FAIL)
5171 || check_for_number_arg(argvars, 1) == FAIL
5172 || check_for_opt_number_arg(argvars, 2) == FAIL))
5173 return;
5174
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005175 if (check_can_index(argvars, TRUE, FALSE) != OK)
5176 return;
5177
5178 copy_tv(argvars, rettv);
5179 eval_index_inner(rettv, TRUE, argvars + 1,
5180 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5181 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005182}
5183
5184/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005185 * Apply index or range to "rettv".
5186 * "var1" is the first index, NULL for [:expr].
5187 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005188 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5189 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005190 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5191 */
5192 int
5193eval_index_inner(
5194 typval_T *rettv,
5195 int is_range,
5196 typval_T *var1,
5197 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005198 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005199 char_u *key,
5200 int keylen,
5201 int verbose)
5202{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005203 varnumber_T n1, n2 = 0;
5204 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005205
5206 n1 = 0;
5207 if (var1 != NULL && rettv->v_type != VAR_DICT)
5208 n1 = tv_get_number(var1);
5209
5210 if (is_range)
5211 {
5212 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005214 if (verbose)
5215 emsg(_(e_cannot_slice_dictionary));
5216 return FAIL;
5217 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005218 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005219 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005220 else
5221 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005222 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005223
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005224 switch (rettv->v_type)
5225 {
5226 case VAR_UNKNOWN:
5227 case VAR_ANY:
5228 case VAR_VOID:
5229 case VAR_FUNC:
5230 case VAR_PARTIAL:
5231 case VAR_FLOAT:
5232 case VAR_BOOL:
5233 case VAR_SPECIAL:
5234 case VAR_JOB:
5235 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005236 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005237 case VAR_CLASS:
5238 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005239 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005240 break; // not evaluating, skipping over subscript
5241
5242 case VAR_NUMBER:
5243 case VAR_STRING:
5244 {
5245 char_u *s = tv_get_string(rettv);
5246
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005248 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005249 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005250 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005251 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005252 else
5253 s = char_from_string(s, n1);
5254 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005255 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 // The resulting variable is a substring. If the indexes
5258 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 if (n1 < 0)
5260 {
5261 n1 = len + n1;
5262 if (n1 < 0)
5263 n1 = 0;
5264 }
5265 if (n2 < 0)
5266 n2 = len + n2;
5267 else if (n2 >= len)
5268 n2 = len;
5269 if (n1 >= len || n2 < 0 || n1 > n2)
5270 s = NULL;
5271 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005272 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 }
5274 else
5275 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 // The resulting variable is a string of a single
5277 // character. If the index is too big or negative the
5278 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 if (n1 >= len || n1 < 0)
5280 s = NULL;
5281 else
5282 s = vim_strnsave(s + n1, 1);
5283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005284 clear_tv(rettv);
5285 rettv->v_type = VAR_STRING;
5286 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005287 }
5288 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005290 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005291 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5292 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005293 break;
5294
5295 case VAR_LIST:
5296 if (var1 == NULL)
5297 n1 = 0;
5298 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005299 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005300 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005301 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005302 return FAIL;
5303 break;
5304
5305 case VAR_DICT:
5306 {
5307 dictitem_T *item;
5308 typval_T tmp;
5309
5310 if (key == NULL)
5311 {
5312 key = tv_get_string_chk(var1);
5313 if (key == NULL)
5314 return FAIL;
5315 }
5316
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005317 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005318
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005319 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005320 {
5321 if (verbose)
5322 {
5323 if (keylen > 0)
5324 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005325 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005326 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005327 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005328 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005329
5330 copy_tv(&item->di_tv, &tmp);
5331 clear_tv(rettv);
5332 *rettv = tmp;
5333 }
5334 break;
5335 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 return OK;
5337}
5338
5339/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005340 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005341 */
5342 char_u *
5343partial_name(partial_T *pt)
5344{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005345 if (pt != NULL)
5346 {
5347 if (pt->pt_name != NULL)
5348 return pt->pt_name;
5349 if (pt->pt_func != NULL)
5350 return pt->pt_func->uf_name;
5351 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005352 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005353}
5354
Bram Moolenaarddecc252016-04-06 22:59:37 +02005355 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005357{
5358 int i;
5359
5360 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005362 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005363 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005364 if (pt->pt_name != NULL)
5365 {
5366 func_unref(pt->pt_name);
5367 vim_free(pt->pt_name);
5368 }
5369 else
5370 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005371 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005372
Bram Moolenaar54656012021-06-09 20:50:46 +02005373 // "out_up" is no longer used, decrement refcount on partial that owns it.
5374 partial_unref(pt->pt_outer.out_up_partial);
5375
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005376 // Using pt_outer from another partial.
5377 partial_unref(pt->pt_outer_partial);
5378
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005379 // Decrease the reference count for the context of a closure. If down
5380 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005381 if (pt->pt_funcstack != NULL)
5382 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005383 --pt->pt_funcstack->fs_refcount;
5384 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005385 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005386 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005387 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5388 if (pt->pt_loopvars[i] != NULL)
5389 {
5390 --pt->pt_loopvars[i]->lvs_refcount;
5391 loopvars_check_refcount(pt->pt_loopvars[i]);
5392 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005393
Bram Moolenaarddecc252016-04-06 22:59:37 +02005394 vim_free(pt);
5395}
5396
5397/*
5398 * Unreference a closure: decrement the reference count and free it when it
5399 * becomes zero.
5400 */
5401 void
5402partial_unref(partial_T *pt)
5403{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005404 if (pt == NULL)
5405 return;
5406
5407 int done = FALSE;
5408
5409 if (--pt->pt_refcount <= 0)
5410 partial_free(pt);
5411
5412 // If the reference count goes down to one, the funcstack may be the
5413 // only reference and can be freed if no other partials reference it.
5414 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005415 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005416 // careful: if the funcstack is freed it may contain this partial
5417 // and it gets freed as well
5418 if (pt->pt_funcstack != NULL)
5419 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005420
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005421 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005422 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005423 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005424
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005425 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5426 if (pt->pt_loopvars[depth] != NULL
5427 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005428 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005429 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005430 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005431}
5432
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005433/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005434 * Return the next (unique) copy ID.
5435 * Used for serializing nested structures.
5436 */
5437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005438get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005439{
5440 current_copyID += COPYID_INC;
5441 return current_copyID;
5442}
5443
5444/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445 * Garbage collection for lists and dictionaries.
5446 *
5447 * We use reference counts to be able to free most items right away when they
5448 * are no longer used. But for composite items it's possible that it becomes
5449 * unused while the reference count is > 0: When there is a recursive
5450 * reference. Example:
5451 * :let l = [1, 2, 3]
5452 * :let d = {9: l}
5453 * :let l[1] = d
5454 *
5455 * Since this is quite unusual we handle this with garbage collection: every
5456 * once in a while find out which lists and dicts are not referenced from any
5457 * variable.
5458 *
5459 * Here is a good reference text about garbage collection (refers to Python
5460 * but it applies to all reference-counting mechanisms):
5461 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005462 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005463
5464/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005465 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005466 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005467 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005468 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005469 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005470garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005471{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005472 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005473 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005474 buf_T *buf;
5475 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005476 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005477 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005479 if (!testing)
5480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005481 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005482 want_garbage_collect = FALSE;
5483 may_garbage_collect = FALSE;
5484 garbage_collect_at_exit = FALSE;
5485 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005486
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005487 // The execution stack can grow big, limit the size.
5488 if (exestack.ga_maxlen - exestack.ga_len > 500)
5489 {
5490 size_t new_len;
5491 char_u *pp;
5492 int n;
5493
5494 // Keep 150% of the current size, with a minimum of the growth size.
5495 n = exestack.ga_len / 2;
5496 if (n < exestack.ga_growsize)
5497 n = exestack.ga_growsize;
5498
5499 // Don't make it bigger though.
5500 if (exestack.ga_len + n < exestack.ga_maxlen)
5501 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005502 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005503 pp = vim_realloc(exestack.ga_data, new_len);
5504 if (pp == NULL)
5505 return FAIL;
5506 exestack.ga_maxlen = exestack.ga_len + n;
5507 exestack.ga_data = pp;
5508 }
5509 }
5510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005511 // We advance by two because we add one for items referenced through
5512 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005513 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005514
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005515 /*
5516 * 1. Go through all accessible variables and mark all lists and dicts
5517 * with copyID.
5518 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // Don't free variables in the previous_funccal list unless they are only
5521 // referenced through previous_funccal. This must be first, because if
5522 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005526 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005528 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005529 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5531 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005534 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005535 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5536 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005537 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005538 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005539 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005540 abort = abort || set_ref_in_item(
5541 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005542#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005543 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005544 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5545 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005546 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005547 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005548 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5549 NULL, NULL);
5550#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005552 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005553 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005554 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5555 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005557 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005560 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005563 abort = abort || set_ref_in_functions(copyID);
5564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005565 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005566 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005567
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005568 // funcstacks keep variables for closures
5569 abort = abort || set_ref_in_funcstacks(copyID);
5570
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005571 // loopvars keep variables for loop blocks
5572 abort = abort || set_ref_in_loopvars(copyID);
5573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005575 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005576
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005577 // callbacks in buffers
5578 abort = abort || set_ref_in_buffers(copyID);
5579
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005580 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5581 abort = abort || set_ref_in_insexpand_funcs(copyID);
5582
5583 // 'operatorfunc' callback
5584 abort = abort || set_ref_in_opfunc(copyID);
5585
5586 // 'tagfunc' callback
5587 abort = abort || set_ref_in_tagfunc(copyID);
5588
5589 // 'imactivatefunc' and 'imstatusfunc' callbacks
5590 abort = abort || set_ref_in_im_funcs(copyID);
5591
Bram Moolenaar1dced572012-04-05 16:54:08 +02005592#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005593 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005594#endif
5595
Bram Moolenaardb913952012-06-29 12:54:53 +02005596#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005597 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005598#endif
5599
5600#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005601 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005602#endif
5603
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005604#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005605 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005606 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005607#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005608#ifdef FEAT_NETBEANS_INTG
5609 abort = abort || set_ref_in_nb_channel(copyID);
5610#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005611
Bram Moolenaare3188e22016-05-31 21:13:04 +02005612#ifdef FEAT_TIMERS
5613 abort = abort || set_ref_in_timer(copyID);
5614#endif
5615
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005616#ifdef FEAT_QUICKFIX
5617 abort = abort || set_ref_in_quickfix(copyID);
5618#endif
5619
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005620#ifdef FEAT_TERMINAL
5621 abort = abort || set_ref_in_term(copyID);
5622#endif
5623
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005624#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005625 abort = abort || set_ref_in_popups(copyID);
5626#endif
5627
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005628 abort = abort || set_ref_in_classes(copyID);
5629
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005630 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005631 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005632 /*
5633 * 2. Free lists and dictionaries that are not referenced.
5634 */
5635 did_free = free_unref_items(copyID);
5636
5637 /*
5638 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005639 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005640 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005642 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005643 else if (p_verbose > 0)
5644 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005645 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005646 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005647
5648 return did_free;
5649}
5650
5651/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005652 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005653 */
5654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005656{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005657 int did_free = FALSE;
5658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005659 // Let all "free" functions know that we are here. This means no
5660 // dictionaries, lists, channels or jobs are to be freed, because we will
5661 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005662 in_free_unref_items = TRUE;
5663
5664 /*
5665 * PASS 1: free the contents of the items. We don't free the items
5666 * themselves yet, so that it is possible to decrement refcount counters
5667 */
5668
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005669 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005670 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005671
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005672 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005673 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005674
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005675 // Go through the list of objects and free items without this copyID.
5676 did_free |= object_free_nonref(copyID);
5677
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005678 // Go through the list of classes and free items without this copyID.
5679 did_free |= class_free_nonref(copyID);
5680
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005681#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005682 // Go through the list of jobs and free items without the copyID. This
5683 // must happen before doing channels, because jobs refer to channels, but
5684 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005685 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005687 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005688 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5689#endif
5690
5691 /*
5692 * PASS 2: free the items themselves.
5693 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005694 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005695 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005696 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005697
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005698#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005699 // Go through the list of jobs and free items without the copyID. This
5700 // must happen before doing channels, because jobs refer to channels, but
5701 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005702 free_unused_jobs(copyID, COPYID_MASK);
5703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005704 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005705 free_unused_channels(copyID, COPYID_MASK);
5706#endif
5707
5708 in_free_unref_items = FALSE;
5709
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005710 return did_free;
5711}
5712
5713/*
5714 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005715 * "list_stack" is used to add lists to be marked. Can be NULL.
5716 *
5717 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005718 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005720set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005721{
5722 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005724 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005725 hashtab_T *cur_ht;
5726 ht_stack_T *ht_stack = NULL;
5727 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005728
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 cur_ht = ht;
5730 for (;;)
5731 {
5732 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005733 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005734 // Mark each item in the hashtab. If the item contains a hashtab
5735 // it is added to ht_stack, if it contains a list it is added to
5736 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005737 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005738 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005739 if (!HASHITEM_EMPTY(hi))
5740 {
5741 --todo;
5742 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5743 &ht_stack, list_stack);
5744 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005745 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746
5747 if (ht_stack == NULL)
5748 break;
5749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005750 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005751 cur_ht = ht_stack->ht;
5752 tempitem = ht_stack;
5753 ht_stack = ht_stack->prev;
5754 free(tempitem);
5755 }
5756
5757 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005758}
5759
Dominique Pelle748b3082022-01-08 12:41:16 +00005760#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5761 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005762/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005763 * Mark a dict and its items with "copyID".
5764 * Returns TRUE if setting references failed somehow.
5765 */
5766 int
5767set_ref_in_dict(dict_T *d, int copyID)
5768{
5769 if (d != NULL && d->dv_copyID != copyID)
5770 {
5771 d->dv_copyID = copyID;
5772 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5773 }
5774 return FALSE;
5775}
Dominique Pelle748b3082022-01-08 12:41:16 +00005776#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005777
5778/*
5779 * Mark a list and its items with "copyID".
5780 * Returns TRUE if setting references failed somehow.
5781 */
5782 int
5783set_ref_in_list(list_T *ll, int copyID)
5784{
5785 if (ll != NULL && ll->lv_copyID != copyID)
5786 {
5787 ll->lv_copyID = copyID;
5788 return set_ref_in_list_items(ll, copyID, NULL);
5789 }
5790 return FALSE;
5791}
5792
5793/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005794 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005795 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5796 *
5797 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005798 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005799 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005800set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005801{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005802 listitem_T *li;
5803 int abort = FALSE;
5804 list_T *cur_l;
5805 list_stack_T *list_stack = NULL;
5806 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005807
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005808 cur_l = l;
5809 for (;;)
5810 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005811 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005812 // Mark each item in the list. If the item contains a hashtab
5813 // it is added to ht_stack, if it contains a list it is added to
5814 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005815 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5816 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5817 ht_stack, &list_stack);
5818 if (list_stack == NULL)
5819 break;
5820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005821 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005822 cur_l = list_stack->list;
5823 tempitem = list_stack;
5824 list_stack = list_stack->prev;
5825 free(tempitem);
5826 }
5827
5828 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005829}
5830
5831/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005832 * Mark the partial in callback 'cb' with "copyID".
5833 */
5834 int
5835set_ref_in_callback(callback_T *cb, int copyID)
5836{
5837 typval_T tv;
5838
5839 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5840 return FALSE;
5841
5842 tv.v_type = VAR_PARTIAL;
5843 tv.vval.v_partial = cb->cb_partial;
5844 return set_ref_in_item(&tv, copyID, NULL, NULL);
5845}
5846
5847/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005848 * Mark the dict "dd" with "copyID".
5849 * Also see set_ref_in_item().
5850 */
5851 static int
5852set_ref_in_item_dict(
5853 dict_T *dd,
5854 int copyID,
5855 ht_stack_T **ht_stack,
5856 list_stack_T **list_stack)
5857{
5858 if (dd == NULL || dd->dv_copyID == copyID)
5859 return FALSE;
5860
5861 // Didn't see this dict yet.
5862 dd->dv_copyID = copyID;
5863 if (ht_stack == NULL)
5864 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5865
5866 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5867 if (newitem == NULL)
5868 return TRUE;
5869
5870 newitem->ht = &dd->dv_hashtab;
5871 newitem->prev = *ht_stack;
5872 *ht_stack = newitem;
5873
5874 return FALSE;
5875}
5876
5877/*
5878 * Mark the list "ll" with "copyID".
5879 * Also see set_ref_in_item().
5880 */
5881 static int
5882set_ref_in_item_list(
5883 list_T *ll,
5884 int copyID,
5885 ht_stack_T **ht_stack,
5886 list_stack_T **list_stack)
5887{
5888 if (ll == NULL || ll->lv_copyID == copyID)
5889 return FALSE;
5890
5891 // Didn't see this list yet.
5892 ll->lv_copyID = copyID;
5893 if (list_stack == NULL)
5894 return set_ref_in_list_items(ll, copyID, ht_stack);
5895
5896 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5897 if (newitem == NULL)
5898 return TRUE;
5899
5900 newitem->list = ll;
5901 newitem->prev = *list_stack;
5902 *list_stack = newitem;
5903
5904 return FALSE;
5905}
5906
5907/*
5908 * Mark the partial "pt" with "copyID".
5909 * Also see set_ref_in_item().
5910 */
5911 static int
5912set_ref_in_item_partial(
5913 partial_T *pt,
5914 int copyID,
5915 ht_stack_T **ht_stack,
5916 list_stack_T **list_stack)
5917{
5918 if (pt == NULL || pt->pt_copyID == copyID)
5919 return FALSE;
5920
5921 // Didn't see this partial yet.
5922 pt->pt_copyID = copyID;
5923
5924 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5925
5926 if (pt->pt_dict != NULL)
5927 {
5928 typval_T dtv;
5929
5930 dtv.v_type = VAR_DICT;
5931 dtv.vval.v_dict = pt->pt_dict;
5932 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5933 }
5934
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005935 if (pt->pt_obj != NULL)
5936 {
5937 typval_T objtv;
5938
5939 objtv.v_type = VAR_OBJECT;
5940 objtv.vval.v_object = pt->pt_obj;
5941 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5942 }
5943
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005944 for (int i = 0; i < pt->pt_argc; ++i)
5945 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5946 ht_stack, list_stack);
5947 // pt_funcstack is handled in set_ref_in_funcstacks()
5948 // pt_loopvars is handled in set_ref_in_loopvars()
5949
5950 return abort;
5951}
5952
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005953#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005954/*
5955 * Mark the job "pt" with "copyID".
5956 * Also see set_ref_in_item().
5957 */
5958 static int
5959set_ref_in_item_job(
5960 job_T *job,
5961 int copyID,
5962 ht_stack_T **ht_stack,
5963 list_stack_T **list_stack)
5964{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005965 typval_T dtv;
5966
5967 if (job == NULL || job->jv_copyID == copyID)
5968 return FALSE;
5969
5970 job->jv_copyID = copyID;
5971 if (job->jv_channel != NULL)
5972 {
5973 dtv.v_type = VAR_CHANNEL;
5974 dtv.vval.v_channel = job->jv_channel;
5975 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5976 }
5977 if (job->jv_exit_cb.cb_partial != NULL)
5978 {
5979 dtv.v_type = VAR_PARTIAL;
5980 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5981 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5982 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005983
5984 return FALSE;
5985}
5986
5987/*
5988 * Mark the channel "ch" with "copyID".
5989 * Also see set_ref_in_item().
5990 */
5991 static int
5992set_ref_in_item_channel(
5993 channel_T *ch,
5994 int copyID,
5995 ht_stack_T **ht_stack,
5996 list_stack_T **list_stack)
5997{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005998 typval_T dtv;
5999
6000 if (ch == NULL || ch->ch_copyID == copyID)
6001 return FALSE;
6002
6003 ch->ch_copyID = copyID;
6004 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
6005 {
6006 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
6007 jq != NULL; jq = jq->jq_next)
6008 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6009 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6010 cq = cq->cq_next)
6011 if (cq->cq_callback.cb_partial != NULL)
6012 {
6013 dtv.v_type = VAR_PARTIAL;
6014 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6015 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6016 }
6017 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6018 {
6019 dtv.v_type = VAR_PARTIAL;
6020 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6021 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6022 }
6023 }
6024 if (ch->ch_callback.cb_partial != NULL)
6025 {
6026 dtv.v_type = VAR_PARTIAL;
6027 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6028 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6029 }
6030 if (ch->ch_close_cb.cb_partial != NULL)
6031 {
6032 dtv.v_type = VAR_PARTIAL;
6033 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6034 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6035 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006036
6037 return FALSE;
6038}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006039#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006040
6041/*
6042 * Mark the class "cl" with "copyID".
6043 * Also see set_ref_in_item().
6044 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006045 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006046set_ref_in_item_class(
6047 class_T *cl,
6048 int copyID,
6049 ht_stack_T **ht_stack,
6050 list_stack_T **list_stack)
6051{
6052 int abort = FALSE;
6053
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006054 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006055 return FALSE;
6056
6057 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006058 if (cl->class_members_tv != NULL)
6059 {
6060 // The "class_members_tv" table is allocated only for regular classes
6061 // and not for interfaces.
6062 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6063 abort = abort || set_ref_in_item(
6064 &cl->class_members_tv[i],
6065 copyID, ht_stack, list_stack);
6066 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006067
6068 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6069 abort = abort || set_ref_in_func(NULL,
6070 cl->class_class_functions[i], copyID);
6071
6072 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6073 abort = abort || set_ref_in_func(NULL,
6074 cl->class_obj_methods[i], copyID);
6075
6076 return abort;
6077}
6078
6079/*
6080 * Mark the object "cl" with "copyID".
6081 * Also see set_ref_in_item().
6082 */
6083 static int
6084set_ref_in_item_object(
6085 object_T *obj,
6086 int copyID,
6087 ht_stack_T **ht_stack,
6088 list_stack_T **list_stack)
6089{
6090 int abort = FALSE;
6091
6092 if (obj == NULL || obj->obj_copyID == copyID)
6093 return FALSE;
6094
6095 obj->obj_copyID = copyID;
6096
6097 // The typval_T array is right after the object_T.
6098 typval_T *mtv = (typval_T *)(obj + 1);
6099 for (int i = 0; !abort
6100 && i < obj->obj_class->class_obj_member_count; ++i)
6101 abort = abort || set_ref_in_item(mtv + i, copyID,
6102 ht_stack, list_stack);
6103
6104 return abort;
6105}
6106
6107/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006108 * Mark all lists, dicts and other container types referenced through typval
6109 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006110 * "list_stack" is used to add lists to be marked. Can be NULL.
6111 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6112 *
6113 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006115 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116set_ref_in_item(
6117 typval_T *tv,
6118 int copyID,
6119 ht_stack_T **ht_stack,
6120 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006122 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006123
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006124 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006125 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006126 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006127 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6128 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006129
6130 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006131 return set_ref_in_item_list(tv->vval.v_list, copyID,
6132 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006133
6134 case VAR_FUNC:
6135 {
6136 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6137 break;
6138 }
6139
6140 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006141 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6142 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006143
6144 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006145#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006146 return set_ref_in_item_job(tv->vval.v_job, copyID,
6147 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006148#else
6149 break;
6150#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006151
6152 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006153#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006154 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006155 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006156#else
6157 break;
6158#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006159
6160 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006161 return set_ref_in_item_class(tv->vval.v_class, copyID,
6162 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006163
6164 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006165 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006166 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006167
6168 case VAR_UNKNOWN:
6169 case VAR_ANY:
6170 case VAR_VOID:
6171 case VAR_BOOL:
6172 case VAR_SPECIAL:
6173 case VAR_NUMBER:
6174 case VAR_FLOAT:
6175 case VAR_STRING:
6176 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006177 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006178 case VAR_INSTR:
6179 // Types that do not contain any other item
6180 break;
6181 }
6182
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006183 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006184}
6185
Bram Moolenaar8c711452005-01-14 21:53:12 +00006186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006187 * Return a string with the string representation of a variable.
6188 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006189 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006190 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006191 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006192 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006193 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006194 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6195 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006196 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006198 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006199echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006200 typval_T *tv,
6201 char_u **tofree,
6202 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006203 int copyID,
6204 int echo_style,
6205 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006206 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006207{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 static int recurse = 0;
6209 char_u *r = NULL;
6210
Bram Moolenaar33570922005-01-25 22:26:29 +00006211 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006212 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006213 if (!did_echo_string_emsg)
6214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 // Only give this message once for a recursive call to avoid
6216 // flooding the user with errors. And stop iterating over lists
6217 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006218 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006219 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006220 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006221 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006222 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006223 }
6224 ++recurse;
6225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226 switch (tv->v_type)
6227 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006228 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006229 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006230 {
6231 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006232 r = tv->vval.v_string;
6233 if (r == NULL)
6234 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006235 }
6236 else
6237 {
6238 *tofree = string_quote(tv->vval.v_string, FALSE);
6239 r = *tofree;
6240 }
6241 break;
6242
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006244 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006245 char_u buf[MAX_FUNC_NAME_LEN];
6246
6247 if (echo_style)
6248 {
LemonBoya5d35902022-04-29 21:15:02 +01006249 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6250 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006251 buf, MAX_FUNC_NAME_LEN);
6252 if (r == buf)
6253 {
6254 r = vim_strsave(buf);
6255 *tofree = r;
6256 }
6257 else
6258 *tofree = NULL;
6259 }
6260 else
6261 {
6262 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6263 : make_ufunc_name_readable(
6264 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6265 TRUE);
6266 r = *tofree;
6267 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006268 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006270
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006271 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006272 {
6273 partial_T *pt = tv->vval.v_partial;
6274 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006275 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006276 garray_T ga;
6277 int i;
6278 char_u *tf;
6279
6280 ga_init2(&ga, 1, 100);
6281 ga_concat(&ga, (char_u *)"function(");
6282 if (fname != NULL)
6283 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006284 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006285 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006286 && vim_isupper(fname[1]))
6287 {
6288 ga_concat(&ga, (char_u *)"'g:");
6289 ga_concat(&ga, fname + 1);
6290 }
6291 else
6292 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006293 vim_free(fname);
6294 }
6295 if (pt != NULL && pt->pt_argc > 0)
6296 {
6297 ga_concat(&ga, (char_u *)", [");
6298 for (i = 0; i < pt->pt_argc; ++i)
6299 {
6300 if (i > 0)
6301 ga_concat(&ga, (char_u *)", ");
6302 ga_concat(&ga,
6303 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6304 vim_free(tf);
6305 }
6306 ga_concat(&ga, (char_u *)"]");
6307 }
6308 if (pt != NULL && pt->pt_dict != NULL)
6309 {
6310 typval_T dtv;
6311
6312 ga_concat(&ga, (char_u *)", ");
6313 dtv.v_type = VAR_DICT;
6314 dtv.vval.v_dict = pt->pt_dict;
6315 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6316 vim_free(tf);
6317 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006318 // terminate with ')' and a NUL
6319 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006320
6321 *tofree = ga.ga_data;
6322 r = *tofree;
6323 break;
6324 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006325
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006326 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006327 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006328 break;
6329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006331 if (tv->vval.v_list == NULL)
6332 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006333 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006334 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006335 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006336 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006337 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6338 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006339 {
6340 *tofree = NULL;
6341 r = (char_u *)"[...]";
6342 }
6343 else
6344 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006345 int old_copyID = tv->vval.v_list->lv_copyID;
6346
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006347 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006348 *tofree = list2string(tv, copyID, restore_copyID);
6349 if (restore_copyID)
6350 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006351 r = *tofree;
6352 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006354
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006356 if (tv->vval.v_dict == NULL)
6357 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006358 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006359 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006360 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006361 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006362 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6363 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006364 {
6365 *tofree = NULL;
6366 r = (char_u *)"{...}";
6367 }
6368 else
6369 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006370 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006371
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006372 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006373 *tofree = dict2string(tv, copyID, restore_copyID);
6374 if (restore_copyID)
6375 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006376 r = *tofree;
6377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006380 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006381 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006382 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006384 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006385 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006386 break;
6387
Bram Moolenaar835dc632016-02-07 14:27:38 +01006388 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006389 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006390#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006392 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6393 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006394 if (composite_val)
6395 {
6396 *tofree = string_quote(r, FALSE);
6397 r = *tofree;
6398 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006399#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006401
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006402 case VAR_INSTR:
6403 *tofree = NULL;
6404 r = (char_u *)"instructions";
6405 break;
6406
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006407 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006408 {
6409 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006410 char *s = "class";
Yegappan Lakshmananda9d3452024-05-02 13:02:36 +02006411 if (cl != NULL && IS_INTERFACE(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006412 s = "interface";
Yegappan Lakshmananda9d3452024-05-02 13:02:36 +02006413 else if (cl != NULL && IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006414 s = "enum";
6415 size_t len = STRLEN(s) + 1 +
6416 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006417 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006418 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006419 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6420 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006421 break;
6422
6423 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006424 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6425 echo_style, restore_copyID,
6426 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006427 break;
6428
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006429 case VAR_FLOAT:
6430 *tofree = NULL;
6431 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6432 r = numbuf;
6433 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006434
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006435 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006436 case VAR_SPECIAL:
6437 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006438 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006439 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006440
6441 case VAR_TYPEALIAS:
6442 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6443 r = *tofree;
6444 if (r == NULL)
6445 r = (char_u *)"";
6446 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448
Bram Moolenaar8502c702014-06-17 12:51:16 +02006449 if (--recurse == 0)
6450 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006451 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006452}
6453
6454/*
6455 * Return a string with the string representation of a variable.
6456 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6457 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006458 * Does not put quotes around strings, as ":echo" displays values.
6459 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6460 * May return NULL.
6461 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006462 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006463echo_string(
6464 typval_T *tv,
6465 char_u **tofree,
6466 char_u *numbuf,
6467 int copyID)
6468{
6469 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6470}
6471
6472/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006473 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6474 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006475 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006476 */
6477 int
6478buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6479{
6480 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006481 char_u *t;
6482 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006483
6484 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6485 return -1;
6486
6487 if (lnum > buf->b_ml.ml_line_count)
6488 lnum = buf->b_ml.ml_line_count;
6489
6490 str = ml_get_buf(buf, lnum, FALSE);
6491 if (str == NULL)
6492 return -1;
6493
6494 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006495 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006496
Bram Moolenaar91458462021-01-13 20:08:38 +01006497 // count the number of characters
6498 t = str;
6499 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6500 t += mb_ptr2len(t);
6501
6502 // In insert mode, when the cursor is at the end of a non-empty line,
6503 // byteidx points to the NUL character immediately past the end of the
6504 // string. In this case, add one to the character count.
6505 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6506 count++;
6507
6508 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006509}
6510
6511/*
6512 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006513 * byte index. Works only for loaded buffers. Returns -1 on failure.
6514 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006515 */
6516 int
6517buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6518{
6519 char_u *str;
6520 char_u *t;
6521
6522 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6523 return -1;
6524
6525 if (lnum > buf->b_ml.ml_line_count)
6526 lnum = buf->b_ml.ml_line_count;
6527
6528 str = ml_get_buf(buf, lnum, FALSE);
6529 if (str == NULL)
6530 return -1;
6531
6532 // Convert the character offset to a byte offset
6533 t = str;
6534 while (*t != NUL && --charidx > 0)
6535 t += mb_ptr2len(t);
6536
Bram Moolenaar91458462021-01-13 20:08:38 +01006537 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006538}
6539
6540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006542 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006545var2fpos(
6546 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006547 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006548 int *fnum, // set to fnum for '0, 'A, etc.
6549 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006551 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006553 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006555 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006556 if (varp->v_type == VAR_LIST)
6557 {
6558 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006559 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006560 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006561 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006562
6563 l = varp->vval.v_list;
6564 if (l == NULL)
6565 return NULL;
6566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006568 pos.lnum = list_find_nr(l, 0L, &error);
6569 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006570 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006571 if (charcol)
6572 len = (long)mb_charlen(ml_get(pos.lnum));
6573 else
John Marriottbfcc8952024-03-11 22:04:45 +01006574 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006575
Bram Moolenaarec65d772020-08-20 22:29:12 +02006576 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006578 li = list_find(l, 1L);
6579 if (li != NULL && li->li_tv.v_type == VAR_STRING
6580 && li->li_tv.vval.v_string != NULL
6581 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006582 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006583 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006584 }
6585 else
6586 {
6587 pos.col = list_find_nr(l, 1L, &error);
6588 if (error)
6589 return NULL;
6590 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006592 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006593 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006595 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006597 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006598 pos.coladd = list_find_nr(l, 2L, &error);
6599 if (error)
6600 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006601
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006602 return &pos;
6603 }
6604
Bram Moolenaarc5809432021-03-27 21:23:30 +01006605 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6606 return NULL;
6607
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006608 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006609 if (name == NULL)
6610 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006611
6612 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006613 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006614 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006615 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006616 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006617 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006618 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006619 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006620 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006621 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006622 pos = VIsual;
6623 else
6624 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006625 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006626 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006627 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006629 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006630 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6632 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006633 pos = *pp;
6634 }
6635 if (pos.lnum != 0)
6636 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006637 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006638 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6639 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006641
Bram Moolenaara5525202006-03-02 22:52:09 +00006642 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006643
Bram Moolenaar477933c2007-07-17 14:32:23 +00006644 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006645 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006646 // the "w_valid" flags are not reset when moving the cursor, but they
6647 // do matter for update_topline() and validate_botline().
6648 check_cursor_moved(curwin);
6649
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006650 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006651 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006652 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006653 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 // In silent Ex mode topline is zero, but that's not a valid line
6655 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006656 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006657 return &pos;
6658 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006659 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006660 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006661 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006662 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006663 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006664 return &pos;
6665 }
6666 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006667 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006669 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
6671 pos.lnum = curbuf->b_ml.ml_line_count;
6672 pos.col = 0;
6673 }
6674 else
6675 {
6676 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006677 if (charcol)
6678 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6679 else
John Marriottbfcc8952024-03-11 22:04:45 +01006680 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 }
6682 return &pos;
6683 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006684 if (in_vim9script())
6685 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 return NULL;
6687}
6688
6689/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006690 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006691 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006692 * Note that the column is passed on as-is, the caller may want to decrement
6693 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006694 * If "charcol" is TRUE use the column as the character index instead of the
6695 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006696 * Return FAIL when conversion is not possible, doesn't check the position for
6697 * validity.
6698 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700list2fpos(
6701 typval_T *arg,
6702 pos_T *posp,
6703 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006704 colnr_T *curswantp,
6705 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006706{
6707 list_T *l = arg->vval.v_list;
6708 long i = 0;
6709 long n;
6710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6712 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006713 if (arg->v_type != VAR_LIST
6714 || l == NULL
6715 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006716 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006717 return FAIL;
6718
6719 if (fnump != NULL)
6720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006721 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006722 if (n < 0)
6723 return FAIL;
6724 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006725 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006726 *fnump = n;
6727 }
6728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006729 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006730 if (n < 0)
6731 return FAIL;
6732 posp->lnum = n;
6733
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006734 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006735 if (n < 0)
6736 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006737 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006738 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006739 if (charcol)
6740 {
6741 buf_T *buf;
6742
6743 // Get the text for the specified line in a loaded buffer
6744 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6745 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6746 return FAIL;
6747
Bram Moolenaar79f23442022-10-10 12:42:57 +01006748 n = buf_charidx_to_byteidx(buf,
6749 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006750 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006751 posp->col = n;
6752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006753 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006754 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006755 posp->coladd = 0;
6756 else
6757 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006758
Bram Moolenaar493c1782014-05-28 14:34:46 +02006759 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006760 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006761
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006762 return OK;
6763}
6764
6765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 * Get the length of an environment variable name.
6767 * Advance "arg" to the first character after the name.
6768 * Return 0 for error.
6769 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 char_u *p;
6774 int len;
6775
6776 for (p = *arg; vim_isIDc(*p); ++p)
6777 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006778 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 return 0;
6780
6781 len = (int)(p - *arg);
6782 *arg = p;
6783 return len;
6784}
6785
6786/*
6787 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006788 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 * Return 0 if something is wrong.
6790 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006792get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 char_u *p;
6795 int len;
6796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006797 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006799 {
6800 if (*p == ':')
6801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006802 // "s:" is start of "s:var", but "n:" is not and can be used in
6803 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006804 len = (int)(p - *arg);
6805 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6806 || len > 1)
6807 break;
6808 }
6809 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006810 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 return 0;
6812
6813 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006814 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
6816 return len;
6817}
6818
6819/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006820 * Get the length of the name of a variable or function.
6821 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006823 * Return -1 if curly braces expansion failed.
6824 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 * If the name contains 'magic' {}'s, expand them and return the
6826 * expanded name in an allocated string via 'alias' - caller must free.
6827 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829get_name_len(
6830 char_u **arg,
6831 char_u **alias,
6832 int evaluate,
6833 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 char_u *p;
6837 char_u *expr_start;
6838 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
6842 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6843 && (*arg)[2] == (int)KE_SNR)
6844 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006845 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 *arg += 3;
6847 return get_id_len(arg) + 3;
6848 }
6849 len = eval_fname_script(*arg);
6850 if (len > 0)
6851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006852 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 *arg += len;
6854 }
6855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006859 p = find_name_end(*arg, &expr_start, &expr_end,
6860 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (expr_start != NULL)
6862 {
6863 char_u *temp_string;
6864
6865 if (!evaluate)
6866 {
6867 len += (int)(p - *arg);
6868 *arg = skipwhite(p);
6869 return len;
6870 }
6871
6872 /*
6873 * Include any <SID> etc in the expanded string:
6874 * Thus the -len here.
6875 */
6876 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6877 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006878 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 *alias = temp_string;
6880 *arg = skipwhite(p);
6881 return (int)STRLEN(temp_string);
6882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006885 // Only give an error when there is something, otherwise it will be
6886 // reported at a higher level.
6887 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006888 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
6890 return len;
6891}
6892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893/*
6894 * Find the end of a variable or function name, taking care of magic braces.
6895 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6896 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006897 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006898 * Return a pointer to just after the name. Equal to "arg" if there is no
6899 * valid name.
6900 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902find_name_end(
6903 char_u *arg,
6904 char_u **expr_start,
6905 char_u **expr_end,
6906 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006908 int mb_nest = 0;
6909 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006911 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006912 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006916 *expr_start = NULL;
6917 *expr_end = NULL;
6918 }
6919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006920 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006921 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006922 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006923 return arg;
6924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925 for (p = arg; *p != NUL
6926 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006927 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006928 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006929 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006931 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006932 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006933 if (*p == '\'')
6934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006935 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006936 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006937 ;
6938 if (*p == NUL)
6939 break;
6940 }
6941 else if (*p == '"')
6942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006943 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006944 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006945 if (*p == '\\' && p[1] != NUL)
6946 ++p;
6947 if (*p == NUL)
6948 break;
6949 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006950 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006952 // "s:" is start of "s:var", but "n:" is not and can be used in
6953 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006954 len = (int)(p - arg);
6955 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006956 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006957 break;
6958 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006959
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006960 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006962 if (*p == '[')
6963 ++br_nest;
6964 else if (*p == ']')
6965 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006967
zeertzjqa93d9cd2023-05-02 16:25:47 +01006968 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006970 if (*p == '{')
6971 {
6972 mb_nest++;
6973 if (expr_start != NULL && *expr_start == NULL)
6974 *expr_start = p;
6975 }
6976 else if (*p == '}')
6977 {
6978 mb_nest--;
6979 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6980 *expr_end = p;
6981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984
6985 return p;
6986}
6987
6988/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006989 * Expands out the 'magic' {}'s in a variable/function name.
6990 * Note that this can call itself recursively, to deal with
6991 * constructs like foo{bar}{baz}{bam}
6992 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6993 * "in_start" ^
6994 * "expr_start" ^
6995 * "expr_end" ^
6996 * "in_end" ^
6997 *
6998 * Returns a new allocated string, which the caller must free.
6999 * Returns NULL for failure.
7000 */
7001 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007002make_expanded_name(
7003 char_u *in_start,
7004 char_u *expr_start,
7005 char_u *expr_end,
7006 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007007{
7008 char_u c1;
7009 char_u *retval = NULL;
7010 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007011
7012 if (expr_end == NULL || in_end == NULL)
7013 return NULL;
7014 *expr_start = NUL;
7015 *expr_end = NUL;
7016 c1 = *in_end;
7017 *in_end = NUL;
7018
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007019 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007020 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007021 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007022 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7023 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007024 if (retval != NULL)
7025 {
7026 STRCPY(retval, in_start);
7027 STRCAT(retval, temp_result);
7028 STRCAT(retval, expr_end + 1);
7029 }
7030 }
7031 vim_free(temp_result);
7032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007033 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007034 *expr_start = '{';
7035 *expr_end = '}';
7036
7037 if (retval != NULL)
7038 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007039 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007040 if (expr_start != NULL)
7041 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007042 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007043 temp_result = make_expanded_name(retval, expr_start,
7044 expr_end, temp_result);
7045 vim_free(retval);
7046 retval = temp_result;
7047 }
7048 }
7049
7050 return retval;
7051}
7052
7053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007055 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007060 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007061}
7062
7063/*
7064 * Return TRUE if character "c" can be used as the first character in a
7065 * variable or function name (excluding '{' and '}').
7066 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007068eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007069{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007070 return ASCII_ISALPHA(c) || c == '_';
7071}
7072
7073/*
7074 * Return TRUE if character "c" can be used as the first character of a
7075 * dictionary key.
7076 */
7077 int
7078eval_isdictc(int c)
7079{
7080 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081}
7082
7083/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007084 * Handle:
7085 * - expr[expr], expr[expr:expr] subscript
7086 * - ".name" lookup
7087 * - function call with Funcref variable: func(expr)
7088 * - method call: var->method()
7089 *
7090 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007091 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007092 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094handle_subscript(
7095 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007096 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007097 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007098 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007099 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007100{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007101 int evaluate = evalarg != NULL
7102 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007103 int ret = OK;
7104 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007105 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007106 int getnext;
7107 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007108
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007109 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007110 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007111 // When at the end of the line and ".name" or "->{" or "->X" follows in
7112 // the next line then consume the line break.
7113 p = eval_next_non_blank(*arg, evalarg, &getnext);
7114 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007115 && ((*p == '.'
7116 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7117 || rettv->v_type == VAR_CLASS
7118 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007119 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7120 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7121 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007122 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007123 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007124 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007125 check_white = FALSE;
7126 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007127
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007128 if (rettv->v_type == VAR_ANY)
7129 {
7130 char_u *exp_name;
7131 int cc;
7132 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007133 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007134 type_T *type;
7135
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007136 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007137 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007138 if (**arg != '.')
7139 {
7140 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007141 semsg(_(e_expected_dot_after_name_str),
7142 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007143 ret = FAIL;
7144 break;
7145 }
7146 ++*arg;
7147 if (IS_WHITE_OR_NUL(**arg))
7148 {
7149 if (verbose)
7150 emsg(_(e_no_white_space_allowed_after_dot));
7151 ret = FAIL;
7152 break;
7153 }
7154
7155 // isolate the name
7156 exp_name = *arg;
7157 while (eval_isnamec(**arg))
7158 ++*arg;
7159 cc = **arg;
7160 **arg = NUL;
7161
7162 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007163 evalarg == NULL ? NULL : evalarg->eval_cctx,
7164 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007165 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007166
7167 if (idx < 0 && ufunc == NULL)
7168 {
7169 ret = FAIL;
7170 break;
7171 }
7172 if (idx >= 0)
7173 {
7174 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7175 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7176
7177 copy_tv(sv->sv_tv, rettv);
7178 }
7179 else
7180 {
7181 rettv->v_type = VAR_FUNC;
7182 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7183 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007184 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007185 }
7186
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007187 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7188 || rettv->v_type == VAR_PARTIAL))
7189 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007190 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007191 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7192 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007193
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007194 // Stop the expression evaluation when immediately aborting on
7195 // error, or when an interrupt occurred or an exception was thrown
7196 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007197 if (aborting())
7198 {
7199 if (ret == OK)
7200 clear_tv(rettv);
7201 ret = FAIL;
7202 }
7203 dict_unref(selfdict);
7204 selfdict = NULL;
7205 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007206 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007207 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007208 if (in_vim9script())
7209 *arg = skipwhite(p + 2);
7210 else
7211 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007212 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007213 {
zeertzjqc481ad32023-03-11 16:18:51 +00007214 emsg(_(e_no_white_space_allowed_before_parenthesis));
7215 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007216 }
zeertzjqc481ad32023-03-11 16:18:51 +00007217 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7218 // expr->{lambda}() or expr->(lambda)()
7219 ret = eval_lambda(arg, rettv, evalarg, verbose);
7220 else
7221 // expr->name()
7222 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007223 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007224 // "." is ".name" lookup when we found a dict or when evaluating and
7225 // scriptversion is at least 2, where string concatenation is "..".
7226 else if (**arg == '['
7227 || (**arg == '.' && (rettv->v_type == VAR_DICT
7228 || (!evaluate
7229 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007230 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007231 {
7232 dict_unref(selfdict);
7233 if (rettv->v_type == VAR_DICT)
7234 {
7235 selfdict = rettv->vval.v_dict;
7236 if (selfdict != NULL)
7237 ++selfdict->dv_refcount;
7238 }
7239 else
7240 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007241 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007242 {
7243 clear_tv(rettv);
7244 ret = FAIL;
7245 }
7246 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007247 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7248 || rettv->v_type == VAR_OBJECT))
7249 {
7250 // class member: SomeClass.varname
7251 // class method: SomeClass.SomeMethod()
7252 // class constructor: SomeClass.new()
7253 // object member: someObject.varname
7254 // object method: someObject.SomeMethod()
7255 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7256 {
7257 clear_tv(rettv);
7258 ret = FAIL;
7259 }
7260 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007261 else
7262 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007263 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007265 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7266 // Don't do this when "Func" is already a partial that was bound
7267 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007268 if (selfdict != NULL
7269 && (rettv->v_type == VAR_FUNC
7270 || (rettv->v_type == VAR_PARTIAL
7271 && (rettv->vval.v_partial->pt_auto
7272 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007273 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007274
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007275 dict_unref(selfdict);
7276 return ret;
7277}
7278
7279/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280 * Make a copy of an item.
7281 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007282 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007283 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7284 * reference to an already copied list/dict can be used.
7285 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007288item_copy(
7289 typval_T *from,
7290 typval_T *to,
7291 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007292 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294{
7295 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007296 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007300 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007301 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 }
7303 ++recurse;
7304
7305 switch (from->v_type)
7306 {
7307 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007308 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 case VAR_STRING:
7310 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007311 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007312 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007313 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007314 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007315 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007316 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007317 case VAR_CLASS:
7318 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007319 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 copy_tv(from, to);
7321 break;
7322 case VAR_LIST:
7323 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007325 if (from->vval.v_list == NULL)
7326 to->vval.v_list = NULL;
7327 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007329 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007330 to->vval.v_list = from->vval.v_list->lv_copylist;
7331 ++to->vval.v_list->lv_refcount;
7332 }
7333 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007334 to->vval.v_list = list_copy(from->vval.v_list,
7335 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007336 if (to->vval.v_list == NULL)
7337 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007339 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007341 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 case VAR_DICT:
7343 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007344 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 if (from->vval.v_dict == NULL)
7346 to->vval.v_dict = NULL;
7347 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7348 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007349 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007350 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7351 ++to->vval.v_dict->dv_refcount;
7352 }
7353 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007354 to->vval.v_dict = dict_copy(from->vval.v_dict,
7355 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 if (to->vval.v_dict == NULL)
7357 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007359 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007360 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007362 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007363 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 }
7365 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367}
7368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 void
7370echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7371{
7372 char_u *tofree;
7373 char_u numbuf[NUMBUFLEN];
7374 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7375
7376 if (*atstart)
7377 {
7378 *atstart = FALSE;
7379 // Call msg_start() after eval1(), evaluating the expression
7380 // may cause a message to appear.
7381 if (with_space)
7382 {
7383 // Mark the saved text as finishing the line, so that what
7384 // follows is displayed on a new line when scrolling back
7385 // at the more prompt.
7386 msg_sb_eol();
7387 msg_start();
7388 }
7389 }
7390 else if (with_space)
7391 msg_puts_attr(" ", echo_attr);
7392
7393 if (p != NULL)
7394 for ( ; *p != NUL && !got_int; ++p)
7395 {
7396 if (*p == '\n' || *p == '\r' || *p == TAB)
7397 {
7398 if (*p != TAB && *needclr)
7399 {
7400 // remove any text still there from the command
7401 msg_clr_eos();
7402 *needclr = FALSE;
7403 }
7404 msg_putchar_attr(*p, echo_attr);
7405 }
7406 else
7407 {
7408 if (has_mbyte)
7409 {
7410 int i = (*mb_ptr2len)(p);
7411
7412 (void)msg_outtrans_len_attr(p, i, echo_attr);
7413 p += i - 1;
7414 }
7415 else
7416 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7417 }
7418 }
7419 vim_free(tofree);
7420}
7421
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 * ":echo expr1 ..." print each argument separated with a space, add a
7424 * newline at the end.
7425 * ":echon expr1 ..." print each argument plain.
7426 */
7427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007432 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 int needclr = TRUE;
7434 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007435 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007436 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007437 evalarg_T evalarg;
7438
Bram Moolenaare707c882020-07-01 13:07:37 +02007439 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 if (eap->skip)
7442 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007443 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007445 // If eval1() causes an error message the text from the command may
7446 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007447 need_clr_eos = needclr;
7448
Bram Moolenaar68db9962021-05-09 23:19:22 +02007449 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007450 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
7452 /*
7453 * Report the invalid expression unless the expression evaluation
7454 * has been cancelled due to an aborting error, an interrupt, or an
7455 * exception.
7456 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007457 if (!aborting() && did_emsg == did_emsg_before
7458 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007459 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007460 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 break;
7462 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007463 need_clr_eos = FALSE;
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007466 {
7467 if (rettv.v_type == VAR_VOID)
7468 {
7469 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7470 break;
7471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007473 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 arg = skipwhite(arg);
7477 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007478 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007479 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481 if (eap->skip)
7482 --emsg_skip;
7483 else
7484 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007485 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (needclr)
7487 msg_clr_eos();
7488 if (eap->cmdidx == CMD_echo)
7489 msg_end();
7490 }
7491}
7492
7493/*
7494 * ":echohl {name}".
7495 */
7496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007499 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500}
7501
7502/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007503 * Returns the :echo attribute
7504 */
7505 int
7506get_echo_attr(void)
7507{
7508 return echo_attr;
7509}
7510
7511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 * ":execute expr1 ..." execute the result of an expression.
7513 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007514 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007516 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 * Each gets spaces around each argument and a newline at the end for
7518 * echo commands
7519 */
7520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007521ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 int ret = OK;
7526 char_u *p;
7527 garray_T ga;
7528 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007529 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
7531 ga_init2(&ga, 1, 80);
7532
7533 if (eap->skip)
7534 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007535 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007537 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007538 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
7541 if (!eap->skip)
7542 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007543 char_u buf[NUMBUFLEN];
7544
7545 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007546 {
7547 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7548 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007549 semsg(_(e_using_invalid_value_as_string_str),
7550 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007551 p = NULL;
7552 }
7553 else
7554 p = tv_get_string_buf(&rettv, buf);
7555 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007556 else
7557 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007558 if (p == NULL)
7559 {
7560 clear_tv(&rettv);
7561 ret = FAIL;
7562 break;
7563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 len = (int)STRLEN(p);
7565 if (ga_grow(&ga, len + 2) == FAIL)
7566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ret = FAIL;
7569 break;
7570 }
7571 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 ga.ga_len += len;
7575 }
7576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 arg = skipwhite(arg);
7579 }
7580
7581 if (ret != FAIL && ga.ga_data != NULL)
7582 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007583 // use the first line of continuation lines for messages
7584 SOURCING_LNUM = start_lnum;
7585
Bram Moolenaar37fef162022-08-29 18:16:32 +01007586 if (eap->cmdidx == CMD_echomsg
7587 || eap->cmdidx == CMD_echowindow
7588 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007589 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007590 // Mark the already saved text as finishing the line, so that what
7591 // follows is displayed on a new line when scrolling back at the
7592 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007593 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007594 }
7595
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007596 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007597 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007598 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007599 out_flush();
7600 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007601 else if (eap->cmdidx == CMD_echowindow)
7602 {
7603#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007604 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007605#endif
7606 msg_attr(ga.ga_data, echo_attr);
7607#ifdef HAS_MESSAGE_WINDOW
7608 end_echowindow();
7609#endif
7610 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007611 else if (eap->cmdidx == CMD_echoconsole)
7612 {
7613 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7614 ui_write((char_u *)"\r\n", 2, TRUE);
7615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 else if (eap->cmdidx == CMD_echoerr)
7617 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007618 int save_did_emsg = did_emsg;
7619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007620 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007621 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (!force_abort)
7623 did_emsg = save_did_emsg;
7624 }
7625 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007626 {
7627 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7628
7629 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7630 sticky_cmdmod_flags = cmdmod.cmod_flags
7631 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007633 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007634 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 }
7637
7638 ga_clear(&ga);
7639
7640 if (eap->skip)
7641 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007642 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643}
7644
7645/*
7646 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7647 * "arg" points to the "&" or '+' when called, to "option" when returning.
7648 * Returns NULL when no option name found. Otherwise pointer to the char
7649 * after the option name.
7650 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007651 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007652find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 char_u *p = *arg;
7655
7656 ++p;
7657 if (*p == 'g' && p[1] == ':')
7658 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007659 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 p += 2;
7661 }
7662 else if (*p == 'l' && p[1] == ':')
7663 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007664 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 p += 2;
7666 }
7667 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007668 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669
7670 if (!ASCII_ISALPHA(*p))
7671 return NULL;
7672 *arg = p;
7673
7674 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007675 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 else
7677 while (ASCII_ISALPHA(*p))
7678 ++p;
7679 return p;
7680}
7681
7682/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007683 * Display script name where an item was last set.
7684 * Should only be invoked when 'verbose' is non-zero.
7685 */
7686 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007687last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007688{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007689 char_u *p;
7690
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007691 if (script_ctx.sc_sid == 0)
7692 return;
7693
7694 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7695 if (p == NULL)
7696 return;
7697
7698 verbose_enter();
7699 msg_puts(_("\n\tLast set from "));
7700 msg_puts((char *)p);
7701 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007702 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007703 msg_puts(_(line_msg));
7704 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007705 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007706 verbose_leave();
7707 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007708}
7709
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007710#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711
7712/*
7713 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007714 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 * "flags" can be "g" to do a global substitute.
7716 * Returns an allocated string, NULL for error.
7717 */
7718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007719do_string_sub(
7720 char_u *str,
7721 char_u *pat,
7722 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007723 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 int sublen;
7727 regmatch_T regmatch;
7728 int i;
7729 int do_all;
7730 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007731 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 garray_T ga;
7733 char_u *ret;
7734 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007735 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007737 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007739 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740
7741 ga_init2(&ga, 1, 200);
7742
7743 do_all = (flags[0] == 'g');
7744
7745 regmatch.rm_ic = p_ic;
7746 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7747 if (regmatch.regprog != NULL)
7748 {
7749 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007750 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007753 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007754 if (regmatch.startp[0] == regmatch.endp[0])
7755 {
7756 if (zero_width == regmatch.startp[0])
7757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007758 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007759 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007760 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7761 (size_t)i);
7762 ga.ga_len += i;
7763 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007764 continue;
7765 }
7766 zero_width = regmatch.startp[0];
7767 }
7768
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 /*
7770 * Get some space for a temporary buffer to do the substitution
7771 * into. It will contain:
7772 * - The text up to where the match is.
7773 * - The substituted text.
7774 * - The text after the match.
7775 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007776 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007777 if (sublen <= 0)
7778 {
7779 ga_clear(&ga);
7780 break;
7781 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007782 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7784 {
7785 ga_clear(&ga);
7786 break;
7787 }
7788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007789 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 i = (int)(regmatch.startp[0] - tail);
7791 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007792 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007793 (void)vim_regsub(&regmatch, sub, expr,
7794 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7795 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007797 tail = regmatch.endp[0];
7798 if (*tail == NUL)
7799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 if (!do_all)
7801 break;
7802 }
7803
7804 if (ga.ga_data != NULL)
7805 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7806
Bram Moolenaar473de612013-06-08 18:19:48 +02007807 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809
7810 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7811 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007812 if (p_cpo == empty_option)
7813 p_cpo = save_cpo;
7814 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007815 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007816 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007817 // If it's still empty it was changed and restored, need to restore in
7818 // the complicated way.
7819 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007820 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007821 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
7824 return ret;
7825}