blob: 9f8442277faf65aefde030fa26b67a6a1d860ff0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
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/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020036
37 void
38func_init()
39{
40 hash_init(&func_hashtab);
41}
42
Dominique Pelle748b3082022-01-08 12:41:16 +000043#if defined(FEAT_PROFILE) || defined(PROTO)
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
Dominique Pelle748b3082022-01-08 12:41:16 +000052#endif
Bram Moolenaar660a10a2019-07-14 15:48:38 +020053
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000059 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 * Return a pointer to after the type.
61 * When something is wrong return "arg".
62 */
63 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010064one_function_arg(
65 char_u *arg,
66 garray_T *newargs,
67 garray_T *argtypes,
68 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
80 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaar057e84a2021-02-28 16:55:11 +010090 // Vim9 script: cannot use script var name for argument. In function: also
91 // check local vars and arguments.
92 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
Bram Moolenaardce24412022-02-08 20:35:30 +000093 evalarg == NULL ? NULL : evalarg->eval_cctx,
94 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010095 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
98 return arg;
99 if (newargs != NULL)
100 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100101 int c;
102 int i;
103
104 c = *p;
105 *p = NUL;
106 arg_copy = vim_strsave(arg);
107 if (arg_copy == NULL)
108 {
109 *p = c;
110 return arg;
111 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200112 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200113 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200114 // Check for duplicate argument name.
115 for (i = 0; i < newargs->ga_len; ++i)
116 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
117 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000118 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200119 vim_free(arg_copy);
120 return arg;
121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
123 newargs->ga_len++;
124
125 *p = c;
126 }
127
128 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100129 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100130 {
131 char_u *type = NULL;
132
Bram Moolenaar6e949782020-04-13 17:21:00 +0200133 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
134 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200135 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200136 arg_copy == NULL ? arg : arg_copy);
137 p = skipwhite(p);
138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 if (*p == ':')
140 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200141 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100142 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200143 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100144 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200145 return arg;
146 }
147 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200148 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100149 if (!skip)
150 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200152 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200154 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200155 arg_copy == NULL ? arg : arg_copy);
156 return arg;
157 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100158 if (!skip)
159 {
160 if (type == NULL && types_optional)
161 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200162 type = vim_strsave((char_u *)
163 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100164 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 }
167
168 return p;
169}
170
171/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000172 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000173 * Get a next line, store it in "eap" if appropriate and put the line in
174 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000175 */
176 static char_u *
177get_function_line(
178 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000179 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000180 int indent,
181 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000182{
183 char_u *theline;
184
185 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000186 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000187 else
188 theline = eap->getline(':', eap->cookie, indent, getline_options);
189 if (theline != NULL)
190 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000191 if (lines_to_free->ga_len > 0
192 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
193 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000194 *eap->cmdlinep = theline;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000195 ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000196 }
197
198 return theline;
199}
200
201/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200202 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100203 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100204 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200205 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100206 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200207get_function_args(
208 char_u **argp,
209 char_u endchar,
210 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100212 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100213 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200214 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200215 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200216 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000217 exarg_T *eap, // can be NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000218 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219{
220 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100221 char_u *arg;
222 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200223 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200224 int any_default = FALSE;
225 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100226 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200227
228 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000229 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000231 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200232 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000233 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200234
235 if (varargs != NULL)
236 *varargs = FALSE;
237
238 /*
239 * Isolate the arguments: "arg1, arg2, ...)"
240 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100241 arg = skipwhite(*argp);
242 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243 while (*p != endchar)
244 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200245 while (eap != NULL && eap->getline != NULL
246 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200247 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200248 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000249 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000250 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000251
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200252 if (theline == NULL)
253 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200254 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200255 p = skipwhite(theline);
256 }
257
258 if (mustend && *p != endchar)
259 {
260 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000261 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100262 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200263 }
264 if (*p == endchar)
265 break;
266
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
268 {
269 if (varargs != NULL)
270 *varargs = TRUE;
271 p += 3;
272 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273
274 if (argtypes != NULL)
275 {
276 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200277 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100279 if (!skip)
280 emsg(_(e_missing_name_after_dots));
281 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 }
283
284 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100285 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000286 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 if (p == arg)
288 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100289 if (*skipwhite(p) == '=')
290 {
291 emsg(_(e_cannot_use_default_for_variable_arguments));
292 break;
293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200295 }
296 else
297 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200298 char_u *np;
299
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200300 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100301 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000302 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200304 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305
Bram Moolenaar015cf102021-06-26 21:52:02 +0200306 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200307 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200308 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200309 if (*np == '=' && np[1] != '=' && np[1] != '~'
310 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200311 {
312 typval_T rettv;
313
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100314 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200315 any_default = TRUE;
316 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200317 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200318 p = skipwhite(p);
319 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200320 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200321 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200322 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200323 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200324 if (ga_grow(default_args, 1) == FAIL)
325 goto err_ret;
326
327 // trim trailing whitespace
328 while (p > expr && VIM_ISWHITE(p[-1]))
329 p--;
330 c = *p;
331 *p = NUL;
332 expr = vim_strsave(expr);
333 if (expr == NULL)
334 {
335 *p = c;
336 goto err_ret;
337 }
338 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200339 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200340 default_args->ga_len++;
341 *p = c;
342 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200343 }
344 else
345 mustend = TRUE;
346 }
347 else if (any_default)
348 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000349 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200350 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200351 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200352
353 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
354 {
355 // Be tolerant when skipping
356 if (!skip)
357 {
358 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
359 goto err_ret;
360 }
361 p = skipwhite(p);
362 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200363 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200364 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200366 // Don't give this error when skipping, it makes the "->" not
367 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100368 // Allow missing space after comma in legacy functions.
369 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200370 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
371 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100372 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200373 goto err_ret;
374 }
375 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 else
377 mustend = TRUE;
378 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200379 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200380 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200382
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200383 if (*p != endchar)
384 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100385 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200386
387 *argp = p;
388 return OK;
389
390err_ret:
391 if (newargs != NULL)
392 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200393 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200394 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200395 return FAIL;
396}
397
398/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100399 * Parse the argument types, filling "fp->uf_arg_types".
400 * Return OK or FAIL.
401 */
402 static int
403parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
404{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200405 int len = 0;
406
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100407 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
408 if (argtypes->ga_len > 0)
409 {
410 // When "varargs" is set the last name/type goes into uf_va_name
411 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200412 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100413
414 if (len > 0)
415 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
416 if (fp->uf_arg_types != NULL)
417 {
418 int i;
419 type_T *type;
420
421 for (i = 0; i < len; ++ i)
422 {
423 char_u *p = ((char_u **)argtypes->ga_data)[i];
424
425 if (p == NULL)
426 // will get the type from the default value
427 type = &t_unknown;
428 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100429 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100430 if (type == NULL)
431 return FAIL;
432 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000433 if (i < fp->uf_args.ga_len
434 && (type->tt_type == VAR_FUNC
435 || type->tt_type == VAR_PARTIAL)
436 && var_wrong_func_name(
437 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
438 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100439 }
440 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100441 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200442
443 if (varargs)
444 {
445 char_u *p;
446
447 // Move the last argument "...name: type" to uf_va_name and
448 // uf_va_type.
449 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
450 [fp->uf_args.ga_len - 1];
451 --fp->uf_args.ga_len;
452 p = ((char_u **)argtypes->ga_data)[len];
453 if (p == NULL)
454 // TODO: get type from default value
455 fp->uf_va_type = &t_list_any;
456 else
457 {
458 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
459 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
460 {
461 semsg(_(e_variable_arguments_type_must_be_list_str),
462 ((char_u **)argtypes->ga_data)[len]);
463 return FAIL;
464 }
465 }
466 if (fp->uf_va_type == NULL)
467 return FAIL;
468 }
469
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100470 return OK;
471}
472
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100473 static int
474parse_return_type(ufunc_T *fp, char_u *ret_type)
475{
476 if (ret_type == NULL)
477 fp->uf_ret_type = &t_void;
478 else
479 {
480 char_u *p = ret_type;
481
482 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
483 if (fp->uf_ret_type == NULL)
484 {
485 fp->uf_ret_type = &t_void;
486 return FAIL;
487 }
488 }
489 return OK;
490}
491
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100492/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200493 * Register function "fp" as using "current_funccal" as its scope.
494 */
495 static int
496register_closure(ufunc_T *fp)
497{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200498 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100499 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200500 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200501 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200502 fp->uf_scoped = current_funccal;
503 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200504
Bram Moolenaar58016442016-07-31 18:30:22 +0200505 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
506 return FAIL;
507 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
508 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200509 return OK;
510}
511
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100512 static void
513set_ufunc_name(ufunc_T *fp, char_u *name)
514{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100515 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
516 // actually extends beyond the struct.
517 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100518
519 if (name[0] == K_SPECIAL)
520 {
521 fp->uf_name_exp = alloc(STRLEN(name) + 3);
522 if (fp->uf_name_exp != NULL)
523 {
524 STRCPY(fp->uf_name_exp, "<SNR>");
525 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
526 }
527 }
528}
529
Bram Moolenaar58016442016-07-31 18:30:22 +0200530/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100531 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
532 * return "buf" filled with a readable function name.
533 * Otherwise just return "name", thus the return value can always be used.
534 * "name" and "buf" may be equal.
535 */
536 char_u *
537make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
538{
539 size_t len;
540
541 if (name[0] != K_SPECIAL)
542 return name;
543 len = STRLEN(name);
544 if (len + 3 > bufsize)
545 return name;
546
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100547 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100548 mch_memmove(buf, "<SNR>", 5);
549 return buf;
550}
551
552/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200553 * Get a name for a lambda. Returned in static memory.
554 */
555 char_u *
556get_lambda_name(void)
557{
558 static char_u name[30];
559 static int lambda_no = 0;
560
561 sprintf((char*)name, "<lambda>%d", ++lambda_no);
562 return name;
563}
564
Bram Moolenaar801ab062020-06-25 19:27:56 +0200565#if defined(FEAT_LUA) || defined(PROTO)
566/*
567 * Registers a native C callback which can be called from Vim script.
568 * Returns the name of the Vim script function.
569 */
570 char_u *
571register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
572{
573 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200574 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200575
576 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
577 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200578 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200579
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200580 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200581 fp->uf_refcount = 1;
582 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000583 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200584 fp->uf_calls = 0;
585 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200586 fp->uf_cb = cb;
587 fp->uf_cb_free = cb_free;
588 fp->uf_cb_state = state;
589
590 set_ufunc_name(fp, name);
591 hash_add(&func_hashtab, UF2HIKEY(fp));
592
593 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200594}
595#endif
596
Bram Moolenaar04b12692020-05-04 23:24:44 +0200597/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100598 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100599 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100600 * If "white_error" is not NULL check for correct use of white space and set
601 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100602 * Return NULL if no valid arrow found.
603 */
604 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100605skip_arrow(
606 char_u *start,
607 int equal_arrow,
608 char_u **ret_type,
609 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100610{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100611 char_u *s = start;
612 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100613
614 if (equal_arrow)
615 {
616 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100617 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100618 if (white_error != NULL && !VIM_ISWHITE(s[1]))
619 {
620 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100621 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100622 return NULL;
623 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100624 s = skipwhite(s + 1);
625 *ret_type = s;
626 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100627 if (s == *ret_type)
628 {
629 emsg(_(e_missing_return_type));
630 return NULL;
631 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100632 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100633 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100634 s = skipwhite(s);
635 if (*s != '=')
636 return NULL;
637 ++s;
638 }
639 if (*s != '>')
640 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100641 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
642 || !IS_WHITE_OR_NUL(s[1])))
643 {
644 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100645 semsg(_(e_white_space_required_before_and_after_str_at_str),
646 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100647 return NULL;
648 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100649 return skipwhite(s + 1);
650}
651
652/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100653 * Check if "*cmd" points to a function command and if so advance "*cmd" and
654 * return TRUE.
655 * Otherwise return FALSE;
656 * Do not consider "function(" to be a command.
657 */
658 static int
659is_function_cmd(char_u **cmd)
660{
661 char_u *p = *cmd;
662
663 if (checkforcmd(&p, "function", 2))
664 {
665 if (*p == '(')
666 return FALSE;
667 *cmd = p;
668 return TRUE;
669 }
670 return FALSE;
671}
672
673/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200674 * Called when defining a function: The context may be needed for script
675 * variables declared in a block that is visible now but not when the function
676 * is compiled or called later.
677 */
678 static void
679function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
680{
681 if (cstack != NULL && cstack->cs_idx >= 0)
682 {
683 int count = cstack->cs_idx + 1;
684 int i;
685
686 fp->uf_block_ids = ALLOC_MULT(int, count);
687 if (fp->uf_block_ids != NULL)
688 {
689 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
690 sizeof(int) * count);
691 fp->uf_block_depth = count;
692 }
693
694 // Set flag in each block to indicate a function was defined. This
695 // is used to keep the variable when leaving the block, see
696 // hide_script_var().
697 for (i = 0; i <= cstack->cs_idx; ++i)
698 cstack->cs_flags[i] |= CSF_FUNC_DEF;
699 }
700}
701
702/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100703 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200704 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100705 * "newlines" must already have been initialized.
706 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
707 */
708 static int
709get_function_body(
710 exarg_T *eap,
711 garray_T *newlines,
712 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000713 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100714{
715 linenr_T sourcing_lnum_top = SOURCING_LNUM;
716 linenr_T sourcing_lnum_off;
717 int saved_wait_return = need_wait_return;
718 char_u *line_arg = line_arg_in;
719 int vim9_function = eap->cmdidx == CMD_def
720 || eap->cmdidx == CMD_block;
721#define MAX_FUNC_NESTING 50
722 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200723 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100724 int nesting = 0;
725 getline_opt_T getline_options;
726 int indent = 2;
727 char_u *skip_until = NULL;
728 int ret = FAIL;
729 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200730 int heredoc_concat_len = 0;
731 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100732 char_u *heredoc_trimmed = NULL;
733
Bram Moolenaar20677332021-06-06 17:02:53 +0200734 ga_init2(&heredoc_ga, 1, 500);
735
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100736 // Detect having skipped over comment lines to find the return
737 // type. Add NULL lines to keep the line count correct.
738 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
739 if (SOURCING_LNUM < sourcing_lnum_off)
740 {
741 sourcing_lnum_off -= SOURCING_LNUM;
742 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
743 goto theend;
744 while (sourcing_lnum_off-- > 0)
745 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
746 }
747
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200748 nesting_def[0] = vim9_function;
749 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100750 getline_options = vim9_function
751 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
752 for (;;)
753 {
754 char_u *theline;
755 char_u *p;
756 char_u *arg;
757
758 if (KeyTyped)
759 {
760 msg_scroll = TRUE;
761 saved_wait_return = FALSE;
762 }
763 need_wait_return = FALSE;
764
765 if (line_arg != NULL)
766 {
767 // Use eap->arg, split up in parts by line breaks.
768 theline = line_arg;
769 p = vim_strchr(theline, '\n');
770 if (p == NULL)
771 line_arg += STRLEN(line_arg);
772 else
773 {
774 *p = NUL;
775 line_arg = p + 1;
776 }
777 }
778 else
779 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000780 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100781 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100782 }
783 if (KeyTyped)
784 lines_left = Rows - 1;
785 if (theline == NULL)
786 {
787 // Use the start of the function for the line number.
788 SOURCING_LNUM = sourcing_lnum_top;
789 if (skip_until != NULL)
790 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200791 else if (nesting_inline[nesting])
792 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100793 else if (eap->cmdidx == CMD_def)
794 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100795 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000796 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100797 goto theend;
798 }
799
800 // Detect line continuation: SOURCING_LNUM increased more than one.
801 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
802 if (SOURCING_LNUM < sourcing_lnum_off)
803 sourcing_lnum_off -= SOURCING_LNUM;
804 else
805 sourcing_lnum_off = 0;
806
807 if (skip_until != NULL)
808 {
809 // Don't check for ":endfunc"/":enddef" between
810 // * ":append" and "."
811 // * ":python <<EOF" and "EOF"
812 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
813 if (heredoc_trimmed == NULL
814 || (is_heredoc && skipwhite(theline) == theline)
815 || STRNCMP(theline, heredoc_trimmed,
816 STRLEN(heredoc_trimmed)) == 0)
817 {
818 if (heredoc_trimmed == NULL)
819 p = theline;
820 else if (is_heredoc)
821 p = skipwhite(theline) == theline
822 ? theline : theline + STRLEN(heredoc_trimmed);
823 else
824 p = theline + STRLEN(heredoc_trimmed);
825 if (STRCMP(p, skip_until) == 0)
826 {
827 VIM_CLEAR(skip_until);
828 VIM_CLEAR(heredoc_trimmed);
829 getline_options = vim9_function
830 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
831 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200832
833 if (heredoc_concat_len > 0)
834 {
835 // Replace the starting line with all the concatenated
836 // lines.
837 ga_concat(&heredoc_ga, theline);
838 vim_free(((char_u **)(newlines->ga_data))[
839 heredoc_concat_len - 1]);
840 ((char_u **)(newlines->ga_data))[
841 heredoc_concat_len - 1] = heredoc_ga.ga_data;
842 ga_init(&heredoc_ga);
843 heredoc_concat_len = 0;
844 theline += STRLEN(theline); // skip the "EOF"
845 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100846 }
847 }
848 }
849 else
850 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200851 int c;
852 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000853 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100854
855 // skip ':' and blanks
856 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
857 ;
858
859 // Check for "endfunction", "enddef" or "}".
860 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000861 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200862 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100863 ? *p == '}'
864 : (checkforcmd(&p, nesting_def[nesting]
865 ? "enddef" : "endfunction", 4)
866 && *p != ':'))
867 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000868 if (!nesting_inline[nesting] && nesting_def[nesting]
869 && p < cmd + 6)
870 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100871 if (nesting-- == 0)
872 {
873 char_u *nextcmd = NULL;
874
875 if (*p == '|' || *p == '}')
876 nextcmd = p + 1;
877 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
878 nextcmd = line_arg;
879 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100880 && (vim9_function || p_verbose > 0))
881 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200882 SOURCING_LNUM = sourcing_lnum_top
883 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100884 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000885 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100886 else
887 give_warning2((char_u *)
888 _("W22: Text found after :endfunction: %s"),
889 p, TRUE);
890 }
891 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100892 {
893 // Another command follows. If the line came from "eap"
894 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000895 // change "eap->cmdlinep" to point to the last fetched
896 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100897 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000898 if (lines_to_free->ga_len > 0
899 && *eap->cmdlinep !=
900 ((char_u **)lines_to_free->ga_data)
901 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100902 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000903 // *cmdlinep will be freed later, thus remove the
904 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100905 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000906 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
907 [lines_to_free->ga_len - 1];
908 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100909 }
910 }
911 break;
912 }
913 }
914
915 // Check for mismatched "endfunc" or "enddef".
916 // We don't check for "def" inside "func" thus we also can't check
917 // for "enddef".
918 // We continue to find the end of the function, although we might
919 // not find it.
920 else if (nesting_def[nesting])
921 {
922 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
923 emsg(_(e_mismatched_endfunction));
924 }
925 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
926 emsg(_(e_mismatched_enddef));
927
928 // Increase indent inside "if", "while", "for" and "try", decrease
929 // at "end".
930 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
931 indent -= 2;
932 else if (STRNCMP(p, "if", 2) == 0
933 || STRNCMP(p, "wh", 2) == 0
934 || STRNCMP(p, "for", 3) == 0
935 || STRNCMP(p, "try", 3) == 0)
936 indent += 2;
937
938 // Check for defining a function inside this function.
939 // Only recognize "def" inside "def", not inside "function",
940 // For backwards compatibility, see Test_function_python().
941 c = *p;
942 if (is_function_cmd(&p)
943 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
944 {
945 if (*p == '!')
946 p = skipwhite(p + 1);
947 p += eval_fname_script(p);
948 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
949 NULL, NULL));
950 if (*skipwhite(p) == '(')
951 {
952 if (nesting == MAX_FUNC_NESTING - 1)
953 emsg(_(e_function_nesting_too_deep));
954 else
955 {
956 ++nesting;
957 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200958 nesting_inline[nesting] = FALSE;
959 indent += 2;
960 }
961 }
962 }
963
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200964 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200965 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200966 // Not a comment line: check for nested inline function.
967 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200968 while (end > p && VIM_ISWHITE(*end))
969 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200970 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200971 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200972 int is_block;
973
974 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200975 --end;
976 while (end > p && VIM_ISWHITE(*end))
977 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200978 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
979 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200980 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200981 char_u *s = p;
982
983 // check for line starting with "au" for :autocmd or
984 // "com" for :command, these can use a {} block
985 is_block = checkforcmd_noparen(&s, "autocmd", 2)
986 || checkforcmd_noparen(&s, "command", 3);
987 }
988
989 if (is_block)
990 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200991 if (nesting == MAX_FUNC_NESTING - 1)
992 emsg(_(e_function_nesting_too_deep));
993 else
994 {
995 ++nesting;
996 nesting_def[nesting] = TRUE;
997 nesting_inline[nesting] = TRUE;
998 indent += 2;
999 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001000 }
1001 }
1002 }
1003
1004 // Check for ":append", ":change", ":insert". Not for :def.
1005 p = skip_range(p, FALSE, NULL);
1006 if (!vim9_function
1007 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1008 || (p[0] == 'c'
1009 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1010 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1011 && (STRNCMP(&p[3], "nge", 3) != 0
1012 || !ASCII_ISALPHA(p[6])))))))
1013 || (p[0] == 'i'
1014 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1015 && (!ASCII_ISALPHA(p[2])
1016 || (p[2] == 's'
1017 && (!ASCII_ISALPHA(p[3])
1018 || p[3] == 'e'))))))))
1019 skip_until = vim_strsave((char_u *)".");
1020
1021 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1022 arg = skipwhite(skiptowhite(p));
1023 if (arg[0] == '<' && arg[1] =='<'
1024 && ((p[0] == 'p' && p[1] == 'y'
1025 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1026 || ((p[2] == '3' || p[2] == 'x')
1027 && !ASCII_ISALPHA(p[3]))))
1028 || (p[0] == 'p' && p[1] == 'e'
1029 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1030 || (p[0] == 't' && p[1] == 'c'
1031 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1032 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1033 && !ASCII_ISALPHA(p[3]))
1034 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1035 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1036 || (p[0] == 'm' && p[1] == 'z'
1037 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1038 ))
1039 {
1040 // ":python <<" continues until a dot, like ":append"
1041 p = skipwhite(arg + 2);
1042 if (STRNCMP(p, "trim", 4) == 0)
1043 {
1044 // Ignore leading white space.
1045 p = skipwhite(p + 4);
1046 heredoc_trimmed = vim_strnsave(theline,
1047 skipwhite(theline) - theline);
1048 }
1049 if (*p == NUL)
1050 skip_until = vim_strsave((char_u *)".");
1051 else
1052 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1053 getline_options = GETLINE_NONE;
1054 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001055 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001056 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001057 }
1058
Bram Moolenaard881d152022-05-13 13:50:36 +01001059 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001060 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001061 // Check for ":cmd v =<< [trim] EOF"
1062 // and ":cmd [a, b] =<< [trim] EOF"
1063 // and "lines =<< [trim] EOF" for Vim9
1064 // Where "cmd" can be "let", "var", "final" or "const".
1065 arg = skipwhite(skiptowhite(p));
1066 if (*arg == '[')
1067 arg = vim_strchr(arg, ']');
1068 if (arg != NULL)
1069 {
1070 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001071 && arg[1] == '<' && arg[2] =='<');
1072
Bram Moolenaard881d152022-05-13 13:50:36 +01001073 if (!found)
1074 // skip over the argument after "cmd"
1075 arg = skipwhite(skiptowhite(arg));
1076 if (found || (arg[0] == '=' && arg[1] == '<'
1077 && arg[2] =='<'
1078 && (checkforcmd(&p, "let", 2)
1079 || checkforcmd(&p, "var", 3)
1080 || checkforcmd(&p, "final", 5)
1081 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001082 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001083 p = skipwhite(arg + 3);
1084 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001085 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001086 if (STRNCMP(p, "trim", 4) == 0)
1087 {
1088 // Ignore leading white space.
1089 p = skipwhite(p + 4);
1090 heredoc_trimmed = vim_strnsave(theline,
1091 skipwhite(theline) - theline);
1092 continue;
1093 }
1094 if (STRNCMP(p, "eval", 4) == 0)
1095 {
1096 // Ignore leading white space.
1097 p = skipwhite(p + 4);
1098 continue;
1099 }
1100 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001101 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001102 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1103 getline_options = GETLINE_NONE;
1104 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001105 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001106 }
1107 }
1108 }
1109
1110 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001111 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001112 goto theend;
1113
Bram Moolenaar20677332021-06-06 17:02:53 +02001114 if (heredoc_concat_len > 0)
1115 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001116 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001117 // to be used for the instruction later.
1118 ga_concat(&heredoc_ga, theline);
1119 ga_concat(&heredoc_ga, (char_u *)"\n");
1120 p = vim_strsave((char_u *)"");
1121 }
1122 else
1123 {
1124 // Copy the line to newly allocated memory. get_one_sourceline()
1125 // allocates 250 bytes per line, this saves 80% on average. The
1126 // cost is an extra alloc/free.
1127 p = vim_strsave(theline);
1128 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001129 if (p == NULL)
1130 goto theend;
1131 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1132
1133 // Add NULL lines for continuation lines, so that the line count is
1134 // equal to the index in the growarray.
1135 while (sourcing_lnum_off-- > 0)
1136 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1137
1138 // Check for end of eap->arg.
1139 if (line_arg != NULL && *line_arg == NUL)
1140 line_arg = NULL;
1141 }
1142
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001143 // Return OK when no error was detected.
1144 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001145 ret = OK;
1146
1147theend:
1148 vim_free(skip_until);
1149 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001150 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001151 need_wait_return |= saved_wait_return;
1152 return ret;
1153}
1154
1155/*
1156 * Handle the body of a lambda. *arg points to the "{", process statements
1157 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001158 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001159 * When successful "rettv" is set to a funcref.
1160 */
1161 static int
1162lambda_function_body(
1163 char_u **arg,
1164 typval_T *rettv,
1165 evalarg_T *evalarg,
1166 garray_T *newargs,
1167 garray_T *argtypes,
1168 int varargs,
1169 garray_T *default_args,
1170 char_u *ret_type)
1171{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001172 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001173 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001174 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001175 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001176 exarg_T eap;
1177 garray_T newlines;
1178 char_u *cmdline = NULL;
1179 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001180 partial_T *pt;
1181 char_u *name;
1182 int lnum_save = -1;
1183 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1184
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001185 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1186 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001187 semsg(_(e_trailing_characters_str), *arg + 1);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001188 return FAIL;
1189 }
1190
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001191 CLEAR_FIELD(eap);
1192 eap.cmdidx = CMD_block;
1193 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001194 eap.cmdlinep = &cmdline;
1195 eap.skip = !evaluate;
1196 if (evalarg->eval_cctx != NULL)
1197 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1198 else
1199 {
1200 eap.getline = evalarg->eval_getline;
1201 eap.cookie = evalarg->eval_cookie;
1202 }
1203
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001204 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001205 if (get_function_body(&eap, &newlines, NULL,
1206 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001207 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001208
1209 // When inside a lambda must add the function lines to evalarg.eval_ga.
1210 evalarg->eval_break_count += newlines.ga_len;
1211 if (gap->ga_itemsize > 0)
1212 {
1213 int idx;
1214 char_u *last;
1215 size_t plen;
1216 char_u *pnl;
1217
1218 for (idx = 0; idx < newlines.ga_len; ++idx)
1219 {
1220 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1221
Bram Moolenaarecb66452021-05-18 15:09:18 +02001222 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001223 goto erret;
1224
1225 // Going to concatenate the lines after parsing. For an empty or
1226 // comment line use an empty string.
1227 // Insert NL characters at the start of each line, the string will
1228 // be split again later in .get_lambda_tv().
1229 if (*p == NUL || vim9_comment_start(p))
1230 p = (char_u *)"";
1231 plen = STRLEN(p);
1232 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1233 if (pnl != NULL)
1234 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001235 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1236 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001237 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001238 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001239 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001240 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001241 // more is following after the "}", which was skipped
1242 last = cmdline;
1243 else
1244 // nothing is following the "}"
1245 last = (char_u *)"}";
1246 plen = STRLEN(last);
1247 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1248 if (pnl != NULL)
1249 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001250 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1251 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001252 }
1253
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001254 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001255 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001256 garray_T *tfgap = &evalarg->eval_tofree_ga;
1257
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001258 // Something comes after the "}".
1259 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001260
1261 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001262 if (ga_grow(tfgap, 1) == OK)
1263 {
1264 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1265 evalarg->eval_using_cmdline = TRUE;
1266 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001267 }
1268 else
1269 *arg = (char_u *)"";
1270
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001271 if (!evaluate)
1272 {
1273 ret = OK;
1274 goto erret;
1275 }
1276
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001277 name = get_lambda_name();
1278 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1279 if (ufunc == NULL)
1280 goto erret;
1281 set_ufunc_name(ufunc, name);
1282 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001283 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001284 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001285 ufunc->uf_refcount = 1;
1286 ufunc->uf_args = *newargs;
1287 newargs->ga_data = NULL;
1288 ufunc->uf_def_args = *default_args;
1289 default_args->ga_data = NULL;
1290 ufunc->uf_func_type = &t_func_any;
1291
1292 // error messages are for the first function line
1293 lnum_save = SOURCING_LNUM;
1294 SOURCING_LNUM = sourcing_lnum_top;
1295
1296 // parse argument types
1297 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1298 {
1299 SOURCING_LNUM = lnum_save;
1300 goto erret;
1301 }
1302
1303 // parse the return type, if any
1304 if (parse_return_type(ufunc, ret_type) == FAIL)
1305 goto erret;
1306
1307 pt = ALLOC_CLEAR_ONE(partial_T);
1308 if (pt == NULL)
1309 goto erret;
1310 pt->pt_func = ufunc;
1311 pt->pt_refcount = 1;
1312
1313 ufunc->uf_lines = newlines;
1314 newlines.ga_data = NULL;
1315 if (sandbox)
1316 ufunc->uf_flags |= FC_SANDBOX;
1317 if (!ASCII_ISUPPER(*ufunc->uf_name))
1318 ufunc->uf_flags |= FC_VIM9;
1319 ufunc->uf_script_ctx = current_sctx;
1320 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1321 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1322 set_function_type(ufunc);
1323
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001324 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1325
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001326 rettv->vval.v_partial = pt;
1327 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001328 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001329 ret = OK;
1330
1331erret:
1332 if (lnum_save >= 0)
1333 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001334 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001335 if (newargs != NULL)
1336 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001338 if (ufunc != NULL)
1339 {
1340 func_clear(ufunc, TRUE);
1341 func_free(ufunc, TRUE);
1342 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001343 return ret;
1344}
1345
1346/*
1347 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001348 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001349 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001350 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1351 */
1352 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001353get_lambda_tv(
1354 char_u **arg,
1355 typval_T *rettv,
1356 int types_optional,
1357 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001358{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001359 int evaluate = evalarg != NULL
1360 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 garray_T newargs;
1362 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001363 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001364 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001365 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001367 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001368 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001369 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001370 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001371 char_u *s;
1372 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001373 int *old_eval_lavars = eval_lavars_used;
1374 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001375 char_u *tofree1 = NULL;
1376 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001377 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001378 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001379 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001380 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001381 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001382
Bram Moolenaar4525a572022-02-13 11:57:33 +00001383 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001384 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001385
1386 ga_init(&newargs);
1387 ga_init(&newlines);
1388
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001389 // First, check if this is really a lambda expression. "->" or "=>" must
1390 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001391 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001392 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001393 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001394 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001395 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001396 {
1397 if (types_optional)
1398 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001399 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001400 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001401
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001402 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001403 if (evaluate)
1404 pnewargs = &newargs;
1405 else
1406 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001407 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001408 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001409 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001410 &varargs, &default_args,
1411 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001412 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001413 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001414 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001415 {
1416 if (types_optional)
1417 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001418 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001419 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001420 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001421 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001423 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1424 if (ret_type != NULL)
1425 {
1426 ret_type = vim_strsave(ret_type);
1427 tofree2 = ret_type;
1428 }
1429
Bram Moolenaare38eab22019-12-05 21:50:01 +01001430 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001431 if (evaluate)
1432 eval_lavars_used = &eval_lavars;
1433
Bram Moolenaar65c44152020-12-24 15:14:01 +01001434 *arg = skipwhite_and_linebreak(*arg, evalarg);
1435
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001436 // Recognize "{" as the start of a function body.
1437 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001438 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001439 if (evalarg == NULL)
1440 // cannot happen?
1441 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001442 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001443 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1444 types_optional ? &argtypes : NULL, varargs,
1445 &default_args, ret_type) == FAIL)
1446 goto errret;
1447 goto theend;
1448 }
1449 if (default_args.ga_len > 0)
1450 {
1451 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001452 goto errret;
1453 }
1454
Bram Moolenaare38eab22019-12-05 21:50:01 +01001455 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001456 start = *arg;
1457 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001458 if (ret == FAIL)
1459 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001460 if (evalarg != NULL)
1461 {
1462 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001463 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001464 evalarg->eval_tofree = NULL;
1465 }
1466
Bram Moolenaar65c44152020-12-24 15:14:01 +01001467 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001468 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001469 *arg = skipwhite_and_linebreak(*arg, evalarg);
1470 if (**arg != '}')
1471 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001472 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001473 goto errret;
1474 }
1475 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001476 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477
1478 if (evaluate)
1479 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001480 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001481 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001482 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001483 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001484 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001485
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001486 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001487 if (fp == NULL)
1488 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001489 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001490 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001491 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001492 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001493
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001494 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001495 if (ga_grow(&newlines, 1) == FAIL)
1496 goto errret;
1497
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001498 // If there are line breaks, we need to split up the string.
1499 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001500 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001501 line_end = end;
1502
1503 // Add "return " before the expression (or the first line).
1504 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001505 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001506 if (p == NULL)
1507 goto errret;
1508 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1509 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001510 vim_strncpy(p + 7, start, line_end - start);
1511
1512 if (line_end != end)
1513 {
1514 // Add more lines, split by line breaks. Thus is used when a
1515 // lambda with { cmds } is encountered.
1516 while (*line_end == '\n')
1517 {
1518 if (ga_grow(&newlines, 1) == FAIL)
1519 goto errret;
1520 start = line_end + 1;
1521 line_end = vim_strchr(start, '\n');
1522 if (line_end == NULL)
1523 line_end = end;
1524 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1525 vim_strnsave(start, line_end - start);
1526 }
1527 }
1528
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001529 if (strstr((char *)p + 7, "a:") == NULL)
1530 // No a: variables are used for sure.
1531 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001532
1533 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001534 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001536 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001537 if (types_optional)
1538 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001539 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001540 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001541 goto errret;
1542 if (ret_type != NULL)
1543 {
1544 fp->uf_ret_type = parse_type(&ret_type,
1545 &fp->uf_type_list, TRUE);
1546 if (fp->uf_ret_type == NULL)
1547 goto errret;
1548 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001549 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001550 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001551 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001552
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001554 if (current_funccal != NULL && eval_lavars)
1555 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001556 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001557 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001558 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001559 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560
1561#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001562 if (prof_def_func())
1563 func_do_profile(fp);
1564#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001565 if (sandbox)
1566 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001567 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001568 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001569 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001570 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001572 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001573 // Use the line number of the arguments.
1574 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001576 function_using_block_scopes(fp, evalarg->eval_cstack);
1577
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001578 pt->pt_func = fp;
1579 pt->pt_refcount = 1;
1580 rettv->vval.v_partial = pt;
1581 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001582
1583 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001584 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001586theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001587 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001588 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001589 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001590 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001591 vim_free(tofree1);
1592 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001593 if (types_optional)
1594 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001595
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001596 return OK;
1597
1598errret:
1599 ga_clear_strings(&newargs);
1600 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001601 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001602 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001603 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001604 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001605 if (fp != NULL)
1606 vim_free(fp->uf_arg_types);
1607 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001609 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001610 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001611 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001612 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001613 vim_free(tofree1);
1614 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001615 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001616 return FAIL;
1617}
1618
1619/*
1620 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1621 * name it contains, otherwise return "name".
1622 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1623 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001624 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1625 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001626 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001627 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628 */
1629 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001630deref_func_name(
1631 char_u *name,
1632 int *lenp,
1633 partial_T **partialp,
1634 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001635 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001636 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001637 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638{
1639 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001640 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001641 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001642 char_u *s = NULL;
1643 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001644 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001645
1646 if (partialp != NULL)
1647 *partialp = NULL;
1648
1649 cc = name[*lenp];
1650 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001651
Bram Moolenaar71f21932022-01-07 18:20:55 +00001652 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001654 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001655 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001656 tv = &v->di_tv;
1657 }
1658 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1659 {
1660 imported_T *import;
1661 char_u *p = name;
1662 int len = *lenp;
1663
1664 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001665 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001666 p = name + 2;
1667 len -= 2;
1668 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001669 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001670
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001671 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001672 if (import != NULL)
1673 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001674 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001675 if (new_function)
1676 semsg(_(e_redefining_imported_item_str), name);
1677 else
1678 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001679 name[len] = cc;
1680 *lenp = 0;
1681 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001682 }
1683 }
1684
1685 if (tv != NULL)
1686 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001687 if (found_var != NULL)
1688 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001689 if (tv->v_type == VAR_FUNC)
1690 {
1691 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001692 {
1693 *lenp = 0;
1694 return (char_u *)""; // just in case
1695 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001696 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001697 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001699
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001700 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001701 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001702 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001703
1704 if (pt == NULL)
1705 {
1706 *lenp = 0;
1707 return (char_u *)""; // just in case
1708 }
1709 if (partialp != NULL)
1710 *partialp = pt;
1711 s = partial_name(pt);
1712 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001714
1715 if (s != NULL)
1716 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001717 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001718 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001719 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001720
1721 if (sv != NULL)
1722 *type = sv->sv_type;
1723 }
1724 return s;
1725 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726 }
1727
1728 return name;
1729}
1730
1731/*
1732 * Give an error message with a function name. Handle <SNR> things.
1733 * "ermsg" is to be passed without translation, use N_() instead of _().
1734 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001735 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736emsg_funcname(char *ermsg, char_u *name)
1737{
Bram Moolenaar54598062022-01-13 12:05:09 +00001738 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739
Bram Moolenaar54598062022-01-13 12:05:09 +00001740 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001742 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 if (p != name)
1744 vim_free(p);
1745}
1746
1747/*
1748 * Allocate a variable for the result of a function.
1749 * Return OK or FAIL.
1750 */
1751 int
1752get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001753 char_u *name, // name of the function
1754 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001756 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001757 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001758 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759{
1760 char_u *argp;
1761 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001762 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1763 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001764 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001765 int evaluate = evalarg == NULL
1766 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767
1768 /*
1769 * Get the arguments.
1770 */
1771 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001772 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1773 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001775 // skip the '(' or ',' and possibly line breaks
1776 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1777
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778 if (*argp == ')' || *argp == ',' || *argp == NUL)
1779 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001780 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 {
1782 ret = FAIL;
1783 break;
1784 }
1785 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001786 // The comma should come right after the argument, but this wasn't
1787 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001788 if (vim9script)
1789 {
1790 if (*argp != ',' && *skipwhite(argp) == ',')
1791 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001792 if (evaluate)
1793 semsg(_(e_no_white_space_allowed_before_str_str),
1794 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001795 ret = FAIL;
1796 break;
1797 }
1798 }
1799 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001800 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 if (*argp != ',')
1802 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001803 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1804 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001805 if (evaluate)
1806 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001807 ret = FAIL;
1808 break;
1809 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001811 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 if (*argp == ')')
1813 ++argp;
1814 else
1815 ret = FAIL;
1816
1817 if (ret == OK)
1818 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001819 int i = 0;
1820 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821
1822 if (get_vim_var_nr(VV_TESTING))
1823 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001824 // Prepare for calling test_garbagecollect_now(), need to know
1825 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001827 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 for (i = 0; i < argcount; ++i)
1829 if (ga_grow(&funcargs, 1) == OK)
1830 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1831 &argvars[i];
1832 }
1833
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001834 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001835 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001836 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001837 // An error in a builtin function does not return FAIL, but we do
1838 // want to abort further processing if an error was given.
1839 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001840 clear_tv(rettv);
1841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842
1843 funcargs.ga_len -= i;
1844 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001845 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 {
1847 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001848 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001850 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 }
1852
1853 while (--argcount >= 0)
1854 clear_tv(&argvars[argcount]);
1855
Bram Moolenaar4525a572022-02-13 11:57:33 +00001856 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001857 *arg = argp;
1858 else
1859 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860 return ret;
1861}
1862
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001863/*
1864 * Return TRUE if "p" starts with "<SID>" or "s:".
1865 * Only works if eval_fname_script() returned non-zero for "p"!
1866 */
1867 static int
1868eval_fname_sid(char_u *p)
1869{
1870 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1871}
1872
1873/*
1874 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1875 * Change <SNR>123_name() to K_SNR 123_name().
1876 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1877 * (slow).
1878 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001879 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1881{
1882 int llen;
1883 char_u *fname;
1884 int i;
1885
1886 llen = eval_fname_script(name);
1887 if (llen > 0)
1888 {
1889 fname_buf[0] = K_SPECIAL;
1890 fname_buf[1] = KS_EXTRA;
1891 fname_buf[2] = (int)KE_SNR;
1892 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001893 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001895 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001896 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 else
1898 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001899 sprintf((char *)fname_buf + 3, "%ld_",
1900 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 i = (int)STRLEN(fname_buf);
1902 }
1903 }
1904 if (i + STRLEN(name + llen) < FLEN_FIXED)
1905 {
1906 STRCPY(fname_buf + i, name + llen);
1907 fname = fname_buf;
1908 }
1909 else
1910 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001911 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001913 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 else
1915 {
1916 *tofree = fname;
1917 mch_memmove(fname, fname_buf, (size_t)i);
1918 STRCPY(fname + i, name + llen);
1919 }
1920 }
1921 }
1922 else
1923 fname = name;
1924 return fname;
1925}
1926
1927/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001928 * Concatenate the script ID and function name into "<SNR>99_name".
1929 * "buffer" must have size MAX_FUNC_NAME_LEN.
1930 */
1931 void
1932func_name_with_sid(char_u *name, int sid, char_u *buffer)
1933{
1934 // A script-local function is stored as "<SNR>99_name".
1935 buffer[0] = K_SPECIAL;
1936 buffer[1] = KS_EXTRA;
1937 buffer[2] = (int)KE_SNR;
1938 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1939 (long)sid, name);
1940}
1941
1942/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 * Find a function "name" in script "sid".
1944 */
1945 static ufunc_T *
1946find_func_with_sid(char_u *name, int sid)
1947{
Bram Moolenaarb8822442022-01-11 15:24:05 +00001948 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001949 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950
Bram Moolenaarb8822442022-01-11 15:24:05 +00001951 if (!SCRIPT_ID_VALID(sid))
1952 return NULL; // not in a script
1953
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001954 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 hi = hash_find(&func_hashtab, buffer);
1956 if (!HASHITEM_EMPTY(hi))
1957 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001958 return NULL;
1959}
1960
1961/*
1962 * Find a function "name" in script "sid" prefixing the autoload prefix.
1963 */
1964 static ufunc_T *
1965find_func_with_prefix(char_u *name, int sid)
1966{
1967 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001968 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00001969 scriptitem_T *si;
1970
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001971 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00001972 return NULL; // already has the prefix
1973 if (!SCRIPT_ID_VALID(sid))
1974 return NULL; // not in a script
1975 si = SCRIPT_ITEM(sid);
1976 if (si->sn_autoload_prefix != NULL)
1977 {
1978 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1979 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001980 char_u *namep;
1981
1982 // skip a "<SNR>99_" prefix
1983 namep = untrans_function_name(name);
1984 if (namep == NULL)
1985 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00001986
1987 // An exported function in an autoload script is stored as
1988 // "dir#path#name".
1989 if (len < sizeof(buffer))
1990 auto_name = buffer;
1991 else
1992 auto_name = alloc(len);
1993 if (auto_name != NULL)
1994 {
1995 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001996 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001997 hi = hash_find(&func_hashtab, auto_name);
1998 if (auto_name != buffer)
1999 vim_free(auto_name);
2000 if (!HASHITEM_EMPTY(hi))
2001 return HI2UF(hi);
2002 }
2003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004
2005 return NULL;
2006}
2007
2008/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002010 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2011 * functions.
2012 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 * Return NULL for unknown function.
2014 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002015 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002016find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002017{
2018 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002021 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002023 // Find script-local function before global one.
2024 if (in_vim9script() && eval_isnamec1(*name)
2025 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002027 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2028 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002029 if (func != NULL)
2030 return func;
2031 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002032 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2033 {
2034 char_u *p = name + 5;
2035 long sid;
2036
2037 // printable "<SNR>123_Name" form
2038 sid = getdigits(&p);
2039 if (*p == '_')
2040 {
2041 func = find_func_with_sid(p + 1, (int)sid);
2042 if (func != NULL)
2043 return func;
2044 }
2045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002047
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002048 if ((flags & FFED_NO_GLOBAL) == 0)
2049 {
2050 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002051 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002052 if (!HASHITEM_EMPTY(hi))
2053 return HI2UF(hi);
2054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055
Bram Moolenaarb8822442022-01-11 15:24:05 +00002056 // Find autoload function if this is an autoload script.
2057 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2058 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059}
2060
2061/*
2062 * Find a function by name, return pointer to it in ufuncs.
2063 * "cctx" is passed in a :def function to find imported functions.
2064 * Return NULL for unknown or dead function.
2065 */
2066 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002067find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002069 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070
2071 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2072 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002073 return NULL;
2074}
2075
2076/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002077 * Return TRUE if "ufunc" is a global function.
2078 */
2079 int
2080func_is_global(ufunc_T *ufunc)
2081{
2082 return ufunc->uf_name[0] != K_SPECIAL;
2083}
2084
2085/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002086 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2087 */
2088 int
2089func_requires_g_prefix(ufunc_T *ufunc)
2090{
2091 return ufunc->uf_name[0] != K_SPECIAL
2092 && (ufunc->uf_flags & FC_LAMBDA) == 0
2093 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2094}
2095
2096/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002097 * Copy the function name of "fp" to buffer "buf".
2098 * "buf" must be able to hold the function name plus three bytes.
2099 * Takes care of script-local function names.
2100 */
2101 static void
2102cat_func_name(char_u *buf, ufunc_T *fp)
2103{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002104 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002105 {
2106 STRCPY(buf, "<SNR>");
2107 STRCAT(buf, fp->uf_name + 3);
2108 }
2109 else
2110 STRCPY(buf, fp->uf_name);
2111}
2112
2113/*
2114 * Add a number variable "name" to dict "dp" with value "nr".
2115 */
2116 static void
2117add_nr_var(
2118 dict_T *dp,
2119 dictitem_T *v,
2120 char *name,
2121 varnumber_T nr)
2122{
2123 STRCPY(v->di_key, name);
2124 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2125 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2126 v->di_tv.v_type = VAR_NUMBER;
2127 v->di_tv.v_lock = VAR_FIXED;
2128 v->di_tv.vval.v_number = nr;
2129}
2130
2131/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002132 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002133 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002134 static void
2135free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002136{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002137 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002138
2139 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2140 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002141 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002142
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002143 // When garbage collecting a funccall_T may be freed before the
2144 // function that references it, clear its uf_scoped field.
2145 // The function may have been redefined and point to another
2146 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002147 if (fp != NULL && fp->uf_scoped == fc)
2148 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002149 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002150 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002152 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002153 vim_free(fc);
2154}
2155
2156/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002157 * Free "fc" and what it contains.
2158 * Can be called only when "fc" is kept beyond the period of it called,
2159 * i.e. after cleanup_function_call(fc).
2160 */
2161 static void
2162free_funccal_contents(funccall_T *fc)
2163{
2164 listitem_T *li;
2165
2166 // Free all l: variables.
2167 vars_clear(&fc->l_vars.dv_hashtab);
2168
2169 // Free all a: variables.
2170 vars_clear(&fc->l_avars.dv_hashtab);
2171
2172 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002173 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002174 clear_tv(&li->li_tv);
2175
2176 free_funccal(fc);
2177}
2178
2179/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002180 * Handle the last part of returning from a function: free the local hashtable.
2181 * Unless it is still in use by a closure.
2182 */
2183 static void
2184cleanup_function_call(funccall_T *fc)
2185{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002186 int may_free_fc = fc->fc_refcount <= 0;
2187 int free_fc = TRUE;
2188
Bram Moolenaar6914c642017-04-01 21:21:30 +02002189 current_funccal = fc->caller;
2190
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002191 // Free all l: variables if not referred.
2192 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2193 vars_clear(&fc->l_vars.dv_hashtab);
2194 else
2195 free_fc = FALSE;
2196
2197 // If the a:000 list and the l: and a: dicts are not referenced and
2198 // there is no closure using it, we can free the funccall_T and what's
2199 // in it.
2200 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2201 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002202 else
2203 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002204 int todo;
2205 hashitem_T *hi;
2206 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002207
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002208 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002209
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002210 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002211 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2212 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2213 {
2214 if (!HASHITEM_EMPTY(hi))
2215 {
2216 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002217 di = HI2DI(hi);
2218 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002219 }
2220 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002221 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002222
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002223 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2224 fc->l_varlist.lv_first = NULL;
2225 else
2226 {
2227 listitem_T *li;
2228
2229 free_fc = FALSE;
2230
2231 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002232 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002233 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002234 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002235
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002236 if (free_fc)
2237 free_funccal(fc);
2238 else
2239 {
2240 static int made_copy = 0;
2241
2242 // "fc" is still in use. This can happen when returning "a:000",
2243 // assigning "l:" to a global variable or defining a closure.
2244 // Link "fc" in the list for garbage collection later.
2245 fc->caller = previous_funccal;
2246 previous_funccal = fc;
2247
2248 if (want_garbage_collect)
2249 // If garbage collector is ready, clear count.
2250 made_copy = 0;
2251 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002252 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002253 // We have made a lot of copies, worth 4 Mbyte. This can happen
2254 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002255 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002256 // too much memory.
2257 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002258 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002259 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002260 }
2261}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002262
2263/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002264 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2265 */
2266 static int
2267numbered_function(char_u *name)
2268{
2269 return isdigit(*name)
2270 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2271}
2272
2273/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002274 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002275 * 1. ordinary names, function defined with :function or :def;
2276 * can start with "<SNR>123_" literally or with K_SPECIAL.
2277 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002278 * For the first we only count the name stored in func_hashtab as a reference,
2279 * using function() does not count as a reference, because the function is
2280 * looked up by name.
2281 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002282 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002283func_name_refcount(char_u *name)
2284{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002285 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002286}
2287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288/*
2289 * Unreference "fc": decrement the reference count and free it when it
2290 * becomes zero. "fp" is detached from "fc".
2291 * When "force" is TRUE we are exiting.
2292 */
2293 static void
2294funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2295{
2296 funccall_T **pfc;
2297 int i;
2298
2299 if (fc == NULL)
2300 return;
2301
2302 if (--fc->fc_refcount <= 0 && (force || (
2303 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2304 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2305 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2306 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2307 {
2308 if (fc == *pfc)
2309 {
2310 *pfc = fc->caller;
2311 free_funccal_contents(fc);
2312 return;
2313 }
2314 }
2315 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2316 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2317 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2318}
2319
2320/*
2321 * Remove the function from the function hashtable. If the function was
2322 * deleted while it still has references this was already done.
2323 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2324 */
2325 static int
2326func_remove(ufunc_T *fp)
2327{
2328 hashitem_T *hi;
2329
2330 // Return if it was already virtually deleted.
2331 if (fp->uf_flags & FC_DEAD)
2332 return FALSE;
2333
2334 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2335 if (!HASHITEM_EMPTY(hi))
2336 {
2337 // When there is a def-function index do not actually remove the
2338 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002339 // Do remove it when it's a copy.
2340 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002341 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002343 return FALSE;
2344 }
2345 hash_remove(&func_hashtab, hi);
2346 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 return TRUE;
2348 }
2349 return FALSE;
2350}
2351
2352 static void
2353func_clear_items(ufunc_T *fp)
2354{
2355 ga_clear_strings(&(fp->uf_args));
2356 ga_clear_strings(&(fp->uf_def_args));
2357 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002359 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002360 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002361 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002362
2363 // Increment the refcount of this function to avoid it being freed
2364 // recursively when the partial is freed.
2365 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002366 partial_unref(fp->uf_partial);
2367 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002368 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002369
2370#ifdef FEAT_LUA
2371 if (fp->uf_cb_free != NULL)
2372 {
2373 fp->uf_cb_free(fp->uf_cb_state);
2374 fp->uf_cb_free = NULL;
2375 }
2376
2377 fp->uf_cb_state = NULL;
2378 fp->uf_cb = NULL;
2379#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380#ifdef FEAT_PROFILE
2381 VIM_CLEAR(fp->uf_tml_count);
2382 VIM_CLEAR(fp->uf_tml_total);
2383 VIM_CLEAR(fp->uf_tml_self);
2384#endif
2385}
2386
2387/*
2388 * Free all things that a function contains. Does not free the function
2389 * itself, use func_free() for that.
2390 * When "force" is TRUE we are exiting.
2391 */
2392 static void
2393func_clear(ufunc_T *fp, int force)
2394{
2395 if (fp->uf_cleared)
2396 return;
2397 fp->uf_cleared = TRUE;
2398
2399 // clear this function
2400 func_clear_items(fp);
2401 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002402 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403}
2404
2405/*
2406 * Free a function and remove it from the list of functions. Does not free
2407 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002408 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002409 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002411 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002412func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413{
2414 // Only remove it when not done already, otherwise we would remove a newer
2415 // version of the function with the same name.
2416 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2417 func_remove(fp);
2418
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002419 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002420 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002421 if (fp->uf_dfunc_idx > 0)
2422 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002423 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002425 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002426 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002427 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428}
2429
2430/*
2431 * Free all things that a function contains and free the function itself.
2432 * When "force" is TRUE we are exiting.
2433 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002434 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435func_clear_free(ufunc_T *fp, int force)
2436{
2437 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002438 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2439 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002440 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002441 else
2442 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443}
2444
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002445/*
2446 * Copy already defined function "lambda" to a new function with name "global".
2447 * This is for when a compiled function defines a global function.
2448 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002449 int
2450copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002451{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002452 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002453 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002454
2455 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002456 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002457 semsg(_(e_lambda_function_not_found_str), lambda);
2458 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002459 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002460
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002461 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002462 if (fp != NULL)
2463 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002464 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002465 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002466 return FAIL;
2467 }
2468
2469 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2470 if (fp == NULL)
2471 return FAIL;
2472
2473 fp->uf_varargs = ufunc->uf_varargs;
2474 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2475 fp->uf_def_status = ufunc->uf_def_status;
2476 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2477 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2478 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2479 == FAIL
2480 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2481 goto failed;
2482
2483 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2484 : vim_strsave(ufunc->uf_name_exp);
2485 if (ufunc->uf_arg_types != NULL)
2486 {
2487 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2488 if (fp->uf_arg_types == NULL)
2489 goto failed;
2490 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2491 sizeof(type_T *) * fp->uf_args.ga_len);
2492 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002493 if (ufunc->uf_va_name != NULL)
2494 {
2495 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2496 if (fp->uf_va_name == NULL)
2497 goto failed;
2498 }
2499 fp->uf_ret_type = ufunc->uf_ret_type;
2500
2501 fp->uf_refcount = 1;
2502 STRCPY(fp->uf_name, global);
2503 hash_add(&func_hashtab, UF2HIKEY(fp));
2504
2505 // the referenced dfunc_T is now used one more time
2506 link_def_function(fp);
2507
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002508 // Create a partial to store the context of the function where it was
2509 // instantiated. Only needs to be done once. Do this on the original
2510 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002511 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2512 {
2513 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2514
2515 if (pt == NULL)
2516 goto failed;
2517 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002518 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002519 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002520 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002521 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002522 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002523 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002524 }
2525
2526 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002527
2528failed:
2529 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002530 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002531}
2532
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002533static int funcdepth = 0;
2534
2535/*
2536 * Increment the function call depth count.
2537 * Return FAIL when going over 'maxfuncdepth'.
2538 * Otherwise return OK, must call funcdepth_decrement() later!
2539 */
2540 int
2541funcdepth_increment(void)
2542{
2543 if (funcdepth >= p_mfd)
2544 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002545 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002546 return FAIL;
2547 }
2548 ++funcdepth;
2549 return OK;
2550}
2551
2552 void
2553funcdepth_decrement(void)
2554{
2555 --funcdepth;
2556}
2557
2558/*
2559 * Get the current function call depth.
2560 */
2561 int
2562funcdepth_get(void)
2563{
2564 return funcdepth;
2565}
2566
2567/*
2568 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002569 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002570 * times as funcdepth_increment().
2571 */
2572 void
2573funcdepth_restore(int depth)
2574{
2575 funcdepth = depth;
2576}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002577
2578/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002579 * Call a user function.
2580 */
2581 static void
2582call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002583 ufunc_T *fp, // pointer to function
2584 int argcount, // nr of args
2585 typval_T *argvars, // arguments
2586 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002587 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002588 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002590 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002591 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002592 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593 funccall_T *fc;
2594 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002595 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002597 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 int i;
2599 int ai;
2600 int islambda = FALSE;
2601 char_u numbuf[NUMBUFLEN];
2602 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002603 typval_T *tv_to_free[MAX_FUNC_ARGS];
2604 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002605#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002606 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002607#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002608 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002609
Bram Moolenaarb2049902021-01-24 12:53:53 +01002610#ifdef FEAT_PROFILE
2611 CLEAR_FIELD(profile_info);
2612#endif
2613
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002614 // If depth of calling is getting too high, don't execute the function.
2615 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002616 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002617 rettv->v_type = VAR_NUMBER;
2618 rettv->vval.v_number = -1;
2619 return;
2620 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621
Bram Moolenaare38eab22019-12-05 21:50:01 +01002622 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002624 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002625 if (fc == NULL)
2626 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627 fc->caller = current_funccal;
2628 current_funccal = fc;
2629 fc->func = fp;
2630 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002632 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002633 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2634 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002635 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002636 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002637 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002639 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002641#ifdef FEAT_PROFILE
2642 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2643#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002644 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002645#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002646 if (do_profiling == PROF_YES)
2647 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002648#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002649 sticky_cmdmod_flags = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002650 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002651 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002652#ifdef FEAT_PROFILE
2653 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002654 || (caller != NULL && caller->uf_profiling)))
2655 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002656#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 free_funccal(fc);
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002659 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 return;
2661 }
2662
Bram Moolenaar38453522021-11-28 22:00:12 +00002663 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664
2665 /*
2666 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2667 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2668 * each argument variable and saves a lot of time.
2669 */
2670 /*
2671 * Init l: variables.
2672 */
2673 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2674 if (selfdict != NULL)
2675 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002676 // Set l:self to "selfdict". Use "name" to avoid a warning from
2677 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002678 v = &fc->fixvar[fixvar_idx++].var;
2679 name = v->di_key;
2680 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002681 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002682 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2683 v->di_tv.v_type = VAR_DICT;
2684 v->di_tv.v_lock = 0;
2685 v->di_tv.vval.v_dict = selfdict;
2686 ++selfdict->dv_refcount;
2687 }
2688
2689 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002690 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002691 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002692 * Set a:000 to a list with room for the "..." arguments.
2693 */
2694 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002695 if ((fp->uf_flags & FC_NOARGS) == 0)
2696 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002697 (varnumber_T)(argcount >= fp->uf_args.ga_len
2698 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002699 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002700 if ((fp->uf_flags & FC_NOARGS) == 0)
2701 {
2702 // Use "name" to avoid a warning from some compiler that checks the
2703 // destination size.
2704 v = &fc->fixvar[fixvar_idx++].var;
2705 name = v->di_key;
2706 STRCPY(name, "000");
2707 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2708 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2709 v->di_tv.v_type = VAR_LIST;
2710 v->di_tv.v_lock = VAR_FIXED;
2711 v->di_tv.vval.v_list = &fc->l_varlist;
2712 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002713 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2715 fc->l_varlist.lv_lock = VAR_FIXED;
2716
2717 /*
2718 * Set a:firstline to "firstline" and a:lastline to "lastline".
2719 * Set a:name to named arguments.
2720 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002721 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002723 if ((fp->uf_flags & FC_NOARGS) == 0)
2724 {
2725 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002726 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002727 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002728 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002729 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002730 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 {
2732 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002733 typval_T def_rettv;
2734 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735
2736 ai = i - fp->uf_args.ga_len;
2737 if (ai < 0)
2738 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002739 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002740 name = FUNCARG(fp, i);
2741 if (islambda)
2742 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002743
2744 // evaluate named argument default expression
2745 isdefault = ai + fp->uf_def_args.ga_len >= 0
2746 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2747 && argvars[i].vval.v_number == VVAL_NONE));
2748 if (isdefault)
2749 {
2750 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002751
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002752 def_rettv.v_type = VAR_NUMBER;
2753 def_rettv.vval.v_number = -1;
2754
2755 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2756 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002758 {
2759 default_arg_err = 1;
2760 break;
2761 }
2762 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 }
2764 else
2765 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002766 if ((fp->uf_flags & FC_NOARGS) != 0)
2767 // Bail out if no a: arguments used (in lambda).
2768 break;
2769
Bram Moolenaare38eab22019-12-05 21:50:01 +01002770 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771 sprintf((char *)numbuf, "%d", ai + 1);
2772 name = numbuf;
2773 }
2774 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2775 {
2776 v = &fc->fixvar[fixvar_idx++].var;
2777 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002778 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 }
2780 else
2781 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002782 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002783 if (v == NULL)
2784 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002785 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002786 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002788 // Note: the values are copied directly to avoid alloc/free.
2789 // "argvars" must have VAR_FIXED for v_lock.
2790 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791 v->di_tv.v_lock = VAR_FIXED;
2792
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002793 if (isdefault)
2794 // Need to free this later, no matter where it's stored.
2795 tv_to_free[tv_to_free_len++] = &v->di_tv;
2796
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797 if (addlocal)
2798 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002799 // Named arguments should be accessed without the "a:" prefix in
2800 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002801 copy_tv(&v->di_tv, &v->di_tv);
2802 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002804 else
2805 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002806
2807 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2808 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002809 listitem_T *li = &fc->l_listitems[ai];
2810
2811 li->li_tv = argvars[i];
2812 li->li_tv.v_lock = VAR_FIXED;
2813 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 }
2815 }
2816
Bram Moolenaare38eab22019-12-05 21:50:01 +01002817 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002819
2820 if (fp->uf_flags & FC_SANDBOX)
2821 {
2822 using_sandbox = TRUE;
2823 ++sandbox;
2824 }
2825
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002826 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002827 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002828 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002830 ++no_wait_return;
2831 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002833 smsg(_("calling %s"), SOURCING_NAME);
2834 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002836 char_u buf[MSG_BUF_LEN];
2837 char_u numbuf2[NUMBUFLEN];
2838 char_u *tofree;
2839 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002840
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002841 msg_puts("(");
2842 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002844 if (i > 0)
2845 msg_puts(", ");
2846 if (argvars[i].v_type == VAR_NUMBER)
2847 msg_outnum((long)argvars[i].vval.v_number);
2848 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002849 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002850 // Do not want errors such as E724 here.
2851 ++emsg_off;
2852 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2853 --emsg_off;
2854 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002856 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002858 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2859 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002861 msg_puts((char *)s);
2862 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 }
2864 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002866 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002868 msg_puts("\n"); // don't overwrite this either
2869
2870 verbose_leave_scroll();
2871 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872 }
2873#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002874 if (do_profiling == PROF_YES)
2875 profile_may_start_func(&profile_info, fp,
2876 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002877#endif
2878
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002879 // "legacy" does not apply to commands in the function
2880 sticky_cmdmod_flags = 0;
2881
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002882 save_current_sctx = current_sctx;
2883 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884 save_did_emsg = did_emsg;
2885 did_emsg = FALSE;
2886
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002887 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2888 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002889 else if (islambda)
2890 {
2891 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2892
2893 // A Lambda always has the command "return {expr}". It is much faster
2894 // to evaluate {expr} directly.
2895 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002897 --ex_nesting_level;
2898 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002899 else
2900 // call do_cmdline() to execute the lines
2901 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2903
2904 --RedrawingDisabled;
2905
Bram Moolenaare38eab22019-12-05 21:50:01 +01002906 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2908 {
2909 clear_tv(rettv);
2910 rettv->v_type = VAR_NUMBER;
2911 rettv->vval.v_number = -1;
2912 }
2913
2914#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002915 if (do_profiling == PROF_YES)
2916 {
2917 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2918
2919 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2920 profile_may_end_func(&profile_info, fp, caller);
2921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922#endif
2923
Bram Moolenaare38eab22019-12-05 21:50:01 +01002924 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002925 if (p_verbose >= 12)
2926 {
2927 ++no_wait_return;
2928 verbose_enter_scroll();
2929
2930 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002931 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002933 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002934 (long)fc->rettv->vval.v_number);
2935 else
2936 {
2937 char_u buf[MSG_BUF_LEN];
2938 char_u numbuf2[NUMBUFLEN];
2939 char_u *tofree;
2940 char_u *s;
2941
Bram Moolenaare38eab22019-12-05 21:50:01 +01002942 // The value may be very long. Skip the middle part, so that we
2943 // have some idea how it starts and ends. smsg() would always
2944 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 ++emsg_off;
2946 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2947 --emsg_off;
2948 if (s != NULL)
2949 {
2950 if (vim_strsize(s) > MSG_BUF_CLEN)
2951 {
2952 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2953 s = buf;
2954 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002955 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956 vim_free(tofree);
2957 }
2958 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002959 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960
2961 verbose_leave_scroll();
2962 --no_wait_return;
2963 }
2964
Bram Moolenaare31ee862020-01-07 20:59:34 +01002965 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002966 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002967 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968#ifdef FEAT_PROFILE
2969 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002970 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002972 if (using_sandbox)
2973 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002974 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002976 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 {
2978 ++no_wait_return;
2979 verbose_enter_scroll();
2980
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002981 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002982 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983
2984 verbose_leave_scroll();
2985 --no_wait_return;
2986 }
2987
2988 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002989 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002990 for (i = 0; i < tv_to_free_len; ++i)
2991 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002992 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993}
2994
2995/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002996 * Check the argument count for user function "fp".
2997 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2998 */
2999 int
3000check_user_func_argcount(ufunc_T *fp, int argcount)
3001{
3002 int regular_args = fp->uf_args.ga_len;
3003
3004 if (argcount < regular_args - fp->uf_def_args.ga_len)
3005 return FCERR_TOOFEW;
3006 else if (!has_varargs(fp) && argcount > regular_args)
3007 return FCERR_TOOMANY;
3008 return FCERR_UNKNOWN;
3009}
3010
3011/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003013 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 int
3015call_user_func_check(
3016 ufunc_T *fp,
3017 int argcount,
3018 typval_T *argvars,
3019 typval_T *rettv,
3020 funcexe_T *funcexe,
3021 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003022{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003024
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003025#ifdef FEAT_LUA
3026 if (fp->uf_flags & FC_CFUNC)
3027 {
3028 cfunc_T cb = fp->uf_cb;
3029
3030 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3031 }
3032#endif
3033
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003034 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3035 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003036 error = check_user_func_argcount(fp, argcount);
3037 if (error != FCERR_UNKNOWN)
3038 return error;
3039 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 error = FCERR_DICT;
3041 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003042 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 int did_save_redo = FALSE;
3044 save_redo_T save_redo;
3045
3046 /*
3047 * Call the user function.
3048 * Save and restore search patterns, script variables and
3049 * redo buffer.
3050 */
3051 save_search_patterns();
3052 if (!ins_compl_active())
3053 {
3054 saveRedobuff(&save_redo);
3055 did_save_redo = TRUE;
3056 }
3057 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003058 call_user_func(fp, argcount, argvars, rettv, funcexe,
3059 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3061 // Function was unreferenced while being used, free it now.
3062 func_clear_free(fp, FALSE);
3063 if (did_save_redo)
3064 restoreRedobuff(&save_redo);
3065 restore_search_patterns();
3066 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003069}
3070
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003071static funccal_entry_T *funccal_stack = NULL;
3072
3073/*
3074 * Save the current function call pointer, and set it to NULL.
3075 * Used when executing autocommands and for ":source".
3076 */
3077 void
3078save_funccal(funccal_entry_T *entry)
3079{
3080 entry->top_funccal = current_funccal;
3081 entry->next = funccal_stack;
3082 funccal_stack = entry;
3083 current_funccal = NULL;
3084}
3085
3086 void
3087restore_funccal(void)
3088{
3089 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003090 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003091 else
3092 {
3093 current_funccal = funccal_stack->top_funccal;
3094 funccal_stack = funccal_stack->next;
3095 }
3096}
3097
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003098 funccall_T *
3099get_current_funccal(void)
3100{
3101 return current_funccal;
3102}
3103
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003104/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003105 * Return TRUE when currently at the script level:
3106 * - not in a function
3107 * - not executing an autocommand
3108 * Note that when an autocommand sources a script the result is FALSE;
3109 */
3110 int
3111at_script_level(void)
3112{
3113 return current_funccal == NULL && autocmd_match == NULL;
3114}
3115
3116/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003117 * Mark all functions of script "sid" as deleted.
3118 */
3119 void
3120delete_script_functions(int sid)
3121{
3122 hashitem_T *hi;
3123 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003124 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003125 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003126 size_t len;
3127
3128 buf[0] = K_SPECIAL;
3129 buf[1] = KS_EXTRA;
3130 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003131 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003132 len = STRLEN(buf);
3133
Bram Moolenaarce658352020-07-31 23:47:12 +02003134 while (todo > 0)
3135 {
3136 todo = func_hashtab.ht_used;
3137 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3138 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003139 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003140 fp = HI2UF(hi);
3141 if (STRNCMP(fp->uf_name, buf, len) == 0)
3142 {
3143 int changed = func_hashtab.ht_changed;
3144
3145 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003146
3147 if (fp->uf_calls > 0)
3148 {
3149 // Function is executing, don't free it but do remove
3150 // it from the hashtable.
3151 if (func_remove(fp))
3152 fp->uf_refcount--;
3153 }
3154 else
3155 {
3156 func_clear(fp, TRUE);
3157 // When clearing a function another function can be
3158 // cleared as a side effect. When that happens start
3159 // over.
3160 if (changed != func_hashtab.ht_changed)
3161 break;
3162 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003163 }
3164 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003165 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003166 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003167}
3168
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169#if defined(EXITFREE) || defined(PROTO)
3170 void
3171free_all_functions(void)
3172{
3173 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003174 ufunc_T *fp;
3175 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003176 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003177 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178
Bram Moolenaare38eab22019-12-05 21:50:01 +01003179 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003180 while (current_funccal != NULL)
3181 {
3182 clear_tv(current_funccal->rettv);
3183 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003184 if (current_funccal == NULL && funccal_stack != NULL)
3185 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003186 }
3187
Bram Moolenaare38eab22019-12-05 21:50:01 +01003188 // First clear what the functions contain. Since this may lower the
3189 // reference count of a function, it may also free a function and change
3190 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003191 while (todo > 0)
3192 {
3193 todo = func_hashtab.ht_used;
3194 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3195 if (!HASHITEM_EMPTY(hi))
3196 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 // clear the def function index now
3198 fp = HI2UF(hi);
3199 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003200 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201
Bram Moolenaare38eab22019-12-05 21:50:01 +01003202 // Only free functions that are not refcounted, those are
3203 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003204 if (func_name_refcount(fp->uf_name))
3205 ++skipped;
3206 else
3207 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003208 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003209 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003210 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003211 {
3212 skipped = 0;
3213 break;
3214 }
3215 }
3216 --todo;
3217 }
3218 }
3219
Bram Moolenaare38eab22019-12-05 21:50:01 +01003220 // Now actually free the functions. Need to start all over every time,
3221 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003222 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003223 while (func_hashtab.ht_used > skipped)
3224 {
3225 todo = func_hashtab.ht_used;
3226 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003227 if (!HASHITEM_EMPTY(hi))
3228 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003229 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003230 // Only free functions that are not refcounted, those are
3231 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003232 fp = HI2UF(hi);
3233 if (func_name_refcount(fp->uf_name))
3234 ++skipped;
3235 else
3236 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003237 if (func_free(fp, FALSE) == OK)
3238 {
3239 skipped = 0;
3240 break;
3241 }
3242 // did not actually free it
3243 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003244 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003245 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003246 }
3247 if (skipped == 0)
3248 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249
3250 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251}
3252#endif
3253
3254/*
3255 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003256 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3257 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 * "len" is the length of "name", or -1 for NUL terminated.
3259 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261builtin_function(char_u *name, int len)
3262{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003263 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003264
Bram Moolenaara26b9702020-04-18 19:53:28 +02003265 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003267 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3268 {
3269 if (name[i] == AUTOLOAD_CHAR)
3270 return FALSE;
3271 if (!eval_isnamec(name[i]))
3272 {
3273 // "name.something" is not a builtin function
3274 if (name[i] == '.')
3275 return FALSE;
3276 break;
3277 }
3278 }
3279 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003280}
3281
3282 int
3283func_call(
3284 char_u *name,
3285 typval_T *args,
3286 partial_T *partial,
3287 dict_T *selfdict,
3288 typval_T *rettv)
3289{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003290 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 listitem_T *item;
3292 typval_T argv[MAX_FUNC_ARGS + 1];
3293 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294 int r = 0;
3295
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003296 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003297 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298 {
3299 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3300 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003301 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302 break;
3303 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003304 // Make a copy of each argument. This is needed to be able to set
3305 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 copy_tv(&item->li_tv, &argv[argc++]);
3307 }
3308
3309 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003310 {
3311 funcexe_T funcexe;
3312
Bram Moolenaara80faa82020-04-12 19:37:17 +02003313 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003314 funcexe.fe_firstline = curwin->w_cursor.lnum;
3315 funcexe.fe_lastline = curwin->w_cursor.lnum;
3316 funcexe.fe_evaluate = TRUE;
3317 funcexe.fe_partial = partial;
3318 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003319 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3320 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003321
Bram Moolenaare38eab22019-12-05 21:50:01 +01003322 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003323 while (argc > 0)
3324 clear_tv(&argv[--argc]);
3325
3326 return r;
3327}
3328
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003329static int callback_depth = 0;
3330
3331 int
3332get_callback_depth(void)
3333{
3334 return callback_depth;
3335}
3336
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003337/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003338 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003339 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003340 */
3341 int
3342call_callback(
3343 callback_T *callback,
3344 int len, // length of "name" or -1 to use strlen()
3345 typval_T *rettv, // return value goes here
3346 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003347 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003348 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003349{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003350 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003351 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003352
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003353 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3354 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003355 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003356 funcexe.fe_evaluate = TRUE;
3357 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003358 ++callback_depth;
3359 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3360 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003361
3362 // When a :def function was called that uses :try an error would be turned
3363 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003364 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003365 {
3366 need_rethrow = FALSE;
3367 handle_did_throw();
3368 }
3369
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003370 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003371}
3372
3373/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003374 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003375 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003376 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3377 */
3378 varnumber_T
3379call_callback_retnr(
3380 callback_T *callback,
3381 int argcount, // number of "argvars"
3382 typval_T *argvars) // vars for arguments, must have "argcount"
3383 // PLUS ONE elements!
3384{
3385 typval_T rettv;
3386 varnumber_T retval;
3387
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003388 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003389 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003390
3391 retval = tv_get_number_chk(&rettv, NULL);
3392 clear_tv(&rettv);
3393 return retval;
3394}
3395
3396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 * Give an error message for the result of a function.
3398 * Nothing if "error" is FCERR_NONE.
3399 */
3400 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003401user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402{
3403 switch (error)
3404 {
3405 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003406 if (funcexe->fe_found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003407 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003408 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003409 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 break;
3411 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003412 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 break;
3414 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003415 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 break;
3417 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003418 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 break;
3420 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003421 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 break;
3423 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003424 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 break;
3426 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003427 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3428 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 break;
3430 }
3431}
3432
3433/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003435 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003436 * Return FAIL when the function can't be called, OK otherwise.
3437 * Also returns OK when an error was encountered while executing the function.
3438 */
3439 int
3440call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003441 char_u *funcname, // name of the function
3442 int len, // length of "name" or -1 to use strlen()
3443 typval_T *rettv, // return value goes here
3444 int argcount_in, // number of "argvars"
3445 typval_T *argvars_in, // vars for arguments, must have "argcount"
3446 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003447 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003448{
3449 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003450 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003451 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003452 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 char_u fname_buf[FLEN_FIXED + 1];
3454 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003455 char_u *fname = NULL;
3456 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003457 int argcount = argcount_in;
3458 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003459 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003460 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003461 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003463 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003464 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003465 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003466 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003467
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003468 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3469 // even when call_func() returns FAIL.
3470 rettv->v_type = VAR_UNKNOWN;
3471
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003472 if (partial != NULL)
3473 fp = partial->pt_func;
3474 if (fp == NULL)
3475 {
3476 // Make a copy of the name, if it comes from a funcref variable it
3477 // could be changed or deleted in the called function.
3478 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3479 if (name == NULL)
3480 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003482 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003485 if (funcexe->fe_doesrange != NULL)
3486 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487
3488 if (partial != NULL)
3489 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003490 // When the function has a partial with a dict and there is a dict
3491 // argument, use the dict argument. That is backwards compatible.
3492 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003493 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003495 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003496 {
3497 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003498 {
3499 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3500 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003501 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003502 goto theend;
3503 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003505 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 for (i = 0; i < argcount_in; ++i)
3507 argv[i + argv_clear] = argvars_in[i];
3508 argvars = argv;
3509 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003510
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003511 if (funcexe->fe_check_type != NULL
3512 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003513 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003514 // Now funcexe->fe_check_type is missing the added arguments,
3515 // make a copy of the type with the correction.
3516 check_type = *funcexe->fe_check_type;
3517 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003518 check_type.tt_args = check_type_args;
3519 CLEAR_FIELD(check_type_args);
3520 for (i = 0; i < check_type.tt_argcount; ++i)
3521 check_type_args[i + partial->pt_argc] =
3522 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003523 check_type.tt_argcount += partial->pt_argc;
3524 check_type.tt_min_argcount += partial->pt_argc;
3525 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 }
3527 }
3528
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003529 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3530 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003531 {
3532 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003533 if (check_argument_types(funcexe->fe_check_type,
3534 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003535 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003536 error = FCERR_OTHER;
3537 }
3538
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003539 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 {
3541 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003542 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543
Bram Moolenaar333894b2020-08-01 18:53:07 +02003544 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003545 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003546 {
3547 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003549 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550
Bram Moolenaare38eab22019-12-05 21:50:01 +01003551 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003553 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003555 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 {
3557 /*
3558 * User defined function.
3559 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003560 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003561 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003562 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003563 if (fp != NULL && !is_global && in_vim9script()
3564 && func_requires_g_prefix(fp))
3565 // In Vim9 script g: is required to find a global
3566 // non-autoload function.
3567 fp = NULL;
3568 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003569
Bram Moolenaare38eab22019-12-05 21:50:01 +01003570 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003571 if (fp == NULL
3572 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003573 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003574 && !aborting())
3575 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003576 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003577 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003579 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3581 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003582 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003583 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003585 if (fp == NULL)
3586 {
3587 char_u *p = untrans_function_name(rfname);
3588
3589 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003590 // Don't do this if the name starts with "s:".
3591 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003592 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003593 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003594
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003595 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003596 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003597 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003598 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003599 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003600 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003601 argcount = funcexe->fe_argv_func(argcount, argvars,
3602 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003603
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003604 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003605 {
3606 // Method call: base->Method()
3607 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003608 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003609 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003610 argvars = argv;
3611 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003612 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 error = call_user_func_check(fp, argcount, argvars, rettv,
3615 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003616 }
3617 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003618 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003619 {
3620 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003621 * expr->method(): Find the method name in the table, call its
3622 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003623 */
3624 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003625 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003626 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003627 else
3628 {
3629 /*
3630 * Find the function name in the table, call its implementation.
3631 */
3632 error = call_internal_func(fname, argcount, argvars, rettv);
3633 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003634
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 /*
3636 * The function call (or "FuncUndefined" autocommand sequence) might
3637 * have been aborted by an error, an interrupt, or an explicitly thrown
3638 * exception that has not been caught so far. This situation can be
3639 * tested for by calling aborting(). For an error in an internal
3640 * function or for the "E132" error in call_user_func(), however, the
3641 * throw point at which the "force_abort" flag (temporarily reset by
3642 * emsg()) is normally updated has not been reached yet. We need to
3643 * update that flag first to make aborting() reliable.
3644 */
3645 update_force_abort();
3646 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003647 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 ret = OK;
3649
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003650theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 /*
3652 * Report an error unless the argument evaluation or function call has been
3653 * cancelled due to an aborting error, an interrupt, or an exception.
3654 */
3655 if (!aborting())
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003656 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003658 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003659 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003660 clear_tv(&argv[--argv_clear + argv_base]);
3661
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 vim_free(tofree);
3663 vim_free(name);
3664
3665 return ret;
3666}
3667
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003668 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669printable_func_name(ufunc_T *fp)
3670{
3671 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3672}
3673
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003675 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676 */
3677 static void
3678list_func_head(ufunc_T *fp, int indent)
3679{
3680 int j;
3681
3682 msg_start();
3683 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003684 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003685 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003686 msg_puts("def ");
3687 else
3688 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 msg_putchar('(');
3691 for (j = 0; j < fp->uf_args.ga_len; ++j)
3692 {
3693 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003694 msg_puts(", ");
3695 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 if (fp->uf_arg_types != NULL)
3697 {
3698 char *tofree;
3699
3700 msg_puts(": ");
3701 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3702 vim_free(tofree);
3703 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003704 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3705 {
3706 msg_puts(" = ");
3707 msg_puts(((char **)(fp->uf_def_args.ga_data))
3708 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3709 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710 }
3711 if (fp->uf_varargs)
3712 {
3713 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003714 msg_puts(", ");
3715 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 if (fp->uf_va_name != NULL)
3718 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003719 if (!fp->uf_varargs)
3720 {
3721 if (j)
3722 msg_puts(", ");
3723 msg_puts("...");
3724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003726 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 {
3728 char *tofree;
3729
3730 msg_puts(": ");
3731 msg_puts(type_name(fp->uf_va_type, &tofree));
3732 vim_free(tofree);
3733 }
3734 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003735 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003736
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003737 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003738 {
3739 if (fp->uf_ret_type != &t_void)
3740 {
3741 char *tofree;
3742
3743 msg_puts(": ");
3744 msg_puts(type_name(fp->uf_ret_type, &tofree));
3745 vim_free(tofree);
3746 }
3747 }
3748 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003749 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003751 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003753 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003754 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003755 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 msg_clr_eos();
3757 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003758 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759}
3760
3761/*
3762 * Get a function name, translating "<SID>" and "<SNR>".
3763 * Also handles a Funcref in a List or Dictionary.
3764 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003765 * Set "*is_global" to TRUE when the function must be global, unless
3766 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767 * flags:
3768 * TFN_INT: internal function name OK
3769 * TFN_QUIET: be quiet
3770 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003771 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772 * Advances "pp" to just after the function name (if no error).
3773 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003774 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775trans_function_name(
3776 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003777 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003778 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003780 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003781 partial_T **partial, // return: partial of a FuncRef
3782 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783{
3784 char_u *name = NULL;
3785 char_u *start;
3786 char_u *end;
3787 int lead;
3788 char_u sid_buf[20];
3789 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003791 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003793 int vim9script = in_vim9script();
3794 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795
3796 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003797 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798 start = *pp;
3799
Bram Moolenaare38eab22019-12-05 21:50:01 +01003800 // Check for hard coded <SNR>: already translated function ID (from a user
3801 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003802 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3803 && (*pp)[2] == (int)KE_SNR)
3804 {
3805 *pp += 3;
3806 len = get_id_len(pp) + 3;
3807 return vim_strnsave(start, len);
3808 }
3809
Bram Moolenaare38eab22019-12-05 21:50:01 +01003810 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3811 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812 lead = eval_fname_script(start);
3813 if (lead > 2)
3814 start += lead;
3815
Bram Moolenaare38eab22019-12-05 21:50:01 +01003816 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003817 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003819 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00003820 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 {
3822 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003823 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 goto theend;
3825 }
3826 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3827 {
3828 /*
3829 * Report an invalid expression in braces, unless the expression
3830 * evaluation has been cancelled due to an aborting error, an
3831 * interrupt, or an exception.
3832 */
3833 if (!aborting())
3834 {
3835 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003836 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 }
3838 else
3839 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3840 goto theend;
3841 }
3842
3843 if (lv.ll_tv != NULL)
3844 {
3845 if (fdp != NULL)
3846 {
3847 fdp->fd_dict = lv.ll_dict;
3848 fdp->fd_newkey = lv.ll_newkey;
3849 lv.ll_newkey = NULL;
3850 fdp->fd_di = lv.ll_di;
3851 }
3852 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3853 {
3854 name = vim_strsave(lv.ll_tv->vval.v_string);
3855 *pp = end;
3856 }
3857 else if (lv.ll_tv->v_type == VAR_PARTIAL
3858 && lv.ll_tv->vval.v_partial != NULL)
3859 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003860 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861 *pp = end;
3862 if (partial != NULL)
3863 *partial = lv.ll_tv->vval.v_partial;
3864 }
3865 else
3866 {
3867 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3868 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003869 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 else
3871 *pp = end;
3872 name = NULL;
3873 }
3874 goto theend;
3875 }
3876
3877 if (lv.ll_name == NULL)
3878 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003879 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003880 *pp = end;
3881 goto theend;
3882 }
3883
Bram Moolenaare38eab22019-12-05 21:50:01 +01003884 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003885 if (lv.ll_exp_name != NULL)
3886 {
3887 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003888 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003889 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 if (name == lv.ll_exp_name)
3891 name = NULL;
3892 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003893 else if (lv.ll_sid > 0)
3894 {
3895 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
3896 int cc = *lv.ll_name_end;
3897
3898 // function in another script. Prefix <SNR>99_ or the autoload prefix.
3899 *lv.ll_name_end = NUL;
3900 if (si->sn_autoload_prefix != NULL)
3901 {
3902 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
3903 }
3904 else
3905 {
3906 sid_buf[0] = K_SPECIAL;
3907 sid_buf[1] = KS_EXTRA;
3908 sid_buf[2] = (int)KE_SNR;
3909 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00003910 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003911 name = concat_str(sid_buf, lv.ll_name);
3912 }
3913 *lv.ll_name_end = cc;
3914 *pp = end;
3915 goto theend;
3916 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003917 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918 {
3919 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003920 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003921 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922 if (name == *pp)
3923 name = NULL;
3924 }
3925 if (name != NULL)
3926 {
3927 name = vim_strsave(name);
3928 *pp = end;
3929 if (STRNCMP(name, "<SNR>", 5) == 0)
3930 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003931 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003932 name[0] = K_SPECIAL;
3933 name[1] = KS_EXTRA;
3934 name[2] = (int)KE_SNR;
3935 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3936 }
3937 goto theend;
3938 }
3939
3940 if (lv.ll_exp_name != NULL)
3941 {
3942 len = (int)STRLEN(lv.ll_exp_name);
3943 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3944 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3945 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003946 // When there was "s:" already or the name expanded to get a
3947 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 lv.ll_name += 2;
3949 len -= 2;
3950 lead = 2;
3951 }
3952 }
3953 else
3954 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003955 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003957 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003958 if (lv.ll_name[0] == 'g')
3959 {
3960 if (is_global != NULL)
3961 {
3962 *is_global = TRUE;
3963 }
3964 else
3965 {
3966 // dropping "g:" without setting "is_global" won't work in
3967 // Vim9script, put it back later
3968 prefix_g = TRUE;
3969 extra = 2;
3970 }
3971 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974 len = (int)(end - lv.ll_name);
3975 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003976 if (len <= 0)
3977 {
3978 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003979 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003980 goto theend;
3981 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003983 // In Vim9 script a user function is script-local by default, unless it
3984 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00003985 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 /*
3988 * Copy the function name to allocated memory.
3989 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3990 * Accept <SNR>123_name() outside a script.
3991 */
3992 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003993 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00003994 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003996 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00003997 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003998 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar3787f262022-02-07 21:54:01 +00003999 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004000 semsg(_(vim9script
Bram Moolenaar3787f262022-02-07 21:54:01 +00004001 ? e_function_name_must_start_with_capital_str
4002 : e_function_name_must_start_with_capital_or_s_str),
4003 start);
4004 goto theend;
4005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004007 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004008 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 || eval_fname_sid(*pp))
4011 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004013 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004015 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016 goto theend;
4017 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004018 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004019 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 extra = 3 + (int)STRLEN(sid_buf);
4021 else
4022 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023 }
4024 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004025 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004026 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004028 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004029 : e_function_name_must_start_with_capital_or_s_str),
4030 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 goto theend;
4032 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004033 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 {
4035 char_u *cp = vim_strchr(lv.ll_name, ':');
4036
4037 if (cp != NULL && cp < end)
4038 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004039 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 goto theend;
4041 }
4042 }
4043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 if (name != NULL)
4046 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004047 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 {
4049 name[0] = K_SPECIAL;
4050 name[1] = KS_EXTRA;
4051 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004052 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004053 STRCPY(name + 3, sid_buf);
4054 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004055 else if (prefix_g)
4056 {
4057 name[0] = 'g';
4058 name[1] = ':';
4059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4061 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004062 }
4063 *pp = end;
4064
4065theend:
4066 clear_lval(&lv);
4067 return name;
4068}
4069
4070/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004071 * Assuming "name" is the result of trans_function_name() and it was prefixed
4072 * to use the script-local name, return the unmodified name (points into
4073 * "name"). Otherwise return NULL.
4074 * This can be used to first search for a script-local function and fall back
4075 * to the global function if not found.
4076 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004077 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004078untrans_function_name(char_u *name)
4079{
4080 char_u *p;
4081
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004082 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004083 {
4084 p = vim_strchr(name, '_');
4085 if (p != NULL)
4086 return p + 1;
4087 }
4088 return NULL;
4089}
4090
4091/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004092 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4093 * current script ID and returns the expanded function name. The caller should
4094 * free the returned name. If not called from a script context or the function
4095 * name doesn't start with these prefixes, then returns NULL.
4096 * This doesn't check whether the script-local function exists or not.
4097 */
4098 char_u *
4099get_scriptlocal_funcname(char_u *funcname)
4100{
4101 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004102 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004103 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004104 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004105
4106 if (funcname == NULL)
4107 return NULL;
4108
4109 if (STRNCMP(funcname, "s:", 2) != 0
4110 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004111 {
4112 ufunc_T *ufunc;
4113
4114 // The function name does not have a script-local prefix. Try finding
4115 // it when in a Vim9 script and there is no "g:" prefix.
4116 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4117 return NULL;
4118 ufunc = find_func(funcname, FALSE);
4119 if (ufunc == NULL || func_is_global(ufunc)
4120 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4121 return NULL;
4122 ++p;
4123 off = 0;
4124 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004125 else
4126 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004127
4128 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4129 {
4130 emsg(_(e_using_sid_not_in_script_context));
4131 return NULL;
4132 }
4133 // Expand s: prefix into <SNR>nr_<name>
4134 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4135 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004136 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004137 if (newname == NULL)
4138 return NULL;
4139 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004140 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004141
4142 return newname;
4143}
4144
4145/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004146 * Return script-local "fname" with the 3-byte sequence replaced by
4147 * printable <SNR> in allocated memory.
4148 */
4149 char_u *
4150alloc_printable_func_name(char_u *fname)
4151{
4152 char_u *n = alloc(STRLEN(fname + 3) + 6);
4153
4154 if (n != NULL)
4155 {
4156 STRCPY(n, "<SNR>");
4157 STRCPY(n + 5, fname + 3);
4158 }
4159 return n;
4160}
4161
4162/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004163 * Call trans_function_name(), except that a lambda is returned as-is.
4164 * Returns the name in allocated memory.
4165 */
4166 char_u *
4167save_function_name(
4168 char_u **name,
4169 int *is_global,
4170 int skip,
4171 int flags,
4172 funcdict_T *fudi)
4173{
4174 char_u *p = *name;
4175 char_u *saved;
4176
4177 if (STRNCMP(p, "<lambda>", 8) == 0)
4178 {
4179 p += 8;
4180 (void)getdigits(&p);
4181 saved = vim_strnsave(*name, p - *name);
4182 if (fudi != NULL)
4183 CLEAR_POINTER(fudi);
4184 }
4185 else
4186 saved = trans_function_name(&p, is_global, skip,
4187 flags, fudi, NULL, NULL);
4188 *name = p;
4189 return saved;
4190}
4191
4192/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004193 * List functions. When "regmatch" is NULL all of then.
4194 * Otherwise functions matching "regmatch".
4195 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004196 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004197list_functions(regmatch_T *regmatch)
4198{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004199 int changed = func_hashtab.ht_changed;
4200 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004201 hashitem_T *hi;
4202
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004203 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004204 {
4205 if (!HASHITEM_EMPTY(hi))
4206 {
4207 ufunc_T *fp = HI2UF(hi);
4208
4209 --todo;
4210 if ((fp->uf_flags & FC_DEAD) == 0
4211 && (regmatch == NULL
4212 ? !message_filtered(fp->uf_name)
4213 && !func_name_refcount(fp->uf_name)
4214 : !isdigit(*fp->uf_name)
4215 && vim_regexec(regmatch, fp->uf_name, 0)))
4216 {
4217 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004218 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004219 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004220 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004221 return;
4222 }
4223 }
4224 }
4225 }
4226}
4227
4228/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004229 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004230 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4231 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004232 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004233 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004235 ufunc_T *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004236define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 int j;
4239 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004240 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004241 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004242 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 char_u *p;
4244 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004245 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 char_u *line_arg = NULL;
4247 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004249 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 garray_T newlines;
4251 int varargs = FALSE;
4252 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004254 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004255 int fp_allocated = FALSE;
4256 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 dictitem_T *v;
4259 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004260 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004263 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004264 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004265 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266
4267 /*
4268 * ":function" without argument: list functions.
4269 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004270 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271 {
4272 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004273 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004274 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004275 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004276 }
4277
4278 /*
4279 * ":function /pat": list functions matching pattern.
4280 */
4281 if (*eap->arg == '/')
4282 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004283 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 if (!eap->skip)
4285 {
4286 regmatch_T regmatch;
4287
4288 c = *p;
4289 *p = NUL;
4290 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4291 *p = c;
4292 if (regmatch.regprog != NULL)
4293 {
4294 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004295 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004296 vim_regfree(regmatch.regprog);
4297 }
4298 }
4299 if (*p == '/')
4300 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004301 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004302 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303 }
4304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 ga_init(&newargs);
4306 ga_init(&argtypes);
4307 ga_init(&default_args);
4308
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 /*
4310 * Get the function name. There are these situations:
4311 * func normal function name
4312 * "name" == func, "fudi.fd_dict" == NULL
4313 * dict.func new dictionary entry
4314 * "name" == NULL, "fudi.fd_dict" set,
4315 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4316 * dict.func existing dict entry with a Funcref
4317 * "name" == func, "fudi.fd_dict" set,
4318 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4319 * dict.func existing dict entry that's not a Funcref
4320 * "name" == NULL, "fudi.fd_dict" set,
4321 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4322 * s:func script-local function name
4323 * g:func global function name, same as "func"
4324 */
4325 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004326 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004328 // nested function, argument is (args).
4329 paren = TRUE;
4330 CLEAR_FIELD(fudi);
4331 }
4332 else
4333 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004334 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004335 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004336 if (p[0] == 's' && p[1] == ':')
4337 {
4338 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4339 return NULL;
4340 }
4341 p = to_name_end(p, TRUE);
4342 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4343 {
4344 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4345 eap->arg);
4346 return NULL;
4347 }
4348 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004349 }
4350
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004351 name = save_function_name(&p, &is_global, eap->skip,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004352 TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004353 paren = (vim_strchr(p, '(') != NULL);
4354 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004356 /*
4357 * Return on an invalid expression in braces, unless the expression
4358 * evaluation has been cancelled due to an aborting error, an
4359 * interrupt, or an exception.
4360 */
4361 if (!aborting())
4362 {
4363 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004364 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004365 vim_free(fudi.fd_newkey);
4366 return NULL;
4367 }
4368 else
4369 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004371
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004372 // For "export def FuncName()" in an autoload script the function name
4373 // is stored with the legacy autoload name "dir#script#FuncName" so
4374 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004375 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004376 {
4377 char_u *prefixed = may_prefix_autoload(name);
4378
4379 if (prefixed != NULL && prefixed != name)
4380 {
4381 vim_free(name);
4382 name = prefixed;
4383 }
4384 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004385 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004386 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004387 {
4388 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4389 goto ret_free;
4390 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 }
4392
Bram Moolenaare38eab22019-12-05 21:50:01 +01004393 // An error in a function call during evaluation of an expression in magic
4394 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004395 saved_did_emsg = did_emsg;
4396 did_emsg = FALSE;
4397
4398 /*
4399 * ":function func" with only function name: list function.
4400 */
4401 if (!paren)
4402 {
4403 if (!ends_excmd(*skipwhite(p)))
4404 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004405 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004406 goto ret_free;
4407 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004408 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 if (eap->nextcmd != NULL)
4410 *p = NUL;
4411 if (!eap->skip && !got_int)
4412 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004413 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004414 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4415 {
4416 char_u *up = untrans_function_name(name);
4417
4418 // With Vim9 script the name was made script-local, if not
4419 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004420 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004421 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004422 }
4423
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 if (fp != NULL)
4425 {
4426 list_func_head(fp, TRUE);
4427 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4428 {
4429 if (FUNCLINE(fp, j) == NULL)
4430 continue;
4431 msg_putchar('\n');
4432 msg_outnum((long)(j + 1));
4433 if (j < 9)
4434 msg_putchar(' ');
4435 if (j < 99)
4436 msg_putchar(' ');
4437 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004438 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 ui_breakcheck();
4440 }
4441 if (!got_int)
4442 {
4443 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004444 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 msg_puts(" enddef");
4446 else
4447 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004448 }
4449 }
4450 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004451 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 }
4453 goto ret_free;
4454 }
4455
4456 /*
4457 * ":function name(arg1, arg2)" Define function.
4458 */
4459 p = skipwhite(p);
4460 if (*p != '(')
4461 {
4462 if (!eap->skip)
4463 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004464 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465 goto ret_free;
4466 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004467 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 if (vim_strchr(p, '(') != NULL)
4469 p = vim_strchr(p, '(');
4470 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004472 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4473 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004474 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004475 goto ret_free;
4476 }
4477
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004478 // In Vim9 script only global functions can be redefined.
4479 if (vim9script && eap->forceit && !is_global)
4480 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004481 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004482 goto ret_free;
4483 }
4484
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004485 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486
Bram Moolenaar04b12692020-05-04 23:24:44 +02004487 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004489 // Check the name of the function. Unless it's a dictionary function
4490 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491 if (name != NULL)
4492 arg = name;
4493 else
4494 arg = fudi.fd_newkey;
4495 if (arg != NULL && (fudi.fd_di == NULL
4496 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4497 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4498 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004499 char_u *name_base = arg;
4500 int i;
4501
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004502 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004503 {
4504 name_base = vim_strchr(arg, '_');
4505 if (name_base == NULL)
4506 name_base = arg + 3;
4507 else
4508 ++name_base;
4509 }
4510 for (i = 0; name_base[i] != NUL && (i == 0
4511 ? eval_isnamec1(name_base[i])
4512 : eval_isnamec(name_base[i])); ++i)
4513 ;
4514 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004515 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004516
4517 // In Vim9 script a function cannot have the same name as a
4518 // variable.
4519 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004520 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4521 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004522 + EVAL_VAR_NO_FUNC) == OK)
4523 {
4524 semsg(_(e_redefining_script_item_str), name_base);
4525 goto ret_free;
4526 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004528 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004529 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004530 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004531 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004532 goto ret_free;
4533 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004534 }
4535
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004536 // This may get more lines and make the pointers into the first line
4537 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004538 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004540 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004541 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004542 eap, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004544 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004549 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004551 if (*p != ':')
4552 {
4553 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4554 p = skipwhite(p);
4555 }
4556 else if (!IS_WHITE_OR_NUL(p[1]))
4557 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004559 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004561 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004562 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004563 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004567 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004568 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004569 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004570 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004571 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004572 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 else
4575 // find extra arguments "range", "dict", "abort" and "closure"
4576 for (;;)
4577 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004578 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 p = skipwhite(p);
4580 if (STRNCMP(p, "range", 5) == 0)
4581 {
4582 flags |= FC_RANGE;
4583 p += 5;
4584 }
4585 else if (STRNCMP(p, "dict", 4) == 0)
4586 {
4587 flags |= FC_DICT;
4588 p += 4;
4589 }
4590 else if (STRNCMP(p, "abort", 5) == 0)
4591 {
4592 flags |= FC_ABORT;
4593 p += 5;
4594 }
4595 else if (STRNCMP(p, "closure", 7) == 0)
4596 {
4597 flags |= FC_CLOSURE;
4598 p += 7;
4599 if (current_funccal == NULL)
4600 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004601 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 name == NULL ? (char_u *)"" : name);
4603 goto erret;
4604 }
4605 }
4606 else
4607 break;
4608 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609
Bram Moolenaare38eab22019-12-05 21:50:01 +01004610 // When there is a line break use what follows for the function body.
4611 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004612 if (*p == '\n')
4613 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004614 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004615 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4616 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004617 && !(VIM_ISWHITE(*whitep) && *p == '#'
4618 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004619 && !eap->skip
4620 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004621 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622
4623 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4625 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626 */
4627 if (KeyTyped)
4628 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004629 // Check if the function already exists, don't let the user type the
4630 // whole function before telling him it doesn't work! For a script we
4631 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 if (!eap->skip && !eap->forceit)
4633 {
4634 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004635 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004636 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004637 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004638 }
4639
4640 if (!eap->skip && did_emsg)
4641 goto erret;
4642
Bram Moolenaare38eab22019-12-05 21:50:01 +01004643 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004644 cmdline_row = msg_row;
4645 }
4646
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004647 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004648 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004649
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004650 // Do not define the function when getting the body fails and when
4651 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004652 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004653 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004654 goto erret;
4655
4656 /*
4657 * If there are no errors, add the function
4658 */
4659 if (fudi.fd_dict == NULL)
4660 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004662 char_u *find_name = name;
4663 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004664 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004666 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004667 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004668 var_conflict = TRUE;
4669
4670 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4671 {
4672 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4673
4674 if (si->sn_autoload_prefix != NULL)
4675 {
4676 if (is_export)
4677 {
4678 find_name = name + STRLEN(si->sn_autoload_prefix);
4679 v = find_var(find_name, &ht, TRUE);
4680 if (v != NULL)
4681 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004682 // Only check if the function already exists in the script,
4683 // global functions can be shadowed.
4684 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004685 }
4686 else
4687 {
4688 char_u *prefixed = may_prefix_autoload(name);
4689
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004690 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004691 {
4692 v = find_var(prefixed, &ht, TRUE);
4693 if (v != NULL)
4694 var_conflict = TRUE;
4695 vim_free(prefixed);
4696 }
4697 }
4698 }
4699 }
4700 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004702 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 goto erret;
4704 }
4705
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004706 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004707 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004709 char_u *uname = untrans_function_name(name);
4710
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004711 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004712 }
4713
4714 if (fp != NULL || import != NULL)
4715 {
4716 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004718 // Function can be replaced with "function!" and when sourcing the
4719 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004720 // A name that is used by an import can not be overruled.
4721 if (import != NULL
4722 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004723 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004724 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004725 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004726 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004727 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004728 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004729 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004730 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004731 goto erret;
4732 }
4733 if (fp->uf_calls > 0)
4734 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004735 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004736 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 goto erret;
4738 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004739 if (fp->uf_refcount > 1)
4740 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004741 // This function is referenced somewhere, don't redefine it but
4742 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004743 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004744 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004745 fp = NULL;
4746 overwrite = TRUE;
4747 }
4748 else
4749 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004750 char_u *exp_name = fp->uf_name_exp;
4751
4752 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004753 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004754 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004755 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004756 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004758#ifdef FEAT_PROFILE
4759 fp->uf_profiling = FALSE;
4760 fp->uf_prof_initialized = FALSE;
4761#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004762 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004764 }
4765 }
4766 else
4767 {
4768 char numbuf[20];
4769
4770 fp = NULL;
4771 if (fudi.fd_newkey == NULL && !eap->forceit)
4772 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004773 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004774 goto erret;
4775 }
4776 if (fudi.fd_di == NULL)
4777 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004778 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004779 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004780 goto erret;
4781 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004782 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004783 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 goto erret;
4785
Bram Moolenaare38eab22019-12-05 21:50:01 +01004786 // Give the function a sequential number. Can only be used with a
4787 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 vim_free(name);
4789 sprintf(numbuf, "%d", ++func_nr);
4790 name = vim_strsave((char_u *)numbuf);
4791 if (name == NULL)
4792 goto erret;
4793 }
4794
4795 if (fp == NULL)
4796 {
4797 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4798 {
4799 int slen, plen;
4800 char_u *scriptname;
4801
Bram Moolenaare38eab22019-12-05 21:50:01 +01004802 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004804 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004805 {
4806 scriptname = autoload_name(name);
4807 if (scriptname != NULL)
4808 {
4809 p = vim_strchr(scriptname, '/');
4810 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004811 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004812 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004813 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004814 j = OK;
4815 vim_free(scriptname);
4816 }
4817 }
4818 if (j == FAIL)
4819 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004820 linenr_T save_lnum = SOURCING_LNUM;
4821
4822 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004823 semsg(_(e_function_name_does_not_match_script_file_name_str),
4824 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004825 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004826 goto erret;
4827 }
4828 }
4829
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004830 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831 if (fp == NULL)
4832 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004833 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834
4835 if (fudi.fd_dict != NULL)
4836 {
4837 if (fudi.fd_di == NULL)
4838 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004839 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4841 if (fudi.fd_di == NULL)
4842 {
4843 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004844 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 goto erret;
4846 }
4847 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4848 {
4849 vim_free(fudi.fd_di);
4850 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004851 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004852 goto erret;
4853 }
4854 }
4855 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004856 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004857 clear_tv(&fudi.fd_di->di_tv);
4858 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860
Bram Moolenaare38eab22019-12-05 21:50:01 +01004861 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 flags |= FC_DICT;
4863 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 }
4865 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004866 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004868 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
4870 if (eap->cmdidx == CMD_def)
4871 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004872 int lnum_save = SOURCING_LNUM;
4873 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004874
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004875 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004876
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004877 // error messages are for the first function line
4878 SOURCING_LNUM = sourcing_lnum_top;
4879
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004880 // The function may use script variables from the context.
4881 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004882
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004883 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004885 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004886 free_fp = fp_allocated;
4887 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004889 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890
4891 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004892 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004894 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004895 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004896 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004898 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004900 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004901 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004903 if (fp_allocated)
4904 {
4905 // insert the new function in the function list
4906 set_ufunc_name(fp, name);
4907 if (overwrite)
4908 {
4909 hi = hash_find(&func_hashtab, name);
4910 hi->hi_key = UF2HIKEY(fp);
4911 }
4912 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4913 {
4914 free_fp = TRUE;
4915 goto erret;
4916 }
4917 fp->uf_refcount = 1;
4918 }
4919
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004921 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004922 if ((flags & FC_CLOSURE) != 0)
4923 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004924 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004925 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004926 }
4927 else
4928 fp->uf_scoped = NULL;
4929
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004930#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004931 if (prof_def_func())
4932 func_do_profile(fp);
4933#endif
4934 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004935 if (sandbox)
4936 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004937 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004938 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 fp->uf_flags = flags;
4940 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004941 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004942 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004943 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004944 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 if (is_export)
4946 {
4947 fp->uf_flags |= FC_EXPORT;
4948 // let ex_export() know the export worked.
4949 is_export = FALSE;
4950 }
4951
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004952 if (eap->cmdidx == CMD_def)
4953 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004954 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4955 // :func does not use Vim9 script syntax, even in a Vim9 script file
4956 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004957
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 goto ret_free;
4959
4960erret:
4961 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004962 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004963 if (fp != NULL)
4964 {
4965 ga_init(&fp->uf_args);
4966 ga_init(&fp->uf_def_args);
4967 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968errret_2:
4969 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004970 if (fp != NULL)
4971 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004972 if (free_fp)
4973 {
4974 vim_free(fp);
4975 fp = NULL;
4976 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004978 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004980 if (name != name_arg)
4981 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004982 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004983 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004984
4985 return fp;
4986}
4987
4988/*
4989 * ":function"
4990 */
4991 void
4992ex_function(exarg_T *eap)
4993{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004994 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004995
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004996 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4997 (void)define_function(eap, NULL, &lines_to_free);
4998 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004999}
5000
5001/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005002 * Find a function by name, including "<lambda>123".
5003 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005004 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005005 * Return NULL if not found.
5006 */
5007 ufunc_T *
5008find_func_by_name(char_u *name, compiletype_T *compile_type)
5009{
5010 char_u *arg = name;
5011 char_u *fname;
5012 ufunc_T *ufunc;
5013 int is_global = FALSE;
5014
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005015 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5016 {
5017 *compile_type = CT_PROFILE;
5018 arg = skipwhite(arg + 7);
5019 }
5020 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5021 {
5022 *compile_type = CT_DEBUG;
5023 arg = skipwhite(arg + 5);
5024 }
5025
5026 if (STRNCMP(arg, "<lambda>", 8) == 0)
5027 {
5028 arg += 8;
5029 (void)getdigits(&arg);
5030 fname = vim_strnsave(name, arg - name);
5031 }
5032 else
5033 fname = trans_function_name(&arg, &is_global, FALSE,
5034 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5035 if (fname == NULL)
5036 {
5037 semsg(_(e_invalid_argument_str), name);
5038 return NULL;
5039 }
5040 if (!ends_excmd2(name, arg))
5041 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005042 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005043 emsg(ex_errmsg(e_trailing_characters_str, arg));
5044 return NULL;
5045 }
5046
5047 ufunc = find_func(fname, is_global);
5048 if (ufunc == NULL)
5049 {
5050 char_u *p = untrans_function_name(fname);
5051
5052 if (p != NULL)
5053 // Try again without making it script-local.
5054 ufunc = find_func(p, FALSE);
5055 }
5056 vim_free(fname);
5057 if (ufunc == NULL)
5058 semsg(_(e_cannot_find_function_str), name);
5059 return ufunc;
5060}
5061
5062/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005063 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005064 * be compiled or the one specified by the argument.
5065 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005066 */
5067 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005068ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005069{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005070 ufunc_T *ufunc;
5071
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005072 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005073 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005074 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005075
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005076 ufunc = find_func_by_name(eap->arg, &compile_type);
5077 if (ufunc != NULL)
5078 {
5079 if (func_needs_compiling(ufunc, compile_type))
5080 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5081 else
5082 smsg(_("Function %s does not need compiling"), eap->arg);
5083 }
5084 }
5085 else
5086 {
5087 long todo = (long)func_hashtab.ht_used;
5088 int changed = func_hashtab.ht_changed;
5089 hashitem_T *hi;
5090
5091 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5092 {
5093 if (!HASHITEM_EMPTY(hi))
5094 {
5095 --todo;
5096 ufunc = HI2UF(hi);
5097 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5098 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5099 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005100 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005101 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5102
5103 if (func_hashtab.ht_changed != changed)
5104 {
5105 // a function has been added or removed, need to start
5106 // over
5107 todo = (long)func_hashtab.ht_used;
5108 changed = func_hashtab.ht_changed;
5109 hi = func_hashtab.ht_array;
5110 --hi;
5111 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005112 }
5113 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005114 }
5115 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116}
5117
5118/*
5119 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5120 * Return 2 if "p" starts with "s:".
5121 * Return 0 otherwise.
5122 */
5123 int
5124eval_fname_script(char_u *p)
5125{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005126 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5127 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5129 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5130 return 5;
5131 if (p[0] == 's' && p[1] == ':')
5132 return 2;
5133 return 0;
5134}
5135
5136 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005137translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138{
5139 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005140 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005141 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142}
5143
5144/*
5145 * Return TRUE when "ufunc" has old-style "..." varargs
5146 * or named varargs "...name: type".
5147 */
5148 int
5149has_varargs(ufunc_T *ufunc)
5150{
5151 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152}
5153
5154/*
5155 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005156 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157 */
5158 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005159function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005160{
5161 char_u *nm = name;
5162 char_u *p;
5163 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005164 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005165 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005166
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005167 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5168 if (no_deref)
5169 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005170 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171 nm = skipwhite(nm);
5172
Bram Moolenaare38eab22019-12-05 21:50:01 +01005173 // Only accept "funcname", "funcname ", "funcname (..." and
5174 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005176 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005177 vim_free(p);
5178 return n;
5179}
5180
Bram Moolenaar113e1072019-01-20 15:30:40 +01005181#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 char_u *
5183get_expanded_name(char_u *name, int check)
5184{
5185 char_u *nm = name;
5186 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005187 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005189 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005190 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005191
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005192 if (p != NULL && *nm == NUL
5193 && (!check || translated_function_exists(p, is_global)))
5194 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005195
5196 vim_free(p);
5197 return NULL;
5198}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005199#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201/*
5202 * Function given to ExpandGeneric() to obtain the list of user defined
5203 * function names.
5204 */
5205 char_u *
5206get_user_func_name(expand_T *xp, int idx)
5207{
5208 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005209 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005210 static hashitem_T *hi;
5211 ufunc_T *fp;
5212
5213 if (idx == 0)
5214 {
5215 done = 0;
5216 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005217 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005218 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005219 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005220 {
5221 if (done++ > 0)
5222 ++hi;
5223 while (HASHITEM_EMPTY(hi))
5224 ++hi;
5225 fp = HI2UF(hi);
5226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 // don't show dead, dict and lambda functions
5228 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005229 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231
5232 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005233 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005234
5235 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005236 if (xp->xp_context != EXPAND_USER_FUNC
5237 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005238 {
5239 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005241 STRCAT(IObuff, ")");
5242 }
5243 return IObuff;
5244 }
5245 return NULL;
5246}
5247
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005248/*
5249 * ":delfunction {name}"
5250 */
5251 void
5252ex_delfunction(exarg_T *eap)
5253{
5254 ufunc_T *fp = NULL;
5255 char_u *p;
5256 char_u *name;
5257 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005258 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259
5260 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005261 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5262 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 vim_free(fudi.fd_newkey);
5264 if (name == NULL)
5265 {
5266 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005267 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005268 return;
5269 }
5270 if (!ends_excmd(*skipwhite(p)))
5271 {
5272 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005273 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 return;
5275 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005276 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277 if (eap->nextcmd != NULL)
5278 *p = NUL;
5279
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005280 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005281 {
5282 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005283 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005284 vim_free(name);
5285 return;
5286 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005288 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 vim_free(name);
5290
5291 if (!eap->skip)
5292 {
5293 if (fp == NULL)
5294 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005295 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005296 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005297 return;
5298 }
5299 if (fp->uf_calls > 0)
5300 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005301 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 return;
5303 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005304 if (fp->uf_flags & FC_VIM9)
5305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005306 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005307 return;
5308 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005309
5310 if (fudi.fd_dict != NULL)
5311 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005312 // Delete the dict item that refers to the function, it will
5313 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005314 dictitem_remove(fudi.fd_dict, fudi.fd_di);
5315 }
5316 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005317 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005318 // A normal function (not a numbered function or lambda) has a
5319 // refcount of 1 for the entry in the hashtable. When deleting
5320 // it and the refcount is more than one, it should be kept.
5321 // A numbered function and lambda should be kept if the refcount is
5322 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005323 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005324 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005325 // Function is still referenced somewhere. Don't free it but
5326 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005327 if (func_remove(fp))
5328 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005329 }
5330 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005331 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005332 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333 }
5334}
5335
5336/*
5337 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005338 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005339 */
5340 void
5341func_unref(char_u *name)
5342{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005343 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005344
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005345 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005346 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005347 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005348 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005351 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005353 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005355 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005356}
5357
5358/*
5359 * Unreference a Function: decrement the reference count and free it when it
5360 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005361 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005362 */
5363 void
5364func_ptr_unref(ufunc_T *fp)
5365{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005366 if (fp != NULL && (--fp->uf_refcount <= 0
5367 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5368 && fp->uf_partial->pt_refcount <= 1
5369 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005370 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005371 // Only delete it when it's not being used. Otherwise it's done
5372 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005373 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005374 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005375 }
5376}
5377
5378/*
5379 * Count a reference to a Function.
5380 */
5381 void
5382func_ref(char_u *name)
5383{
5384 ufunc_T *fp;
5385
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005386 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005387 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005388 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005389 if (fp != NULL)
5390 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005391 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005392 // Only give an error for a numbered function.
5393 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005394 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005395}
5396
5397/*
5398 * Count a reference to a Function.
5399 */
5400 void
5401func_ptr_ref(ufunc_T *fp)
5402{
5403 if (fp != NULL)
5404 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405}
5406
5407/*
5408 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5409 * referenced from anywhere that is in use.
5410 */
5411 static int
5412can_free_funccal(funccall_T *fc, int copyID)
5413{
5414 return (fc->l_varlist.lv_copyID != copyID
5415 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005416 && fc->l_avars.dv_copyID != copyID
5417 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005418}
5419
5420/*
5421 * ":return [expr]"
5422 */
5423 void
5424ex_return(exarg_T *eap)
5425{
5426 char_u *arg = eap->arg;
5427 typval_T rettv;
5428 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005429 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005430
5431 if (current_funccal == NULL)
5432 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005433 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 return;
5435 }
5436
Bram Moolenaar844fb642021-10-23 13:32:30 +01005437 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005438 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5439
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005440 if (eap->skip)
5441 ++emsg_skip;
5442
5443 eap->nextcmd = NULL;
5444 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005445 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005446 {
5447 if (!eap->skip)
5448 returning = do_return(eap, FALSE, TRUE, &rettv);
5449 else
5450 clear_tv(&rettv);
5451 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005452 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005453 else if (!eap->skip)
5454 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005455 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005456 update_force_abort();
5457
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 /*
5459 * Return unless the expression evaluation has been cancelled due to an
5460 * aborting error, an interrupt, or an exception.
5461 */
5462 if (!aborting())
5463 returning = do_return(eap, FALSE, TRUE, NULL);
5464 }
5465
Bram Moolenaare38eab22019-12-05 21:50:01 +01005466 // When skipping or the return gets pending, advance to the next command
5467 // in this line (!returning). Otherwise, ignore the rest of the line.
5468 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005469 if (returning)
5470 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005471 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005472 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005473
5474 if (eap->skip)
5475 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005476 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005477}
5478
5479/*
5480 * ":1,25call func(arg1, arg2)" function call.
5481 */
5482 void
5483ex_call(exarg_T *eap)
5484{
5485 char_u *arg = eap->arg;
5486 char_u *startarg;
5487 char_u *name;
5488 char_u *tofree;
5489 int len;
5490 typval_T rettv;
5491 linenr_T lnum;
5492 int doesrange;
5493 int failed = FALSE;
5494 funcdict_T fudi;
5495 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005496 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005497 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005498 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005499 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005500
Bram Moolenaare6b53242020-07-01 17:28:33 +02005501 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005502 if (eap->skip)
5503 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005504 // trans_function_name() doesn't work well when skipping, use eval0()
5505 // instead to skip to any following command, e.g. for:
5506 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005507 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005508 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509 clear_tv(&rettv);
5510 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005511 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005512 return;
5513 }
5514
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005515 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005516 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005517 if (fudi.fd_newkey != NULL)
5518 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005519 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005520 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005521 vim_free(fudi.fd_newkey);
5522 }
5523 if (tofree == NULL)
5524 return;
5525
Bram Moolenaare38eab22019-12-05 21:50:01 +01005526 // Increase refcount on dictionary, it could get deleted when evaluating
5527 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005528 if (fudi.fd_dict != NULL)
5529 ++fudi.fd_dict->dv_refcount;
5530
Bram Moolenaare38eab22019-12-05 21:50:01 +01005531 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5532 // contents. For VAR_PARTIAL get its partial, unless we already have one
5533 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005535 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005536 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00005537 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005538
Bram Moolenaare38eab22019-12-05 21:50:01 +01005539 // Skip white space to allow ":call func ()". Not good, but required for
5540 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005541 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005542 if (*startarg != '(')
5543 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005544 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005545 goto end;
5546 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00005547 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005548 {
5549 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5550 goto end;
5551 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005552
5553 /*
5554 * When skipping, evaluate the function once, to find the end of the
5555 * arguments.
5556 * When the function takes a range, this is discovered after the first
5557 * call, and the loop is broken.
5558 */
5559 if (eap->skip)
5560 {
5561 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005562 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005563 }
5564 else
5565 lnum = eap->line1;
5566 for ( ; lnum <= eap->line2; ++lnum)
5567 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005568 funcexe_T funcexe;
5569
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005570 if (!eap->skip && eap->addr_count > 0)
5571 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005572 if (lnum > curbuf->b_ml.ml_line_count)
5573 {
5574 // If the function deleted lines or switched to another buffer
5575 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005576 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005577 break;
5578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005579 curwin->w_cursor.lnum = lnum;
5580 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005581 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005582 }
5583 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005584
Bram Moolenaara80faa82020-04-12 19:37:17 +02005585 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005586 funcexe.fe_firstline = eap->line1;
5587 funcexe.fe_lastline = eap->line2;
5588 funcexe.fe_doesrange = &doesrange;
5589 funcexe.fe_evaluate = !eap->skip;
5590 funcexe.fe_partial = partial;
5591 funcexe.fe_selfdict = fudi.fd_dict;
5592 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005593 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005594 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005595 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005596 {
5597 failed = TRUE;
5598 break;
5599 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005600 if (has_watchexpr())
5601 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005602
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005603 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005604 if (handle_subscript(&arg, NULL, &rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005605 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005606 {
5607 failed = TRUE;
5608 break;
5609 }
5610
5611 clear_tv(&rettv);
5612 if (doesrange || eap->skip)
5613 break;
5614
Bram Moolenaare38eab22019-12-05 21:50:01 +01005615 // Stop when immediately aborting on error, or when an interrupt
5616 // occurred or an exception was thrown but not caught.
5617 // get_func_tv() returned OK, so that the check for trailing
5618 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005619 if (aborting())
5620 break;
5621 }
5622 if (eap->skip)
5623 --emsg_skip;
5624
Bram Moolenaar1d341892021-09-18 15:25:52 +02005625 // When inside :try we need to check for following "| catch" or "| endtry".
5626 // Not when there was an error, but do check if an exception was thrown.
5627 if ((!aborting() || did_throw)
5628 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005629 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005630 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005631 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005632 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005633 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005634 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005635 {
5636 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005637 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005638 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005639 }
5640 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005641 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005642 }
Bram Moolenaara929c922022-04-18 15:21:17 +01005643 // Must be after using "arg", it may point into memory cleared here.
5644 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005645
5646end:
5647 dict_unref(fudi.fd_dict);
5648 vim_free(tofree);
5649}
5650
5651/*
5652 * Return from a function. Possibly makes the return pending. Also called
5653 * for a pending return at the ":endtry" or after returning from an extra
5654 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5655 * when called due to a ":return" command. "rettv" may point to a typval_T
5656 * with the return rettv. Returns TRUE when the return can be carried out,
5657 * FALSE when the return gets pending.
5658 */
5659 int
5660do_return(
5661 exarg_T *eap,
5662 int reanimate,
5663 int is_cmd,
5664 void *rettv)
5665{
5666 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005667 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668
5669 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005670 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 current_funccal->returned = FALSE;
5672
5673 /*
5674 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5675 * not in its finally clause (which then is to be executed next) is found.
5676 * In this case, make the ":return" pending for execution at the ":endtry".
5677 * Otherwise, return normally.
5678 */
5679 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5680 if (idx >= 0)
5681 {
5682 cstack->cs_pending[idx] = CSTP_RETURN;
5683
5684 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005685 // A pending return again gets pending. "rettv" points to an
5686 // allocated variable with the rettv of the original ":return"'s
5687 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 cstack->cs_rettv[idx] = rettv;
5689 else
5690 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005691 // When undoing a return in order to make it pending, get the stored
5692 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693 if (reanimate)
5694 rettv = current_funccal->rettv;
5695
5696 if (rettv != NULL)
5697 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005698 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5700 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5701 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005702 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005703 }
5704 else
5705 cstack->cs_rettv[idx] = NULL;
5706
5707 if (reanimate)
5708 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005709 // The pending return value could be overwritten by a ":return"
5710 // without argument in a finally clause; reset the default
5711 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712 current_funccal->rettv->v_type = VAR_NUMBER;
5713 current_funccal->rettv->vval.v_number = 0;
5714 }
5715 }
5716 report_make_pending(CSTP_RETURN, rettv);
5717 }
5718 else
5719 {
5720 current_funccal->returned = TRUE;
5721
Bram Moolenaare38eab22019-12-05 21:50:01 +01005722 // If the return is carried out now, store the return value. For
5723 // a return immediately after reanimation, the value is already
5724 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005725 if (!reanimate && rettv != NULL)
5726 {
5727 clear_tv(current_funccal->rettv);
5728 *current_funccal->rettv = *(typval_T *)rettv;
5729 if (!is_cmd)
5730 vim_free(rettv);
5731 }
5732 }
5733
5734 return idx < 0;
5735}
5736
5737/*
5738 * Free the variable with a pending return value.
5739 */
5740 void
5741discard_pending_return(void *rettv)
5742{
5743 free_tv((typval_T *)rettv);
5744}
5745
5746/*
5747 * Generate a return command for producing the value of "rettv". The result
5748 * is an allocated string. Used by report_pending() for verbose messages.
5749 */
5750 char_u *
5751get_return_cmd(void *rettv)
5752{
5753 char_u *s = NULL;
5754 char_u *tofree = NULL;
5755 char_u numbuf[NUMBUFLEN];
5756
5757 if (rettv != NULL)
5758 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5759 if (s == NULL)
5760 s = (char_u *)"";
5761
5762 STRCPY(IObuff, ":return ");
5763 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5764 if (STRLEN(s) + 8 >= IOSIZE)
5765 STRCPY(IObuff + IOSIZE - 4, "...");
5766 vim_free(tofree);
5767 return vim_strsave(IObuff);
5768}
5769
5770/*
5771 * Get next function line.
5772 * Called by do_cmdline() to get the next line.
5773 * Returns allocated string, or NULL for end of function.
5774 */
5775 char_u *
5776get_func_line(
5777 int c UNUSED,
5778 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005779 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005780 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005781{
5782 funccall_T *fcp = (funccall_T *)cookie;
5783 ufunc_T *fp = fcp->func;
5784 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005785 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005786
Bram Moolenaare38eab22019-12-05 21:50:01 +01005787 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005788 if (fcp->dbg_tick != debug_tick)
5789 {
5790 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005791 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005792 fcp->dbg_tick = debug_tick;
5793 }
5794#ifdef FEAT_PROFILE
5795 if (do_profiling == PROF_YES)
5796 func_line_end(cookie);
5797#endif
5798
5799 gap = &fp->uf_lines;
5800 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5801 || fcp->returned)
5802 retval = NULL;
5803 else
5804 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005805 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005806 while (fcp->linenr < gap->ga_len
5807 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5808 ++fcp->linenr;
5809 if (fcp->linenr >= gap->ga_len)
5810 retval = NULL;
5811 else
5812 {
5813 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005814 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815#ifdef FEAT_PROFILE
5816 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005817 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818#endif
5819 }
5820 }
5821
Bram Moolenaare38eab22019-12-05 21:50:01 +01005822 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005823 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005825 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005826 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005828 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005829 fcp->dbg_tick = debug_tick;
5830 }
5831
5832 return retval;
5833}
5834
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835/*
5836 * Return TRUE if the currently active function should be ended, because a
5837 * return was encountered or an error occurred. Used inside a ":while".
5838 */
5839 int
5840func_has_ended(void *cookie)
5841{
5842 funccall_T *fcp = (funccall_T *)cookie;
5843
Bram Moolenaare38eab22019-12-05 21:50:01 +01005844 // Ignore the "abort" flag if the abortion behavior has been changed due to
5845 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005846 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5847 || fcp->returned);
5848}
5849
5850/*
5851 * return TRUE if cookie indicates a function which "abort"s on errors.
5852 */
5853 int
5854func_has_abort(
5855 void *cookie)
5856{
5857 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5858}
5859
5860
5861/*
5862 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5863 * Don't do this when "Func" is already a partial that was bound
5864 * explicitly (pt_auto is FALSE).
5865 * Changes "rettv" in-place.
5866 * Returns the updated "selfdict_in".
5867 */
5868 dict_T *
5869make_partial(dict_T *selfdict_in, typval_T *rettv)
5870{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005871 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005872 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873 char_u fname_buf[FLEN_FIXED + 1];
5874 int error;
5875 dict_T *selfdict = selfdict_in;
5876
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005877 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
5878 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005879 fp = rettv->vval.v_partial->pt_func;
5880 else
5881 {
5882 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005883 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005884 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005885 if (fname == NULL)
5886 {
5887 // There is no point binding a dict to a NULL function, just create
5888 // a function reference.
5889 rettv->v_type = VAR_FUNC;
5890 rettv->vval.v_string = NULL;
5891 }
5892 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00005893 {
5894 char_u *tofree = NULL;
5895
5896 // Translate "s:func" to the stored function name.
5897 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
5898 fp = find_func(fname, FALSE);
5899 vim_free(tofree);
5900 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005901 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005902
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005903 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005904 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005905 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005906
5907 if (pt != NULL)
5908 {
5909 pt->pt_refcount = 1;
5910 pt->pt_dict = selfdict;
5911 pt->pt_auto = TRUE;
5912 selfdict = NULL;
5913 if (rettv->v_type == VAR_FUNC)
5914 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005915 // Just a function: Take over the function name and use
5916 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005917 pt->pt_name = rettv->vval.v_string;
5918 }
5919 else
5920 {
5921 partial_T *ret_pt = rettv->vval.v_partial;
5922 int i;
5923
Bram Moolenaare38eab22019-12-05 21:50:01 +01005924 // Partial: copy the function name, use selfdict and copy
5925 // args. Can't take over name or args, the partial might
5926 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005927 if (ret_pt->pt_name != NULL)
5928 {
5929 pt->pt_name = vim_strsave(ret_pt->pt_name);
5930 func_ref(pt->pt_name);
5931 }
5932 else
5933 {
5934 pt->pt_func = ret_pt->pt_func;
5935 func_ptr_ref(pt->pt_func);
5936 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005937 if (ret_pt->pt_argc > 0)
5938 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005939 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005940 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005941 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005942 pt->pt_argc = 0;
5943 else
5944 {
5945 pt->pt_argc = ret_pt->pt_argc;
5946 for (i = 0; i < pt->pt_argc; i++)
5947 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5948 }
5949 }
5950 partial_unref(ret_pt);
5951 }
5952 rettv->v_type = VAR_PARTIAL;
5953 rettv->vval.v_partial = pt;
5954 }
5955 }
5956 return selfdict;
5957}
5958
5959/*
5960 * Return the name of the executed function.
5961 */
5962 char_u *
5963func_name(void *cookie)
5964{
5965 return ((funccall_T *)cookie)->func->uf_name;
5966}
5967
5968/*
5969 * Return the address holding the next breakpoint line for a funccall cookie.
5970 */
5971 linenr_T *
5972func_breakpoint(void *cookie)
5973{
5974 return &((funccall_T *)cookie)->breakpoint;
5975}
5976
5977/*
5978 * Return the address holding the debug tick for a funccall cookie.
5979 */
5980 int *
5981func_dbg_tick(void *cookie)
5982{
5983 return &((funccall_T *)cookie)->dbg_tick;
5984}
5985
5986/*
5987 * Return the nesting level for a funccall cookie.
5988 */
5989 int
5990func_level(void *cookie)
5991{
5992 return ((funccall_T *)cookie)->level;
5993}
5994
5995/*
5996 * Return TRUE when a function was ended by a ":return" command.
5997 */
5998 int
5999current_func_returned(void)
6000{
6001 return current_funccal->returned;
6002}
6003
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006004 int
6005free_unref_funccal(int copyID, int testing)
6006{
6007 int did_free = FALSE;
6008 int did_free_funccal = FALSE;
6009 funccall_T *fc, **pfc;
6010
6011 for (pfc = &previous_funccal; *pfc != NULL; )
6012 {
6013 if (can_free_funccal(*pfc, copyID))
6014 {
6015 fc = *pfc;
6016 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006017 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006018 did_free = TRUE;
6019 did_free_funccal = TRUE;
6020 }
6021 else
6022 pfc = &(*pfc)->caller;
6023 }
6024 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006025 // When a funccal was freed some more items might be garbage
6026 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006027 (void)garbage_collect(testing);
6028
6029 return did_free;
6030}
6031
6032/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006033 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006034 */
6035 static funccall_T *
6036get_funccal(void)
6037{
6038 int i;
6039 funccall_T *funccal;
6040 funccall_T *temp_funccal;
6041
6042 funccal = current_funccal;
6043 if (debug_backtrace_level > 0)
6044 {
6045 for (i = 0; i < debug_backtrace_level; i++)
6046 {
6047 temp_funccal = funccal->caller;
6048 if (temp_funccal)
6049 funccal = temp_funccal;
6050 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006051 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006052 debug_backtrace_level = i;
6053 }
6054 }
6055 return funccal;
6056}
6057
6058/*
6059 * Return the hashtable used for local variables in the current funccal.
6060 * Return NULL if there is no current funccal.
6061 */
6062 hashtab_T *
6063get_funccal_local_ht()
6064{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 return NULL;
6067 return &get_funccal()->l_vars.dv_hashtab;
6068}
6069
6070/*
6071 * Return the l: scope variable.
6072 * Return NULL if there is no current funccal.
6073 */
6074 dictitem_T *
6075get_funccal_local_var()
6076{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006078 return NULL;
6079 return &get_funccal()->l_vars_var;
6080}
6081
6082/*
6083 * Return the hashtable used for argument in the current funccal.
6084 * Return NULL if there is no current funccal.
6085 */
6086 hashtab_T *
6087get_funccal_args_ht()
6088{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006089 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006090 return NULL;
6091 return &get_funccal()->l_avars.dv_hashtab;
6092}
6093
6094/*
6095 * Return the a: scope variable.
6096 * Return NULL if there is no current funccal.
6097 */
6098 dictitem_T *
6099get_funccal_args_var()
6100{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006102 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01006103 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006104}
6105
6106/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006107 * List function variables, if there is a function.
6108 */
6109 void
6110list_func_vars(int *first)
6111{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006113 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006114 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006115}
6116
6117/*
6118 * If "ht" is the hashtable for local variables in the current funccal, return
6119 * the dict that contains it.
6120 * Otherwise return NULL.
6121 */
6122 dict_T *
6123get_current_funccal_dict(hashtab_T *ht)
6124{
6125 if (current_funccal != NULL
6126 && ht == &current_funccal->l_vars.dv_hashtab)
6127 return &current_funccal->l_vars;
6128 return NULL;
6129}
6130
6131/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006132 * Search hashitem in parent scope.
6133 */
6134 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006135find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006136{
6137 funccall_T *old_current_funccal = current_funccal;
6138 hashtab_T *ht;
6139 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006140 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006141
6142 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6143 return NULL;
6144
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006145 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006146 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006147 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006148 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006149 ht = find_var_ht(name, &varname);
6150 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006151 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006152 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006153 if (!HASHITEM_EMPTY(hi))
6154 {
6155 *pht = ht;
6156 break;
6157 }
6158 }
6159 if (current_funccal == current_funccal->func->uf_scoped)
6160 break;
6161 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006162 }
6163 current_funccal = old_current_funccal;
6164
6165 return hi;
6166}
6167
6168/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006169 * Search variable in parent scope.
6170 */
6171 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006172find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006173{
6174 dictitem_T *v = NULL;
6175 funccall_T *old_current_funccal = current_funccal;
6176 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006177 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006178
6179 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6180 return NULL;
6181
Bram Moolenaare38eab22019-12-05 21:50:01 +01006182 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006183 current_funccal = current_funccal->func->uf_scoped;
6184 while (current_funccal)
6185 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006186 ht = find_var_ht(name, &varname);
6187 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006188 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006189 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006190 if (v != NULL)
6191 break;
6192 }
6193 if (current_funccal == current_funccal->func->uf_scoped)
6194 break;
6195 current_funccal = current_funccal->func->uf_scoped;
6196 }
6197 current_funccal = old_current_funccal;
6198
6199 return v;
6200}
6201
6202/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006203 * Set "copyID + 1" in previous_funccal and callers.
6204 */
6205 int
6206set_ref_in_previous_funccal(int copyID)
6207{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006208 funccall_T *fc;
6209
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006210 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006211 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006212 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006213 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
6214 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
6215 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
6216 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006217 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006218 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006219}
6220
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006221 static int
6222set_ref_in_funccal(funccall_T *fc, int copyID)
6223{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006224 if (fc->fc_copyID != copyID)
6225 {
6226 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006227 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
6228 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
6229 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
6230 || set_ref_in_func(NULL, fc->func, copyID))
6231 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006232 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006233 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006234}
6235
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006236/*
6237 * Set "copyID" in all local vars and arguments in the call stack.
6238 */
6239 int
6240set_ref_in_call_stack(int copyID)
6241{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006242 funccall_T *fc;
6243 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006244
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006245 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6246 if (set_ref_in_funccal(fc, copyID))
6247 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006248
6249 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006250 for (entry = funccal_stack; entry != NULL; entry = entry->next)
6251 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
6252 if (set_ref_in_funccal(fc, copyID))
6253 return TRUE;
6254 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006255}
6256
6257/*
6258 * Set "copyID" in all functions available by name.
6259 */
6260 int
6261set_ref_in_functions(int copyID)
6262{
6263 int todo;
6264 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006265 ufunc_T *fp;
6266
6267 todo = (int)func_hashtab.ht_used;
6268 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006269 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006270 if (!HASHITEM_EMPTY(hi))
6271 {
6272 --todo;
6273 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006274 if (!func_name_refcount(fp->uf_name)
6275 && set_ref_in_func(NULL, fp, copyID))
6276 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006277 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006279 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006280}
6281
6282/*
6283 * Set "copyID" in all function arguments.
6284 */
6285 int
6286set_ref_in_func_args(int copyID)
6287{
6288 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006289
6290 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006291 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6292 copyID, NULL, NULL))
6293 return TRUE;
6294 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006295}
6296
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006297/*
6298 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006299 * Returns TRUE if setting references failed somehow.
6300 */
6301 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006302set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006303{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006304 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006305 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006306 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006307 char_u fname_buf[FLEN_FIXED + 1];
6308 char_u *tofree = NULL;
6309 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006310 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006311
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006312 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006313 return FALSE;
6314
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006315 if (fp_in == NULL)
6316 {
6317 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006318 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006319 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006320 if (fp != NULL)
6321 {
6322 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006323 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006324 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006325
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006326 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006327 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006328}
6329
Bram Moolenaare38eab22019-12-05 21:50:01 +01006330#endif // FEAT_EVAL