blob: 21ba1bcfcde37c4d83b5f754a049fbc23552c06c [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,
1173 typval_T *rettv,
1174 scid_T imp_sid,
1175 char_u *p,
1176 dictitem_T **dip,
1177 int fne_flags,
1178 int vim9script)
1179{
1180 ufunc_T *ufunc;
1181 type_T *type = NULL;
1182 int cc;
1183 int rc = FAIL;
1184
1185 p = skipwhite(p);
1186
1187 import_check_sourced_sid(&imp_sid);
1188 lp->ll_sid = imp_sid;
1189 lp->ll_name = p;
1190 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1191 lp->ll_name_end = p;
1192
1193 // check the item is exported
1194 cc = *p;
1195 *p = NUL;
1196 if (find_exported(imp_sid, lp->ll_name, &ufunc, &type, NULL, NULL,
1197 TRUE) == -1)
1198 goto failed;
1199
1200 if (vim9script && type != NULL)
1201 {
1202 where_T where = WHERE_INIT;
1203
1204 // In a vim9 script, do type check and make sure the variable is
1205 // writable.
1206 if (check_typval_type(type, rettv, where) == FAIL)
1207 goto failed;
1208 }
1209
1210 // Get the typval for the exported item
1211 hashtab_T *ht = &SCRIPT_VARS(imp_sid);
1212 if (ht == NULL)
1213 goto failed;
1214
1215 dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
1216 if (di == NULL)
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001217 // script is autoloaded. So variable will be found later
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001218 goto success;
1219
1220 *dip = di;
1221
1222 // Check whether the variable is writable.
1223 svar_T *sv = find_typval_in_script(&di->di_tv, imp_sid, FALSE);
1224 if (sv != NULL && sv->sv_const != 0)
1225 {
1226 semsg(_(e_cannot_change_readonly_variable_str), lp->ll_name);
1227 goto failed;
1228 }
1229
1230 // check whether variable is locked
1231 if (value_check_lock(di->di_tv.v_lock, lp->ll_name, FALSE))
1232 goto failed;
1233
1234 lp->ll_tv = &di->di_tv;
1235
1236success:
1237 rc = OK;
1238
1239failed:
1240 *p = cc;
1241 return rc == OK ? p : NULL;
1242}
1243
1244/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 * Get an lval: variable, Dict item or List item that can be assigned a value
1246 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1247 * "name.key", "name.key[expr]" etc.
1248 * Indexing only works if "name" is an existing List or Dictionary.
1249 * "name" points to the start of the name.
1250 * If "rettv" is not NULL it points to the value to be assigned.
1251 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1252 * wrong; must end in space or cmd separator.
1253 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001254 * flags:
1255 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001256 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001257 * GLV_NO_AUTOLOAD: do not use script autoloading
1258 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001260 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001263 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001264get_lval(
1265 char_u *name,
1266 typval_T *rettv,
1267 lval_T *lp,
1268 int unlet,
1269 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001270 int flags, // GLV_ values
1271 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 char_u *expr_start, *expr_end;
1275 int cc;
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001276 dictitem_T *v = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T var1;
1278 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001280 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001281 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001282 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001283 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001284 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001285 int vim9script = in_vim9script();
Ernie Rael64885642023-10-04 20:16:22 +02001286 class_T *cl_exec = NULL; // class that is executing, or NULL.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287
Ernie Raelee865f32023-09-29 19:53:55 +02001288#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02001289 if (lval_root == NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001290 ch_log(NULL, "LKVAR: get_lval(): name: %s, lval_root (nil)", name);
Ernie Rael64885642023-10-04 20:16:22 +02001291 else
Ernie Rael4c8da022023-10-11 21:35:11 +02001292 ch_log(NULL, "LKVAR: get_lval(): name: %s, lr_tv %p lr_is_arg %d",
1293 name, (void*)lval_root->lr_tv, lval_root->lr_is_arg);
Ernie Rael64885642023-10-04 20:16:22 +02001294 char buf[80];
Ernie Rael4c8da022023-10-11 21:35:11 +02001295 ch_log(NULL, "LKVAR: ...: GLV flags: %s",
Ernie Rael64885642023-10-04 20:16:22 +02001296 flags_tostring(flags, glv_flag_strings, buf, sizeof(buf)));
Ernie Raelee865f32023-09-29 19:53:55 +02001297#endif
1298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001299 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001300 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001302 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001304 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001306 lp->ll_name_end = find_name_end(name, NULL, NULL,
1307 FNE_INCL_BR | fne_flags);
1308 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 }
1310
Bram Moolenaara749a422022-02-12 19:52:25 +00001311 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001312 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001313 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1314 {
1315 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1316 return NULL;
1317 }
1318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001319 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001320 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 if (expr_start != NULL)
1323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001325 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 && *p != '[' && *p != '.')
1327 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001328 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 return NULL;
1330 }
1331
1332 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1333 if (lp->ll_exp_name == NULL)
1334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001335 // Report an invalid expression in braces, unless the
1336 // expression evaluation has been cancelled due to an
1337 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 if (!aborting() && !quiet)
1339 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001340 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001341 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 return NULL;
1343 }
1344 }
1345 lp->ll_name = lp->ll_exp_name;
1346 }
1347 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 lp->ll_name = name;
1350
Bram Moolenaar4525a572022-02-13 11:57:33 +00001351 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001353 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001354 // However, "g:[key]" is indexing a dictionary.
1355 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001356 {
1357 --p;
1358 lp->ll_name_end = p;
1359 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001360 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001361 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001362 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001363
Bram Moolenaar9510d222022-09-11 15:14:05 +01001364 if (is_scoped_variable(name))
1365 {
1366 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1367 return NULL;
1368 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001369 if (VIM_ISWHITE(*p))
1370 {
1371 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1372 return NULL;
1373 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001374 if (tp == p + 1 && !quiet)
1375 {
1376 semsg(_(e_white_space_required_after_str_str), ":", p);
1377 return NULL;
1378 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001379 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001380 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001381 semsg(_(e_using_type_not_in_script_context_str), p);
1382 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001383 }
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001384 if (vim9script && (flags & GLV_NO_DECL) &&
1385 !(flags & GLV_FOR_LOOP))
1386 {
1387 // Using a type and not in a "var" declaration.
1388 semsg(_(e_trailing_characters_str), p);
1389 return NULL;
1390 }
1391
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001392
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001393 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001394 lp->ll_type = parse_type(&tp,
1395 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1396 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001397 if (lp->ll_type == NULL && !quiet)
1398 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001399 lp->ll_name_end = tp;
1400 }
Ernie Raelee865f32023-09-29 19:53:55 +02001401 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 }
1403 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001404 if (lp->ll_name == NULL)
1405 return p;
1406
Bram Moolenaar62aec932022-01-29 21:45:34 +00001407 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001408 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001409 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001410 if (import != NULL)
1411 {
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001412 p++; // skip '.'
1413 p = get_lval_imported(lp, rettv, import->imp_sid, p, &v,
1414 fne_flags, vim9script);
1415 if (p == NULL)
Bram Moolenaar5d982692022-01-12 15:15:27 +00001416 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001417 }
1418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419
Ernie Rael0f058d12023-10-14 11:25:04 +02001420 // Without [idx] or .key we are done.
1421 if (*p != '[' && *p != '.')
Ernie Raelee865f32023-09-29 19:53:55 +02001422 {
1423 if (lval_root != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001424 fill_lval_from_lval_root(lp, lval_root);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427
Ernie Rael0f058d12023-10-14 11:25:04 +02001428 if (vim9script && lval_root != NULL)
1429 cl_exec = lval_root->lr_cl_exec;
Ernie Rael4c8da022023-10-11 21:35:11 +02001430 if (vim9script && lval_root != NULL && lval_root->lr_tv != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001431 {
1432 // using local variable
Ernie Rael64885642023-10-04 20:16:22 +02001433 lp->ll_tv = lval_root->lr_tv;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001434 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001435 }
Yegappan Lakshmananf1750ca2024-04-02 20:41:04 +02001436 else if (lp->ll_tv == NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001437 {
1438 cc = *p;
1439 *p = NUL;
1440 // When we would write to the variable pass &ht and prevent autoload.
1441 writing = !(flags & GLV_READ_ONLY);
1442 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001443 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001444 if (v == NULL && !quiet)
1445 semsg(_(e_undefined_variable_str), lp->ll_name);
1446 *p = cc;
1447 if (v == NULL)
1448 return NULL;
1449 lp->ll_tv = &v->di_tv;
1450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451
Bram Moolenaar4525a572022-02-13 11:57:33 +00001452 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001453 {
1454 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001455 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001456 return NULL;
1457 }
1458
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 /*
1460 * Loop until no more [idx] or .key is following.
1461 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001462 var1.v_type = VAR_UNKNOWN;
1463 var2.v_type = VAR_UNKNOWN;
Ernie Rael0f058d12023-10-14 11:25:04 +02001464 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001465 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001466 vartype_T v_type = lp->ll_tv->v_type;
1467
1468 if (*p == '.' && v_type != VAR_DICT
1469 && v_type != VAR_OBJECT
1470 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001471 {
1472 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001473 semsg(_(e_dot_not_allowed_after_str_str),
1474 vartype_name(v_type), name);
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001475 return NULL;
1476 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001477 if (v_type != VAR_LIST
1478 && v_type != VAR_DICT
1479 && v_type != VAR_BLOB
1480 && v_type != VAR_OBJECT
1481 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 if (!quiet)
Ernie Raelf8da3242023-10-11 21:22:12 +02001484 semsg(_(e_index_not_allowed_after_str_str),
1485 vartype_name(v_type), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001488
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001489 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001490 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001491 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001492 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001493 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001494 r = rettv_blob_alloc(lp->ll_tv);
1495 if (r == FAIL)
1496 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001497
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001501 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001504#ifdef LOG_LOCKVAR
1505 ch_log(NULL, "LKVAR: get_lval() loop: p: %s, type: %s", p,
1506 vartype_name(v_type));
1507#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001508
Bram Moolenaar4525a572022-02-13 11:57:33 +00001509 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001510 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001511 && lp->ll_tv == &v->di_tv
1512 && ht != NULL && ht == get_script_local_ht())
1513 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001514 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001515
1516 // Vim9 script local variable: get the type
1517 if (sv != NULL)
Ernie Rael4c8da022023-10-11 21:35:11 +02001518 {
Bram Moolenaar10c65862020-10-08 21:16:42 +02001519 lp->ll_valtype = sv->sv_type;
Ernie Rael4c8da022023-10-11 21:35:11 +02001520#ifdef LOG_LOCKVAR
1521 ch_log(NULL, "LKVAR: ... loop: vim9 assign type: %s",
1522 vartype_name(lp->ll_valtype->tt_type));
1523#endif
1524 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001525 }
1526
Bram Moolenaar8c711452005-01-14 21:53:12 +00001527 len = -1;
Ernie Rael0f058d12023-10-14 11:25:04 +02001528 if (*p == '.')
Bram Moolenaar8c711452005-01-14 21:53:12 +00001529 {
1530 key = p + 1;
1531 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1532 ;
1533 if (len == 0)
1534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001535 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001536 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001537 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001538 }
1539 p = key + len;
1540 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001541 else
1542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001543 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001544 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001545 if (*p == ':')
1546 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001547 else
1548 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001549 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001550 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001551 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001552 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001553 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001554 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001555 clear_tv(&var1);
1556 return NULL;
1557 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001558 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001559 }
1560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001561 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001562 if (*p == ':')
1563 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001564 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001566 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001567 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001568 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001569 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001570 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 if (rettv != NULL
1572 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001573 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001574 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001575 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001577 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001578 emsg(_(e_slice_requires_list_or_blob_value));
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 }
1582 p = skipwhite(p + 1);
1583 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001584 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001585 else
1586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001587 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001588 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001589 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001590 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001591 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001592 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001593 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001594 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001595 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001596 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001597 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001598 clear_tv(&var2);
1599 return NULL;
1600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001601 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001602 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001604 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001605 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001606
Bram Moolenaar8c711452005-01-14 21:53:12 +00001607 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001609 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001610 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001611 clear_tv(&var1);
1612 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001613 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001614 }
1615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001616 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001617 ++p;
1618 }
Ernie Rael4c8da022023-10-11 21:35:11 +02001619#ifdef LOG_LOCKVAR
Ernie Rael0f058d12023-10-14 11:25:04 +02001620 if (len == -1)
Ernie Rael4c8da022023-10-11 21:35:11 +02001621 ch_log(NULL, "LKVAR: ... loop: p: %s, '[' key: %s", p,
1622 empty1 ? ":" : (char*)tv_get_string(&var1));
1623 else
1624 ch_log(NULL, "LKVAR: ... loop: p: %s, '.' key: %s", p, key);
Ernie Rael4c8da022023-10-11 21:35:11 +02001625#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00001626
Bram Moolenaard505d172022-12-18 21:42:55 +00001627 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001628 {
1629 if (len == -1)
1630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001631 // "[key]": get key from "var1"
1632 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001633 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001635 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001636 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001637 }
1638 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001639 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001640 lp->ll_object = NULL;
1641 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001642
1643 // a NULL dict is equivalent with an empty dict
1644 if (lp->ll_tv->vval.v_dict == NULL)
1645 {
1646 lp->ll_tv->vval.v_dict = dict_alloc();
1647 if (lp->ll_tv->vval.v_dict == NULL)
1648 {
1649 clear_tv(&var1);
1650 return NULL;
1651 }
1652 ++lp->ll_tv->vval.v_dict->dv_refcount;
1653 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001655
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001656 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001658 // When assigning to a scope dictionary check that a function and
1659 // variable name is valid (only variable name unless it is l: or
1660 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001661 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001662 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001663 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001664
1665 if (len != -1)
1666 {
1667 prevval = key[len];
1668 key[len] = NUL;
1669 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001670 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001671 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001672 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001673 && (rettv->v_type == VAR_FUNC
1674 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001675 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001676 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001677 if (len != -1)
1678 key[len] = prevval;
1679 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001680 {
1681 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001682 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001683 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001684 }
1685
Bram Moolenaar10c65862020-10-08 21:16:42 +02001686 if (lp->ll_valtype != NULL)
1687 // use the type of the member
1688 lp->ll_valtype = lp->ll_valtype->tt_member;
1689
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001690 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001691 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001692 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001693 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001694 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001695 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001696 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001697 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001698 return NULL;
1699 }
1700
Bram Moolenaar31b81602019-02-10 22:14:27 +01001701 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001702 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001704 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001705 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001708 }
1709 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001710 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001711 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001712 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001715 p = NULL;
1716 break;
1717 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001718 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001719 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001720 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1721 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001722 {
1723 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001724 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001725 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001726
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001728 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001729 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001730 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001732 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1733
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 /*
1735 * Get the number and item for the only or first index of the List.
1736 */
1737 if (empty1)
1738 lp->ll_n1 = 0;
1739 else
1740 // is number or string
1741 lp->ll_n1 = (long)tv_get_number(&var1);
1742 clear_tv(&var1);
1743
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001744 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001746 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 return NULL;
1748 }
1749 if (lp->ll_range && !lp->ll_empty2)
1750 {
1751 lp->ll_n2 = (long)tv_get_number(&var2);
1752 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001753 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1754 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001755 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001756 }
1757 lp->ll_blob = lp->ll_tv->vval.v_blob;
1758 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001759 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001760 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001761 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001762 {
1763 /*
1764 * Get the number and item for the only or first index of the List.
1765 */
1766 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001767 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001768 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001769 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001770 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001771 clear_tv(&var1);
1772
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001774 lp->ll_object = NULL;
1775 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001776 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001777 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1778 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001779 if (lp->ll_li == NULL)
1780 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001781 clear_tv(&var2);
1782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001783 }
1784
Bram Moolenaar10c65862020-10-08 21:16:42 +02001785 if (lp->ll_valtype != NULL)
1786 // use the type of the member
1787 lp->ll_valtype = lp->ll_valtype->tt_member;
1788
Bram Moolenaar8c711452005-01-14 21:53:12 +00001789 /*
1790 * May need to find the item or absolute index for the second
1791 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 * When no index given: "lp->ll_empty2" is TRUE.
1793 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001794 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001795 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001797 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001798 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001799 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001800 if (check_range_index_two(lp->ll_list,
1801 &lp->ll_n1, lp->ll_li,
1802 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001804 }
1805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001807 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001808 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001809 {
Ernie Raelee865f32023-09-29 19:53:55 +02001810 lp->ll_dict = NULL;
1811 lp->ll_list = NULL;
1812
1813 class_T *cl;
Ernie Rael4c8da022023-10-11 21:35:11 +02001814 if (v_type == VAR_OBJECT)
Ernie Raelee865f32023-09-29 19:53:55 +02001815 {
Ernie Rael4c8da022023-10-11 21:35:11 +02001816 if (lp->ll_tv->vval.v_object == NULL)
1817 {
1818 if (!quiet)
1819 emsg(_(e_using_null_object));
1820 return NULL;
1821 }
Ernie Raelee865f32023-09-29 19:53:55 +02001822 cl = lp->ll_tv->vval.v_object->obj_class;
1823 lp->ll_object = lp->ll_tv->vval.v_object;
1824 }
1825 else
1826 {
1827 cl = lp->ll_tv->vval.v_class;
1828 lp->ll_object = NULL;
1829 }
1830 lp->ll_class = cl;
1831
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001832 // TODO: what if class is NULL?
1833 if (cl != NULL)
1834 {
1835 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001836
1837 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001838 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001839 // First look for a function with this name.
1840 // round 1: class functions (skipped for an object)
1841 // round 2: object methods
1842 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001843 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001844 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001845 int m_idx;
1846 ufunc_T *fp;
1847
1848 fp = method_lookup(cl,
1849 round == 1 ? VAR_CLASS : VAR_OBJECT,
1850 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001851 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001852 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001853 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001854 lp->ll_ufunc = fp;
1855 lp->ll_valtype = fp->uf_func_type;
1856 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001857 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001858 }
1859 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001860
1861 if (lp->ll_valtype == NULL)
1862 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001863 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001864 ocmember_T *om
1865 = member_lookup(cl, v_type, key, p - key, &m_idx);
1866 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001867 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001868 {
Ernie Rael64885642023-10-04 20:16:22 +02001869 if (get_lval_check_access(cl_exec, cl, om,
1870 p, flags) == FAIL)
1871 return NULL;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001872
Yegappan Lakshmanane5437c52023-12-16 14:11:19 +01001873 // When lhs is used to modify the variable, check it is
1874 // not a read-only variable.
1875 if ((flags & GLV_READ_ONLY) == 0
1876 && (*p != '.' && *p != '[')
1877 && oc_var_check_ro(cl, om))
1878 return NULL;
1879
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001880 lp->ll_valtype = om->ocm_type;
1881
1882 if (v_type == VAR_OBJECT)
1883 lp->ll_tv = ((typval_T *)(
1884 lp->ll_tv->vval.v_object + 1)) + m_idx;
1885 else
1886 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001887 }
1888 }
1889
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001890 if (lp->ll_valtype == NULL)
1891 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001892 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001893 return NULL;
1894 }
1895 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 }
1898
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001899 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 return p;
1902}
1903
1904/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001908clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909{
1910 vim_free(lp->ll_exp_name);
1911 vim_free(lp->ll_newkey);
1912}
1913
1914/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001915 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001917 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1918 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001921set_var_lval(
1922 lval_T *lp,
1923 char_u *endp,
1924 typval_T *rettv,
1925 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001926 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1927 char_u *op,
1928 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929{
1930 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932
1933 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001935 cc = *endp;
1936 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001937 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001938 return;
1939
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 if (lp->ll_blob != NULL)
1941 {
1942 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001943
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001944 if (op != NULL && *op != '=')
1945 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001946 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001947 return;
1948 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001949 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001950 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001951
1952 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1953 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001954 if (lp->ll_empty2)
1955 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1956
Bram Moolenaar68452172021-04-12 21:21:02 +02001957 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1958 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001959 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001960 }
1961 else
1962 {
1963 val = (int)tv_get_number_chk(rettv, &error);
1964 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001965 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001966 }
1967 }
1968 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001970 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001972 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1973 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001974 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001975 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001976 *endp = cc;
1977 return;
1978 }
1979
Bram Moolenaarff697e62019-02-12 22:28:33 +01001980 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001981 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001982 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001983 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001984 {
Ernie Raele75fde62023-12-21 17:18:54 +01001985 if (di != NULL && check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001986 {
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001987 clear_tv(&tv);
1988 return;
1989 }
1990
Bram Moolenaar79518e22017-02-17 16:31:35 +01001991 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001992 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1993 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001994 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001995 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01001996 ASSIGN_NO_DECL | ASSIGN_COMPOUND_OP, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001997 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002000 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002001 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002002 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
2003 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002004 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002005 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002006 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02002007 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002008 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002010 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002011 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002012 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002013 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 else if (lp->ll_range)
2015 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002016 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2017 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002018 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002019 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002020 return;
2021 }
2022
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002023 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
2024 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 }
2026 else
2027 {
2028 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00002029 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002031 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
2032 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02002033 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002034 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02002035 return;
2036 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02002037
2038 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002039 && check_typval_arg_type(lp->ll_valtype, rettv,
2040 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02002041 return;
2042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (lp->ll_newkey != NULL)
2044 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045 if (op != NULL && *op != '=')
2046 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002047 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 return;
2049 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002050 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
2051 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002052 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002054 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002055 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (di == NULL)
2057 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002058 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2059 {
2060 vim_free(di);
2061 return;
2062 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002064 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 else if (op != NULL && *op != '=')
2066 {
2067 tv_op(lp->ll_tv, rettv, op);
2068 return;
2069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 /*
2074 * Assign the value to the variable or list item.
2075 */
2076 if (copy)
2077 copy_tv(rettv, lp->ll_tv);
2078 else
2079 {
2080 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002081 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 }
2084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085}
2086
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002088 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2089 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 * Returns OK or FAIL.
2091 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002093tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002095 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 char_u numbuf[NUMBUFLEN];
2097 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002098 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099
Ernie Raele75fde62023-12-21 17:18:54 +01002100 // Can't do anything with a Funcref or Dict or Type on the right.
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002101 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002102 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Ernie Raele75fde62023-12-21 17:18:54 +01002103 && tv2->v_type != VAR_CLASS && tv2->v_type != VAR_TYPEALIAS
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02002104 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
2105 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002106 {
2107 switch (tv1->v_type)
2108 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002109 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002110 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 case VAR_DICT:
2113 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002114 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002115 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002116 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002117 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002118 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002119 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002120 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121 break;
Ernie Raele75fde62023-12-21 17:18:54 +01002122 case VAR_CLASS:
2123 case VAR_TYPEALIAS:
2124 check_typval_is_value(tv1);
2125 return FAIL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002127 case VAR_BLOB:
2128 if (*op != '+' || tv2->v_type != VAR_BLOB)
2129 break;
2130 // BLOB += BLOB
2131 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2132 {
2133 blob_T *b1 = tv1->vval.v_blob;
2134 blob_T *b2 = tv2->vval.v_blob;
2135 int i, len = blob_len(b2);
2136 for (i = 0; i < len; i++)
2137 ga_append(&b1->bv_ga, blob_get(b2, i));
2138 }
2139 return OK;
2140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002141 case VAR_LIST:
2142 if (*op != '+' || tv2->v_type != VAR_LIST)
2143 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002144 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02002145 if (tv2->vval.v_list != NULL)
2146 {
2147 if (tv1->vval.v_list == NULL)
2148 {
2149 tv1->vval.v_list = tv2->vval.v_list;
2150 ++tv1->vval.v_list->lv_refcount;
2151 }
2152 else
2153 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2154 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002155 return OK;
2156
2157 case VAR_NUMBER:
2158 case VAR_STRING:
2159 if (tv2->v_type == VAR_LIST)
2160 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002161 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002163 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002164 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002165 if (tv2->v_type == VAR_FLOAT)
2166 {
2167 float_T f = n;
2168
Bram Moolenaarff697e62019-02-12 22:28:33 +01002169 if (*op == '%')
2170 break;
2171 switch (*op)
2172 {
2173 case '+': f += tv2->vval.v_float; break;
2174 case '-': f -= tv2->vval.v_float; break;
2175 case '*': f *= tv2->vval.v_float; break;
2176 case '/': f /= tv2->vval.v_float; break;
2177 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002178 clear_tv(tv1);
2179 tv1->v_type = VAR_FLOAT;
2180 tv1->vval.v_float = f;
2181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002182 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002183 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002184 switch (*op)
2185 {
2186 case '+': n += tv_get_number(tv2); break;
2187 case '-': n -= tv_get_number(tv2); break;
2188 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002189 case '/': n = num_divide(n, tv_get_number(tv2),
2190 &failed); break;
2191 case '%': n = num_modulus(n, tv_get_number(tv2),
2192 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002193 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002194 clear_tv(tv1);
2195 tv1->v_type = VAR_NUMBER;
2196 tv1->vval.v_number = n;
2197 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 }
2199 else
2200 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002201 if (tv2->v_type == VAR_FLOAT)
2202 break;
2203
Bram Moolenaarff697e62019-02-12 22:28:33 +01002204 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002205 s = tv_get_string(tv1);
2206 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 clear_tv(tv1);
2208 tv1->v_type = VAR_STRING;
2209 tv1->vval.v_string = s;
2210 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01002211 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002213 case VAR_FLOAT:
2214 {
2215 float_T f;
2216
Bram Moolenaarff697e62019-02-12 22:28:33 +01002217 if (*op == '%' || *op == '.'
2218 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002219 && tv2->v_type != VAR_NUMBER
2220 && tv2->v_type != VAR_STRING))
2221 break;
2222 if (tv2->v_type == VAR_FLOAT)
2223 f = tv2->vval.v_float;
2224 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002225 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002226 switch (*op)
2227 {
2228 case '+': tv1->vval.v_float += f; break;
2229 case '-': tv1->vval.v_float -= f; break;
2230 case '*': tv1->vval.v_float *= f; break;
2231 case '/': tv1->vval.v_float /= f; break;
2232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002233 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002234 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002235 }
2236 }
2237
Ernie Raele75fde62023-12-21 17:18:54 +01002238 if (check_typval_is_value(tv2) == OK)
2239 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 return FAIL;
2241}
2242
2243/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002244 * Evaluate the expression used in a ":for var in expr" command.
2245 * "arg" points to "var".
2246 * Set "*errp" to TRUE for an error, FALSE otherwise;
2247 * Return a pointer that holds the info. Null when there is an error.
2248 */
2249 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250eval_for_line(
2251 char_u *arg,
2252 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002253 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002254 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002255{
Bram Moolenaar33570922005-01-25 22:26:29 +00002256 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002257 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002258 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002259 typval_T tv;
2260 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002261 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002263 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002264
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002265 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002266 if (fi == NULL)
2267 return NULL;
2268
Bram Moolenaar404557e2021-07-05 21:41:48 +02002269 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2270 &fi->fi_semicolon, FALSE);
2271 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002272 return fi;
2273
Bram Moolenaar404557e2021-07-05 21:41:48 +02002274 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002275 if (expr[0] != 'i' || expr[1] != 'n'
2276 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002277 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002278 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2279 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2280 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002281 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282 return fi;
2283 }
2284
2285 if (skip)
2286 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002287 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2288 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002289 {
2290 *errp = FALSE;
2291 if (!skip)
2292 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002293 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002294 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002295 l = tv.vval.v_list;
2296 if (l == NULL)
2297 {
2298 // a null list is like an empty list: do nothing
2299 clear_tv(&tv);
2300 }
2301 else
2302 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002304 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002306 // No need to increment the refcount, it's already set for
2307 // the list being used in "tv".
2308 fi->fi_list = l;
2309 list_add_watch(l, &fi->fi_lw);
2310 fi->fi_lw.lw_item = l->lv_first;
2311 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002312 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002313 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002314 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002315 fi->fi_bi = 0;
2316 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002317 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002318 typval_T btv;
2319
2320 // Make a copy, so that the iteration still works when the
2321 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002323 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002324 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002325 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002326 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002327 else if (tv.v_type == VAR_STRING)
2328 {
2329 fi->fi_byte_idx = 0;
2330 fi->fi_string = tv.vval.v_string;
2331 tv.vval.v_string = NULL;
2332 if (fi->fi_string == NULL)
2333 fi->fi_string = vim_strsave((char_u *)"");
2334 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002335 else
2336 {
Sean Dewar80d73952021-08-04 19:25:54 +02002337 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002338 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002339 }
2340 }
2341 }
2342 if (skip)
2343 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002344 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345
2346 return fi;
2347}
2348
2349/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002350 * Used when looping over a :for line, skip the "in expr" part.
2351 */
2352 void
2353skip_for_lines(void *fi_void, evalarg_T *evalarg)
2354{
2355 forinfo_T *fi = (forinfo_T *)fi_void;
2356 int i;
2357
2358 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002359 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002360}
2361
2362/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002363 * Use the first item in a ":for" list. Advance to the next.
2364 * Assign the values to the variable (list). "arg" points to the first one.
2365 * Return TRUE when a valid item was found, FALSE when at end of list or
2366 * something wrong.
2367 */
2368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002370{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002371 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002373 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002374 ? (ASSIGN_FINAL
2375 // first round: error if variable exists
2376 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002377 | ASSIGN_NO_MEMBER_TYPE
2378 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002379 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002380 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002381 int skip_assign = in_vim9script() && arg[0] == '_'
2382 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002384 if (fi->fi_blob != NULL)
2385 {
2386 typval_T tv;
2387
2388 if (fi->fi_bi >= blob_len(fi->fi_blob))
2389 return FALSE;
2390 tv.v_type = VAR_NUMBER;
2391 tv.v_lock = VAR_FIXED;
2392 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2393 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002394 if (skip_assign)
2395 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002396 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002397 fi->fi_varcount, flag, NULL) == OK;
2398 }
2399
2400 if (fi->fi_string != NULL)
2401 {
2402 typval_T tv;
2403 int len;
2404
2405 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2406 if (len == 0)
2407 return FALSE;
2408 tv.v_type = VAR_STRING;
2409 tv.v_lock = VAR_FIXED;
2410 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2411 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002412 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002413 if (skip_assign)
2414 result = TRUE;
2415 else
2416 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002417 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002418 vim_free(tv.vval.v_string);
2419 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002420 }
2421
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 item = fi->fi_lw.lw_item;
2423 if (item == NULL)
2424 result = FALSE;
2425 else
2426 {
2427 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002428 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002429 if (skip_assign)
2430 result = TRUE;
2431 else
2432 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002433 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434 }
2435 return result;
2436}
2437
2438/*
2439 * Free the structure used to store info used by ":for".
2440 */
2441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002442free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443{
Bram Moolenaar33570922005-01-25 22:26:29 +00002444 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002446 if (fi == NULL)
2447 return;
2448 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002449 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002451 list_unref(fi->fi_list);
2452 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002453 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002454 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002455 else
2456 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002457 vim_free(fi);
2458}
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002461set_context_for_expression(
2462 expand_T *xp,
2463 char_u *arg,
2464 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002466 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002470 if (cmdidx == CMD_let || cmdidx == CMD_var
2471 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472 {
2473 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002474 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002476 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002477 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002478 {
2479 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002480 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002481 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002482 break;
2483 }
2484 return;
2485 }
2486 }
2487 else
2488 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2489 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 while ((xp->xp_pattern = vim_strpbrk(arg,
2491 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2492 {
2493 c = *xp->xp_pattern;
2494 if (c == '&')
2495 {
2496 c = xp->xp_pattern[1];
2497 if (c == '&')
2498 {
2499 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002500 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002505 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2506 xp->xp_pattern += 2;
2507
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510 else if (c == '$')
2511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002512 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 xp->xp_context = EXPAND_ENV_VARS;
2514 }
2515 else if (c == '=')
2516 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002517 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 xp->xp_context = EXPAND_EXPRESSION;
2519 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002520 else if (c == '#'
2521 && xp->xp_context == EXPAND_EXPRESSION)
2522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002523 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002524 break;
2525 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002526 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 && xp->xp_context == EXPAND_FUNCTIONS
2528 && vim_strchr(xp->xp_pattern, '(') == NULL)
2529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002530 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 break;
2532 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002533 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002535 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2538 if (c == '\\' && xp->xp_pattern[1] != NUL)
2539 ++xp->xp_pattern;
2540 xp->xp_context = EXPAND_NOTHING;
2541 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002542 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002544 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2546 /* skip */ ;
2547 xp->xp_context = EXPAND_NOTHING;
2548 }
2549 else if (c == '|')
2550 {
2551 if (xp->xp_pattern[1] == '|')
2552 {
2553 ++xp->xp_pattern;
2554 xp->xp_context = EXPAND_EXPRESSION;
2555 }
2556 else
2557 xp->xp_context = EXPAND_COMMANDS;
2558 }
2559 else
2560 xp->xp_context = EXPAND_EXPRESSION;
2561 }
2562 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002563 // Doesn't look like something valid, expand as an expression
2564 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002565 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 arg = xp->xp_pattern;
2567 if (*arg != NUL)
2568 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2569 /* skip */ ;
2570 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002571
2572 // ":exe one two" completes "two"
2573 if ((cmdidx == CMD_execute
2574 || cmdidx == CMD_echo
2575 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002576 || cmdidx == CMD_echomsg
2577 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002578 && xp->xp_context == EXPAND_EXPRESSION)
2579 {
2580 for (;;)
2581 {
2582 char_u *n = skiptowhite(arg);
2583
2584 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2585 break;
2586 arg = skipwhite(n);
2587 }
2588 }
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 xp->xp_pattern = arg;
2591}
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002594 * Return TRUE if "pat" matches "text".
2595 * Does not use 'cpo' and always uses 'magic'.
2596 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002597 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002598pattern_match(char_u *pat, char_u *text, int ic)
2599{
2600 int matches = FALSE;
2601 char_u *save_cpo;
2602 regmatch_T regmatch;
2603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002604 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002605 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002606 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002607 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2608 if (regmatch.regprog != NULL)
2609 {
2610 regmatch.rm_ic = ic;
2611 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2612 vim_regfree(regmatch.regprog);
2613 }
2614 p_cpo = save_cpo;
2615 return matches;
2616}
2617
2618/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002619 * Handle a name followed by "(". Both for just "name(arg)" and for
2620 * "expr->name(arg)".
2621 * Returns OK or FAIL.
2622 */
2623 static int
2624eval_func(
2625 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002626 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002627 char_u *name,
2628 int name_len,
2629 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002630 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002631 typval_T *basetv) // "expr" for "expr->name(arg)"
2632{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002633 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002634 char_u *s = name;
2635 int len = name_len;
2636 partial_T *partial;
2637 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002638 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002639 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002640
2641 if (!evaluate)
2642 check_vars(s, len);
2643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002644 // If "s" is the name of a variable of type VAR_FUNC
2645 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002646 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002647 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002649 // Need to make a copy, in case evaluating the arguments makes
2650 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002651 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002652 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002653 ret = FAIL;
2654 else
2655 {
2656 funcexe_T funcexe;
2657
2658 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002659 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002660 funcexe.fe_firstline = curwin->w_cursor.lnum;
2661 funcexe.fe_lastline = curwin->w_cursor.lnum;
2662 funcexe.fe_evaluate = evaluate;
2663 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02002664 if (partial != NULL)
2665 {
2666 funcexe.fe_object = partial->pt_obj;
2667 if (funcexe.fe_object != NULL)
2668 ++funcexe.fe_object->obj_refcount;
2669 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002670 funcexe.fe_basetv = basetv;
2671 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002672 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002673 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002674 }
2675 vim_free(s);
2676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002677 // If evaluate is FALSE rettv->v_type was not set in
2678 // get_func_tv, but it's needed in handle_subscript() to parse
2679 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002680 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2681 {
2682 rettv->vval.v_string = NULL;
2683 rettv->v_type = VAR_FUNC;
2684 }
2685
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002686 // Stop the expression evaluation when immediately
2687 // aborting on error, or when an interrupt occurred or
2688 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002689 if (evaluate && aborting())
2690 {
2691 if (ret == OK)
2692 clear_tv(rettv);
2693 ret = FAIL;
2694 }
2695 return ret;
2696}
2697
2698/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002699 * After a NL, skip over empty lines and comment-only lines.
2700 */
2701 static char_u *
2702newline_skip_comments(char_u *arg)
2703{
2704 char_u *p = arg + 1;
2705
2706 for (;;)
2707 {
2708 p = skipwhite(p);
2709
2710 if (*p == NUL)
2711 break;
2712 if (vim9_comment_start(p))
2713 {
2714 char_u *nl = vim_strchr(p, NL);
2715
2716 if (nl == NULL)
2717 break;
2718 p = nl;
2719 }
2720 if (*p != NL)
2721 break;
2722 ++p; // skip another NL
2723 }
2724 return p;
2725}
2726
2727/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002728 * Get the next line source line without advancing. But do skip over comment
2729 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002730 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002731 */
2732 static char_u *
2733getline_peek_skip_comments(evalarg_T *evalarg)
2734{
2735 for (;;)
2736 {
2737 char_u *next = getline_peek(evalarg->eval_getline,
2738 evalarg->eval_cookie);
2739 char_u *p;
2740
2741 if (next == NULL)
2742 break;
2743 p = skipwhite(next);
2744 if (*p != NUL && !vim9_comment_start(p))
2745 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002746 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002747 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002748 }
2749 return NULL;
2750}
2751
2752/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002753 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2754 * comment) and there is a next line, return the next line (skipping blanks)
2755 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002756 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2757 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002758 * "arg" must point somewhere inside a line, not at the start.
2759 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002760 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002761eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2762{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002763 char_u *p = skipwhite(arg);
2764
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002765 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002766 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002768 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2769 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002770 && (*p == NUL || *p == NL
2771 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002772 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002773 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002774
Bram Moolenaare442d592022-05-05 12:20:28 +01002775 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002776 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002777 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002778 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002779 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002780 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002781
Bram Moolenaar9d489562020-07-30 20:08:50 +02002782 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002783 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002784 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002785 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002786 }
2787 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002788 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002789}
2790
2791/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002792 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002793 * Only called for Vim9 script.
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002794 *
2795 * If "arg" is not NULL, then the caller should assign the return value to
2796 * "arg".
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002797 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002798 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002799eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002800{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002801 garray_T *gap = &evalarg->eval_ga;
2802 char_u *line;
2803
Bram Moolenaar39be4982022-05-06 21:51:50 +01002804 if (arg != NULL)
2805 {
2806 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002807 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002808 // Truncate before a trailing comment, so that concatenating the lines
2809 // won't turn the rest into a comment.
2810 if (*skipwhite(arg) == '#')
2811 *arg = NUL;
2812 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002813
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002814 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002815 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2816 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002817 else
2818 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002819 if (line == NULL)
2820 return NULL;
2821
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002822 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002823 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2824 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002825 char_u *p = skipwhite(line);
2826
2827 // Going to concatenate the lines after parsing. For an empty or
2828 // comment line use an empty string.
2829 if (*p == NUL || vim9_comment_start(p))
2830 {
2831 vim_free(line);
2832 line = vim_strsave((char_u *)"");
2833 }
2834
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002835 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2836 ++gap->ga_len;
2837 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002838 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002839 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002840 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002841 evalarg->eval_tofree = line;
2842 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002843
2844 // Advanced to the next line, "arg" no longer points into the previous
Yegappan Lakshmanan28d71b52024-01-12 17:21:55 +01002845 // line. The caller assigns the return value to "arg".
2846 // If "arg" is NULL, then the return value is discarded. In that case,
2847 // "arg" still points to the previous line. So don't reset
2848 // "eval_using_cmdline".
2849 if (arg != NULL)
2850 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002851 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002852}
2853
Bram Moolenaare6b53242020-07-01 17:28:33 +02002854/*
2855 * Call eval_next_non_blank() and get the next line if needed.
2856 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002857 char_u *
2858skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2859{
2860 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002861 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002862
Bram Moolenaar962d7212020-07-04 14:15:00 +02002863 if (evalarg == NULL)
2864 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002865 eval_next_non_blank(p, evalarg, &getnext);
2866 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002867 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002868 return p;
2869}
2870
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002871/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002872 * The "eval" functions have an "evalarg" argument: When NULL or
2873 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2874 * parsed but not executed. The functions may return OK, but the rettv will be
2875 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
2877
2878/*
2879 * Handle zero level expression.
2880 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002881 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002882 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * Return OK or FAIL.
2885 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002887eval0(
2888 char_u *arg,
2889 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002890 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002893 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2894}
2895
2896/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002897 * If "arg" is a simple function call without arguments then call it and return
2898 * the result. Otherwise return NOTDONE.
2899 */
2900 int
2901may_call_simple_func(
2902 char_u *arg,
2903 typval_T *rettv)
2904{
2905 char_u *parens = (char_u *)strstr((char *)arg, "()");
2906 int r = NOTDONE;
2907
2908 // If the expression is "FuncName()" then we can skip a lot of overhead.
2909 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2910 {
2911 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2912
2913 if (to_name_end(p, TRUE) == parens)
2914 r = call_simple_func(arg, (int)(parens - arg), rettv);
2915 }
2916 return r;
2917}
2918
2919/*
2920 * Handle zero level expression with optimization for a simple function call.
2921 * Same arguments and return value as eval0().
2922 */
2923 int
2924eval0_simple_funccal(
2925 char_u *arg,
2926 typval_T *rettv,
2927 exarg_T *eap,
2928 evalarg_T *evalarg)
2929{
2930 int r = may_call_simple_func(arg, rettv);
2931
2932 if (r == NOTDONE)
2933 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2934 return r;
2935}
2936
2937/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002938 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2939 * expression and don't check what comes after the expression.
2940 */
2941 int
2942eval0_retarg(
2943 char_u *arg,
2944 typval_T *rettv,
2945 exarg_T *eap,
2946 evalarg_T *evalarg,
2947 char_u **retarg)
2948{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 int ret;
2950 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002951 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002952 int did_emsg_before = did_emsg;
2953 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002954 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002955 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002959
Bram Moolenaar79481362022-06-27 20:15:10 +01002960 if (ret != FAIL)
2961 {
2962 expr_end = p;
2963 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002964
Bram Moolenaar79481362022-06-27 20:15:10 +01002965 // In Vim9 script a command block is not split at NL characters for
2966 // commands using an expression argument. Skip over a '#' comment to
2967 // check for a following NL. Require white space before the '#'.
2968 if (in_vim9script() && p > expr_end && retarg == NULL)
2969 while (*p == '#')
2970 {
2971 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002972
Bram Moolenaar79481362022-06-27 20:15:10 +01002973 if (nl == NULL)
2974 break;
2975 p = skipwhite(nl + 1);
2976 if (eap != NULL && *p != NUL)
2977 eap->nextcmd = p;
2978 check_for_end = FALSE;
2979 }
2980
2981 if (check_for_end)
2982 end_error = !ends_excmd2(arg, p);
2983 }
2984
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002985 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
2987 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002988 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 /*
2990 * Report the invalid expression unless the expression evaluation has
2991 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002992 * exception, or we already gave a more specific error.
2993 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002995 if (!aborting()
2996 && did_emsg == did_emsg_before
2997 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002998 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002999 {
3000 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00003001 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003002 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003003 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02003004 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003005
zeertzjqf2588b62023-05-05 17:22:35 +01003006 if (eap != NULL && p != NULL)
3007 {
3008 // Some of the expression may not have been consumed.
3009 // Only execute a next command if it cannot be a "||" operator.
3010 // The next command may be "catch".
3011 char_u *nextcmd = check_nextcmd(p);
3012 if (nextcmd != NULL && *nextcmd != '|')
3013 eap->nextcmd = nextcmd;
3014 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01003015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003017
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003018 if (retarg != NULL)
3019 *retarg = p;
3020 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02003021 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 return ret;
3024}
3025
3026/*
3027 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003028 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003029 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 *
3031 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003032 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003034 * Note: "rettv.v_lock" is not set.
3035 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 * Return OK or FAIL.
3037 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003038 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
Bram Moolenaar793648f2020-06-26 21:28:25 +02003041 char_u *p;
3042 int getnext;
3043
Bram Moolenaar4a091b92020-09-12 22:10:00 +02003044 CLEAR_POINTER(rettv);
3045
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 /*
3047 * Get the first variable.
3048 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 return FAIL;
3051
Bram Moolenaar793648f2020-06-26 21:28:25 +02003052 p = eval_next_non_blank(*arg, evalarg, &getnext);
3053 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003055 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 int result;
3057 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003058 evalarg_T *evalarg_used = evalarg;
3059 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003060 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02003061 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02003062 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003063
3064 if (evalarg == NULL)
3065 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003066 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003067 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003068 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003069 orig_flags = evalarg_used->eval_flags;
3070 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003071
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003072 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003073 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003074 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003075 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003076 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003077 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003078 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003079 clear_tv(rettv);
3080 return FAIL;
3081 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003082 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003083 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003086 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003088 int error = FALSE;
3089
Bram Moolenaar13106602020-10-04 16:06:05 +02003090 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003091 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02003092 else if (vim9script)
3093 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02003094 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003096 if (error || !op_falsy || !result)
3097 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003098 if (error)
3099 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101
3102 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02003103 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003105 if (op_falsy)
3106 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02003107 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003108 {
mityu4ac198c2021-05-28 17:52:40 +02003109 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003110 clear_tv(rettv);
3111 return FAIL;
3112 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003113 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003114 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01003115 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003116 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02003117 {
3118 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02003120 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003121 if (!op_falsy || !result)
3122 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003124 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003126 /*
3127 * Check for the ":".
3128 */
3129 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
3130 if (*p != ':')
3131 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003132 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003133 if (evaluate && result)
3134 clear_tv(rettv);
3135 evalarg_used->eval_flags = orig_flags;
3136 return FAIL;
3137 }
3138 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003139 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003140 else
3141 {
Bram Moolenaar13106602020-10-04 16:06:05 +02003142 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003143 {
3144 error_white_both(p, 1);
3145 clear_tv(rettv);
3146 evalarg_used->eval_flags = orig_flags;
3147 return FAIL;
3148 }
3149 *arg = p;
3150 }
3151
3152 /*
3153 * Get the third variable. Recursive!
3154 */
Bram Moolenaar13106602020-10-04 16:06:05 +02003155 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003156 {
mityu4ac198c2021-05-28 17:52:40 +02003157 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003158 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02003159 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003160 return FAIL;
3161 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003162 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
3163 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003164 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02003165 if (eval1(arg, &var2, evalarg_used) == FAIL)
3166 {
3167 if (evaluate && result)
3168 clear_tv(rettv);
3169 evalarg_used->eval_flags = orig_flags;
3170 return FAIL;
3171 }
3172 if (evaluate && !result)
3173 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003175
3176 if (evalarg == NULL)
3177 clear_evalarg(&local_evalarg, NULL);
3178 else
3179 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
3181
3182 return OK;
3183}
3184
3185/*
3186 * Handle first level expression:
3187 * expr2 || expr2 || expr2 logical OR
3188 *
3189 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003190 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 *
3192 * Return OK or FAIL.
3193 */
3194 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003197 char_u *p;
3198 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003201 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003203 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 return FAIL;
3205
3206 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003207 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003209 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003210 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003212 evalarg_T *evalarg_used = evalarg;
3213 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003214 int evaluate;
3215 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003216 long result = FALSE;
3217 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003218 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003219 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003220
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003221 if (evalarg == NULL)
3222 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003223 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003224 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003225 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003226 orig_flags = evalarg_used->eval_flags;
3227 evaluate = orig_flags & EVAL_EVALUATE;
3228 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003229 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003230 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003231 result = tv_get_bool_chk(rettv, &error);
3232 else if (tv_get_number_chk(rettv, &error) != 0)
3233 result = TRUE;
3234 clear_tv(rettv);
3235 if (error)
3236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238
3239 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003240 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003242 while (p[0] == '|' && p[1] == '|')
3243 {
3244 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003245 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003246 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003247 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003248 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003249 {
3250 error_white_both(p, 2);
3251 clear_tv(rettv);
3252 return FAIL;
3253 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003254 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003255 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003256
3257 /*
3258 * Get the second variable.
3259 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003260 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003261 {
mityu4ac198c2021-05-28 17:52:40 +02003262 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003263 clear_tv(rettv);
3264 return FAIL;
3265 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003266 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3267 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003268 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003269 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003270 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003271
3272 /*
3273 * Compute the result.
3274 */
3275 if (evaluate && !result)
3276 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003277 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003278 result = tv_get_bool_chk(&var2, &error);
3279 else if (tv_get_number_chk(&var2, &error) != 0)
3280 result = TRUE;
3281 clear_tv(&var2);
3282 if (error)
3283 return FAIL;
3284 }
3285 if (evaluate)
3286 {
3287 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003288 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003289 rettv->v_type = VAR_BOOL;
3290 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003291 }
3292 else
3293 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003294 rettv->v_type = VAR_NUMBER;
3295 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003296 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003297 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003298
3299 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003301
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003302 if (evalarg == NULL)
3303 clear_evalarg(&local_evalarg, NULL);
3304 else
3305 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307
3308 return OK;
3309}
3310
3311/*
3312 * Handle second level expression:
3313 * expr3 && expr3 && expr3 logical AND
3314 *
3315 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003316 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 *
3318 * Return OK or FAIL.
3319 */
3320 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003321eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003323 char_u *p;
3324 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
3326 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003327 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003329 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return FAIL;
3331
3332 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003333 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003335 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003336 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003338 evalarg_T *evalarg_used = evalarg;
3339 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003340 int orig_flags;
3341 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003342 long result = TRUE;
3343 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003344 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003345 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003346
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003347 if (evalarg == NULL)
3348 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003349 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003350 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003351 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003352 orig_flags = evalarg_used->eval_flags;
3353 evaluate = orig_flags & EVAL_EVALUATE;
3354 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003355 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003356 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003357 result = tv_get_bool_chk(rettv, &error);
3358 else if (tv_get_number_chk(rettv, &error) == 0)
3359 result = FALSE;
3360 clear_tv(rettv);
3361 if (error)
3362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364
3365 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003366 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003368 while (p[0] == '&' && p[1] == '&')
3369 {
3370 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003371 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003372 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003373 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003374 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003375 {
3376 error_white_both(p, 2);
3377 clear_tv(rettv);
3378 return FAIL;
3379 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003380 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003381 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003382
3383 /*
3384 * Get the second variable.
3385 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003386 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003387 {
mityu4ac198c2021-05-28 17:52:40 +02003388 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003389 clear_tv(rettv);
3390 return FAIL;
3391 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003392 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3393 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003394 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003395 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003396 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003398
3399 /*
3400 * Compute the result.
3401 */
3402 if (evaluate && result)
3403 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003404 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003405 result = tv_get_bool_chk(&var2, &error);
3406 else if (tv_get_number_chk(&var2, &error) == 0)
3407 result = FALSE;
3408 clear_tv(&var2);
3409 if (error)
3410 return FAIL;
3411 }
3412 if (evaluate)
3413 {
3414 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003415 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003416 rettv->v_type = VAR_BOOL;
3417 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003418 }
3419 else
3420 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003421 rettv->v_type = VAR_NUMBER;
3422 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003423 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003424 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003425
3426 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003428
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003429 if (evalarg == NULL)
3430 clear_evalarg(&local_evalarg, NULL);
3431 else
3432 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434
3435 return OK;
3436}
3437
3438/*
3439 * Handle third level expression:
3440 * var1 == var2
3441 * var1 =~ var2
3442 * var1 != var2
3443 * var1 !~ var2
3444 * var1 > var2
3445 * var1 >= var2
3446 * var1 < var2
3447 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003448 * var1 is var2
3449 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 *
3451 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003452 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 *
3454 * Return OK or FAIL.
3455 */
3456 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003457eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003460 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003461 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003463 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003466 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003468 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 return FAIL;
3470
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003471 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003472
Bram Moolenaar696ba232020-07-29 21:20:41 +02003473 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003476 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003478 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003480 typval_T var2;
3481 int ic;
3482 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003483 int evaluate = evalarg == NULL
3484 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003485 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003486
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003487 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003488 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003489 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003490 p = *arg;
3491 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003492 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3493 {
mityu4ac198c2021-05-28 17:52:40 +02003494 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003495 clear_tv(rettv);
3496 return FAIL;
3497 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003498
Bram Moolenaar696ba232020-07-29 21:20:41 +02003499 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3500 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003501 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003502 clear_tv(rettv);
3503 return FAIL;
3504 }
3505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (p[len] == '?')
3508 {
3509 ic = TRUE;
3510 ++len;
3511 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 else if (p[len] == '#')
3514 {
3515 ic = FALSE;
3516 ++len;
3517 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003518 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003520 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522 /*
3523 * Get the second variable.
3524 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003525 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3526 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003527 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003528 clear_tv(rettv);
3529 return FAIL;
3530 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003531 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003532 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 return FAIL;
3536 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003537 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003538 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003539 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003540
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003541 // use the line of the comparison for messages
3542 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003543 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003544 {
3545 ret = FAIL;
3546 clear_tv(rettv);
3547 }
3548 else
3549 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003550 clear_tv(&var2);
3551 return ret;
3552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
3555 return OK;
3556}
3557
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003558/*
3559 * Make a copy of blob "tv1" and append blob "tv2".
3560 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 void
3562eval_addblob(typval_T *tv1, typval_T *tv2)
3563{
3564 blob_T *b1 = tv1->vval.v_blob;
3565 blob_T *b2 = tv2->vval.v_blob;
3566 blob_T *b = blob_alloc();
3567 int i;
3568
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003569 if (b == NULL)
3570 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003572 for (i = 0; i < blob_len(b1); i++)
3573 ga_append(&b->bv_ga, blob_get(b1, i));
3574 for (i = 0; i < blob_len(b2); i++)
3575 ga_append(&b->bv_ga, blob_get(b2, i));
3576
3577 clear_tv(tv1);
3578 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579}
3580
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003581/*
3582 * Make a copy of list "tv1" and append list "tv2".
3583 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 int
3585eval_addlist(typval_T *tv1, typval_T *tv2)
3586{
3587 typval_T var3;
3588
3589 // concatenate Lists
3590 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3591 {
3592 clear_tv(tv1);
3593 clear_tv(tv2);
3594 return FAIL;
3595 }
3596 clear_tv(tv1);
3597 *tv1 = var3;
3598 return OK;
3599}
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003602 * Handle the bitwise left/right shift operator expression:
3603 * var1 << var2
3604 * var1 >> var2
3605 *
3606 * "arg" must point to the first non-white of the expression.
3607 * "arg" is advanced to just after the recognized expression.
3608 *
3609 * Return OK or FAIL.
3610 */
3611 static int
3612eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3613{
3614 /*
3615 * Get the first expression.
3616 */
3617 if (eval6(arg, rettv, evalarg) == FAIL)
3618 return FAIL;
3619
3620 /*
3621 * Repeat computing, until no '<<' or '>>' is following.
3622 */
3623 for (;;)
3624 {
3625 char_u *p;
3626 int getnext;
3627 exprtype_T type;
3628 int evaluate;
3629 typval_T var2;
3630 int vim9script;
3631
3632 p = eval_next_non_blank(*arg, evalarg, &getnext);
3633 if (p[0] == '<' && p[1] == '<')
3634 type = EXPR_LSHIFT;
3635 else if (p[0] == '>' && p[1] == '>')
3636 type = EXPR_RSHIFT;
3637 else
3638 return OK;
3639
3640 // Handle a bitwise left or right shift operator
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003641 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3642 if (evaluate && rettv->v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003643 {
3644 // left operand should be a number
3645 emsg(_(e_bitshift_ops_must_be_number));
3646 clear_tv(rettv);
3647 return FAIL;
3648 }
3649
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003650 vim9script = in_vim9script();
3651 if (getnext)
3652 {
3653 *arg = eval_next_line(*arg, evalarg);
3654 p = *arg;
3655 }
3656 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3657 {
3658 error_white_both(*arg, 2);
3659 clear_tv(rettv);
3660 return FAIL;
3661 }
3662
3663 /*
3664 * Get the second variable.
3665 */
3666 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3667 {
3668 error_white_both(p, 2);
3669 clear_tv(rettv);
3670 return FAIL;
3671 }
3672 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3673 if (eval6(arg, &var2, evalarg) == FAIL)
3674 {
3675 clear_tv(rettv);
3676 return FAIL;
3677 }
3678
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003679 if (evaluate)
3680 {
Yegappan Lakshmanande3295d2023-10-15 09:44:50 +02003681 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3682 {
3683 // right operand should be a positive number
3684 if (var2.v_type != VAR_NUMBER)
3685 emsg(_(e_bitshift_ops_must_be_number));
3686 else
3687 emsg(_(e_bitshift_ops_must_be_positive));
3688 clear_tv(rettv);
3689 clear_tv(&var2);
3690 return FAIL;
3691 }
3692
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003693 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3694 // shifting more bits than we have always results in zero
3695 rettv->vval.v_number = 0;
3696 else if (type == EXPR_LSHIFT)
3697 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003698 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003699 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003700 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003701 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003702 }
3703
3704 clear_tv(&var2);
3705 }
3706
3707 return OK;
3708}
3709
3710/*
3711 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003712 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003714 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003715 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 *
3717 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003718 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 *
3720 * Return OK or FAIL.
3721 */
3722 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003723eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003726 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003728 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return FAIL;
3730
3731 /*
3732 * Repeat computing, until no '+', '-' or '.' is following.
3733 */
3734 for (;;)
3735 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003736 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003737 int getnext;
3738 char_u *p;
3739 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003740 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003741 int concat;
3742 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003743 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003744
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003745 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003746 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003747 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003748 p = eval_next_non_blank(*arg, evalarg, &getnext);
3749 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003750 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003751 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3752 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003754 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3755 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003756
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003757 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003758 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003759 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003760 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003761 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003762 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003763 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003764 {
mityu4ac198c2021-05-28 17:52:40 +02003765 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003766 clear_tv(rettv);
3767 return FAIL;
3768 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003769 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003770 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003771 if ((op != '+' || (rettv->v_type != VAR_LIST
3772 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003773 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003774 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003776 int error = FALSE;
3777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003778 // For "list + ...", an illegal use of the first operand as
3779 // a number cannot be determined before evaluating the 2nd
3780 // operand: if this is also a list, all is ok.
3781 // For "something . ...", "something - ..." or "non-list + ...",
3782 // we know that the first operand needs to be a string or number
3783 // without evaluating the 2nd operand. So check before to avoid
3784 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003785 if (op != '.')
3786 tv_get_number_chk(rettv, &error);
3787 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 {
3789 clear_tv(rettv);
3790 return FAIL;
3791 }
3792 }
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 /*
3795 * Get the second variable.
3796 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003797 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003798 {
mityu89dcb4d2021-05-28 13:50:17 +02003799 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003800 clear_tv(rettv);
3801 return FAIL;
3802 }
3803 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003804 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return FAIL;
3808 }
3809
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003810 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
3812 /*
3813 * Compute the result.
3814 */
3815 if (op == '.')
3816 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003817 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3818 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003819 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003820
Bram Moolenaar2e086612020-08-25 22:37:48 +02003821 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003822 || var2.v_type == VAR_CHANNEL
3823 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003824 semsg(_(e_using_invalid_value_as_string_str),
3825 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003826 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003827 {
3828 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3829 var2.vval.v_float);
3830 s2 = buf2;
3831 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003832 else
3833 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 {
3836 clear_tv(rettv);
3837 clear_tv(&var2);
3838 return FAIL;
3839 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003840 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
3842 rettv->v_type = VAR_STRING;
3843 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003845 else if (op == '+' && rettv->v_type == VAR_BLOB
3846 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003848 else if (op == '+' && rettv->v_type == VAR_LIST
3849 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003850 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003852 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 else
3855 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003856 int error = FALSE;
3857 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003858 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003859
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003860 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003862 f1 = rettv->vval.v_float;
3863 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003864 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003867 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 if (error)
3869 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003870 // This can only happen for "list + non-list" or
3871 // "blob + non-blob". For "non-list + ..." or
3872 // "something - ...", we returned before evaluating the
3873 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003874 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003875 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003876 return FAIL;
3877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003878 if (var2.v_type == VAR_FLOAT)
3879 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003880 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003881 if (var2.v_type == VAR_FLOAT)
3882 {
3883 f2 = var2.vval.v_float;
3884 n2 = 0;
3885 }
3886 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003888 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889 if (error)
3890 {
3891 clear_tv(rettv);
3892 clear_tv(&var2);
3893 return FAIL;
3894 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003895 if (rettv->v_type == VAR_FLOAT)
3896 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003900 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003901 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3902 {
3903 if (op == '+')
3904 f1 = f1 + f2;
3905 else
3906 f1 = f1 - f2;
3907 rettv->v_type = VAR_FLOAT;
3908 rettv->vval.v_float = f1;
3909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003911 {
3912 if (op == '+')
3913 n1 = n1 + n2;
3914 else
3915 n1 = n1 - n2;
3916 rettv->v_type = VAR_NUMBER;
3917 rettv->vval.v_number = n1;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003920 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 }
3923 return OK;
3924}
3925
3926/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003927 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 * * number multiplication
3929 * / number division
3930 * % number modulo
3931 *
3932 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003933 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 *
3935 * Return OK or FAIL.
3936 */
3937 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003938eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939 char_u **arg,
3940 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003941 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003942 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003944 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003947 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003949 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 return FAIL;
3951
3952 /*
3953 * Repeat computing, until no '*', '/' or '%' is following.
3954 */
3955 for (;;)
3956 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003957 int evaluate;
3958 int getnext;
3959 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003960 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003961 int op;
3962 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003963 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003964 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003965
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003966 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003967 p = eval_next_non_blank(*arg, evalarg, &getnext);
3968 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003969 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003971
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003972 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003973 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003974 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003975 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003976 {
3977 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3978 {
mityu4ac198c2021-05-28 17:52:40 +02003979 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003980 clear_tv(rettv);
3981 return FAIL;
3982 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003983 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003986 f1 = 0;
3987 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003988 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003989 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003991 if (rettv->v_type == VAR_FLOAT)
3992 {
3993 f1 = rettv->vval.v_float;
3994 use_float = TRUE;
3995 n1 = 0;
3996 }
3997 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003998 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004000 if (error)
4001 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 else
4004 n1 = 0;
4005
4006 /*
4007 * Get the second variable.
4008 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004009 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
4010 {
Bram Moolenaara9749532021-03-06 18:18:19 +01004011 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02004012 clear_tv(rettv);
4013 return FAIL;
4014 }
4015 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004016 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return FAIL;
4018
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004019 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004021 if (var2.v_type == VAR_FLOAT)
4022 {
4023 if (!use_float)
4024 {
4025 f1 = n1;
4026 use_float = TRUE;
4027 }
4028 f2 = var2.vval.v_float;
4029 n2 = 0;
4030 }
4031 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004033 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004034 clear_tv(&var2);
4035 if (error)
4036 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004037 if (use_float)
4038 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 /*
4042 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004043 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004047 if (op == '*')
4048 f1 = f1 * f2;
4049 else if (op == '/')
4050 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004051#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004052 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004053 if (f2 == 0.0)
4054 {
4055 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004056 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004057 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004058 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004059 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004060 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004061 }
4062 else
4063 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004064#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004065 // We rely on the floating point library to handle divide
4066 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004067 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004068#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004071 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004072 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004073 return FAIL;
4074 }
4075 rettv->v_type = VAR_FLOAT;
4076 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 else
4079 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004080 int failed = FALSE;
4081
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 if (op == '*')
4083 n1 = n1 * n2;
4084 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004085 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004087 n1 = num_modulus(n1, n2, &failed);
4088 if (failed)
4089 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01004090
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004091 rettv->v_type = VAR_NUMBER;
4092 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 }
4096
4097 return OK;
4098}
4099
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004100/*
4101 * Handle a type cast before a base level expression.
4102 * "arg" must point to the first non-white of the expression.
4103 * "arg" is advanced to just after the recognized expression.
4104 * Return OK or FAIL.
4105 */
4106 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004107eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004108 char_u **arg,
4109 typval_T *rettv,
4110 evalarg_T *evalarg,
4111 int want_string) // after "." operator
4112{
4113 type_T *want_type = NULL;
4114 garray_T type_list; // list of pointers to allocated types
4115 int res;
4116 int evaluate = evalarg == NULL ? 0
4117 : (evalarg->eval_flags & EVAL_EVALUATE);
4118
4119 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02004120 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
4121 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004122 {
4123 ++*arg;
4124 ga_init2(&type_list, sizeof(type_T *), 10);
4125 want_type = parse_type(arg, &type_list, TRUE);
4126 if (want_type == NULL && (evaluate || **arg != '>'))
4127 {
4128 clear_type_list(&type_list);
4129 return FAIL;
4130 }
4131
4132 if (**arg != '>')
4133 {
4134 if (*skipwhite(*arg) == '>')
4135 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
4136 else
4137 emsg(_(e_missing_gt));
4138 clear_type_list(&type_list);
4139 return FAIL;
4140 }
4141 ++*arg;
4142 *arg = skipwhite_and_linebreak(*arg, evalarg);
4143 }
4144
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004145 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004146
4147 if (want_type != NULL && evaluate)
4148 {
4149 if (res == OK)
4150 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00004151 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
4152 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004153
Bram Moolenaar60dc8272021-07-29 22:48:54 +02004154 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004155 {
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02004156 if (want_type->tt_type == VAR_BOOL
4157 && actual->tt_type != VAR_BOOL
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004158 && (actual->tt_flags & TTFLAG_BOOL_OK))
4159 {
4160 int n = tv2bool(rettv);
4161
4162 // can use "0" and "1" for boolean in some places
4163 clear_tv(rettv);
4164 rettv->v_type = VAR_BOOL;
4165 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4166 }
4167 else
4168 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02004169 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004170
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02004171 res = check_type(want_type, actual, TRUE, where);
4172 }
4173 }
4174 }
4175 clear_type_list(&type_list);
4176 }
4177
4178 return res;
4179}
4180
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004181 int
4182eval_leader(char_u **arg, int vim9)
4183{
4184 char_u *s = *arg;
4185 char_u *p = *arg;
4186
4187 while (*p == '!' || *p == '-' || *p == '+')
4188 {
4189 char_u *n = skipwhite(p + 1);
4190
4191 // ++, --, -+ and +- are not accepted in Vim9 script
4192 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
4193 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004194 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004195 return FAIL;
4196 }
4197 p = n;
4198 }
4199 *arg = p;
4200 return OK;
4201}
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004204 * Check for a predefined value "true", "false" and "null.*".
4205 * Return OK when recognized.
4206 */
4207 int
4208handle_predefined(char_u *s, int len, typval_T *rettv)
4209{
4210 switch (len)
4211 {
4212 case 4: if (STRNCMP(s, "true", 4) == 0)
4213 {
4214 rettv->v_type = VAR_BOOL;
4215 rettv->vval.v_number = VVAL_TRUE;
4216 return OK;
4217 }
4218 if (STRNCMP(s, "null", 4) == 0)
4219 {
4220 rettv->v_type = VAR_SPECIAL;
4221 rettv->vval.v_number = VVAL_NULL;
4222 return OK;
4223 }
4224 break;
4225 case 5: if (STRNCMP(s, "false", 5) == 0)
4226 {
4227 rettv->v_type = VAR_BOOL;
4228 rettv->vval.v_number = VVAL_FALSE;
4229 return OK;
4230 }
4231 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004232 case 8: if (STRNCMP(s, "null_job", 8) == 0)
4233 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004234#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004235 rettv->v_type = VAR_JOB;
4236 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004237#else
4238 rettv->v_type = VAR_SPECIAL;
4239 rettv->vval.v_number = VVAL_NULL;
4240#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004241 return OK;
4242 }
4243 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004244 case 9:
4245 if (STRNCMP(s, "null_", 5) != 0)
4246 break;
4247 if (STRNCMP(s + 5, "list", 4) == 0)
4248 {
4249 rettv->v_type = VAR_LIST;
4250 rettv->vval.v_list = NULL;
4251 return OK;
4252 }
4253 if (STRNCMP(s + 5, "dict", 4) == 0)
4254 {
4255 rettv->v_type = VAR_DICT;
4256 rettv->vval.v_dict = NULL;
4257 return OK;
4258 }
4259 if (STRNCMP(s + 5, "blob", 4) == 0)
4260 {
4261 rettv->v_type = VAR_BLOB;
4262 rettv->vval.v_blob = NULL;
4263 return OK;
4264 }
4265 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004266 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4267 {
4268 rettv->v_type = VAR_CLASS;
4269 rettv->vval.v_class = NULL;
4270 return OK;
4271 }
4272 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004273 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4274 {
4275 rettv->v_type = VAR_STRING;
4276 rettv->vval.v_string = NULL;
4277 return OK;
4278 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004279 if (STRNCMP(s, "null_object", 11) == 0)
4280 {
4281 rettv->v_type = VAR_OBJECT;
4282 rettv->vval.v_object = NULL;
4283 return OK;
4284 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004285 break;
4286 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004287 if (STRNCMP(s, "null_channel", 12) == 0)
4288 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004289#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004290 rettv->v_type = VAR_CHANNEL;
4291 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004292#else
4293 rettv->v_type = VAR_SPECIAL;
4294 rettv->vval.v_number = VVAL_NULL;
4295#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004296 return OK;
4297 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004298 if (STRNCMP(s, "null_partial", 12) == 0)
4299 {
4300 rettv->v_type = VAR_PARTIAL;
4301 rettv->vval.v_partial = NULL;
4302 return OK;
4303 }
4304 break;
4305 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4306 {
4307 rettv->v_type = VAR_FUNC;
4308 rettv->vval.v_string = NULL;
4309 return OK;
4310 }
4311 break;
4312 }
4313 return FAIL;
4314}
4315
4316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 * Handle sixth level expression:
4318 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004319 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004320 * "string" string constant
4321 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 * &option-name option value
4323 * @r register contents
4324 * identifier variable value
4325 * function() function call
4326 * $VAR environment variable
4327 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004328 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004330 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004331 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 *
4333 * Also handle:
4334 * ! in front logical NOT
4335 * - in front unary minus
4336 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004337 * trailing [] subscript in String or List
4338 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004339 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 *
4341 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004342 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 *
4344 * Return OK or FAIL.
4345 */
4346 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004347eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004348 char_u **arg,
4349 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004350 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004353 int evaluate = evalarg != NULL
4354 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 int len;
4356 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004357 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 char_u *start_leader, *end_leader;
4359 int ret = OK;
4360 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004361 static int recurse = 0;
4362 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004366 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004371 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004374 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004375 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 end_leader = *arg;
4377
Keith Thompson184f71c2024-01-04 21:19:04 +01004378 if (**arg == '.' && (!SAFE_isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004379 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004380 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004381 ++*arg;
4382 return FAIL;
4383 }
4384
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004385 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004386 // and crash. With MSVC the stack is smaller.
4387 if (recurse ==
4388#ifdef _MSC_VER
4389 300
4390#else
4391 1000
4392#endif
4393 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004394 {
4395 semsg(_(e_expression_too_recursive_str), *arg);
4396 return FAIL;
4397 }
4398 ++recurse;
4399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 switch (**arg)
4401 {
4402 /*
4403 * Number constant.
4404 */
4405 case '0':
4406 case '1':
4407 case '2':
4408 case '3':
4409 case '4':
4410 case '5':
4411 case '6':
4412 case '7':
4413 case '8':
4414 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004415 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004416
4417 // Apply prefixed "-" and "+" now. Matters especially when
4418 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004419 if (ret == OK && evaluate && end_leader > start_leader
4420 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004421 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 break;
4423
4424 /*
4425 * String constant: "string".
4426 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004427 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 break;
4429
4430 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004433 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 break;
4435
4436 /*
4437 * List: [expr, expr]
4438 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004439 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441
4442 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004443 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004444 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004445 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004446 {
4447 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4448 }
4449 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004450 {
4451 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004452 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004453 }
4454 else
4455 ret = NOTDONE;
4456 break;
4457
4458 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004459 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004460 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004462 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004463 ret = NOTDONE;
4464 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004465 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004466 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004467 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 break;
4469
4470 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004471 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004473 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
4475
4476 /*
4477 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004478 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 */
LemonBoy2eaef102022-05-06 13:14:50 +01004480 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4481 ret = eval_interp_string(arg, rettv, evaluate);
4482 else
4483 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485
4486 /*
4487 * Register contents: @r.
4488 */
4489 case '@': ++*arg;
4490 if (evaluate)
4491 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004492 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004493 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004494 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004495 emsg_invreg(**arg);
4496 else
4497 {
4498 rettv->v_type = VAR_STRING;
4499 rettv->vval.v_string = get_reg_contents(**arg,
4500 GREG_EXPR_SRC);
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503 if (**arg != NUL)
4504 ++*arg;
4505 break;
4506
4507 /*
4508 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004509 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004511 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004512 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004513 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004514 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004515 if (ret == OK && evaluate)
4516 {
4517 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4518
Bram Moolenaara9931532021-06-12 15:58:16 +02004519 // Compile it here to get the return type. The return
4520 // type is optional, when it's missing use t_unknown.
4521 // This is recognized in compile_return().
4522 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4523 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004524 if (compile_def_function(ufunc, FALSE,
4525 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004526 {
4527 clear_tv(rettv);
4528 ret = FAIL;
4529 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004530 }
4531 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004532 if (ret == NOTDONE)
4533 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004534 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004535 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004536
Bram Moolenaar9215f012020-06-27 21:18:00 +02004537 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004538 if (**arg == ')')
4539 ++*arg;
4540 else if (ret == OK)
4541 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004542 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004543 clear_tv(rettv);
4544 ret = FAIL;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 break;
4548
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 break;
4551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552
4553 if (ret == NOTDONE)
4554 {
4555 /*
4556 * Must be a variable or function name.
4557 * Can also be a curly-braces kind of name: {expr}.
4558 */
4559 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004560 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 if (alias != NULL)
4562 s = alias;
4563
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004564 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 ret = FAIL;
4566 else
4567 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004568 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4569
Bram Moolenaar4525a572022-02-13 11:57:33 +00004570 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004571 {
4572 emsg(_(e_cannot_use_underscore_here));
4573 ret = FAIL;
4574 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004575 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004576 && s[0] == 's' && s[1] == ':')
4577 {
4578 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4579 ret = FAIL;
4580 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004581 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004582 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004583 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004584 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004585 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004586 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004588 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004589 // get the value of "true", "false", etc. or a variable
4590 ret = FAIL;
4591 if (vim9script)
4592 ret = handle_predefined(s, len, rettv);
4593 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004594 {
4595 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004596 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004597 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004598 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004599 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004600 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004601 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004602 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004603 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004604 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004607 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 }
4609
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4611 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004612 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004613 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 /*
4616 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4617 */
4618 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004619 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004620
4621 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004622 return ret;
4623}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004625/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004626 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004627 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004628 * Adjusts "end_leaderp" until it is at "start_leader".
4629 */
4630 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004631eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004632 typval_T *rettv,
4633 int numeric_only,
4634 char_u *start_leader,
4635 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004636{
4637 char_u *end_leader = *end_leaderp;
4638 int ret = OK;
4639 int error = FALSE;
4640 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004641 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004642 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004643 float_T f = 0.0;
4644
4645 if (rettv->v_type == VAR_FLOAT)
4646 f = rettv->vval.v_float;
4647 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004648 {
4649 while (VIM_ISWHITE(end_leader[-1]))
4650 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004651 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004652 val = tv2bool(rettv);
4653 else
4654 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004655 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004656 if (error)
4657 {
4658 clear_tv(rettv);
4659 ret = FAIL;
4660 }
4661 else
4662 {
4663 while (end_leader > start_leader)
4664 {
4665 --end_leader;
4666 if (*end_leader == '!')
4667 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004668 if (numeric_only)
4669 {
4670 ++end_leader;
4671 break;
4672 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004673 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004674 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004675 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004676 {
4677 rettv->v_type = VAR_BOOL;
4678 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4679 }
4680 else
4681 f = !f;
4682 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004683 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004684 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004685 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004686 type = VAR_BOOL;
4687 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004688 }
4689 else if (*end_leader == '-')
4690 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004691 if (rettv->v_type == VAR_FLOAT)
4692 f = -f;
4693 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004694 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004695 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004696 type = VAR_NUMBER;
4697 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004698 }
4699 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004700 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004703 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 else
4706 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004707 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004708 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004709 rettv->v_type = type;
4710 else
4711 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004712 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004715 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return ret;
4717}
4718
4719/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004720 * Call the function referred to in "rettv".
4721 */
4722 static int
4723call_func_rettv(
4724 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004725 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004726 typval_T *rettv,
4727 int evaluate,
4728 dict_T *selfdict,
4729 typval_T *basetv)
4730{
4731 partial_T *pt = NULL;
4732 funcexe_T funcexe;
4733 typval_T functv;
4734 char_u *s;
4735 int ret;
4736
4737 // need to copy the funcref so that we can clear rettv
4738 if (evaluate)
4739 {
4740 functv = *rettv;
4741 rettv->v_type = VAR_UNKNOWN;
4742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004744 if (functv.v_type == VAR_PARTIAL)
4745 {
4746 pt = functv.vval.v_partial;
4747 s = partial_name(pt);
4748 }
4749 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004750 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004751 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004752 if (s == NULL || *s == NUL)
4753 {
4754 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004755 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004756 goto theend;
4757 }
4758 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004759 }
4760 else
4761 s = (char_u *)"";
4762
Bram Moolenaara80faa82020-04-12 19:37:17 +02004763 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004764 funcexe.fe_firstline = curwin->w_cursor.lnum;
4765 funcexe.fe_lastline = curwin->w_cursor.lnum;
4766 funcexe.fe_evaluate = evaluate;
4767 funcexe.fe_partial = pt;
4768 funcexe.fe_selfdict = selfdict;
4769 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004770 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004771
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004772theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // Clear the funcref afterwards, so that deleting it while
4774 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004775 if (evaluate)
4776 clear_tv(&functv);
4777
4778 return ret;
4779}
4780
4781/*
4782 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004783 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004784 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4785 */
4786 static int
4787eval_lambda(
4788 char_u **arg,
4789 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004790 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004792{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004793 int evaluate = evalarg != NULL
4794 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004795 typval_T base = *rettv;
4796 int ret;
4797
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004798 rettv->v_type = VAR_UNKNOWN;
4799
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004800 if (**arg == '{')
4801 {
4802 // ->{lambda}()
4803 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4804 }
4805 else
4806 {
4807 // ->(lambda)()
4808 ++*arg;
4809 ret = eval1(arg, rettv, evalarg);
4810 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004811 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004812 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004813 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004814 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004815 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004816 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4817 && rettv->v_type != VAR_PARTIAL)
4818 {
4819 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4820 return FAIL;
4821 }
4822 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004823 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004824 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004825 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004826
4827 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004828 {
4829 if (verbose)
4830 {
4831 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004832 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004833 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004834 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004835 }
4836 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004837 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004838 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004839 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004840 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004841
4842 // Clear the funcref afterwards, so that deleting it while
4843 // evaluating the arguments is possible (see test55).
4844 if (evaluate)
4845 clear_tv(&base);
4846
4847 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004848}
4849
4850/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004851 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004852 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004853 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4854 */
4855 static int
4856eval_method(
4857 char_u **arg,
4858 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004859 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004860 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004861{
4862 char_u *name;
4863 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004864 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004865 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004866 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004867 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004868 int evaluate = evalarg != NULL
4869 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004870
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004871 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004872
Bram Moolenaarac92e252019-08-03 21:58:38 +02004873 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004874 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004875 if (alias != NULL)
4876 name = alias;
4877
4878 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004879 {
4880 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004881 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004882 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004883 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004884 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004885 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004886 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004887
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004888 // If there is no "(" immediately following, but there is further on,
4889 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4890 // Does not handle anything where "(" is part of the expression.
4891 *arg = skipwhite(*arg);
4892
4893 if (**arg != '(' && alias == NULL
4894 && (paren = vim_strchr(*arg, '(')) != NULL)
4895 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004896 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004897
4898 // Truncate the name a the "(". Avoid trying to get another line
4899 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004900 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004901 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4902 if (evalarg != NULL)
4903 {
4904 getline = evalarg->eval_getline;
4905 evalarg->eval_getline = NULL;
4906 }
4907
4908 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004909 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004910 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004911 *arg = name + len;
4912 ret = FAIL;
4913 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004914 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004915 {
4916 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004917 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004918 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004919
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004920 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004921 if (getline != NULL)
4922 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004923 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004924
4925 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004926 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004927 *arg = skipwhite(*arg);
4928
4929 if (**arg != '(')
4930 {
4931 if (verbose)
4932 semsg(_(e_missing_parenthesis_str), name);
4933 ret = FAIL;
4934 }
4935 else if (VIM_ISWHITE((*arg)[-1]))
4936 {
4937 if (verbose)
4938 emsg(_(e_no_white_space_allowed_before_parenthesis));
4939 ret = FAIL;
4940 }
4941 else
4942 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004943 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004944 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004945 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004946
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004947 // Clear the funcref afterwards, so that deleting it while
4948 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004949 if (evaluate)
4950 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004951 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004952
Bram Moolenaarac92e252019-08-03 21:58:38 +02004953 return ret;
4954}
4955
4956/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004957 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4958 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4960 */
4961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004962eval_index(
4963 char_u **arg,
4964 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004965 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004968 int evaluate = evalarg != NULL
4969 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004974 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004975 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004977 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4978 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004979
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004980 init_tv(&var1);
4981 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 /*
4985 * dict.name
4986 */
4987 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004988 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004990 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004991 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004992 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 }
4994 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 /*
4997 * something[idx]
4998 *
4999 * Get the (first) variable from inside the [].
5000 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005001 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 if (**arg == ':')
5003 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005004 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005006 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005007 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005008 semsg(_(e_white_space_required_before_and_after_str_at_str),
5009 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005010 clear_tv(&var1);
5011 return FAIL;
5012 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005013 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005014 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005015 int error = FALSE;
5016
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005017 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00005018 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005019 && var1.v_type == VAR_FLOAT)
5020 {
5021 var1.vval.v_string = typval_tostring(&var1, TRUE);
5022 var1.v_type = VAR_STRING;
5023 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01005024
Bram Moolenaar4525a572022-02-13 11:57:33 +00005025 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005026 tv_get_number_chk(&var1, &error);
5027 else
5028 error = tv_get_string_chk(&var1) == NULL;
5029 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01005030 {
5031 // not a number or string
5032 clear_tv(&var1);
5033 return FAIL;
5034 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036
5037 /*
5038 * Get the second variable from inside the [:].
5039 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005040 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 if (**arg == ':')
5042 {
5043 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005044 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005045 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005046 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005047 semsg(_(e_white_space_required_before_and_after_str_at_str),
5048 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01005049 if (!empty1)
5050 clear_tv(&var1);
5051 return FAIL;
5052 }
5053 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 if (**arg == ']')
5055 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005056 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005058 if (!empty1)
5059 clear_tv(&var1);
5060 return FAIL;
5061 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005062 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005065 if (!empty1)
5066 clear_tv(&var1);
5067 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 return FAIL;
5069 }
5070 }
5071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005073 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 if (**arg != ']')
5075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00005077 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 clear_tv(&var1);
5079 if (range)
5080 clear_tv(&var2);
5081 return FAIL;
5082 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02005083 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 }
5085
5086 if (evaluate)
5087 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005088 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005089 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005090 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005091
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005092 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005095 clear_tv(&var2);
5096 return res;
5097 }
5098 return OK;
5099}
5100
5101/*
5102 * Check if "rettv" can have an [index] or [sli:ce]
5103 */
5104 int
5105check_can_index(typval_T *rettv, int evaluate, int verbose)
5106{
5107 switch (rettv->v_type)
5108 {
5109 case VAR_FUNC:
5110 case VAR_PARTIAL:
5111 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00005112 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005113 return FAIL;
5114 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005115 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005116 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005117 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005118 case VAR_BOOL:
5119 case VAR_SPECIAL:
5120 case VAR_JOB:
5121 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005122 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005123 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005124 if (verbose)
5125 emsg(_(e_cannot_index_special_variable));
5126 return FAIL;
Ernie Raele75fde62023-12-21 17:18:54 +01005127 case VAR_CLASS:
5128 case VAR_TYPEALIAS:
5129 if (verbose)
5130 check_typval_is_value(rettv);
5131 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005132 case VAR_UNKNOWN:
5133 case VAR_ANY:
5134 case VAR_VOID:
5135 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005137 emsg(_(e_cannot_index_special_variable));
5138 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005140 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005142 case VAR_STRING:
5143 case VAR_LIST:
5144 case VAR_DICT:
5145 case VAR_BLOB:
5146 break;
5147 case VAR_NUMBER:
5148 if (in_vim9script())
5149 emsg(_(e_cannot_index_number));
5150 break;
5151 }
5152 return OK;
5153}
5154
5155/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01005156 * slice() function
5157 */
5158 void
5159f_slice(typval_T *argvars, typval_T *rettv)
5160{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005161 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005162 && ((argvars[0].v_type != VAR_STRING
5163 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005164 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005165 && check_for_list_arg(argvars, 0) == FAIL)
5166 || check_for_number_arg(argvars, 1) == FAIL
5167 || check_for_opt_number_arg(argvars, 2) == FAIL))
5168 return;
5169
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005170 if (check_can_index(argvars, TRUE, FALSE) != OK)
5171 return;
5172
5173 copy_tv(argvars, rettv);
5174 eval_index_inner(rettv, TRUE, argvars + 1,
5175 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
5176 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005177}
5178
5179/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005180 * Apply index or range to "rettv".
5181 * "var1" is the first index, NULL for [:expr].
5182 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01005183 * "exclusive" is TRUE for slice(): second index is exclusive, use character
5184 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005185 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
5186 */
5187 int
5188eval_index_inner(
5189 typval_T *rettv,
5190 int is_range,
5191 typval_T *var1,
5192 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005193 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005194 char_u *key,
5195 int keylen,
5196 int verbose)
5197{
Bram Moolenaar6601b622021-01-13 21:47:15 +01005198 varnumber_T n1, n2 = 0;
5199 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005200
5201 n1 = 0;
5202 if (var1 != NULL && rettv->v_type != VAR_DICT)
5203 n1 = tv_get_number(var1);
5204
5205 if (is_range)
5206 {
5207 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005209 if (verbose)
5210 emsg(_(e_cannot_slice_dictionary));
5211 return FAIL;
5212 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01005213 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005214 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005215 else
5216 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005217 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005218
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005219 switch (rettv->v_type)
5220 {
5221 case VAR_UNKNOWN:
5222 case VAR_ANY:
5223 case VAR_VOID:
5224 case VAR_FUNC:
5225 case VAR_PARTIAL:
5226 case VAR_FLOAT:
5227 case VAR_BOOL:
5228 case VAR_SPECIAL:
5229 case VAR_JOB:
5230 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005231 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005232 case VAR_CLASS:
5233 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02005234 case VAR_TYPEALIAS:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005235 break; // not evaluating, skipping over subscript
5236
5237 case VAR_NUMBER:
5238 case VAR_STRING:
5239 {
5240 char_u *s = tv_get_string(rettv);
5241
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005243 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005244 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005245 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005246 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005247 else
5248 s = char_from_string(s, n1);
5249 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005250 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // The resulting variable is a substring. If the indexes
5253 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 if (n1 < 0)
5255 {
5256 n1 = len + n1;
5257 if (n1 < 0)
5258 n1 = 0;
5259 }
5260 if (n2 < 0)
5261 n2 = len + n2;
5262 else if (n2 >= len)
5263 n2 = len;
5264 if (n1 >= len || n2 < 0 || n1 > n2)
5265 s = NULL;
5266 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005267 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 }
5269 else
5270 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 // The resulting variable is a string of a single
5272 // character. If the index is too big or negative the
5273 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 if (n1 >= len || n1 < 0)
5275 s = NULL;
5276 else
5277 s = vim_strnsave(s + n1, 1);
5278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 clear_tv(rettv);
5280 rettv->v_type = VAR_STRING;
5281 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005282 }
5283 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005285 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005286 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5287 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005288 break;
5289
5290 case VAR_LIST:
5291 if (var1 == NULL)
5292 n1 = 0;
5293 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005294 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005295 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005296 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005297 return FAIL;
5298 break;
5299
5300 case VAR_DICT:
5301 {
5302 dictitem_T *item;
5303 typval_T tmp;
5304
5305 if (key == NULL)
5306 {
5307 key = tv_get_string_chk(var1);
5308 if (key == NULL)
5309 return FAIL;
5310 }
5311
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005312 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005313
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005314 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005315 {
5316 if (verbose)
5317 {
5318 if (keylen > 0)
5319 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005320 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005321 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005322 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005323 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005324
5325 copy_tv(&item->di_tv, &tmp);
5326 clear_tv(rettv);
5327 *rettv = tmp;
5328 }
5329 break;
5330 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 return OK;
5332}
5333
5334/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005335 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005336 */
5337 char_u *
5338partial_name(partial_T *pt)
5339{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005340 if (pt != NULL)
5341 {
5342 if (pt->pt_name != NULL)
5343 return pt->pt_name;
5344 if (pt->pt_func != NULL)
5345 return pt->pt_func->uf_name;
5346 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005347 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005348}
5349
Bram Moolenaarddecc252016-04-06 22:59:37 +02005350 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005351partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005352{
5353 int i;
5354
5355 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005357 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005358 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005359 if (pt->pt_name != NULL)
5360 {
5361 func_unref(pt->pt_name);
5362 vim_free(pt->pt_name);
5363 }
5364 else
5365 func_ptr_unref(pt->pt_func);
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005366 object_unref(pt->pt_obj);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005367
Bram Moolenaar54656012021-06-09 20:50:46 +02005368 // "out_up" is no longer used, decrement refcount on partial that owns it.
5369 partial_unref(pt->pt_outer.out_up_partial);
5370
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005371 // Using pt_outer from another partial.
5372 partial_unref(pt->pt_outer_partial);
5373
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005374 // Decrease the reference count for the context of a closure. If down
5375 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005376 if (pt->pt_funcstack != NULL)
5377 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005378 --pt->pt_funcstack->fs_refcount;
5379 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005380 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005381 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005382 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5383 if (pt->pt_loopvars[i] != NULL)
5384 {
5385 --pt->pt_loopvars[i]->lvs_refcount;
5386 loopvars_check_refcount(pt->pt_loopvars[i]);
5387 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005388
Bram Moolenaarddecc252016-04-06 22:59:37 +02005389 vim_free(pt);
5390}
5391
5392/*
5393 * Unreference a closure: decrement the reference count and free it when it
5394 * becomes zero.
5395 */
5396 void
5397partial_unref(partial_T *pt)
5398{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005399 if (pt == NULL)
5400 return;
5401
5402 int done = FALSE;
5403
5404 if (--pt->pt_refcount <= 0)
5405 partial_free(pt);
5406
5407 // If the reference count goes down to one, the funcstack may be the
5408 // only reference and can be freed if no other partials reference it.
5409 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005410 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005411 // careful: if the funcstack is freed it may contain this partial
5412 // and it gets freed as well
5413 if (pt->pt_funcstack != NULL)
5414 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005415
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005416 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005417 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005418 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005419
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005420 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5421 if (pt->pt_loopvars[depth] != NULL
5422 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005423 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005424 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005425 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005426}
5427
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005428/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005429 * Return the next (unique) copy ID.
5430 * Used for serializing nested structures.
5431 */
5432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005433get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005434{
5435 current_copyID += COPYID_INC;
5436 return current_copyID;
5437}
5438
5439/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005440 * Garbage collection for lists and dictionaries.
5441 *
5442 * We use reference counts to be able to free most items right away when they
5443 * are no longer used. But for composite items it's possible that it becomes
5444 * unused while the reference count is > 0: When there is a recursive
5445 * reference. Example:
5446 * :let l = [1, 2, 3]
5447 * :let d = {9: l}
5448 * :let l[1] = d
5449 *
5450 * Since this is quite unusual we handle this with garbage collection: every
5451 * once in a while find out which lists and dicts are not referenced from any
5452 * variable.
5453 *
5454 * Here is a good reference text about garbage collection (refers to Python
5455 * but it applies to all reference-counting mechanisms):
5456 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005457 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005458
5459/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005460 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005461 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005463 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005465garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005466{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005467 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005468 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005469 buf_T *buf;
5470 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005471 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005472 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005473
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005474 if (!testing)
5475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005477 want_garbage_collect = FALSE;
5478 may_garbage_collect = FALSE;
5479 garbage_collect_at_exit = FALSE;
5480 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005481
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005482 // The execution stack can grow big, limit the size.
5483 if (exestack.ga_maxlen - exestack.ga_len > 500)
5484 {
5485 size_t new_len;
5486 char_u *pp;
5487 int n;
5488
5489 // Keep 150% of the current size, with a minimum of the growth size.
5490 n = exestack.ga_len / 2;
5491 if (n < exestack.ga_growsize)
5492 n = exestack.ga_growsize;
5493
5494 // Don't make it bigger though.
5495 if (exestack.ga_len + n < exestack.ga_maxlen)
5496 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005497 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005498 pp = vim_realloc(exestack.ga_data, new_len);
5499 if (pp == NULL)
5500 return FAIL;
5501 exestack.ga_maxlen = exestack.ga_len + n;
5502 exestack.ga_data = pp;
5503 }
5504 }
5505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 // We advance by two because we add one for items referenced through
5507 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005508 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005509
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510 /*
5511 * 1. Go through all accessible variables and mark all lists and dicts
5512 * with copyID.
5513 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005515 // Don't free variables in the previous_funccal list unless they are only
5516 // referenced through previous_funccal. This must be first, because if
5517 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005521 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005523 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005524 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005525 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5526 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005528 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005529 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5531 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005532 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005533 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005534 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005535 abort = abort || set_ref_in_item(
5536 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005537#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005538 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005539 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5540 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005541 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005542 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005543 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5544 NULL, NULL);
5545#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005548 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005549 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5550 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005551 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005552 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005554 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005555 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005558 abort = abort || set_ref_in_functions(copyID);
5559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005561 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005562
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005563 // funcstacks keep variables for closures
5564 abort = abort || set_ref_in_funcstacks(copyID);
5565
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005566 // loopvars keep variables for loop blocks
5567 abort = abort || set_ref_in_loopvars(copyID);
5568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005570 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005571
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005572 // callbacks in buffers
5573 abort = abort || set_ref_in_buffers(copyID);
5574
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005575 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5576 abort = abort || set_ref_in_insexpand_funcs(copyID);
5577
5578 // 'operatorfunc' callback
5579 abort = abort || set_ref_in_opfunc(copyID);
5580
5581 // 'tagfunc' callback
5582 abort = abort || set_ref_in_tagfunc(copyID);
5583
5584 // 'imactivatefunc' and 'imstatusfunc' callbacks
5585 abort = abort || set_ref_in_im_funcs(copyID);
5586
Bram Moolenaar1dced572012-04-05 16:54:08 +02005587#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005588 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005589#endif
5590
Bram Moolenaardb913952012-06-29 12:54:53 +02005591#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005592 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005593#endif
5594
5595#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005596 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005597#endif
5598
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005599#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005600 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005601 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005602#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005603#ifdef FEAT_NETBEANS_INTG
5604 abort = abort || set_ref_in_nb_channel(copyID);
5605#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005606
Bram Moolenaare3188e22016-05-31 21:13:04 +02005607#ifdef FEAT_TIMERS
5608 abort = abort || set_ref_in_timer(copyID);
5609#endif
5610
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005611#ifdef FEAT_QUICKFIX
5612 abort = abort || set_ref_in_quickfix(copyID);
5613#endif
5614
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005615#ifdef FEAT_TERMINAL
5616 abort = abort || set_ref_in_term(copyID);
5617#endif
5618
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005619#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005620 abort = abort || set_ref_in_popups(copyID);
5621#endif
5622
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005623 abort = abort || set_ref_in_classes(copyID);
5624
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005625 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005626 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005627 /*
5628 * 2. Free lists and dictionaries that are not referenced.
5629 */
5630 did_free = free_unref_items(copyID);
5631
5632 /*
5633 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005635 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005636 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005637 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005638 else if (p_verbose > 0)
5639 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005640 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005641 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005642
5643 return did_free;
5644}
5645
5646/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005647 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005648 */
5649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005650free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005651{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005652 int did_free = FALSE;
5653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005654 // Let all "free" functions know that we are here. This means no
5655 // dictionaries, lists, channels or jobs are to be freed, because we will
5656 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005657 in_free_unref_items = TRUE;
5658
5659 /*
5660 * PASS 1: free the contents of the items. We don't free the items
5661 * themselves yet, so that it is possible to decrement refcount counters
5662 */
5663
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005664 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005665 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005666
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005667 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005668 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005669
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005670 // Go through the list of objects and free items without this copyID.
5671 did_free |= object_free_nonref(copyID);
5672
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005673 // Go through the list of classes and free items without this copyID.
5674 did_free |= class_free_nonref(copyID);
5675
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005676#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005677 // Go through the list of jobs and free items without the copyID. This
5678 // must happen before doing channels, because jobs refer to channels, but
5679 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005680 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005682 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005683 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5684#endif
5685
5686 /*
5687 * PASS 2: free the items themselves.
5688 */
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005689 object_free_items(copyID);
Bram Moolenaarcd524592016-07-17 14:57:05 +02005690 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005691 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005692
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005693#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005694 // Go through the list of jobs and free items without the copyID. This
5695 // must happen before doing channels, because jobs refer to channels, but
5696 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005697 free_unused_jobs(copyID, COPYID_MASK);
5698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005699 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005700 free_unused_channels(copyID, COPYID_MASK);
5701#endif
5702
5703 in_free_unref_items = FALSE;
5704
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005705 return did_free;
5706}
5707
5708/*
5709 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005710 * "list_stack" is used to add lists to be marked. Can be NULL.
5711 *
5712 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005713 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005716{
5717 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005718 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005719 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005720 hashtab_T *cur_ht;
5721 ht_stack_T *ht_stack = NULL;
5722 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005723
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005724 cur_ht = ht;
5725 for (;;)
5726 {
5727 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 // Mark each item in the hashtab. If the item contains a hashtab
5730 // it is added to ht_stack, if it contains a list it is added to
5731 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005732 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005733 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005734 if (!HASHITEM_EMPTY(hi))
5735 {
5736 --todo;
5737 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5738 &ht_stack, list_stack);
5739 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005740 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005741
5742 if (ht_stack == NULL)
5743 break;
5744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005745 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746 cur_ht = ht_stack->ht;
5747 tempitem = ht_stack;
5748 ht_stack = ht_stack->prev;
5749 free(tempitem);
5750 }
5751
5752 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005753}
5754
Dominique Pelle748b3082022-01-08 12:41:16 +00005755#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5756 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005757/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005758 * Mark a dict and its items with "copyID".
5759 * Returns TRUE if setting references failed somehow.
5760 */
5761 int
5762set_ref_in_dict(dict_T *d, int copyID)
5763{
5764 if (d != NULL && d->dv_copyID != copyID)
5765 {
5766 d->dv_copyID = copyID;
5767 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5768 }
5769 return FALSE;
5770}
Dominique Pelle748b3082022-01-08 12:41:16 +00005771#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005772
5773/*
5774 * Mark a list and its items with "copyID".
5775 * Returns TRUE if setting references failed somehow.
5776 */
5777 int
5778set_ref_in_list(list_T *ll, int copyID)
5779{
5780 if (ll != NULL && ll->lv_copyID != copyID)
5781 {
5782 ll->lv_copyID = copyID;
5783 return set_ref_in_list_items(ll, copyID, NULL);
5784 }
5785 return FALSE;
5786}
5787
5788/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005789 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005790 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5791 *
5792 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005793 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005794 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005795set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005796{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005797 listitem_T *li;
5798 int abort = FALSE;
5799 list_T *cur_l;
5800 list_stack_T *list_stack = NULL;
5801 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005802
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005803 cur_l = l;
5804 for (;;)
5805 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005806 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // Mark each item in the list. If the item contains a hashtab
5808 // it is added to ht_stack, if it contains a list it is added to
5809 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005810 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5811 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5812 ht_stack, &list_stack);
5813 if (list_stack == NULL)
5814 break;
5815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005816 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005817 cur_l = list_stack->list;
5818 tempitem = list_stack;
5819 list_stack = list_stack->prev;
5820 free(tempitem);
5821 }
5822
5823 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824}
5825
5826/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005827 * Mark the partial in callback 'cb' with "copyID".
5828 */
5829 int
5830set_ref_in_callback(callback_T *cb, int copyID)
5831{
5832 typval_T tv;
5833
5834 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5835 return FALSE;
5836
5837 tv.v_type = VAR_PARTIAL;
5838 tv.vval.v_partial = cb->cb_partial;
5839 return set_ref_in_item(&tv, copyID, NULL, NULL);
5840}
5841
5842/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005843 * Mark the dict "dd" with "copyID".
5844 * Also see set_ref_in_item().
5845 */
5846 static int
5847set_ref_in_item_dict(
5848 dict_T *dd,
5849 int copyID,
5850 ht_stack_T **ht_stack,
5851 list_stack_T **list_stack)
5852{
5853 if (dd == NULL || dd->dv_copyID == copyID)
5854 return FALSE;
5855
5856 // Didn't see this dict yet.
5857 dd->dv_copyID = copyID;
5858 if (ht_stack == NULL)
5859 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5860
5861 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5862 if (newitem == NULL)
5863 return TRUE;
5864
5865 newitem->ht = &dd->dv_hashtab;
5866 newitem->prev = *ht_stack;
5867 *ht_stack = newitem;
5868
5869 return FALSE;
5870}
5871
5872/*
5873 * Mark the list "ll" with "copyID".
5874 * Also see set_ref_in_item().
5875 */
5876 static int
5877set_ref_in_item_list(
5878 list_T *ll,
5879 int copyID,
5880 ht_stack_T **ht_stack,
5881 list_stack_T **list_stack)
5882{
5883 if (ll == NULL || ll->lv_copyID == copyID)
5884 return FALSE;
5885
5886 // Didn't see this list yet.
5887 ll->lv_copyID = copyID;
5888 if (list_stack == NULL)
5889 return set_ref_in_list_items(ll, copyID, ht_stack);
5890
5891 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5892 if (newitem == NULL)
5893 return TRUE;
5894
5895 newitem->list = ll;
5896 newitem->prev = *list_stack;
5897 *list_stack = newitem;
5898
5899 return FALSE;
5900}
5901
5902/*
5903 * Mark the partial "pt" with "copyID".
5904 * Also see set_ref_in_item().
5905 */
5906 static int
5907set_ref_in_item_partial(
5908 partial_T *pt,
5909 int copyID,
5910 ht_stack_T **ht_stack,
5911 list_stack_T **list_stack)
5912{
5913 if (pt == NULL || pt->pt_copyID == copyID)
5914 return FALSE;
5915
5916 // Didn't see this partial yet.
5917 pt->pt_copyID = copyID;
5918
5919 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5920
5921 if (pt->pt_dict != NULL)
5922 {
5923 typval_T dtv;
5924
5925 dtv.v_type = VAR_DICT;
5926 dtv.vval.v_dict = pt->pt_dict;
5927 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5928 }
5929
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02005930 if (pt->pt_obj != NULL)
5931 {
5932 typval_T objtv;
5933
5934 objtv.v_type = VAR_OBJECT;
5935 objtv.vval.v_object = pt->pt_obj;
5936 set_ref_in_item(&objtv, copyID, ht_stack, list_stack);
5937 }
5938
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005939 for (int i = 0; i < pt->pt_argc; ++i)
5940 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5941 ht_stack, list_stack);
5942 // pt_funcstack is handled in set_ref_in_funcstacks()
5943 // pt_loopvars is handled in set_ref_in_loopvars()
5944
5945 return abort;
5946}
5947
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005948#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005949/*
5950 * Mark the job "pt" with "copyID".
5951 * Also see set_ref_in_item().
5952 */
5953 static int
5954set_ref_in_item_job(
5955 job_T *job,
5956 int copyID,
5957 ht_stack_T **ht_stack,
5958 list_stack_T **list_stack)
5959{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005960 typval_T dtv;
5961
5962 if (job == NULL || job->jv_copyID == copyID)
5963 return FALSE;
5964
5965 job->jv_copyID = copyID;
5966 if (job->jv_channel != NULL)
5967 {
5968 dtv.v_type = VAR_CHANNEL;
5969 dtv.vval.v_channel = job->jv_channel;
5970 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5971 }
5972 if (job->jv_exit_cb.cb_partial != NULL)
5973 {
5974 dtv.v_type = VAR_PARTIAL;
5975 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5976 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5977 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005978
5979 return FALSE;
5980}
5981
5982/*
5983 * Mark the channel "ch" with "copyID".
5984 * Also see set_ref_in_item().
5985 */
5986 static int
5987set_ref_in_item_channel(
5988 channel_T *ch,
5989 int copyID,
5990 ht_stack_T **ht_stack,
5991 list_stack_T **list_stack)
5992{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005993 typval_T dtv;
5994
5995 if (ch == NULL || ch->ch_copyID == copyID)
5996 return FALSE;
5997
5998 ch->ch_copyID = copyID;
5999 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
6000 {
6001 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
6002 jq != NULL; jq = jq->jq_next)
6003 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6004 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6005 cq = cq->cq_next)
6006 if (cq->cq_callback.cb_partial != NULL)
6007 {
6008 dtv.v_type = VAR_PARTIAL;
6009 dtv.vval.v_partial = cq->cq_callback.cb_partial;
6010 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6011 }
6012 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
6013 {
6014 dtv.v_type = VAR_PARTIAL;
6015 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
6016 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6017 }
6018 }
6019 if (ch->ch_callback.cb_partial != NULL)
6020 {
6021 dtv.v_type = VAR_PARTIAL;
6022 dtv.vval.v_partial = ch->ch_callback.cb_partial;
6023 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6024 }
6025 if (ch->ch_close_cb.cb_partial != NULL)
6026 {
6027 dtv.v_type = VAR_PARTIAL;
6028 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
6029 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6030 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006031
6032 return FALSE;
6033}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006034#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006035
6036/*
6037 * Mark the class "cl" with "copyID".
6038 * Also see set_ref_in_item().
6039 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006040 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006041set_ref_in_item_class(
6042 class_T *cl,
6043 int copyID,
6044 ht_stack_T **ht_stack,
6045 list_stack_T **list_stack)
6046{
6047 int abort = FALSE;
6048
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02006049 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006050 return FALSE;
6051
6052 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02006053 if (cl->class_members_tv != NULL)
6054 {
6055 // The "class_members_tv" table is allocated only for regular classes
6056 // and not for interfaces.
6057 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
6058 abort = abort || set_ref_in_item(
6059 &cl->class_members_tv[i],
6060 copyID, ht_stack, list_stack);
6061 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006062
6063 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
6064 abort = abort || set_ref_in_func(NULL,
6065 cl->class_class_functions[i], copyID);
6066
6067 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
6068 abort = abort || set_ref_in_func(NULL,
6069 cl->class_obj_methods[i], copyID);
6070
6071 return abort;
6072}
6073
6074/*
6075 * Mark the object "cl" with "copyID".
6076 * Also see set_ref_in_item().
6077 */
6078 static int
6079set_ref_in_item_object(
6080 object_T *obj,
6081 int copyID,
6082 ht_stack_T **ht_stack,
6083 list_stack_T **list_stack)
6084{
6085 int abort = FALSE;
6086
6087 if (obj == NULL || obj->obj_copyID == copyID)
6088 return FALSE;
6089
6090 obj->obj_copyID = copyID;
6091
6092 // The typval_T array is right after the object_T.
6093 typval_T *mtv = (typval_T *)(obj + 1);
6094 for (int i = 0; !abort
6095 && i < obj->obj_class->class_obj_member_count; ++i)
6096 abort = abort || set_ref_in_item(mtv + i, copyID,
6097 ht_stack, list_stack);
6098
6099 return abort;
6100}
6101
6102/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006103 * Mark all lists, dicts and other container types referenced through typval
6104 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006105 * "list_stack" is used to add lists to be marked. Can be NULL.
6106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6107 *
6108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111set_ref_in_item(
6112 typval_T *tv,
6113 int copyID,
6114 ht_stack_T **ht_stack,
6115 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006117 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006118
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006119 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006120 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006121 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006122 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
6123 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006124
6125 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006126 return set_ref_in_item_list(tv->vval.v_list, copyID,
6127 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006128
6129 case VAR_FUNC:
6130 {
6131 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
6132 break;
6133 }
6134
6135 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006136 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
6137 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006138
6139 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006140#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006141 return set_ref_in_item_job(tv->vval.v_job, copyID,
6142 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006143#else
6144 break;
6145#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006146
6147 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006148#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006149 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006150 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00006151#else
6152 break;
6153#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006154
6155 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006156 return set_ref_in_item_class(tv->vval.v_class, copyID,
6157 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006158
6159 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00006160 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006161 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006162
6163 case VAR_UNKNOWN:
6164 case VAR_ANY:
6165 case VAR_VOID:
6166 case VAR_BOOL:
6167 case VAR_SPECIAL:
6168 case VAR_NUMBER:
6169 case VAR_FLOAT:
6170 case VAR_STRING:
6171 case VAR_BLOB:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006172 case VAR_TYPEALIAS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +00006173 case VAR_INSTR:
6174 // Types that do not contain any other item
6175 break;
6176 }
6177
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006178 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006179}
6180
Bram Moolenaar8c711452005-01-14 21:53:12 +00006181/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 * Return a string with the string representation of a variable.
6183 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006185 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006186 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006187 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02006188 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006189 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6190 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006191 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006193 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006194echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006195 typval_T *tv,
6196 char_u **tofree,
6197 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006198 int copyID,
6199 int echo_style,
6200 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006201 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006203 static int recurse = 0;
6204 char_u *r = NULL;
6205
Bram Moolenaar33570922005-01-25 22:26:29 +00006206 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006208 if (!did_echo_string_emsg)
6209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006210 // Only give this message once for a recursive call to avoid
6211 // flooding the user with errors. And stop iterating over lists
6212 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02006213 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00006214 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006215 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006216 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006217 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006218 }
6219 ++recurse;
6220
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 switch (tv->v_type)
6222 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006223 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006224 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006225 {
6226 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006227 r = tv->vval.v_string;
6228 if (r == NULL)
6229 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006230 }
6231 else
6232 {
6233 *tofree = string_quote(tv->vval.v_string, FALSE);
6234 r = *tofree;
6235 }
6236 break;
6237
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006238 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006239 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006240 char_u buf[MAX_FUNC_NAME_LEN];
6241
6242 if (echo_style)
6243 {
LemonBoya5d35902022-04-29 21:15:02 +01006244 r = tv->vval.v_string == NULL ? (char_u *)"function()"
6245 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01006246 buf, MAX_FUNC_NAME_LEN);
6247 if (r == buf)
6248 {
6249 r = vim_strsave(buf);
6250 *tofree = r;
6251 }
6252 else
6253 *tofree = NULL;
6254 }
6255 else
6256 {
6257 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6258 : make_ufunc_name_readable(
6259 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6260 TRUE);
6261 r = *tofree;
6262 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006264 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006265
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006266 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006267 {
6268 partial_T *pt = tv->vval.v_partial;
6269 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006270 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006271 garray_T ga;
6272 int i;
6273 char_u *tf;
6274
6275 ga_init2(&ga, 1, 100);
6276 ga_concat(&ga, (char_u *)"function(");
6277 if (fname != NULL)
6278 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006279 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006280 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006281 && vim_isupper(fname[1]))
6282 {
6283 ga_concat(&ga, (char_u *)"'g:");
6284 ga_concat(&ga, fname + 1);
6285 }
6286 else
6287 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006288 vim_free(fname);
6289 }
6290 if (pt != NULL && pt->pt_argc > 0)
6291 {
6292 ga_concat(&ga, (char_u *)", [");
6293 for (i = 0; i < pt->pt_argc; ++i)
6294 {
6295 if (i > 0)
6296 ga_concat(&ga, (char_u *)", ");
6297 ga_concat(&ga,
6298 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6299 vim_free(tf);
6300 }
6301 ga_concat(&ga, (char_u *)"]");
6302 }
6303 if (pt != NULL && pt->pt_dict != NULL)
6304 {
6305 typval_T dtv;
6306
6307 ga_concat(&ga, (char_u *)", ");
6308 dtv.v_type = VAR_DICT;
6309 dtv.vval.v_dict = pt->pt_dict;
6310 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6311 vim_free(tf);
6312 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006313 // terminate with ')' and a NUL
6314 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006315
6316 *tofree = ga.ga_data;
6317 r = *tofree;
6318 break;
6319 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006320
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006321 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006322 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006323 break;
6324
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006326 if (tv->vval.v_list == NULL)
6327 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006328 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006329 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006330 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006331 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006332 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6333 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006334 {
6335 *tofree = NULL;
6336 r = (char_u *)"[...]";
6337 }
6338 else
6339 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006340 int old_copyID = tv->vval.v_list->lv_copyID;
6341
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006342 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006343 *tofree = list2string(tv, copyID, restore_copyID);
6344 if (restore_copyID)
6345 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006346 r = *tofree;
6347 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006349
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006351 if (tv->vval.v_dict == NULL)
6352 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006353 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006354 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006355 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006356 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006357 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6358 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006359 {
6360 *tofree = NULL;
6361 r = (char_u *)"{...}";
6362 }
6363 else
6364 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006365 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006366
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006367 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006368 *tofree = dict2string(tv, copyID, restore_copyID);
6369 if (restore_copyID)
6370 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006371 r = *tofree;
6372 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006373 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006375 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006376 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006377 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006379 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006380 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006381 break;
6382
Bram Moolenaar835dc632016-02-07 14:27:38 +01006383 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006384 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006385#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006387 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6388 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006389 if (composite_val)
6390 {
6391 *tofree = string_quote(r, FALSE);
6392 r = *tofree;
6393 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006394#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006396
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006397 case VAR_INSTR:
6398 *tofree = NULL;
6399 r = (char_u *)"instructions";
6400 break;
6401
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006402 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006403 {
6404 class_T *cl = tv->vval.v_class;
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006405 char *s = "class";
Yegappan Lakshmananb2e42b92024-05-01 11:44:17 +02006406 if (cl && IS_INTERFACE(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006407 s = "interface";
Yegappan Lakshmananb2e42b92024-05-01 11:44:17 +02006408 else if (cl && IS_ENUM(cl))
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006409 s = "enum";
6410 size_t len = STRLEN(s) + 1 +
6411 (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006412 r = *tofree = alloc(len);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01006413 vim_snprintf((char *)r, len, "%s %s", s,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006414 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6415 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006416 break;
6417
6418 case VAR_OBJECT:
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01006419 *tofree = r = object_string(tv->vval.v_object, numbuf, copyID,
6420 echo_style, restore_copyID,
6421 composite_val);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006422 break;
6423
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006424 case VAR_FLOAT:
6425 *tofree = NULL;
6426 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6427 r = numbuf;
6428 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006429
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006430 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006431 case VAR_SPECIAL:
6432 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006433 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006434 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02006435
6436 case VAR_TYPEALIAS:
6437 *tofree = vim_strsave(tv->vval.v_typealias->ta_name);
6438 r = *tofree;
6439 if (r == NULL)
6440 r = (char_u *)"";
6441 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443
Bram Moolenaar8502c702014-06-17 12:51:16 +02006444 if (--recurse == 0)
6445 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006446 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006447}
6448
6449/*
6450 * Return a string with the string representation of a variable.
6451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6452 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006453 * Does not put quotes around strings, as ":echo" displays values.
6454 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6455 * May return NULL.
6456 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006457 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006458echo_string(
6459 typval_T *tv,
6460 char_u **tofree,
6461 char_u *numbuf,
6462 int copyID)
6463{
6464 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6465}
6466
6467/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006468 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6469 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006470 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006471 */
6472 int
6473buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6474{
6475 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006476 char_u *t;
6477 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006478
6479 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6480 return -1;
6481
6482 if (lnum > buf->b_ml.ml_line_count)
6483 lnum = buf->b_ml.ml_line_count;
6484
6485 str = ml_get_buf(buf, lnum, FALSE);
6486 if (str == NULL)
6487 return -1;
6488
6489 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006490 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006491
Bram Moolenaar91458462021-01-13 20:08:38 +01006492 // count the number of characters
6493 t = str;
6494 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6495 t += mb_ptr2len(t);
6496
6497 // In insert mode, when the cursor is at the end of a non-empty line,
6498 // byteidx points to the NUL character immediately past the end of the
6499 // string. In this case, add one to the character count.
6500 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6501 count++;
6502
6503 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006504}
6505
6506/*
6507 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006508 * byte index. Works only for loaded buffers. Returns -1 on failure.
6509 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006510 */
6511 int
6512buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6513{
6514 char_u *str;
6515 char_u *t;
6516
6517 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6518 return -1;
6519
6520 if (lnum > buf->b_ml.ml_line_count)
6521 lnum = buf->b_ml.ml_line_count;
6522
6523 str = ml_get_buf(buf, lnum, FALSE);
6524 if (str == NULL)
6525 return -1;
6526
6527 // Convert the character offset to a byte offset
6528 t = str;
6529 while (*t != NUL && --charidx > 0)
6530 t += mb_ptr2len(t);
6531
Bram Moolenaar91458462021-01-13 20:08:38 +01006532 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006533}
6534
6535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006537 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540var2fpos(
6541 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006542 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006543 int *fnum, // set to fnum for '0, 'A, etc.
6544 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006546 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006548 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006550 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006551 if (varp->v_type == VAR_LIST)
6552 {
6553 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006554 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006555 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006556 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006557
6558 l = varp->vval.v_list;
6559 if (l == NULL)
6560 return NULL;
6561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006562 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006563 pos.lnum = list_find_nr(l, 0L, &error);
6564 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006565 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006566 if (charcol)
6567 len = (long)mb_charlen(ml_get(pos.lnum));
6568 else
John Marriottbfcc8952024-03-11 22:04:45 +01006569 len = (long)ml_get_len(pos.lnum);
Bram Moolenaar477933c2007-07-17 14:32:23 +00006570
Bram Moolenaarec65d772020-08-20 22:29:12 +02006571 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006573 li = list_find(l, 1L);
6574 if (li != NULL && li->li_tv.v_type == VAR_STRING
6575 && li->li_tv.vval.v_string != NULL
6576 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006577 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006578 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006579 }
6580 else
6581 {
6582 pos.col = list_find_nr(l, 1L, &error);
6583 if (error)
6584 return NULL;
6585 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006587 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006588 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006589 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006590 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006592 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006593 pos.coladd = list_find_nr(l, 2L, &error);
6594 if (error)
6595 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006596
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006597 return &pos;
6598 }
6599
Bram Moolenaarc5809432021-03-27 21:23:30 +01006600 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6601 return NULL;
6602
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006603 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006604 if (name == NULL)
6605 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006606
6607 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006608 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006609 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006610 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006611 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006612 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006613 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006614 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006615 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006616 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006617 pos = VIsual;
6618 else
6619 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006620 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006621 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006622 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006624 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006625 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6627 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006628 pos = *pp;
6629 }
6630 if (pos.lnum != 0)
6631 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006632 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006633 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6634 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006636
Bram Moolenaara5525202006-03-02 22:52:09 +00006637 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006638
Bram Moolenaar477933c2007-07-17 14:32:23 +00006639 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006640 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006641 // the "w_valid" flags are not reset when moving the cursor, but they
6642 // do matter for update_topline() and validate_botline().
6643 check_cursor_moved(curwin);
6644
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006645 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006647 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006648 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 // In silent Ex mode topline is zero, but that's not a valid line
6650 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006651 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006652 return &pos;
6653 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006655 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006656 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006657 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006658 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006659 return &pos;
6660 }
6661 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006662 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006664 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 pos.lnum = curbuf->b_ml.ml_line_count;
6667 pos.col = 0;
6668 }
6669 else
6670 {
6671 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006672 if (charcol)
6673 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6674 else
John Marriottbfcc8952024-03-11 22:04:45 +01006675 pos.col = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
6677 return &pos;
6678 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006679 if (in_vim9script())
6680 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 return NULL;
6682}
6683
6684/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006685 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006686 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006687 * Note that the column is passed on as-is, the caller may want to decrement
6688 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006689 * If "charcol" is TRUE use the column as the character index instead of the
6690 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006691 * Return FAIL when conversion is not possible, doesn't check the position for
6692 * validity.
6693 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006695list2fpos(
6696 typval_T *arg,
6697 pos_T *posp,
6698 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006699 colnr_T *curswantp,
6700 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006701{
6702 list_T *l = arg->vval.v_list;
6703 long i = 0;
6704 long n;
6705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006706 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6707 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006708 if (arg->v_type != VAR_LIST
6709 || l == NULL
6710 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006711 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006712 return FAIL;
6713
6714 if (fnump != NULL)
6715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006716 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006717 if (n < 0)
6718 return FAIL;
6719 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006720 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006721 *fnump = n;
6722 }
6723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006724 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006725 if (n < 0)
6726 return FAIL;
6727 posp->lnum = n;
6728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006729 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006730 if (n < 0)
6731 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006732 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006733 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006734 if (charcol)
6735 {
6736 buf_T *buf;
6737
6738 // Get the text for the specified line in a loaded buffer
6739 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6740 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6741 return FAIL;
6742
Bram Moolenaar79f23442022-10-10 12:42:57 +01006743 n = buf_charidx_to_byteidx(buf,
6744 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006745 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006746 posp->col = n;
6747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006748 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006749 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006750 posp->coladd = 0;
6751 else
6752 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006753
Bram Moolenaar493c1782014-05-28 14:34:46 +02006754 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006755 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006756
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006757 return OK;
6758}
6759
6760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 * Get the length of an environment variable name.
6762 * Advance "arg" to the first character after the name.
6763 * Return 0 for error.
6764 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006766get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 char_u *p;
6769 int len;
6770
6771 for (p = *arg; vim_isIDc(*p); ++p)
6772 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006773 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 return 0;
6775
6776 len = (int)(p - *arg);
6777 *arg = p;
6778 return len;
6779}
6780
6781/*
6782 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006783 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 * Return 0 if something is wrong.
6785 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006786 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006787get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 char_u *p;
6790 int len;
6791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006792 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006794 {
6795 if (*p == ':')
6796 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006797 // "s:" is start of "s:var", but "n:" is not and can be used in
6798 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006799 len = (int)(p - *arg);
6800 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6801 || len > 1)
6802 break;
6803 }
6804 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006805 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 return 0;
6807
6808 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006809 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
6811 return len;
6812}
6813
6814/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006815 * Get the length of the name of a variable or function.
6816 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006818 * Return -1 if curly braces expansion failed.
6819 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 * If the name contains 'magic' {}'s, expand them and return the
6821 * expanded name in an allocated string via 'alias' - caller must free.
6822 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824get_name_len(
6825 char_u **arg,
6826 char_u **alias,
6827 int evaluate,
6828 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 char_u *p;
6832 char_u *expr_start;
6833 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006835 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
6837 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6838 && (*arg)[2] == (int)KE_SNR)
6839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 *arg += 3;
6842 return get_id_len(arg) + 3;
6843 }
6844 len = eval_fname_script(*arg);
6845 if (len > 0)
6846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006847 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 *arg += len;
6849 }
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006854 p = find_name_end(*arg, &expr_start, &expr_end,
6855 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (expr_start != NULL)
6857 {
6858 char_u *temp_string;
6859
6860 if (!evaluate)
6861 {
6862 len += (int)(p - *arg);
6863 *arg = skipwhite(p);
6864 return len;
6865 }
6866
6867 /*
6868 * Include any <SID> etc in the expanded string:
6869 * Thus the -len here.
6870 */
6871 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6872 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006873 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 *alias = temp_string;
6875 *arg = skipwhite(p);
6876 return (int)STRLEN(temp_string);
6877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
6879 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006880 // Only give an error when there is something, otherwise it will be
6881 // reported at a higher level.
6882 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006883 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
6885 return len;
6886}
6887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006888/*
6889 * Find the end of a variable or function name, taking care of magic braces.
6890 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6891 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006892 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 * Return a pointer to just after the name. Equal to "arg" if there is no
6894 * valid name.
6895 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006896 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006897find_name_end(
6898 char_u *arg,
6899 char_u **expr_start,
6900 char_u **expr_end,
6901 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006903 int mb_nest = 0;
6904 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006906 int len;
zeertzjq9a91d2b2024-04-09 21:47:10 +02006907 int allow_curly = !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006911 *expr_start = NULL;
6912 *expr_end = NULL;
6913 }
6914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006916 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006917 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006918 return arg;
6919
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920 for (p = arg; *p != NUL
6921 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006922 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006923 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006924 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006926 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006927 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006928 if (*p == '\'')
6929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006930 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006931 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006932 ;
6933 if (*p == NUL)
6934 break;
6935 }
6936 else if (*p == '"')
6937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006938 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006939 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006940 if (*p == '\\' && p[1] != NUL)
6941 ++p;
6942 if (*p == NUL)
6943 break;
6944 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006945 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006947 // "s:" is start of "s:var", but "n:" is not and can be used in
6948 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006949 len = (int)(p - arg);
6950 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006951 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006952 break;
6953 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006955 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006957 if (*p == '[')
6958 ++br_nest;
6959 else if (*p == ']')
6960 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006962
zeertzjqa93d9cd2023-05-02 16:25:47 +01006963 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006965 if (*p == '{')
6966 {
6967 mb_nest++;
6968 if (expr_start != NULL && *expr_start == NULL)
6969 *expr_start = p;
6970 }
6971 else if (*p == '}')
6972 {
6973 mb_nest--;
6974 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6975 *expr_end = p;
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979
6980 return p;
6981}
6982
6983/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006984 * Expands out the 'magic' {}'s in a variable/function name.
6985 * Note that this can call itself recursively, to deal with
6986 * constructs like foo{bar}{baz}{bam}
6987 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6988 * "in_start" ^
6989 * "expr_start" ^
6990 * "expr_end" ^
6991 * "in_end" ^
6992 *
6993 * Returns a new allocated string, which the caller must free.
6994 * Returns NULL for failure.
6995 */
6996 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006997make_expanded_name(
6998 char_u *in_start,
6999 char_u *expr_start,
7000 char_u *expr_end,
7001 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007002{
7003 char_u c1;
7004 char_u *retval = NULL;
7005 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007006
7007 if (expr_end == NULL || in_end == NULL)
7008 return NULL;
7009 *expr_start = NUL;
7010 *expr_end = NUL;
7011 c1 = *in_end;
7012 *in_end = NUL;
7013
Bram Moolenaara4e0b972022-10-01 19:43:52 +01007014 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007015 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007016 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007017 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7018 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007019 if (retval != NULL)
7020 {
7021 STRCPY(retval, in_start);
7022 STRCAT(retval, temp_result);
7023 STRCAT(retval, expr_end + 1);
7024 }
7025 }
7026 vim_free(temp_result);
7027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007028 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007029 *expr_start = '{';
7030 *expr_end = '}';
7031
7032 if (retval != NULL)
7033 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007034 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007035 if (expr_start != NULL)
7036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007037 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007038 temp_result = make_expanded_name(retval, expr_start,
7039 expr_end, temp_result);
7040 vim_free(retval);
7041 retval = temp_result;
7042 }
7043 }
7044
7045 return retval;
7046}
7047
7048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007050 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007053eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007055 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007056}
7057
7058/*
7059 * Return TRUE if character "c" can be used as the first character in a
7060 * variable or function name (excluding '{' and '}').
7061 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007063eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007064{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02007065 return ASCII_ISALPHA(c) || c == '_';
7066}
7067
7068/*
7069 * Return TRUE if character "c" can be used as the first character of a
7070 * dictionary key.
7071 */
7072 int
7073eval_isdictc(int c)
7074{
7075 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077
7078/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007079 * Handle:
7080 * - expr[expr], expr[expr:expr] subscript
7081 * - ".name" lookup
7082 * - function call with Funcref variable: func(expr)
7083 * - method call: var->method()
7084 *
7085 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007086 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007087 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089handle_subscript(
7090 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007091 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007092 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007093 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02007094 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007095{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007096 int evaluate = evalarg != NULL
7097 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007098 int ret = OK;
7099 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007100 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007101 int getnext;
7102 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007103
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007104 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007105 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007106 // When at the end of the line and ".name" or "->{" or "->X" follows in
7107 // the next line then consume the line break.
7108 p = eval_next_non_blank(*arg, evalarg, &getnext);
7109 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00007110 && ((*p == '.'
7111 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
7112 || rettv->v_type == VAR_CLASS
7113 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02007114 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
7115 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
7116 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007117 {
Bram Moolenaare442d592022-05-05 12:20:28 +01007118 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02007119 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02007120 check_white = FALSE;
7121 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007122
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007123 if (rettv->v_type == VAR_ANY)
7124 {
7125 char_u *exp_name;
7126 int cc;
7127 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01007128 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007129 type_T *type;
7130
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007131 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00007132 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007133 if (**arg != '.')
7134 {
7135 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00007136 semsg(_(e_expected_dot_after_name_str),
7137 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007138 ret = FAIL;
7139 break;
7140 }
7141 ++*arg;
7142 if (IS_WHITE_OR_NUL(**arg))
7143 {
7144 if (verbose)
7145 emsg(_(e_no_white_space_allowed_after_dot));
7146 ret = FAIL;
7147 break;
7148 }
7149
7150 // isolate the name
7151 exp_name = *arg;
7152 while (eval_isnamec(**arg))
7153 ++*arg;
7154 cc = **arg;
7155 **arg = NUL;
7156
7157 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01007158 evalarg == NULL ? NULL : evalarg->eval_cctx,
7159 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007160 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007161
7162 if (idx < 0 && ufunc == NULL)
7163 {
7164 ret = FAIL;
7165 break;
7166 }
7167 if (idx >= 0)
7168 {
7169 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
7170 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
7171
7172 copy_tv(sv->sv_tv, rettv);
7173 }
7174 else
7175 {
7176 rettv->v_type = VAR_FUNC;
7177 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
7178 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00007179 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01007180 }
7181
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007182 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7183 || rettv->v_type == VAR_PARTIAL))
7184 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007185 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02007186 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
7187 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007188
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007189 // Stop the expression evaluation when immediately aborting on
7190 // error, or when an interrupt occurred or an exception was thrown
7191 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007192 if (aborting())
7193 {
7194 if (ret == OK)
7195 clear_tv(rettv);
7196 ret = FAIL;
7197 }
7198 dict_unref(selfdict);
7199 selfdict = NULL;
7200 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02007201 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02007202 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02007203 if (in_vim9script())
7204 *arg = skipwhite(p + 2);
7205 else
7206 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00007207 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007208 {
zeertzjqc481ad32023-03-11 16:18:51 +00007209 emsg(_(e_no_white_space_allowed_before_parenthesis));
7210 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007211 }
zeertzjqc481ad32023-03-11 16:18:51 +00007212 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
7213 // expr->{lambda}() or expr->(lambda)()
7214 ret = eval_lambda(arg, rettv, evalarg, verbose);
7215 else
7216 // expr->name()
7217 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007218 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007219 // "." is ".name" lookup when we found a dict or when evaluating and
7220 // scriptversion is at least 2, where string concatenation is "..".
7221 else if (**arg == '['
7222 || (**arg == '.' && (rettv->v_type == VAR_DICT
7223 || (!evaluate
7224 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02007225 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007226 {
7227 dict_unref(selfdict);
7228 if (rettv->v_type == VAR_DICT)
7229 {
7230 selfdict = rettv->vval.v_dict;
7231 if (selfdict != NULL)
7232 ++selfdict->dv_refcount;
7233 }
7234 else
7235 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02007236 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007237 {
7238 clear_tv(rettv);
7239 ret = FAIL;
7240 }
7241 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007242 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7243 || rettv->v_type == VAR_OBJECT))
7244 {
7245 // class member: SomeClass.varname
7246 // class method: SomeClass.SomeMethod()
7247 // class constructor: SomeClass.new()
7248 // object member: someObject.varname
7249 // object method: someObject.SomeMethod()
7250 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7251 {
7252 clear_tv(rettv);
7253 ret = FAIL;
7254 }
7255 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007256 else
7257 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007258 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007260 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7261 // Don't do this when "Func" is already a partial that was bound
7262 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007263 if (selfdict != NULL
7264 && (rettv->v_type == VAR_FUNC
7265 || (rettv->v_type == VAR_PARTIAL
7266 && (rettv->vval.v_partial->pt_auto
7267 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007268 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007269
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007270 dict_unref(selfdict);
7271 return ret;
7272}
7273
7274/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 * Make a copy of an item.
7276 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007277 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007278 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7279 * reference to an already copied list/dict can be used.
7280 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283item_copy(
7284 typval_T *from,
7285 typval_T *to,
7286 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007287 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007288 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289{
7290 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007291 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007295 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007296 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 }
7298 ++recurse;
7299
7300 switch (from->v_type)
7301 {
7302 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007303 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 case VAR_STRING:
7305 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007306 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007307 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007308 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007309 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007310 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007311 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007312 case VAR_CLASS:
7313 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007314 case VAR_TYPEALIAS:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315 copy_tv(from, to);
7316 break;
7317 case VAR_LIST:
7318 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007319 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007320 if (from->vval.v_list == NULL)
7321 to->vval.v_list = NULL;
7322 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007324 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007325 to->vval.v_list = from->vval.v_list->lv_copylist;
7326 ++to->vval.v_list->lv_refcount;
7327 }
7328 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007329 to->vval.v_list = list_copy(from->vval.v_list,
7330 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007331 if (to->vval.v_list == NULL)
7332 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007334 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007336 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 case VAR_DICT:
7338 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007339 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007340 if (from->vval.v_dict == NULL)
7341 to->vval.v_dict = NULL;
7342 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007344 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7346 ++to->vval.v_dict->dv_refcount;
7347 }
7348 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007349 to->vval.v_dict = dict_copy(from->vval.v_dict,
7350 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007351 if (to->vval.v_dict == NULL)
7352 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007354 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007355 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007357 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007359 }
7360 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362}
7363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 void
7365echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7366{
7367 char_u *tofree;
7368 char_u numbuf[NUMBUFLEN];
7369 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7370
7371 if (*atstart)
7372 {
7373 *atstart = FALSE;
7374 // Call msg_start() after eval1(), evaluating the expression
7375 // may cause a message to appear.
7376 if (with_space)
7377 {
7378 // Mark the saved text as finishing the line, so that what
7379 // follows is displayed on a new line when scrolling back
7380 // at the more prompt.
7381 msg_sb_eol();
7382 msg_start();
7383 }
7384 }
7385 else if (with_space)
7386 msg_puts_attr(" ", echo_attr);
7387
7388 if (p != NULL)
7389 for ( ; *p != NUL && !got_int; ++p)
7390 {
7391 if (*p == '\n' || *p == '\r' || *p == TAB)
7392 {
7393 if (*p != TAB && *needclr)
7394 {
7395 // remove any text still there from the command
7396 msg_clr_eos();
7397 *needclr = FALSE;
7398 }
7399 msg_putchar_attr(*p, echo_attr);
7400 }
7401 else
7402 {
7403 if (has_mbyte)
7404 {
7405 int i = (*mb_ptr2len)(p);
7406
7407 (void)msg_outtrans_len_attr(p, i, echo_attr);
7408 p += i - 1;
7409 }
7410 else
7411 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7412 }
7413 }
7414 vim_free(tofree);
7415}
7416
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 * ":echo expr1 ..." print each argument separated with a space, add a
7419 * newline at the end.
7420 * ":echon expr1 ..." print each argument plain.
7421 */
7422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007427 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 int needclr = TRUE;
7429 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007430 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007431 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007432 evalarg_T evalarg;
7433
Bram Moolenaare707c882020-07-01 13:07:37 +02007434 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436 if (eap->skip)
7437 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007438 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007440 // If eval1() causes an error message the text from the command may
7441 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007442 need_clr_eos = needclr;
7443
Bram Moolenaar68db9962021-05-09 23:19:22 +02007444 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007445 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 /*
7448 * Report the invalid expression unless the expression evaluation
7449 * has been cancelled due to an aborting error, an interrupt, or an
7450 * exception.
7451 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007452 if (!aborting() && did_emsg == did_emsg_before
7453 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007454 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007455 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 break;
7457 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007458 need_clr_eos = FALSE;
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007461 {
7462 if (rettv.v_type == VAR_VOID)
7463 {
7464 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7465 break;
7466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007468 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 arg = skipwhite(arg);
7472 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007473 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007474 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 if (eap->skip)
7477 --emsg_skip;
7478 else
7479 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007480 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 if (needclr)
7482 msg_clr_eos();
7483 if (eap->cmdidx == CMD_echo)
7484 msg_end();
7485 }
7486}
7487
7488/*
7489 * ":echohl {name}".
7490 */
7491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007492ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007494 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495}
7496
7497/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007498 * Returns the :echo attribute
7499 */
7500 int
7501get_echo_attr(void)
7502{
7503 return echo_attr;
7504}
7505
7506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 * ":execute expr1 ..." execute the result of an expression.
7508 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007509 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007511 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 * Each gets spaces around each argument and a newline at the end for
7513 * echo commands
7514 */
7515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007516ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
7518 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 int ret = OK;
7521 char_u *p;
7522 garray_T ga;
7523 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007524 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 ga_init2(&ga, 1, 80);
7527
7528 if (eap->skip)
7529 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007530 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007532 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007533 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
7536 if (!eap->skip)
7537 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007538 char_u buf[NUMBUFLEN];
7539
7540 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007541 {
7542 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7543 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007544 semsg(_(e_using_invalid_value_as_string_str),
7545 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007546 p = NULL;
7547 }
7548 else
7549 p = tv_get_string_buf(&rettv, buf);
7550 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007551 else
7552 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007553 if (p == NULL)
7554 {
7555 clear_tv(&rettv);
7556 ret = FAIL;
7557 break;
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 len = (int)STRLEN(p);
7560 if (ga_grow(&ga, len + 2) == FAIL)
7561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 ret = FAIL;
7564 break;
7565 }
7566 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ga.ga_len += len;
7570 }
7571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 arg = skipwhite(arg);
7574 }
7575
7576 if (ret != FAIL && ga.ga_data != NULL)
7577 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007578 // use the first line of continuation lines for messages
7579 SOURCING_LNUM = start_lnum;
7580
Bram Moolenaar37fef162022-08-29 18:16:32 +01007581 if (eap->cmdidx == CMD_echomsg
7582 || eap->cmdidx == CMD_echowindow
7583 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007585 // Mark the already saved text as finishing the line, so that what
7586 // follows is displayed on a new line when scrolling back at the
7587 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007588 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007589 }
7590
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007591 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007592 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007593 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007594 out_flush();
7595 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007596 else if (eap->cmdidx == CMD_echowindow)
7597 {
7598#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007599 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007600#endif
7601 msg_attr(ga.ga_data, echo_attr);
7602#ifdef HAS_MESSAGE_WINDOW
7603 end_echowindow();
7604#endif
7605 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007606 else if (eap->cmdidx == CMD_echoconsole)
7607 {
7608 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7609 ui_write((char_u *)"\r\n", 2, TRUE);
7610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 else if (eap->cmdidx == CMD_echoerr)
7612 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007613 int save_did_emsg = did_emsg;
7614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007615 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (!force_abort)
7618 did_emsg = save_did_emsg;
7619 }
7620 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007621 {
7622 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7623
7624 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7625 sticky_cmdmod_flags = cmdmod.cmod_flags
7626 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 do_cmdline((char_u *)ga.ga_data,
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01007628 eap->ea_getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007629 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632
7633 ga_clear(&ga);
7634
7635 if (eap->skip)
7636 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007637 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638}
7639
7640/*
7641 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7642 * "arg" points to the "&" or '+' when called, to "option" when returning.
7643 * Returns NULL when no option name found. Otherwise pointer to the char
7644 * after the option name.
7645 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007646 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007647find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
7649 char_u *p = *arg;
7650
7651 ++p;
7652 if (*p == 'g' && p[1] == ':')
7653 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007654 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 p += 2;
7656 }
7657 else if (*p == 'l' && p[1] == ':')
7658 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007659 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 p += 2;
7661 }
7662 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007663 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
7665 if (!ASCII_ISALPHA(*p))
7666 return NULL;
7667 *arg = p;
7668
7669 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 else
7672 while (ASCII_ISALPHA(*p))
7673 ++p;
7674 return p;
7675}
7676
7677/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007678 * Display script name where an item was last set.
7679 * Should only be invoked when 'verbose' is non-zero.
7680 */
7681 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007682last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007683{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007684 char_u *p;
7685
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007686 if (script_ctx.sc_sid == 0)
7687 return;
7688
7689 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7690 if (p == NULL)
7691 return;
7692
7693 verbose_enter();
7694 msg_puts(_("\n\tLast set from "));
7695 msg_puts((char *)p);
7696 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007697 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007698 msg_puts(_(line_msg));
7699 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007700 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007701 verbose_leave();
7702 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007703}
7704
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007705#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706
7707/*
7708 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007709 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 * "flags" can be "g" to do a global substitute.
7711 * Returns an allocated string, NULL for error.
7712 */
7713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714do_string_sub(
7715 char_u *str,
7716 char_u *pat,
7717 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007718 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007719 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 int sublen;
7722 regmatch_T regmatch;
7723 int i;
7724 int do_all;
7725 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007726 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 garray_T ga;
7728 char_u *ret;
7729 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007730 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007732 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007734 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736 ga_init2(&ga, 1, 200);
7737
7738 do_all = (flags[0] == 'g');
7739
7740 regmatch.rm_ic = p_ic;
7741 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7742 if (regmatch.regprog != NULL)
7743 {
7744 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007745 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007748 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007749 if (regmatch.startp[0] == regmatch.endp[0])
7750 {
7751 if (zero_width == regmatch.startp[0])
7752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007753 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007754 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007755 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7756 (size_t)i);
7757 ga.ga_len += i;
7758 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007759 continue;
7760 }
7761 zero_width = regmatch.startp[0];
7762 }
7763
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 /*
7765 * Get some space for a temporary buffer to do the substitution
7766 * into. It will contain:
7767 * - The text up to where the match is.
7768 * - The substituted text.
7769 * - The text after the match.
7770 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007771 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007772 if (sublen <= 0)
7773 {
7774 ga_clear(&ga);
7775 break;
7776 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007777 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7779 {
7780 ga_clear(&ga);
7781 break;
7782 }
7783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007784 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 i = (int)(regmatch.startp[0] - tail);
7786 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007787 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007788 (void)vim_regsub(&regmatch, sub, expr,
7789 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7790 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007792 tail = regmatch.endp[0];
7793 if (*tail == NUL)
7794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (!do_all)
7796 break;
7797 }
7798
7799 if (ga.ga_data != NULL)
7800 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7801
Bram Moolenaar473de612013-06-08 18:19:48 +02007802 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804
7805 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7806 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007807 if (p_cpo == empty_option)
7808 p_cpo = save_cpo;
7809 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007811 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007812 // If it's still empty it was changed and restored, need to restore in
7813 // the complicated way.
7814 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007815 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007816 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818
7819 return ret;
7820}